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> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 47 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | #include <proto/fd.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 49 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 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 | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 52 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 53 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 55 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | #include <proto/queue.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 57 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 59 | #include <proto/stream_interface.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 60 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | #include <proto/task.h> |
| 62 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 63 | const char HTTP_100[] = |
| 64 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 65 | |
| 66 | const struct chunk http_100_chunk = { |
| 67 | .str = (char *)&HTTP_100, |
| 68 | .len = sizeof(HTTP_100)-1 |
| 69 | }; |
| 70 | |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 71 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 72 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 73 | "HTTP/1.0 200 OK\r\n" |
| 74 | "Cache-Control: no-cache\r\n" |
| 75 | "Connection: close\r\n" |
| 76 | "Content-Type: text/html\r\n" |
| 77 | "\r\n" |
| 78 | "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n"; |
| 79 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 80 | const struct chunk http_200_chunk = { |
| 81 | .str = (char *)&HTTP_200, |
| 82 | .len = sizeof(HTTP_200)-1 |
| 83 | }; |
| 84 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 85 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 86 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 87 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 88 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 89 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 90 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 91 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 92 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 93 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 94 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 95 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 96 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 97 | |
| 98 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 99 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 100 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 101 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 102 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 103 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 104 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 105 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 106 | const char *HTTP_401_fmt = |
| 107 | "HTTP/1.0 401 Unauthorized\r\n" |
| 108 | "Cache-Control: no-cache\r\n" |
| 109 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 110 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 111 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 112 | "\r\n" |
| 113 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 114 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 115 | const char *HTTP_407_fmt = |
| 116 | "HTTP/1.0 407 Unauthorized\r\n" |
| 117 | "Cache-Control: no-cache\r\n" |
| 118 | "Connection: close\r\n" |
| 119 | "Content-Type: text/html\r\n" |
| 120 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 121 | "\r\n" |
| 122 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 123 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 124 | |
| 125 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 126 | [HTTP_ERR_400] = 400, |
| 127 | [HTTP_ERR_403] = 403, |
| 128 | [HTTP_ERR_408] = 408, |
| 129 | [HTTP_ERR_500] = 500, |
| 130 | [HTTP_ERR_502] = 502, |
| 131 | [HTTP_ERR_503] = 503, |
| 132 | [HTTP_ERR_504] = 504, |
| 133 | }; |
| 134 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 135 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 136 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 137 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 138 | "Cache-Control: no-cache\r\n" |
| 139 | "Connection: close\r\n" |
| 140 | "Content-Type: text/html\r\n" |
| 141 | "\r\n" |
| 142 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 143 | |
| 144 | [HTTP_ERR_403] = |
| 145 | "HTTP/1.0 403 Forbidden\r\n" |
| 146 | "Cache-Control: no-cache\r\n" |
| 147 | "Connection: close\r\n" |
| 148 | "Content-Type: text/html\r\n" |
| 149 | "\r\n" |
| 150 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 151 | |
| 152 | [HTTP_ERR_408] = |
| 153 | "HTTP/1.0 408 Request Time-out\r\n" |
| 154 | "Cache-Control: no-cache\r\n" |
| 155 | "Connection: close\r\n" |
| 156 | "Content-Type: text/html\r\n" |
| 157 | "\r\n" |
| 158 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 159 | |
| 160 | [HTTP_ERR_500] = |
| 161 | "HTTP/1.0 500 Server Error\r\n" |
| 162 | "Cache-Control: no-cache\r\n" |
| 163 | "Connection: close\r\n" |
| 164 | "Content-Type: text/html\r\n" |
| 165 | "\r\n" |
| 166 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 167 | |
| 168 | [HTTP_ERR_502] = |
| 169 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 170 | "Cache-Control: no-cache\r\n" |
| 171 | "Connection: close\r\n" |
| 172 | "Content-Type: text/html\r\n" |
| 173 | "\r\n" |
| 174 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 175 | |
| 176 | [HTTP_ERR_503] = |
| 177 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 178 | "Cache-Control: no-cache\r\n" |
| 179 | "Connection: close\r\n" |
| 180 | "Content-Type: text/html\r\n" |
| 181 | "\r\n" |
| 182 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 183 | |
| 184 | [HTTP_ERR_504] = |
| 185 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 186 | "Cache-Control: no-cache\r\n" |
| 187 | "Connection: close\r\n" |
| 188 | "Content-Type: text/html\r\n" |
| 189 | "\r\n" |
| 190 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 191 | |
| 192 | }; |
| 193 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 194 | /* We must put the messages here since GCC cannot initialize consts depending |
| 195 | * on strlen(). |
| 196 | */ |
| 197 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 198 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 199 | #define FD_SETS_ARE_BITFIELDS |
| 200 | #ifdef FD_SETS_ARE_BITFIELDS |
| 201 | /* |
| 202 | * This map is used with all the FD_* macros to check whether a particular bit |
| 203 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 204 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 205 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 206 | * exclusively to the macros. |
| 207 | */ |
| 208 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 209 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 210 | |
| 211 | #else |
| 212 | #error "Check if your OS uses bitfields for fd_sets" |
| 213 | #endif |
| 214 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 215 | void init_proto_http() |
| 216 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 217 | int i; |
| 218 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 219 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 220 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 221 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 222 | if (!http_err_msgs[msg]) { |
| 223 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 224 | abort(); |
| 225 | } |
| 226 | |
| 227 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 228 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 229 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 230 | |
| 231 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 232 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 233 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 234 | * printable characters above. |
| 235 | */ |
| 236 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 237 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 238 | for (i = 0; i < 32; i++) { |
| 239 | FD_SET(i, hdr_encode_map); |
| 240 | FD_SET(i, url_encode_map); |
| 241 | } |
| 242 | for (i = 127; i < 256; i++) { |
| 243 | FD_SET(i, hdr_encode_map); |
| 244 | FD_SET(i, url_encode_map); |
| 245 | } |
| 246 | |
| 247 | tmp = "\"#{|}"; |
| 248 | while (*tmp) { |
| 249 | FD_SET(*tmp, hdr_encode_map); |
| 250 | tmp++; |
| 251 | } |
| 252 | |
| 253 | tmp = "\"#"; |
| 254 | while (*tmp) { |
| 255 | FD_SET(*tmp, url_encode_map); |
| 256 | tmp++; |
| 257 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 258 | |
| 259 | /* memory allocations */ |
| 260 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 261 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 262 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 263 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 264 | /* |
| 265 | * We have 26 list of methods (1 per first letter), each of which can have |
| 266 | * up to 3 entries (2 valid, 1 null). |
| 267 | */ |
| 268 | struct http_method_desc { |
| 269 | http_meth_t meth; |
| 270 | int len; |
| 271 | const char text[8]; |
| 272 | }; |
| 273 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 274 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 275 | ['C' - 'A'] = { |
| 276 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 277 | }, |
| 278 | ['D' - 'A'] = { |
| 279 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 280 | }, |
| 281 | ['G' - 'A'] = { |
| 282 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 283 | }, |
| 284 | ['H' - 'A'] = { |
| 285 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 286 | }, |
| 287 | ['P' - 'A'] = { |
| 288 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 289 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 290 | }, |
| 291 | ['T' - 'A'] = { |
| 292 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 293 | }, |
| 294 | /* rest is empty like this : |
| 295 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 296 | */ |
| 297 | }; |
| 298 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 299 | /* 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] | 300 | * 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] | 301 | * RFC2616 for those chars. |
| 302 | */ |
| 303 | |
| 304 | const char http_is_spht[256] = { |
| 305 | [' '] = 1, ['\t'] = 1, |
| 306 | }; |
| 307 | |
| 308 | const char http_is_crlf[256] = { |
| 309 | ['\r'] = 1, ['\n'] = 1, |
| 310 | }; |
| 311 | |
| 312 | const char http_is_lws[256] = { |
| 313 | [' '] = 1, ['\t'] = 1, |
| 314 | ['\r'] = 1, ['\n'] = 1, |
| 315 | }; |
| 316 | |
| 317 | const char http_is_sep[256] = { |
| 318 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 319 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 320 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 321 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 322 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 323 | }; |
| 324 | |
| 325 | const char http_is_ctl[256] = { |
| 326 | [0 ... 31] = 1, |
| 327 | [127] = 1, |
| 328 | }; |
| 329 | |
| 330 | /* |
| 331 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 332 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 333 | * them correctly. Instead, define every non-CTL char's status. |
| 334 | */ |
| 335 | const char http_is_token[256] = { |
| 336 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 337 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 338 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 339 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 340 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 341 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 342 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 343 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 344 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 345 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 346 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 347 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 348 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 349 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 350 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 351 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 352 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 353 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 354 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 355 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 356 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 357 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 358 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 359 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 360 | }; |
| 361 | |
| 362 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 363 | /* |
| 364 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 365 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 366 | */ |
| 367 | const char http_is_ver_token[256] = { |
| 368 | ['.'] = 1, ['/'] = 1, |
| 369 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 370 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 371 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 372 | }; |
| 373 | |
| 374 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 375 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 376 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 377 | */ |
| 378 | #if defined(DEBUG_FSM) |
| 379 | static void http_silent_debug(int line, struct session *s) |
| 380 | { |
| 381 | int size = 0; |
| 382 | size += snprintf(trash + size, sizeof(trash) - size, |
| 383 | "[%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", |
| 384 | line, |
| 385 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| 386 | 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); |
| 387 | write(-1, trash, size); |
| 388 | size = 0; |
| 389 | size += snprintf(trash + size, sizeof(trash) - size, |
| 390 | " %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", |
| 391 | line, |
| 392 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| 393 | 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); |
| 394 | |
| 395 | write(-1, trash, size); |
| 396 | } |
| 397 | #else |
| 398 | #define http_silent_debug(l,s) do { } while (0) |
| 399 | #endif |
| 400 | |
| 401 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 402 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 403 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 404 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 405 | * of headers is automatically adjusted. The number of bytes added is returned |
| 406 | * on success, otherwise <0 is returned indicating an error. |
| 407 | */ |
| 408 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 409 | struct hdr_idx *hdr_idx, const char *text) |
| 410 | { |
| 411 | int bytes, len; |
| 412 | |
| 413 | len = strlen(text); |
| 414 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 415 | if (!bytes) |
| 416 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 417 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 418 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 423 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 424 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 425 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 426 | * of headers is automatically adjusted. The number of bytes added is returned |
| 427 | * on success, otherwise <0 is returned indicating an error. |
| 428 | */ |
| 429 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 430 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 431 | { |
| 432 | int bytes; |
| 433 | |
| 434 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 435 | if (!bytes) |
| 436 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 437 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 438 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 439 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 440 | |
| 441 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 442 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 443 | * If so, returns the position of the first non-space character relative to |
| 444 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 445 | * to return a pointer to the place after the first space. Returns 0 if the |
| 446 | * header name does not match. Checks are case-insensitive. |
| 447 | */ |
| 448 | int http_header_match2(const char *hdr, const char *end, |
| 449 | const char *name, int len) |
| 450 | { |
| 451 | const char *val; |
| 452 | |
| 453 | if (hdr + len >= end) |
| 454 | return 0; |
| 455 | if (hdr[len] != ':') |
| 456 | return 0; |
| 457 | if (strncasecmp(hdr, name, len) != 0) |
| 458 | return 0; |
| 459 | val = hdr + len + 1; |
| 460 | while (val < end && HTTP_IS_SPHT(*val)) |
| 461 | val++; |
| 462 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 463 | return len + 2; /* we may replace starting from second space */ |
| 464 | return val - hdr; |
| 465 | } |
| 466 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 467 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 468 | * par 2.2 for more information. Note that it requires a valid header to return |
| 469 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 470 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 471 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 472 | { |
| 473 | int quoted, qdpair; |
| 474 | |
| 475 | quoted = qdpair = 0; |
| 476 | for (; s < e; s++) { |
| 477 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 478 | else if (quoted) { |
| 479 | if (*s == '\\') qdpair = 1; |
| 480 | else if (*s == '"') quoted = 0; |
| 481 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 482 | else if (*s == '"') quoted = 1; |
| 483 | else if (*s == ',') return s; |
| 484 | } |
| 485 | return s; |
| 486 | } |
| 487 | |
| 488 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 489 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 490 | * structure holds everything necessary to use the header and find next |
| 491 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 492 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 493 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 494 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 495 | * is NULL when searching for a new values of a header, the current header is |
| 496 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 497 | */ |
| 498 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 499 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 500 | struct hdr_ctx *ctx) |
| 501 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 502 | char *eol, *sov; |
| 503 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 504 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 505 | cur_idx = ctx->idx; |
| 506 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 507 | /* We have previously returned a value, let's search |
| 508 | * another one on the same line. |
| 509 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 510 | sol = ctx->line; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 511 | ctx->del = ctx->val + ctx->vlen; |
| 512 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 513 | eol = sol + idx->v[cur_idx].len; |
| 514 | |
| 515 | if (sov >= eol) |
| 516 | /* no more values in this header */ |
| 517 | goto next_hdr; |
| 518 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 519 | /* values remaining for this header, skip the comma but save it |
| 520 | * for later use (eg: for header deletion). |
| 521 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 522 | sov++; |
| 523 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 524 | sov++; |
| 525 | |
| 526 | goto return_hdr; |
| 527 | } |
| 528 | |
| 529 | /* first request for this header */ |
| 530 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 531 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 532 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 533 | while (cur_idx) { |
| 534 | eol = sol + idx->v[cur_idx].len; |
| 535 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 536 | if (len == 0) { |
| 537 | /* No argument was passed, we want any header. |
| 538 | * To achieve this, we simply build a fake request. */ |
| 539 | while (sol + len < eol && sol[len] != ':') |
| 540 | len++; |
| 541 | name = sol; |
| 542 | } |
| 543 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 544 | if ((len < eol - sol) && |
| 545 | (sol[len] == ':') && |
| 546 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 547 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 548 | sov = sol + len + 1; |
| 549 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 550 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 551 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 552 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 553 | ctx->prev = old_idx; |
| 554 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 555 | ctx->idx = cur_idx; |
| 556 | ctx->val = sov - sol; |
| 557 | |
| 558 | eol = find_hdr_value_end(sov, eol); |
| 559 | ctx->vlen = eol - sov; |
| 560 | return 1; |
| 561 | } |
| 562 | next_hdr: |
| 563 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 564 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 565 | cur_idx = idx->v[cur_idx].next; |
| 566 | } |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 571 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 572 | struct hdr_ctx *ctx) |
| 573 | { |
| 574 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 575 | } |
| 576 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 577 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 578 | * the http_find_header functions. The value is removed, as well as surrounding |
| 579 | * commas if any. If the removed value was alone, the whole header is removed. |
| 580 | * The ctx is always updated accordingly, as well as buffer <buf> and HTTP |
| 581 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 582 | * no more header, so any processing may stop. The ctx is always left in a form |
| 583 | * that can be handled by http_find_header2() to find next occurrence. |
| 584 | */ |
| 585 | int http_remove_header2(struct http_msg *msg, struct buffer *buf, |
| 586 | struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 587 | { |
| 588 | int cur_idx = ctx->idx; |
| 589 | char *sol = ctx->line; |
| 590 | struct hdr_idx_elem *hdr; |
| 591 | int delta, skip_comma; |
| 592 | |
| 593 | if (!cur_idx) |
| 594 | return 0; |
| 595 | |
| 596 | hdr = &idx->v[cur_idx]; |
| 597 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) { |
| 598 | /* This was the only value of the header, we must now remove it entirely. */ |
| 599 | delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
| 600 | http_msg_move_end(msg, delta); |
| 601 | idx->used--; |
| 602 | hdr->len = 0; /* unused entry */ |
| 603 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
| 604 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 605 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 606 | ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */ |
| 607 | ctx->vlen = 0; |
| 608 | return ctx->idx; |
| 609 | } |
| 610 | |
| 611 | /* This was not the only value of this header. We have to remove between |
| 612 | * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry |
| 613 | * of the list, we remove the last separator. |
| 614 | */ |
| 615 | |
| 616 | skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1; |
| 617 | delta = buffer_replace2(buf, sol + ctx->del + skip_comma, |
| 618 | sol + ctx->val + ctx->vlen + skip_comma, |
| 619 | NULL, 0); |
| 620 | hdr->len += delta; |
| 621 | http_msg_move_end(msg, delta); |
| 622 | ctx->val = ctx->del; |
| 623 | ctx->vlen = 0; |
| 624 | return ctx->idx; |
| 625 | } |
| 626 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 627 | /* This function handles a server error at the stream interface level. The |
| 628 | * stream interface is assumed to be already in a closed state. An optional |
| 629 | * message is copied into the input buffer, and an HTTP status code stored. |
| 630 | * 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] | 631 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 632 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 633 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 634 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 635 | { |
Willy Tarreau | d5fd51c | 2010-01-22 14:17:47 +0100 | [diff] [blame] | 636 | buffer_auto_read(si->ob); |
| 637 | buffer_abort(si->ob); |
| 638 | buffer_auto_close(si->ob); |
| 639 | buffer_erase(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 640 | buffer_auto_close(si->ib); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 641 | buffer_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 642 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 643 | t->txn.status = status; |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 644 | buffer_write(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 645 | } |
| 646 | if (!(t->flags & SN_ERR_MASK)) |
| 647 | t->flags |= err; |
| 648 | if (!(t->flags & SN_FINST_MASK)) |
| 649 | t->flags |= finst; |
| 650 | } |
| 651 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 652 | /* This function returns the appropriate error location for the given session |
| 653 | * and message. |
| 654 | */ |
| 655 | |
| 656 | struct chunk *error_message(struct session *s, int msgnum) |
| 657 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 658 | if (s->be->errmsg[msgnum].str) |
| 659 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 660 | else if (s->fe->errmsg[msgnum].str) |
| 661 | return &s->fe->errmsg[msgnum]; |
| 662 | else |
| 663 | return &http_err_chunks[msgnum]; |
| 664 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 665 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 666 | /* |
| 667 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 668 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 669 | */ |
| 670 | static http_meth_t find_http_meth(const char *str, const int len) |
| 671 | { |
| 672 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 673 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 674 | |
| 675 | m = ((unsigned)*str - 'A'); |
| 676 | |
| 677 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 678 | for (h = http_methods[m]; h->len > 0; h++) { |
| 679 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 680 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 681 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 682 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 683 | }; |
| 684 | return HTTP_METH_OTHER; |
| 685 | } |
| 686 | return HTTP_METH_NONE; |
| 687 | |
| 688 | } |
| 689 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 690 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 691 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 692 | * It is returned otherwise. |
| 693 | */ |
| 694 | static char * |
| 695 | http_get_path(struct http_txn *txn) |
| 696 | { |
| 697 | char *ptr, *end; |
| 698 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 699 | ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 700 | end = ptr + txn->req.sl.rq.u_l; |
| 701 | |
| 702 | if (ptr >= end) |
| 703 | return NULL; |
| 704 | |
| 705 | /* RFC2616, par. 5.1.2 : |
| 706 | * Request-URI = "*" | absuri | abspath | authority |
| 707 | */ |
| 708 | |
| 709 | if (*ptr == '*') |
| 710 | return NULL; |
| 711 | |
| 712 | if (isalpha((unsigned char)*ptr)) { |
| 713 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 714 | ptr++; |
| 715 | while (ptr < end && |
| 716 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 717 | ptr++; |
| 718 | /* skip '://' */ |
| 719 | if (ptr == end || *ptr++ != ':') |
| 720 | return NULL; |
| 721 | if (ptr == end || *ptr++ != '/') |
| 722 | return NULL; |
| 723 | if (ptr == end || *ptr++ != '/') |
| 724 | return NULL; |
| 725 | } |
| 726 | /* skip [user[:passwd]@]host[:[port]] */ |
| 727 | |
| 728 | while (ptr < end && *ptr != '/') |
| 729 | ptr++; |
| 730 | |
| 731 | if (ptr == end) |
| 732 | return NULL; |
| 733 | |
| 734 | /* OK, we got the '/' ! */ |
| 735 | return ptr; |
| 736 | } |
| 737 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 738 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 739 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 740 | * left unchanged and will follow normal proxy processing. |
| 741 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 742 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 743 | { |
| 744 | struct http_txn *txn; |
| 745 | struct chunk rdr; |
| 746 | char *path; |
| 747 | int len; |
| 748 | |
| 749 | /* 1: create the response header */ |
| 750 | rdr.len = strlen(HTTP_302); |
| 751 | rdr.str = trash; |
Willy Tarreau | 59e0b0f | 2010-01-09 21:29:23 +0100 | [diff] [blame] | 752 | rdr.size = sizeof(trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 753 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 754 | |
| 755 | /* 2: add the server's prefix */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 756 | if (rdr.len + s->srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 757 | return; |
| 758 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 759 | /* special prefix "/" means don't change URL */ |
| 760 | if (s->srv->rdr_len != 1 || *s->srv->rdr_pfx != '/') { |
| 761 | memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len); |
| 762 | rdr.len += s->srv->rdr_len; |
| 763 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 764 | |
| 765 | /* 3: add the request URI */ |
| 766 | txn = &s->txn; |
| 767 | path = http_get_path(txn); |
| 768 | if (!path) |
| 769 | return; |
| 770 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 771 | 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] | 772 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 773 | return; |
| 774 | |
| 775 | memcpy(rdr.str + rdr.len, path, len); |
| 776 | rdr.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 777 | |
| 778 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 779 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 780 | rdr.len += 29; |
| 781 | } else { |
| 782 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 783 | rdr.len += 23; |
| 784 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 785 | |
| 786 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 787 | si->shutr(si); |
| 788 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 789 | si->err_type = SI_ET_NONE; |
| 790 | si->err_loc = NULL; |
| 791 | si->state = SI_ST_CLO; |
| 792 | |
| 793 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 794 | 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] | 795 | |
| 796 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 797 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 798 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 799 | } |
| 800 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 801 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 802 | * that the server side is closed. Note that err_type is actually a |
| 803 | * bitmask, where almost only aborts may be cumulated with other |
| 804 | * values. We consider that aborted operations are more important |
| 805 | * than timeouts or errors due to the fact that nobody else in the |
| 806 | * logs might explain incomplete retries. All others should avoid |
| 807 | * being cumulated. It should normally not be possible to have multiple |
| 808 | * aborts at once, but just in case, the first one in sequence is reported. |
| 809 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 810 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 811 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 812 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 813 | |
| 814 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 815 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 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_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 818 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 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_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 821 | http_server_error(s, si, SN_ERR_SRVTO, 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_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 824 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 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_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 827 | http_server_error(s, si, SN_ERR_SRVTO, 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 if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 830 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 831 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 832 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 833 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 834 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 835 | } |
| 836 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 837 | extern const char sess_term_cond[8]; |
| 838 | extern const char sess_fin_state[8]; |
| 839 | extern const char *monthname[12]; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 840 | const char sess_cookie[8] = "NIDVEO67"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, unknown */ |
| 841 | const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive), |
| 842 | Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten, |
| 843 | Set-cookie Updated, unknown, unknown */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 844 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 845 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 846 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 847 | void http_sess_clflog(struct session *s) |
| 848 | { |
| 849 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 850 | struct proxy *fe = s->fe; |
| 851 | struct proxy *be = s->be; |
| 852 | struct proxy *prx_log; |
| 853 | struct http_txn *txn = &s->txn; |
| 854 | int tolog, level, err; |
| 855 | char *uri, *h; |
| 856 | char *svid; |
| 857 | struct tm tm; |
| 858 | static char tmpline[MAX_SYSLOG_LEN]; |
| 859 | int hdr; |
| 860 | size_t w; |
| 861 | int t_request; |
| 862 | |
| 863 | prx_log = fe; |
| 864 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 865 | (s->req->cons->conn_retries != be->conn_retries) || |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 866 | txn->status >= 500; |
| 867 | |
| 868 | if (s->cli_addr.ss_family == AF_INET) |
| 869 | inet_ntop(AF_INET, |
| 870 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 871 | pn, sizeof(pn)); |
| 872 | else |
| 873 | inet_ntop(AF_INET6, |
| 874 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 875 | pn, sizeof(pn)); |
| 876 | |
| 877 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
| 878 | |
| 879 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 880 | tolog = fe->to_log; |
| 881 | |
| 882 | h = tmpline; |
| 883 | |
| 884 | w = snprintf(h, sizeof(tmpline), |
| 885 | "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]", |
| 886 | pn, |
| 887 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 888 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 889 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 890 | goto trunc; |
| 891 | h += w; |
| 892 | |
| 893 | if (h >= tmpline + sizeof(tmpline) - 4) |
| 894 | goto trunc; |
| 895 | |
| 896 | *(h++) = ' '; |
| 897 | *(h++) = '\"'; |
| 898 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 899 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 900 | '#', url_encode_map, uri); |
| 901 | *(h++) = '\"'; |
| 902 | |
| 903 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out); |
| 904 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 905 | goto trunc; |
| 906 | h += w; |
| 907 | |
| 908 | if (h >= tmpline + sizeof(tmpline) - 9) |
| 909 | goto trunc; |
| 910 | memcpy(h, " \"-\" \"-\"", 8); |
| 911 | h += 8; |
| 912 | |
| 913 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 914 | " %d %03d", |
| 915 | (s->cli_addr.ss_family == AF_INET) ? |
| 916 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 917 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 918 | (int)s->logs.accept_date.tv_usec/1000); |
| 919 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 920 | goto trunc; |
| 921 | h += w; |
| 922 | |
| 923 | w = strlen(fe->id); |
| 924 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 925 | goto trunc; |
| 926 | *(h++) = ' '; |
| 927 | *(h++) = '\"'; |
| 928 | memcpy(h, fe->id, w); |
| 929 | h += w; |
| 930 | *(h++) = '\"'; |
| 931 | |
| 932 | w = strlen(be->id); |
| 933 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 934 | goto trunc; |
| 935 | *(h++) = ' '; |
| 936 | *(h++) = '\"'; |
| 937 | memcpy(h, be->id, w); |
| 938 | h += w; |
| 939 | *(h++) = '\"'; |
| 940 | |
| 941 | svid = (tolog & LW_SVID) ? |
| 942 | (s->data_source != DATA_SRC_STATS) ? |
| 943 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 944 | |
| 945 | w = strlen(svid); |
| 946 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 947 | goto trunc; |
| 948 | *(h++) = ' '; |
| 949 | *(h++) = '\"'; |
| 950 | memcpy(h, svid, w); |
| 951 | h += w; |
| 952 | *(h++) = '\"'; |
| 953 | |
| 954 | t_request = -1; |
| 955 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 956 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 957 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 958 | " %d %ld %ld %ld %ld", |
| 959 | t_request, |
| 960 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
| 961 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 962 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 963 | s->logs.t_close); |
| 964 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 965 | goto trunc; |
| 966 | h += w; |
| 967 | |
| 968 | if (h >= tmpline + sizeof(tmpline) - 8) |
| 969 | goto trunc; |
| 970 | *(h++) = ' '; |
| 971 | *(h++) = '\"'; |
| 972 | *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]; |
| 973 | *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]; |
| 974 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 975 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-'; |
| 976 | *(h++) = '\"'; |
| 977 | |
| 978 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 979 | " %d %d %d %d %d %ld %ld", |
| 980 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 981 | (s->req->cons->conn_retries > 0) ? (be->conn_retries - s->req->cons->conn_retries) : be->conn_retries, |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 982 | s->logs.srv_queue_size, s->logs.prx_queue_size); |
| 983 | |
| 984 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 985 | goto trunc; |
| 986 | h += w; |
| 987 | |
| 988 | if (txn->cli_cookie) { |
| 989 | w = strlen(txn->cli_cookie); |
| 990 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 991 | goto trunc; |
| 992 | *(h++) = ' '; |
| 993 | *(h++) = '\"'; |
| 994 | memcpy(h, txn->cli_cookie, w); |
| 995 | h += w; |
| 996 | *(h++) = '\"'; |
| 997 | } else { |
| 998 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 999 | goto trunc; |
| 1000 | memcpy(h, " \"-\"", 4); |
| 1001 | h += 4; |
| 1002 | } |
| 1003 | |
| 1004 | if (txn->srv_cookie) { |
| 1005 | w = strlen(txn->srv_cookie); |
| 1006 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 1007 | goto trunc; |
| 1008 | *(h++) = ' '; |
| 1009 | *(h++) = '\"'; |
| 1010 | memcpy(h, txn->srv_cookie, w); |
| 1011 | h += w; |
| 1012 | *(h++) = '\"'; |
| 1013 | } else { |
| 1014 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 1015 | goto trunc; |
| 1016 | memcpy(h, " \"-\"", 4); |
| 1017 | h += 4; |
| 1018 | } |
| 1019 | |
| 1020 | if ((fe->to_log & LW_REQHDR) && txn->req.cap) { |
| 1021 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1022 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1023 | goto trunc; |
Cyril Bonté | 7f2c539 | 2010-03-13 15:15:07 +0100 | [diff] [blame] | 1024 | if (txn->req.cap[hdr] != NULL) { |
| 1025 | *(h++) = ' '; |
| 1026 | *(h++) = '\"'; |
| 1027 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1028 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1029 | *(h++) = '\"'; |
| 1030 | } else { |
| 1031 | memcpy(h, " \"-\"", 4); |
| 1032 | h += 4; |
| 1033 | } |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) { |
| 1038 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1039 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1040 | goto trunc; |
Cyril Bonté | 7f2c539 | 2010-03-13 15:15:07 +0100 | [diff] [blame] | 1041 | if (txn->rsp.cap[hdr] != NULL) { |
| 1042 | *(h++) = ' '; |
| 1043 | *(h++) = '\"'; |
| 1044 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1045 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1046 | *(h++) = '\"'; |
| 1047 | } else { |
| 1048 | memcpy(h, " \"-\"", 4); |
| 1049 | h += 4; |
| 1050 | } |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | trunc: |
| 1055 | *h = '\0'; |
| 1056 | |
| 1057 | level = LOG_INFO; |
| 1058 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1059 | level = LOG_ERR; |
| 1060 | |
| 1061 | send_log(prx_log, level, "%s\n", tmpline); |
| 1062 | |
| 1063 | s->logs.logwait = 0; |
| 1064 | } |
| 1065 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1066 | /* |
| 1067 | * send a log for the session when we have enough info about it. |
| 1068 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1069 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1070 | void http_sess_log(struct session *s) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1071 | { |
| 1072 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 1073 | struct proxy *fe = s->fe; |
| 1074 | struct proxy *be = s->be; |
| 1075 | struct proxy *prx_log; |
| 1076 | struct http_txn *txn = &s->txn; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1077 | int tolog, level, err; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1078 | char *uri, *h; |
| 1079 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1080 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1081 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1082 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1083 | int hdr; |
| 1084 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1085 | /* if we don't want to log normal traffic, return now */ |
| 1086 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 1087 | (s->req->cons->conn_retries != be->conn_retries) || |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1088 | txn->status >= 500; |
| 1089 | if (!err && (fe->options2 & PR_O2_NOLOGNORM)) |
| 1090 | return; |
| 1091 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1092 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 1093 | return; |
| 1094 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1095 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1096 | if (prx_log->options2 & PR_O2_CLFLOG) |
| 1097 | return http_sess_clflog(s); |
| 1098 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1099 | if (s->cli_addr.ss_family == AF_INET) |
| 1100 | inet_ntop(AF_INET, |
| 1101 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 1102 | pn, sizeof(pn)); |
| 1103 | else |
| 1104 | inet_ntop(AF_INET6, |
| 1105 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 1106 | pn, sizeof(pn)); |
| 1107 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1108 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1109 | |
| 1110 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 1111 | tolog = fe->to_log; |
| 1112 | |
| 1113 | h = tmpline; |
| 1114 | if (fe->to_log & LW_REQHDR && |
| 1115 | txn->req.cap && |
| 1116 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 1117 | *(h++) = ' '; |
| 1118 | *(h++) = '{'; |
| 1119 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1120 | if (hdr) |
| 1121 | *(h++) = '|'; |
| 1122 | if (txn->req.cap[hdr] != NULL) |
| 1123 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1124 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1125 | } |
| 1126 | *(h++) = '}'; |
| 1127 | } |
| 1128 | |
| 1129 | if (fe->to_log & LW_RSPHDR && |
| 1130 | txn->rsp.cap && |
| 1131 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1132 | *(h++) = ' '; |
| 1133 | *(h++) = '{'; |
| 1134 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1135 | if (hdr) |
| 1136 | *(h++) = '|'; |
| 1137 | if (txn->rsp.cap[hdr] != NULL) |
| 1138 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1139 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1140 | } |
| 1141 | *(h++) = '}'; |
| 1142 | } |
| 1143 | |
| 1144 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1145 | *(h++) = ' '; |
| 1146 | *(h++) = '"'; |
| 1147 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1148 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1149 | '#', url_encode_map, uri); |
| 1150 | *(h++) = '"'; |
| 1151 | } |
| 1152 | *h = '\0'; |
| 1153 | |
| 1154 | svid = (tolog & LW_SVID) ? |
| 1155 | (s->data_source != DATA_SRC_STATS) ? |
| 1156 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 1157 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1158 | t_request = -1; |
| 1159 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1160 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1161 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1162 | level = LOG_INFO; |
| 1163 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1164 | level = LOG_ERR; |
| 1165 | |
| 1166 | send_log(prx_log, level, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1167 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1168 | " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld" |
| 1169 | " %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] | 1170 | pn, |
| 1171 | (s->cli_addr.ss_family == AF_INET) ? |
| 1172 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 1173 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1174 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1175 | 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] | 1176 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1177 | t_request, |
| 1178 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1179 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1180 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1181 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1182 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1183 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1184 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1185 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1186 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1187 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1188 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1189 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 1190 | 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] | 1191 | (s->flags & SN_REDISP)?"+":"", |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 1192 | (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1193 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1194 | |
| 1195 | s->logs.logwait = 0; |
| 1196 | } |
| 1197 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1198 | |
| 1199 | /* |
| 1200 | * Capture headers from message starting at <som> according to header list |
| 1201 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1202 | */ |
| 1203 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1204 | char **cap, struct cap_hdr *cap_hdr) |
| 1205 | { |
| 1206 | char *eol, *sol, *col, *sov; |
| 1207 | int cur_idx; |
| 1208 | struct cap_hdr *h; |
| 1209 | int len; |
| 1210 | |
| 1211 | sol = som + hdr_idx_first_pos(idx); |
| 1212 | cur_idx = hdr_idx_first_idx(idx); |
| 1213 | |
| 1214 | while (cur_idx) { |
| 1215 | eol = sol + idx->v[cur_idx].len; |
| 1216 | |
| 1217 | col = sol; |
| 1218 | while (col < eol && *col != ':') |
| 1219 | col++; |
| 1220 | |
| 1221 | sov = col + 1; |
| 1222 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1223 | sov++; |
| 1224 | |
| 1225 | for (h = cap_hdr; h; h = h->next) { |
| 1226 | if ((h->namelen == col - sol) && |
| 1227 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1228 | if (cap[h->index] == NULL) |
| 1229 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1230 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1231 | |
| 1232 | if (cap[h->index] == NULL) { |
| 1233 | Alert("HTTP capture : out of memory.\n"); |
| 1234 | continue; |
| 1235 | } |
| 1236 | |
| 1237 | len = eol - sov; |
| 1238 | if (len > h->len) |
| 1239 | len = h->len; |
| 1240 | |
| 1241 | memcpy(cap[h->index], sov, len); |
| 1242 | cap[h->index][len]=0; |
| 1243 | } |
| 1244 | } |
| 1245 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1246 | cur_idx = idx->v[cur_idx].next; |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1251 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1252 | */ |
| 1253 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1254 | |
| 1255 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1256 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1257 | */ |
| 1258 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1259 | ptr++; \ |
| 1260 | if (likely(ptr < end)) \ |
| 1261 | goto good; \ |
| 1262 | else { \ |
| 1263 | state = (st); \ |
| 1264 | goto http_msg_ood; \ |
| 1265 | } \ |
| 1266 | } while (0) |
| 1267 | |
| 1268 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1269 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1270 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1271 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1272 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1273 | * will give undefined results. |
| 1274 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1275 | * and that msg->sol points to the beginning of the response. |
| 1276 | * If a complete line is found (which implies that at least one CR or LF is |
| 1277 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1278 | * returned indicating an incomplete line (which does not mean that parts have |
| 1279 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1280 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1281 | * upon next call. |
| 1282 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1283 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1284 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1285 | * 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] | 1286 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1287 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1288 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1289 | unsigned int state, const char *ptr, const char *end, |
| 1290 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1291 | { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1292 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1293 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1294 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1295 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1296 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1297 | |
| 1298 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1299 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1300 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1301 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1302 | state = HTTP_MSG_ERROR; |
| 1303 | break; |
| 1304 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1305 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1306 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1307 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1308 | msg->sl.st.c = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1309 | goto http_msg_rpcode; |
| 1310 | } |
| 1311 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1312 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1313 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1314 | state = HTTP_MSG_ERROR; |
| 1315 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1316 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1317 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1318 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1319 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1320 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1321 | |
| 1322 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1323 | 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] | 1324 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1325 | } |
| 1326 | |
| 1327 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1328 | 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] | 1329 | http_msg_rsp_reason: |
| 1330 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1331 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1332 | msg->sl.st.r_l = 0; |
| 1333 | goto http_msg_rpline_eol; |
| 1334 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1335 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1336 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1337 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1338 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1339 | goto http_msg_rpreason; |
| 1340 | } |
| 1341 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1342 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1343 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1344 | goto http_msg_rsp_reason; |
| 1345 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1346 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1347 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1348 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1349 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1350 | 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] | 1351 | http_msg_rpline_eol: |
| 1352 | /* We have seen the end of line. Note that we do not |
| 1353 | * necessarily have the \n yet, but at least we know that we |
| 1354 | * have EITHER \r OR \n, otherwise the response would not be |
| 1355 | * complete. We can then record the response length and return |
| 1356 | * to the caller which will be able to register it. |
| 1357 | */ |
| 1358 | msg->sl.st.l = ptr - msg->sol; |
| 1359 | return ptr; |
| 1360 | |
| 1361 | #ifdef DEBUG_FULL |
| 1362 | default: |
| 1363 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1364 | exit(1); |
| 1365 | #endif |
| 1366 | } |
| 1367 | |
| 1368 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1369 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1370 | if (ret_state) |
| 1371 | *ret_state = state; |
| 1372 | if (ret_ptr) |
| 1373 | *ret_ptr = (char *)ptr; |
| 1374 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1375 | } |
| 1376 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1377 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1378 | * This function parses a request line between <ptr> and <end>, starting with |
| 1379 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1380 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1381 | * will give undefined results. |
| 1382 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1383 | * and that msg->sol points to the beginning of the request. |
| 1384 | * If a complete line is found (which implies that at least one CR or LF is |
| 1385 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1386 | * returned indicating an incomplete line (which does not mean that parts have |
| 1387 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1388 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1389 | * upon next call. |
| 1390 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1391 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1392 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1393 | * 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] | 1394 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1395 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1396 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1397 | unsigned int state, const char *ptr, const char *end, |
| 1398 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1399 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1400 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1401 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1402 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1403 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1404 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1405 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1406 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1407 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1408 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1409 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1410 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1411 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1412 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1413 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1414 | http_msg_req09_uri: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1415 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1416 | http_msg_req09_uri_e: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1417 | 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] | 1418 | http_msg_req09_ver: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1419 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1420 | msg->sl.rq.v_l = 0; |
| 1421 | goto http_msg_rqline_eol; |
| 1422 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1423 | state = HTTP_MSG_ERROR; |
| 1424 | break; |
| 1425 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1426 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1427 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1428 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1429 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1430 | goto http_msg_rquri; |
| 1431 | } |
| 1432 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1433 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1434 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1435 | goto http_msg_req09_uri; |
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 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1438 | http_msg_rquri: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1439 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1440 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1441 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1442 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1443 | 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] | 1444 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1445 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1446 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1447 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1448 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1449 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1450 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1451 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1452 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1453 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1454 | goto http_msg_rqver; |
| 1455 | } |
| 1456 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1457 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1458 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1459 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1460 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1461 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1462 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1463 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1464 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1465 | |
| 1466 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1467 | 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] | 1468 | http_msg_rqline_eol: |
| 1469 | /* We have seen the end of line. Note that we do not |
| 1470 | * necessarily have the \n yet, but at least we know that we |
| 1471 | * have EITHER \r OR \n, otherwise the request would not be |
| 1472 | * complete. We can then record the request length and return |
| 1473 | * to the caller which will be able to register it. |
| 1474 | */ |
| 1475 | msg->sl.rq.l = ptr - msg->sol; |
| 1476 | return ptr; |
| 1477 | } |
| 1478 | |
| 1479 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1480 | state = HTTP_MSG_ERROR; |
| 1481 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1482 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1483 | #ifdef DEBUG_FULL |
| 1484 | default: |
| 1485 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1486 | exit(1); |
| 1487 | #endif |
| 1488 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1489 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1490 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1491 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1492 | if (ret_state) |
| 1493 | *ret_state = state; |
| 1494 | if (ret_ptr) |
| 1495 | *ret_ptr = (char *)ptr; |
| 1496 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1497 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1498 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1499 | /* |
| 1500 | * Returns the data from Authorization header. Function may be called more |
| 1501 | * than once so data is stored in txn->auth_data. When no header is found |
| 1502 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1503 | * searching again for something we are unable to find anyway. |
| 1504 | */ |
| 1505 | |
| 1506 | char get_http_auth_buff[BUFSIZE]; |
| 1507 | |
| 1508 | int |
| 1509 | get_http_auth(struct session *s) |
| 1510 | { |
| 1511 | |
| 1512 | struct http_txn *txn = &s->txn; |
| 1513 | struct chunk auth_method; |
| 1514 | struct hdr_ctx ctx; |
| 1515 | char *h, *p; |
| 1516 | int len; |
| 1517 | |
| 1518 | #ifdef DEBUG_AUTH |
| 1519 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1520 | #endif |
| 1521 | |
| 1522 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1523 | return 0; |
| 1524 | |
| 1525 | if (txn->auth.method) |
| 1526 | return 1; |
| 1527 | |
| 1528 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1529 | |
| 1530 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1531 | |
| 1532 | if (txn->flags & TX_USE_PX_CONN) { |
| 1533 | h = "Proxy-Authorization"; |
| 1534 | len = strlen(h); |
| 1535 | } else { |
| 1536 | h = "Authorization"; |
| 1537 | len = strlen(h); |
| 1538 | } |
| 1539 | |
| 1540 | 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] | 1541 | return 0; |
| 1542 | |
| 1543 | h = ctx.line + ctx.val; |
| 1544 | |
| 1545 | p = memchr(h, ' ', ctx.vlen); |
| 1546 | if (!p || p == h) |
| 1547 | return 0; |
| 1548 | |
| 1549 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1550 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1551 | |
| 1552 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1553 | |
| 1554 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
| 1555 | get_http_auth_buff, BUFSIZE - 1); |
| 1556 | |
| 1557 | if (len < 0) |
| 1558 | return 0; |
| 1559 | |
| 1560 | |
| 1561 | get_http_auth_buff[len] = '\0'; |
| 1562 | |
| 1563 | p = strchr(get_http_auth_buff, ':'); |
| 1564 | |
| 1565 | if (!p) |
| 1566 | return 0; |
| 1567 | |
| 1568 | txn->auth.user = get_http_auth_buff; |
| 1569 | *p = '\0'; |
| 1570 | txn->auth.pass = p+1; |
| 1571 | |
| 1572 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1573 | return 1; |
| 1574 | } |
| 1575 | |
| 1576 | return 0; |
| 1577 | } |
| 1578 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1579 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1580 | /* |
| 1581 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1582 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1583 | * when data are missing and recalled at the exact same location with no |
| 1584 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1585 | * 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] | 1586 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1587 | * the first state, so that none of the msg pointers has to be initialized |
| 1588 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1589 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1590 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1591 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1592 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1593 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1594 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1595 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1596 | ptr = buf->lr; |
| 1597 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1598 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1599 | if (unlikely(ptr >= end)) |
| 1600 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1601 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1602 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1603 | /* |
| 1604 | * First, states that are specific to the response only. |
| 1605 | * We check them first so that request and headers are |
| 1606 | * closer to each other (accessed more often). |
| 1607 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1608 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1609 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1610 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1611 | /* we have a start of message, but we have to check |
| 1612 | * first if we need to remove some CRLF. We can only |
| 1613 | * do this when send_max=0. |
| 1614 | */ |
| 1615 | char *beg = buf->w + buf->send_max; |
| 1616 | if (beg >= buf->data + buf->size) |
| 1617 | beg -= buf->size; |
| 1618 | if (unlikely(ptr != beg)) { |
| 1619 | if (buf->send_max) |
| 1620 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1621 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1622 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1623 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1624 | msg->som = ptr - buf->data; |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1625 | msg->sol = ptr; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1626 | hdr_idx_init(idx); |
| 1627 | state = HTTP_MSG_RPVER; |
| 1628 | goto http_msg_rpver; |
| 1629 | } |
| 1630 | |
| 1631 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1632 | goto http_msg_invalid; |
| 1633 | |
| 1634 | if (unlikely(*ptr == '\n')) |
| 1635 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1636 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1637 | /* stop here */ |
| 1638 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1639 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1640 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1641 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1642 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1643 | /* stop here */ |
| 1644 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1645 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1646 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1647 | case HTTP_MSG_RPVER_SP: |
| 1648 | case HTTP_MSG_RPCODE: |
| 1649 | case HTTP_MSG_RPCODE_SP: |
| 1650 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1651 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1652 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1653 | if (unlikely(!ptr)) |
| 1654 | return; |
| 1655 | |
| 1656 | /* we have a full response and we know that we have either a CR |
| 1657 | * or an LF at <ptr>. |
| 1658 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1659 | //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] | 1660 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1661 | |
| 1662 | msg->sol = ptr; |
| 1663 | if (likely(*ptr == '\r')) |
| 1664 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1665 | goto http_msg_rpline_end; |
| 1666 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1667 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1668 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1669 | /* msg->sol must point to the first of CR or LF. */ |
| 1670 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1671 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1672 | /* stop here */ |
| 1673 | |
| 1674 | /* |
| 1675 | * Second, states that are specific to the request only |
| 1676 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1677 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1678 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1679 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1680 | /* we have a start of message, but we have to check |
| 1681 | * first if we need to remove some CRLF. We can only |
| 1682 | * do this when send_max=0. |
| 1683 | */ |
| 1684 | char *beg = buf->w + buf->send_max; |
| 1685 | if (beg >= buf->data + buf->size) |
| 1686 | beg -= buf->size; |
| 1687 | if (likely(ptr != beg)) { |
| 1688 | if (buf->send_max) |
| 1689 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1690 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1691 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1692 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1693 | msg->som = ptr - buf->data; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1694 | msg->sol = ptr; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1695 | /* we will need this when keep-alive will be supported |
| 1696 | hdr_idx_init(idx); |
| 1697 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1698 | state = HTTP_MSG_RQMETH; |
| 1699 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1700 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1701 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1702 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1703 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1704 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1705 | if (unlikely(*ptr == '\n')) |
| 1706 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1707 | 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] | 1708 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1709 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1710 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1711 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1712 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1713 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1714 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1715 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1716 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1717 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1718 | case HTTP_MSG_RQMETH_SP: |
| 1719 | case HTTP_MSG_RQURI: |
| 1720 | case HTTP_MSG_RQURI_SP: |
| 1721 | case HTTP_MSG_RQVER: |
| 1722 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1723 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1724 | if (unlikely(!ptr)) |
| 1725 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1726 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1727 | /* we have a full request and we know that we have either a CR |
| 1728 | * or an LF at <ptr>. |
| 1729 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1730 | //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] | 1731 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
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 | msg->sol = ptr; |
| 1734 | if (likely(*ptr == '\r')) |
| 1735 | 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] | 1736 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1737 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1738 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1739 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1740 | /* check for HTTP/0.9 request : no version information available. |
| 1741 | * msg->sol must point to the first of CR or LF. |
| 1742 | */ |
| 1743 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1744 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1745 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1746 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1747 | 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] | 1748 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1749 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1750 | /* |
| 1751 | * Common states below |
| 1752 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1753 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1754 | http_msg_hdr_first: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1755 | msg->sol = ptr; |
| 1756 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1757 | goto http_msg_hdr_name; |
| 1758 | } |
| 1759 | |
| 1760 | if (likely(*ptr == '\r')) |
| 1761 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1762 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1763 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1764 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1765 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1766 | /* assumes msg->sol points to the first char */ |
| 1767 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1768 | 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] | 1769 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1770 | if (likely(*ptr == ':')) { |
| 1771 | msg->col = ptr - buf->data; |
| 1772 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1773 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1774 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1775 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1776 | goto http_msg_invalid; |
| 1777 | |
| 1778 | if (msg->err_pos == -1) /* capture error pointer */ |
| 1779 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 1780 | |
| 1781 | /* and we still accept this non-token character */ |
| 1782 | 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] | 1783 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1784 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1785 | http_msg_hdr_l1_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1786 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1787 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1788 | 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] | 1789 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1790 | /* header value can be basically anything except CR/LF */ |
| 1791 | msg->sov = ptr - buf->data; |
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 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1794 | goto http_msg_hdr_val; |
| 1795 | } |
| 1796 | |
| 1797 | if (likely(*ptr == '\r')) |
| 1798 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1799 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1800 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1801 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1802 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1803 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1804 | 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] | 1805 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1806 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1807 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1808 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1809 | /* replace HT,CR,LF with spaces */ |
| 1810 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1811 | buf->data[msg->sov] = ' '; |
| 1812 | goto http_msg_hdr_l1_sp; |
| 1813 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1814 | /* we had a header consisting only in spaces ! */ |
| 1815 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1816 | goto http_msg_complete_header; |
| 1817 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1818 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1819 | http_msg_hdr_val: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1820 | /* assumes msg->sol points to the first char, msg->col to the |
| 1821 | * colon, and msg->sov points to the first character of the |
| 1822 | * value. |
| 1823 | */ |
| 1824 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1825 | 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] | 1826 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1827 | msg->eol = ptr; |
| 1828 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1829 | * real header end in case it ends with lots of LWS, but is this |
| 1830 | * really needed ? |
| 1831 | */ |
| 1832 | if (likely(*ptr == '\r')) |
| 1833 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1834 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1835 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1836 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1837 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1838 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1839 | 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] | 1840 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1841 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1842 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1843 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1844 | /* LWS: replace HT,CR,LF with spaces */ |
| 1845 | for (; msg->eol < ptr; msg->eol++) |
| 1846 | *msg->eol = ' '; |
| 1847 | goto http_msg_hdr_val; |
| 1848 | } |
| 1849 | http_msg_complete_header: |
| 1850 | /* |
| 1851 | * It was a new header, so the last one is finished. |
| 1852 | * Assumes msg->sol points to the first char, msg->col to the |
| 1853 | * colon, msg->sov points to the first character of the value |
| 1854 | * and msg->eol to the first CR or LF so we know how the line |
| 1855 | * ends. We insert last header into the index. |
| 1856 | */ |
| 1857 | /* |
| 1858 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1859 | write(2, msg->sol, msg->eol-msg->sol); |
| 1860 | fprintf(stderr,"\n"); |
| 1861 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1862 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1863 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1864 | idx, idx->tail) < 0)) |
| 1865 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1866 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1867 | msg->sol = ptr; |
| 1868 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1869 | goto http_msg_hdr_name; |
| 1870 | } |
| 1871 | |
| 1872 | if (likely(*ptr == '\r')) |
| 1873 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1874 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1875 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1876 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1877 | http_msg_last_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1878 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1879 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1880 | ptr++; |
| 1881 | buf->lr = ptr; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1882 | msg->col = msg->sov = buf->lr - buf->data; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1883 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1884 | msg->sol = buf->data + msg->som; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1885 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1886 | return; |
| 1887 | #ifdef DEBUG_FULL |
| 1888 | default: |
| 1889 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1890 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1891 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1892 | } |
| 1893 | http_msg_ood: |
| 1894 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1895 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1896 | buf->lr = ptr; |
| 1897 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1898 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1899 | http_msg_invalid: |
| 1900 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1901 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1902 | buf->lr = ptr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1903 | return; |
| 1904 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1905 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1906 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1907 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1908 | * nothing is done and 1 is returned. |
| 1909 | */ |
| 1910 | static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn) |
| 1911 | { |
| 1912 | int delta; |
| 1913 | char *cur_end; |
| 1914 | |
| 1915 | if (msg->sl.rq.v_l != 0) |
| 1916 | return 1; |
| 1917 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1918 | cur_end = msg->sol + msg->sl.rq.l; |
| 1919 | delta = 0; |
| 1920 | |
| 1921 | if (msg->sl.rq.u_l == 0) { |
| 1922 | /* if no URI was set, add "/" */ |
| 1923 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1924 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1925 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1926 | } |
| 1927 | /* add HTTP version */ |
| 1928 | 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] | 1929 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1930 | cur_end += delta; |
| 1931 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1932 | HTTP_MSG_RQMETH, |
| 1933 | msg->sol, cur_end + 1, |
| 1934 | NULL, NULL); |
| 1935 | if (unlikely(!cur_end)) |
| 1936 | return 0; |
| 1937 | |
| 1938 | /* we have a full HTTP/1.0 request now and we know that |
| 1939 | * we have either a CR or an LF at <ptr>. |
| 1940 | */ |
| 1941 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1942 | return 1; |
| 1943 | } |
| 1944 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1945 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
| 1946 | * and "keep-alive" values. If a buffer is provided and we already know that |
| 1947 | * some headers may safely be removed, we remove them now. The <to_del> flags |
| 1948 | * are used for that : |
| 1949 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1950 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
| 1951 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1952 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1953 | * harmless combinations may be removed. Do not call that after changes have |
| 1954 | * been processed. If unused, the buffer can be NULL, and no data will be |
| 1955 | * changed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1956 | */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1957 | 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] | 1958 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1959 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1960 | const char *hdr_val = "Connection"; |
| 1961 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1962 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1963 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1964 | return; |
| 1965 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1966 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1967 | hdr_val = "Proxy-Connection"; |
| 1968 | hdr_len = 16; |
| 1969 | } |
| 1970 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1971 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1972 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1973 | 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] | 1974 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1975 | txn->flags |= TX_HDR_CONN_KAL; |
| 1976 | if ((to_del & 2) && buf) |
| 1977 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1978 | else |
| 1979 | txn->flags |= TX_CON_KAL_SET; |
| 1980 | } |
| 1981 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1982 | txn->flags |= TX_HDR_CONN_CLO; |
| 1983 | if ((to_del & 1) && buf) |
| 1984 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1985 | else |
| 1986 | txn->flags |= TX_CON_CLO_SET; |
| 1987 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1988 | } |
| 1989 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1990 | txn->flags |= TX_HDR_CONN_PRS; |
| 1991 | return; |
| 1992 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1993 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1994 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1995 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1996 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1997 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1998 | */ |
| 1999 | void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted) |
| 2000 | { |
| 2001 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2002 | const char *hdr_val = "Connection"; |
| 2003 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2004 | |
| 2005 | ctx.idx = 0; |
| 2006 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2007 | |
| 2008 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2009 | hdr_val = "Proxy-Connection"; |
| 2010 | hdr_len = 16; |
| 2011 | } |
| 2012 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2013 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2014 | 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] | 2015 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2016 | if (wanted & TX_CON_KAL_SET) |
| 2017 | txn->flags |= TX_CON_KAL_SET; |
| 2018 | else |
| 2019 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2020 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2021 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2022 | if (wanted & TX_CON_CLO_SET) |
| 2023 | txn->flags |= TX_CON_CLO_SET; |
| 2024 | else |
| 2025 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2026 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2027 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2028 | |
| 2029 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 2030 | return; |
| 2031 | |
| 2032 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 2033 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2034 | hdr_val = "Connection: close"; |
| 2035 | hdr_len = 17; |
| 2036 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2037 | hdr_val = "Proxy-Connection: close"; |
| 2038 | hdr_len = 23; |
| 2039 | } |
| 2040 | 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] | 2041 | } |
| 2042 | |
| 2043 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 2044 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2045 | hdr_val = "Connection: keep-alive"; |
| 2046 | hdr_len = 22; |
| 2047 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2048 | hdr_val = "Proxy-Connection: keep-alive"; |
| 2049 | hdr_len = 28; |
| 2050 | } |
| 2051 | 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] | 2052 | } |
| 2053 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2054 | } |
| 2055 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2056 | /* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the |
| 2057 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
| 2058 | * so that we know we can forward between ->som and ->sov. Note that due to |
| 2059 | * possible wrapping at the end of the buffer, it is possible that msg->sov is |
| 2060 | * lower than msg->som. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2061 | * 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] | 2062 | * 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] | 2063 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2064 | int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2065 | { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2066 | char *ptr = buf->lr; |
| 2067 | char *end = buf->data + buf->size; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2068 | unsigned int chunk = 0; |
| 2069 | |
| 2070 | /* The chunk size is in the following form, though we are only |
| 2071 | * interested in the size and CRLF : |
| 2072 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 2073 | */ |
| 2074 | while (1) { |
| 2075 | int c; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2076 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2077 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2078 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2079 | if (c < 0) /* not a hex digit anymore */ |
| 2080 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2081 | if (++ptr >= end) |
| 2082 | ptr = buf->data; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2083 | if (chunk & 0xF000000) /* overflow will occur */ |
| 2084 | return -1; |
| 2085 | chunk = (chunk << 4) + c; |
| 2086 | } |
| 2087 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2088 | /* empty size not allowed */ |
| 2089 | if (ptr == buf->lr) |
| 2090 | return -1; |
| 2091 | |
| 2092 | while (http_is_spht[(unsigned char)*ptr]) { |
| 2093 | if (++ptr >= end) |
| 2094 | ptr = buf->data; |
| 2095 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2096 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2097 | } |
| 2098 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2099 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 2100 | * for the end of chunk size. |
| 2101 | */ |
| 2102 | while (1) { |
| 2103 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2104 | /* we now have a CR or an LF at ptr */ |
| 2105 | if (likely(*ptr == '\r')) { |
| 2106 | if (++ptr >= end) |
| 2107 | ptr = buf->data; |
| 2108 | if (ptr == buf->r) |
| 2109 | return 0; |
| 2110 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2111 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2112 | if (*ptr != '\n') |
| 2113 | return -1; |
| 2114 | if (++ptr >= end) |
| 2115 | ptr = buf->data; |
| 2116 | /* done */ |
| 2117 | break; |
| 2118 | } |
| 2119 | else if (*ptr == ';') { |
| 2120 | /* chunk extension, ends at next CRLF */ |
| 2121 | if (++ptr >= end) |
| 2122 | ptr = buf->data; |
| 2123 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2124 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2125 | |
| 2126 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2127 | if (++ptr >= end) |
| 2128 | ptr = buf->data; |
| 2129 | if (ptr == buf->r) |
| 2130 | return 0; |
| 2131 | } |
| 2132 | /* we have a CRLF now, loop above */ |
| 2133 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2134 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2135 | else |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2136 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2137 | } |
| 2138 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2139 | /* OK we found our CRLF and now <ptr> points to the next byte, |
| 2140 | * which may or may not be present. We save that into ->lr and |
| 2141 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2142 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2143 | msg->sov += ptr - buf->lr; |
| 2144 | buf->lr = ptr; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2145 | msg->hdr_content_len = chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2146 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2147 | return 1; |
| 2148 | } |
| 2149 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2150 | /* This function skips trailers in the buffer <buf> associated with HTTP |
| 2151 | * message <msg>. The first visited position is buf->lr. If the end of |
| 2152 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2153 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2154 | * If not enough data are available, the function does not change anything |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2155 | * except maybe buf->lr and msg->sov if it could parse some lines, and returns |
| 2156 | * zero. If a parse error is encountered, the function returns < 0 and does not |
| 2157 | * change anything except maybe buf->lr and msg->sov. Note that the message |
| 2158 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 2159 | * which implies that all non-trailers data have already been scheduled for |
| 2160 | * forwarding, and that the difference between msg->som and msg->sov exactly |
| 2161 | * matches the length of trailers already parsed and not forwarded. It is also |
| 2162 | * important to note that this function is designed to be able to parse wrapped |
| 2163 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2164 | */ |
| 2165 | int http_forward_trailers(struct buffer *buf, struct http_msg *msg) |
| 2166 | { |
| 2167 | /* we have buf->lr which points to next line. Look for CRLF. */ |
| 2168 | while (1) { |
| 2169 | char *p1 = NULL, *p2 = NULL; |
| 2170 | char *ptr = buf->lr; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2171 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2172 | |
| 2173 | /* scan current line and stop at LF or CRLF */ |
| 2174 | while (1) { |
| 2175 | if (ptr == buf->r) |
| 2176 | return 0; |
| 2177 | |
| 2178 | if (*ptr == '\n') { |
| 2179 | if (!p1) |
| 2180 | p1 = ptr; |
| 2181 | p2 = ptr; |
| 2182 | break; |
| 2183 | } |
| 2184 | |
| 2185 | if (*ptr == '\r') { |
| 2186 | if (p1) |
| 2187 | return -1; |
| 2188 | p1 = ptr; |
| 2189 | } |
| 2190 | |
| 2191 | ptr++; |
| 2192 | if (ptr >= buf->data + buf->size) |
| 2193 | ptr = buf->data; |
| 2194 | } |
| 2195 | |
| 2196 | /* after LF; point to beginning of next line */ |
| 2197 | p2++; |
| 2198 | if (p2 >= buf->data + buf->size) |
| 2199 | p2 = buf->data; |
| 2200 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2201 | bytes = p2 - buf->lr; |
| 2202 | if (bytes < 0) |
| 2203 | bytes += buf->size; |
| 2204 | |
| 2205 | /* schedule this line for forwarding */ |
| 2206 | msg->sov += bytes; |
| 2207 | if (msg->sov >= buf->size) |
| 2208 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2209 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2210 | if (p1 == buf->lr) { |
| 2211 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2212 | * Everything was scheduled for forwarding, there's nothing |
| 2213 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2214 | */ |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2215 | buf->lr = p2; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2216 | msg->msg_state = HTTP_MSG_DONE; |
| 2217 | return 1; |
| 2218 | } |
| 2219 | /* OK, next line then */ |
| 2220 | buf->lr = p2; |
| 2221 | } |
| 2222 | } |
| 2223 | |
| 2224 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 2225 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
| 2226 | * ->som, buf->lr in order to include this part into the next forwarding phase. |
| 2227 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2228 | * not enough data are available, the function does not change anything and |
| 2229 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2230 | * does not change anything. Note: this function is designed to parse wrapped |
| 2231 | * CRLF at the end of the buffer. |
| 2232 | */ |
| 2233 | int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg) |
| 2234 | { |
| 2235 | char *ptr; |
| 2236 | int bytes; |
| 2237 | |
| 2238 | /* NB: we'll check data availabilty at the end. It's not a |
| 2239 | * problem because whatever we match first will be checked |
| 2240 | * against the correct length. |
| 2241 | */ |
| 2242 | bytes = 1; |
| 2243 | ptr = buf->lr; |
| 2244 | if (*ptr == '\r') { |
| 2245 | bytes++; |
| 2246 | ptr++; |
| 2247 | if (ptr >= buf->data + buf->size) |
| 2248 | ptr = buf->data; |
| 2249 | } |
| 2250 | |
Willy Tarreau | bf3f1de | 2010-03-17 15:54:24 +0100 | [diff] [blame] | 2251 | if (bytes > buf->l - buf->send_max) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2252 | return 0; |
| 2253 | |
| 2254 | if (*ptr != '\n') |
| 2255 | return -1; |
| 2256 | |
| 2257 | ptr++; |
| 2258 | if (ptr >= buf->data + buf->size) |
| 2259 | ptr = buf->data; |
| 2260 | buf->lr = ptr; |
| 2261 | /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */ |
| 2262 | msg->sov = ptr - buf->data; |
| 2263 | msg->som = msg->sov - bytes; |
| 2264 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2265 | return 1; |
| 2266 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2267 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2268 | void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg) |
| 2269 | { |
| 2270 | char *end = buf->data + buf->size; |
| 2271 | int off = buf->data + buf->size - buf->w; |
| 2272 | |
| 2273 | /* two possible cases : |
| 2274 | * - the buffer is in one contiguous block, we move it in-place |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2275 | * - the buffer is in two blocks, we move it via the swap_buffer |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2276 | */ |
| 2277 | if (buf->l) { |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2278 | int block1 = buf->l; |
| 2279 | int block2 = 0; |
| 2280 | if (buf->r <= buf->w) { |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2281 | /* non-contiguous block */ |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2282 | block1 = buf->data + buf->size - buf->w; |
| 2283 | block2 = buf->r - buf->data; |
| 2284 | } |
| 2285 | if (block2) |
| 2286 | memcpy(swap_buffer, buf->data, block2); |
| 2287 | memmove(buf->data, buf->w, block1); |
| 2288 | if (block2) |
| 2289 | memcpy(buf->data + block1, swap_buffer, block2); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | /* adjust all known pointers */ |
| 2293 | buf->w = buf->data; |
| 2294 | buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size; |
| 2295 | buf->r += off; if (buf->r >= end) buf->r -= buf->size; |
| 2296 | msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size; |
| 2297 | msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size; |
| 2298 | |
| 2299 | /* adjust relative pointers */ |
| 2300 | msg->som = 0; |
| 2301 | msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size; |
| 2302 | msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size; |
| 2303 | msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size; |
| 2304 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2305 | if (msg->err_pos >= 0) { |
| 2306 | msg->err_pos += off; |
| 2307 | if (msg->err_pos >= buf->size) |
| 2308 | msg->err_pos -= buf->size; |
| 2309 | } |
| 2310 | |
| 2311 | buf->flags &= ~BF_FULL; |
| 2312 | if (buf->l >= buffer_max_len(buf)) |
| 2313 | buf->flags |= BF_FULL; |
| 2314 | } |
| 2315 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2316 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2317 | * processing can continue on next analysers, or zero if it either needs more |
| 2318 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2319 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2320 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2321 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2322 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2323 | 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] | 2324 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2325 | /* |
| 2326 | * We will parse the partial (or complete) lines. |
| 2327 | * We will check the request syntax, and also join multi-line |
| 2328 | * headers. An index of all the lines will be elaborated while |
| 2329 | * parsing. |
| 2330 | * |
| 2331 | * For the parsing, we use a 28 states FSM. |
| 2332 | * |
| 2333 | * Here is the information we currently have : |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 2334 | * req->data + msg->som = beginning of request |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2335 | * req->data + msg->eoh = end of processed headers / start of current one |
| 2336 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2337 | * req->lr = first non-visited byte |
| 2338 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2339 | * |
| 2340 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2341 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2342 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2343 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2344 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2345 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2346 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2347 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2348 | struct http_txn *txn = &s->txn; |
| 2349 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2350 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2351 | |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2352 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2353 | now_ms, __FUNCTION__, |
| 2354 | s, |
| 2355 | req, |
| 2356 | req->rex, req->wex, |
| 2357 | req->flags, |
| 2358 | req->l, |
| 2359 | req->analysers); |
| 2360 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2361 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2362 | s->srv_error = http_return_srv_error; |
| 2363 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2364 | /* There's a protected area at the end of the buffer for rewriting |
| 2365 | * purposes. We don't want to start to parse the request if the |
| 2366 | * protected area is affected, because we may have to move processed |
| 2367 | * data later, which is much more complicated. |
| 2368 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2369 | if (req->l && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2370 | if ((txn->flags & TX_NOT_FIRST) && |
| 2371 | unlikely((req->flags & BF_FULL) || |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2372 | req->r < req->lr || |
| 2373 | req->r > req->data + req->size - global.tune.maxrewrite)) { |
| 2374 | if (req->send_max) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2375 | if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2376 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2377 | /* some data has still not left the buffer, wake us once that's done */ |
| 2378 | buffer_dont_connect(req); |
| 2379 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2380 | return 0; |
| 2381 | } |
| 2382 | if (req->l <= req->size - global.tune.maxrewrite) |
| 2383 | http_buffer_heavy_realign(req, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2384 | } |
| 2385 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2386 | /* Note that we have the same problem with the response ; we |
| 2387 | * may want to send a redirect, error or anything which requires |
| 2388 | * some spare space. So we'll ensure that we have at least |
| 2389 | * maxrewrite bytes available in the response buffer before |
| 2390 | * processing that one. This will only affect pipelined |
| 2391 | * keep-alive requests. |
| 2392 | */ |
| 2393 | if ((txn->flags & TX_NOT_FIRST) && |
| 2394 | unlikely((s->rep->flags & BF_FULL) || |
| 2395 | s->rep->r < s->rep->lr || |
| 2396 | s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) { |
| 2397 | if (s->rep->send_max) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2398 | if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2399 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2400 | /* don't let a connection request be initiated */ |
| 2401 | buffer_dont_connect(req); |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2402 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2403 | return 0; |
| 2404 | } |
| 2405 | } |
| 2406 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2407 | if (likely(req->lr < req->r)) |
| 2408 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2409 | } |
| 2410 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2411 | /* 1: we might have to print this header in debug mode */ |
| 2412 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2413 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 2414 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2415 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2416 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2417 | |
Willy Tarreau | 663308b | 2010-06-07 14:06:08 +0200 | [diff] [blame] | 2418 | sol = req->data + msg->som; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2419 | eol = sol + msg->sl.rq.l; |
| 2420 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2421 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2422 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2423 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
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 | while (cur_idx) { |
| 2426 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2427 | debug_hdr("clihdr", s, sol, eol); |
| 2428 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2429 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2430 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2431 | } |
| 2432 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2433 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2434 | /* |
| 2435 | * Now we quickly check if we have found a full valid request. |
| 2436 | * If not so, we check the FD and buffer states before leaving. |
| 2437 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2438 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2439 | * requests are checked first. When waiting for a second request |
| 2440 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2441 | * we note the error in the session flags but don't set any state. |
| 2442 | * Since the error will be noted there, it will not be counted by |
| 2443 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2444 | * Last, we may increase some tracked counters' http request errors on |
| 2445 | * the cases that are deliberately the client's fault. For instance, |
| 2446 | * a timeout or connection reset is not counted as an error. However |
| 2447 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2448 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2449 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2450 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2451 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2452 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2453 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2454 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2455 | session_inc_http_req_ctr(s); |
| 2456 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2457 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2458 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2459 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2460 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2461 | /* 1: Since we are in header mode, if there's no space |
| 2462 | * left for headers, we won't be able to free more |
| 2463 | * later, so the session will never terminate. We |
| 2464 | * must terminate it now. |
| 2465 | */ |
| 2466 | if (unlikely(req->flags & BF_FULL)) { |
| 2467 | /* FIXME: check if URI is set and return Status |
| 2468 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2469 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2470 | session_inc_http_req_ctr(s); |
| 2471 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2472 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2473 | goto return_bad_req; |
| 2474 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2475 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2476 | /* 2: have we encountered a read error ? */ |
| 2477 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2478 | if (!(s->flags & SN_ERR_MASK)) |
| 2479 | s->flags |= SN_ERR_CLICL; |
| 2480 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2481 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2482 | goto failed_keep_alive; |
| 2483 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2484 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2485 | if (msg->err_pos >= 0) { |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2486 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2487 | session_inc_http_err_ctr(s); |
| 2488 | } |
| 2489 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2490 | msg->msg_state = HTTP_MSG_ERROR; |
| 2491 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2492 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2493 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2494 | proxy_inc_fe_req_ctr(s->fe); |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2495 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2496 | if (s->listener->counters) |
| 2497 | s->listener->counters->failed_req++; |
| 2498 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2499 | if (!(s->flags & SN_FINST_MASK)) |
| 2500 | s->flags |= SN_FINST_R; |
| 2501 | return 0; |
| 2502 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2503 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2504 | /* 3: has the read timeout expired ? */ |
| 2505 | 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] | 2506 | if (!(s->flags & SN_ERR_MASK)) |
| 2507 | s->flags |= SN_ERR_CLITO; |
| 2508 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2509 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2510 | goto failed_keep_alive; |
| 2511 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2512 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2513 | if (msg->err_pos >= 0) { |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2514 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2515 | session_inc_http_err_ctr(s); |
| 2516 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2517 | txn->status = 408; |
| 2518 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2519 | msg->msg_state = HTTP_MSG_ERROR; |
| 2520 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2521 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2522 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2523 | proxy_inc_fe_req_ctr(s->fe); |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2524 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2525 | if (s->listener->counters) |
| 2526 | s->listener->counters->failed_req++; |
| 2527 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2528 | if (!(s->flags & SN_FINST_MASK)) |
| 2529 | s->flags |= SN_FINST_R; |
| 2530 | return 0; |
| 2531 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2532 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2533 | /* 4: have we encountered a close ? */ |
| 2534 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2535 | if (!(s->flags & SN_ERR_MASK)) |
| 2536 | s->flags |= SN_ERR_CLICL; |
| 2537 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2538 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2539 | goto failed_keep_alive; |
| 2540 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2541 | if (msg->err_pos >= 0) |
| 2542 | 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] | 2543 | txn->status = 400; |
| 2544 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2545 | msg->msg_state = HTTP_MSG_ERROR; |
| 2546 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2547 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2548 | session_inc_http_err_ctr(s); |
| 2549 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2550 | proxy_inc_fe_req_ctr(s->fe); |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2551 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2552 | if (s->listener->counters) |
| 2553 | s->listener->counters->failed_req++; |
| 2554 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2555 | if (!(s->flags & SN_FINST_MASK)) |
| 2556 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2557 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2558 | } |
| 2559 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2560 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2561 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2562 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2563 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2564 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2565 | /* If the client starts to talk, let's fall back to |
| 2566 | * request timeout processing. |
| 2567 | */ |
| 2568 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2569 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2570 | } |
| 2571 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2572 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2573 | if (!tick_isset(req->analyse_exp)) { |
| 2574 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2575 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2576 | tick_isset(s->be->timeout.httpka)) |
| 2577 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2578 | else |
| 2579 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2580 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2581 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2582 | /* we're not ready yet */ |
| 2583 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2584 | |
| 2585 | failed_keep_alive: |
| 2586 | /* Here we process low-level errors for keep-alive requests. In |
| 2587 | * short, if the request is not the first one and it experiences |
| 2588 | * a timeout, read error or shutdown, we just silently close so |
| 2589 | * that the client can try again. |
| 2590 | */ |
| 2591 | txn->status = 0; |
| 2592 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2593 | req->analysers = 0; |
| 2594 | s->logs.logwait = 0; |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2595 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2596 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2597 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2598 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2599 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2600 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2601 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2602 | * later. At this point, we have the last CRLF at req->data + msg->eoh. |
| 2603 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
| 2604 | * points to the CRLF of the request line. req->lr points to the first |
| 2605 | * byte after the last LF. msg->col and msg->sov point to the first |
| 2606 | * byte of data. msg->eol cannot be trusted because it may have been |
| 2607 | * left uninitialized (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2608 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2609 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2610 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2611 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2612 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2613 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2614 | /* kill the pending keep-alive timeout */ |
| 2615 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2616 | req->analyse_exp = TICK_ETERNITY; |
| 2617 | } |
| 2618 | |
| 2619 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2620 | /* Maybe we found in invalid header name while we were configured not |
| 2621 | * to block on that, so we have to capture it now. |
| 2622 | */ |
| 2623 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2624 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2625 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2626 | /* |
| 2627 | * 1: identify the method |
| 2628 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2629 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2630 | |
| 2631 | /* we can make use of server redirect on GET and HEAD */ |
| 2632 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2633 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2634 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2635 | /* |
| 2636 | * 2: check if the URI matches the monitor_uri. |
| 2637 | * We have to do this for every request which gets in, because |
| 2638 | * the monitor-uri is defined by the frontend. |
| 2639 | */ |
| 2640 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2641 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2642 | !memcmp(msg->sol + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2643 | s->fe->monitor_uri, |
| 2644 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2645 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2646 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2647 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2648 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2649 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2650 | s->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2651 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2652 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2653 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| 2654 | 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] | 2655 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2656 | ret = acl_pass(ret); |
| 2657 | if (cond->pol == ACL_COND_UNLESS) |
| 2658 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2659 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2660 | if (ret) { |
| 2661 | /* we fail this request, let's return 503 service unavail */ |
| 2662 | txn->status = 503; |
| 2663 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2664 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2665 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2666 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2667 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2668 | /* nothing to fail, let's reply normaly */ |
| 2669 | txn->status = 200; |
| 2670 | stream_int_retnclose(req->prod, &http_200_chunk); |
| 2671 | goto return_prx_cond; |
| 2672 | } |
| 2673 | |
| 2674 | /* |
| 2675 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2676 | * Note: we cannot log anymore if the request has been |
| 2677 | * classified as invalid. |
| 2678 | */ |
| 2679 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2680 | /* we have a complete HTTP request that we must log */ |
| 2681 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2682 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2683 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2684 | if (urilen >= REQURI_LEN) |
| 2685 | urilen = REQURI_LEN - 1; |
| 2686 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2687 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2688 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2689 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2690 | s->do_log(s); |
| 2691 | } else { |
| 2692 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2693 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2694 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2695 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2696 | /* 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] | 2697 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) |
| 2698 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2699 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2700 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2701 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2702 | ((msg->sol[msg->sl.rq.v + 5] > '1') || |
| 2703 | ((msg->sol[msg->sl.rq.v + 5] == '1') && |
| 2704 | (msg->sol[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2705 | txn->flags |= TX_REQ_VER_11; |
| 2706 | |
| 2707 | /* "connection" has not been parsed yet */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2708 | 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] | 2709 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2710 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2711 | * we have what looks like a proxied connection instead of a connection, |
| 2712 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2713 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2714 | * happen to do that despite being non-standard :-( |
| 2715 | * We consider that a request not beginning with either '/' or '*' is |
| 2716 | * a proxied connection, which covers both "scheme://location" and |
| 2717 | * CONNECT ip:port. |
| 2718 | */ |
| 2719 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
| 2720 | msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*') |
| 2721 | txn->flags |= TX_USE_PX_CONN; |
| 2722 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2723 | /* transfer length unknown*/ |
| 2724 | txn->flags &= ~TX_REQ_XFER_LEN; |
| 2725 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2726 | /* 5: we may need to capture headers */ |
| 2727 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2728 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2729 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2730 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2731 | /* 6: determine the transfer-length. |
| 2732 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2733 | * the presence of a message-body in a REQUEST and its transfer length |
| 2734 | * must be determined that way (in order of precedence) : |
| 2735 | * 1. The presence of a message-body in a request is signaled by the |
| 2736 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2737 | * in the request's header fields. When a request message contains |
| 2738 | * both a message-body of non-zero length and a method that does |
| 2739 | * not define any semantics for that request message-body, then an |
| 2740 | * origin server SHOULD either ignore the message-body or respond |
| 2741 | * with an appropriate error message (e.g., 413). A proxy or |
| 2742 | * gateway, when presented the same request, SHOULD either forward |
| 2743 | * the request inbound with the message- body or ignore the |
| 2744 | * message-body when determining a response. |
| 2745 | * |
| 2746 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2747 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2748 | * transfer-length is defined by the use of this transfer-coding. |
| 2749 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2750 | * transfer-coding is not present, the transfer-length is defined |
| 2751 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2752 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2753 | * 3. If a Content-Length header field is present, its decimal value in |
| 2754 | * OCTETs represents both the entity-length and the transfer-length. |
| 2755 | * If a message is received with both a Transfer-Encoding header |
| 2756 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2757 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2758 | * 4. By the server closing the connection. (Closing the connection |
| 2759 | * cannot be used to indicate the end of a request body, since that |
| 2760 | * would leave no possibility for the server to send back a response.) |
| 2761 | * |
| 2762 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2763 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2764 | * it is terminated by closing the connection. When the "chunked" |
| 2765 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2766 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2767 | */ |
| 2768 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2769 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2770 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2771 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 2772 | while ((txn->flags & TX_REQ_VER_11) && |
| 2773 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2774 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 2775 | txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2776 | else if (txn->flags & TX_REQ_TE_CHNK) { |
| 2777 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2778 | use_close_only = 1; |
| 2779 | txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2780 | break; |
| 2781 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2782 | } |
| 2783 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2784 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2785 | while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only && |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2786 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2787 | signed long long cl; |
| 2788 | |
| 2789 | if (!ctx.vlen) |
| 2790 | goto return_bad_req; |
| 2791 | |
| 2792 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 2793 | goto return_bad_req; /* parse failure */ |
| 2794 | |
| 2795 | if (cl < 0) |
| 2796 | goto return_bad_req; |
| 2797 | |
| 2798 | if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 2799 | goto return_bad_req; /* already specified, was different */ |
| 2800 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2801 | txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2802 | msg->hdr_content_len = cl; |
| 2803 | } |
| 2804 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2805 | /* bodyless requests have a known length */ |
| 2806 | if (!use_close_only) |
| 2807 | txn->flags |= TX_REQ_XFER_LEN; |
| 2808 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2809 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2810 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2811 | req->analyse_exp = TICK_ETERNITY; |
| 2812 | return 1; |
| 2813 | |
| 2814 | return_bad_req: |
| 2815 | /* We centralize bad requests processing here */ |
| 2816 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2817 | /* we detected a parsing error. We want to archive this request |
| 2818 | * in the dedicated proxy area for later troubleshooting. |
| 2819 | */ |
| 2820 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2821 | } |
| 2822 | |
| 2823 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2824 | txn->status = 400; |
| 2825 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2826 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2827 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2828 | if (s->listener->counters) |
| 2829 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2830 | |
| 2831 | return_prx_cond: |
| 2832 | if (!(s->flags & SN_ERR_MASK)) |
| 2833 | s->flags |= SN_ERR_PRXCOND; |
| 2834 | if (!(s->flags & SN_FINST_MASK)) |
| 2835 | s->flags |= SN_FINST_R; |
| 2836 | |
| 2837 | req->analysers = 0; |
| 2838 | req->analyse_exp = TICK_ETERNITY; |
| 2839 | return 0; |
| 2840 | } |
| 2841 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2842 | /* We reached the stats page through a POST request. |
| 2843 | * Parse the posted data and enable/disable servers if necessary. |
| 2844 | * Returns 0 if request was parsed. |
| 2845 | * Returns 1 if there was a problem parsing the posted data. |
| 2846 | */ |
| 2847 | int http_process_req_stat_post(struct session *s, struct buffer *req) |
| 2848 | { |
| 2849 | struct http_txn *txn = &s->txn; |
| 2850 | struct proxy *px; |
| 2851 | struct server *sv; |
| 2852 | |
| 2853 | char *backend = NULL; |
| 2854 | int action = 0; |
| 2855 | |
| 2856 | char *first_param, *cur_param, *next_param, *end_params; |
| 2857 | |
| 2858 | first_param = req->data + txn->req.eoh + 2; |
| 2859 | end_params = first_param + txn->req.hdr_content_len; |
| 2860 | |
| 2861 | cur_param = next_param = end_params; |
| 2862 | |
| 2863 | if (end_params >= req->data + req->size) { |
| 2864 | /* Prevent buffer overflow */ |
| 2865 | s->data_ctx.stats.st_code = STAT_STATUS_EXCD; |
| 2866 | return 1; |
| 2867 | } |
| 2868 | else if (end_params > req->data + req->l) { |
| 2869 | /* This condition also rejects a request with Expect: 100-continue */ |
| 2870 | s->data_ctx.stats.st_code = STAT_STATUS_EXCD; |
| 2871 | return 1; |
| 2872 | } |
| 2873 | |
| 2874 | *end_params = '\0'; |
| 2875 | |
| 2876 | s->data_ctx.stats.st_code = STAT_STATUS_NONE; |
| 2877 | |
| 2878 | /* |
| 2879 | * Parse the parameters in reverse order to only store the last value. |
| 2880 | * From the html form, the backend and the action are at the end. |
| 2881 | */ |
| 2882 | while (cur_param > first_param) { |
| 2883 | char *key, *value; |
| 2884 | |
| 2885 | cur_param--; |
| 2886 | if ((*cur_param == '&') || (cur_param == first_param)) { |
| 2887 | /* Parse the key */ |
| 2888 | key = cur_param; |
| 2889 | if (cur_param != first_param) { |
| 2890 | /* delimit the string for the next loop */ |
| 2891 | *key++ = '\0'; |
| 2892 | } |
| 2893 | |
| 2894 | /* Parse the value */ |
| 2895 | value = key; |
| 2896 | while (*value != '\0' && *value != '=') { |
| 2897 | value++; |
| 2898 | } |
| 2899 | if (*value == '=') { |
| 2900 | /* Ok, a value is found, we can mark the end of the key */ |
| 2901 | *value++ = '\0'; |
| 2902 | } |
| 2903 | |
| 2904 | /* Now we can check the key to see what to do */ |
| 2905 | if (!backend && strcmp(key, "b") == 0) { |
| 2906 | backend = value; |
| 2907 | } |
| 2908 | else if (!action && strcmp(key, "action") == 0) { |
| 2909 | if (strcmp(value, "disable") == 0) { |
| 2910 | action = 1; |
| 2911 | } |
| 2912 | else if (strcmp(value, "enable") == 0) { |
| 2913 | action = 2; |
| 2914 | } else { |
| 2915 | /* unknown action, no need to continue */ |
| 2916 | break; |
| 2917 | } |
| 2918 | } |
| 2919 | else if (strcmp(key, "s") == 0) { |
| 2920 | if (backend && action && get_backend_server(backend, value, &px, &sv)) { |
| 2921 | switch (action) { |
| 2922 | case 1: |
| 2923 | if (! (sv->state & SRV_MAINTAIN)) { |
| 2924 | /* Not already in maintenance, we can change the server state */ |
| 2925 | sv->state |= SRV_MAINTAIN; |
| 2926 | set_server_down(sv); |
| 2927 | s->data_ctx.stats.st_code = STAT_STATUS_DONE; |
| 2928 | } |
| 2929 | break; |
| 2930 | case 2: |
| 2931 | if ((sv->state & SRV_MAINTAIN)) { |
| 2932 | /* Already in maintenance, we can change the server state */ |
| 2933 | set_server_up(sv); |
Willy Tarreau | 7046130 | 2010-10-22 14:39:02 +0200 | [diff] [blame^] | 2934 | sv->health = sv->rise; /* up, but will fall down at first failure */ |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2935 | s->data_ctx.stats.st_code = STAT_STATUS_DONE; |
| 2936 | } |
| 2937 | break; |
| 2938 | } |
| 2939 | } |
| 2940 | } |
| 2941 | next_param = cur_param; |
| 2942 | } |
| 2943 | } |
| 2944 | return 0; |
| 2945 | } |
| 2946 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2947 | /* This stream analyser runs all HTTP request processing which is common to |
| 2948 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2949 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2950 | * 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] | 2951 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2952 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2953 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2954 | 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] | 2955 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2956 | struct http_txn *txn = &s->txn; |
| 2957 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2958 | struct acl_cond *cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2959 | struct req_acl_rule *req_acl, *req_acl_final = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2960 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 2961 | struct cond_wordlist *wl; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2962 | int del_ka, del_cl, do_stats; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2963 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2964 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2965 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2966 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2967 | return 0; |
| 2968 | } |
| 2969 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2970 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2971 | req->analyse_exp = TICK_ETERNITY; |
| 2972 | |
| 2973 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2974 | now_ms, __FUNCTION__, |
| 2975 | s, |
| 2976 | req, |
| 2977 | req->rex, req->wex, |
| 2978 | req->flags, |
| 2979 | req->l, |
| 2980 | req->analysers); |
| 2981 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2982 | /* first check whether we have some ACLs set to block this request */ |
| 2983 | list_for_each_entry(cond, &px->block_cond, list) { |
| 2984 | int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2985 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2986 | ret = acl_pass(ret); |
| 2987 | if (cond->pol == ACL_COND_UNLESS) |
| 2988 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2989 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2990 | if (ret) { |
| 2991 | txn->status = 403; |
| 2992 | /* let's log the request time */ |
| 2993 | s->logs.tv_request = now; |
| 2994 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2995 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2996 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2997 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2998 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2999 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3000 | do_stats = stats_check_uri(s, px); |
| 3001 | |
| 3002 | 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] | 3003 | int ret = 1; |
| 3004 | |
| 3005 | if (req_acl->action >= PR_REQ_ACL_ACT_MAX) |
| 3006 | continue; |
| 3007 | |
| 3008 | /* check condition, but only if attached */ |
Krzysztof Olędzki | 711ad9e | 2010-02-01 12:36:53 +0100 | [diff] [blame] | 3009 | if (req_acl->cond) { |
| 3010 | ret = acl_exec_cond(req_acl->cond, px, s, txn, ACL_DIR_REQ); |
| 3011 | ret = acl_pass(ret); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 3012 | |
Krzysztof Olędzki | 711ad9e | 2010-02-01 12:36:53 +0100 | [diff] [blame] | 3013 | if (req_acl->cond->pol == ACL_COND_UNLESS) |
| 3014 | ret = !ret; |
| 3015 | } |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3016 | |
| 3017 | if (ret) { |
| 3018 | req_acl_final = req_acl; |
| 3019 | break; |
| 3020 | } |
| 3021 | } |
| 3022 | |
| 3023 | if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_DENY) { |
| 3024 | txn->status = 403; |
| 3025 | s->logs.tv_request = now; |
| 3026 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3027 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3028 | goto return_prx_cond; |
| 3029 | } |
| 3030 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3031 | /* try headers filters */ |
| 3032 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3033 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3034 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3035 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3036 | /* has the request been denied ? */ |
| 3037 | if (txn->flags & TX_CLDENY) { |
| 3038 | /* no need to go further */ |
| 3039 | txn->status = 403; |
| 3040 | /* let's log the request time */ |
| 3041 | s->logs.tv_request = now; |
| 3042 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3043 | session_inc_http_err_ctr(s); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3044 | goto return_prx_cond; |
| 3045 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3046 | |
| 3047 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3048 | * which may be the same as the connect timeout if unspecified. |
| 3049 | * If unset, then set it to zero because we really want it to |
| 3050 | * eventually expire. We build the tarpit as an analyser. |
| 3051 | */ |
| 3052 | if (txn->flags & TX_CLTARPIT) { |
| 3053 | buffer_erase(s->req); |
| 3054 | /* wipe the request out so that we can drop the connection early |
| 3055 | * if the client closes first. |
| 3056 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3057 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3058 | req->analysers = 0; /* remove switching rules etc... */ |
| 3059 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3060 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3061 | if (!req->analyse_exp) |
| 3062 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3063 | session_inc_http_err_ctr(s); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3064 | return 1; |
| 3065 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3066 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3067 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3068 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 3069 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3070 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 3071 | * 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] | 3072 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 3073 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 3074 | * The method consists in checking if options changed between the two calls |
| 3075 | * (implying that either one is non-null, or one of them is non-null and we |
| 3076 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 3077 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3078 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3079 | del_cl = del_ka = 0; |
| 3080 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 3081 | if ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 3082 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 3083 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 3084 | (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] | 3085 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3086 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3087 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE) |
| 3088 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 3089 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 3090 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3091 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3092 | tmp = TX_CON_WANT_CLO; |
| 3093 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3094 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 3095 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3096 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3097 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 3098 | /* parse the Connection header and possibly clean it */ |
| 3099 | int to_del = 0; |
| 3100 | if ((txn->flags & TX_REQ_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 3101 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 3102 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3103 | to_del |= 2; /* remove "keep-alive" */ |
| 3104 | if (!(txn->flags & TX_REQ_VER_11)) |
| 3105 | to_del |= 1; /* remove "close" */ |
| 3106 | http_parse_connection_header(txn, msg, req, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3107 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3108 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3109 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 3110 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 3111 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 3112 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 3113 | (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */ |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3114 | (((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) && /* httpclose without pretend-ka... */ |
| 3115 | 1/*!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)*/) || /* ... +any = forceclose */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 3116 | !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */ |
| 3117 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3118 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 3119 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3120 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3121 | /* add request headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 3122 | list_for_each_entry(wl, &px->req_add, list) { |
Willy Tarreau | 8abd4cd | 2010-01-31 14:30:44 +0100 | [diff] [blame] | 3123 | if (wl->cond) { |
| 3124 | int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ); |
| 3125 | ret = acl_pass(ret); |
| 3126 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3127 | ret = !ret; |
| 3128 | if (!ret) |
| 3129 | continue; |
| 3130 | } |
| 3131 | |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 3132 | 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] | 3133 | goto return_bad_req; |
| 3134 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3135 | |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3136 | if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_HTTP_AUTH) { |
| 3137 | struct chunk msg; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3138 | char *realm = req_acl->http_auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3139 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3140 | if (!realm) |
| 3141 | realm = do_stats?STATS_DEFAULT_REALM:px->id; |
| 3142 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 3143 | 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] | 3144 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
| 3145 | txn->status = 401; |
| 3146 | stream_int_retnclose(req->prod, &msg); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3147 | /* on 401 we still count one error, because normal browsing |
| 3148 | * won't significantly increase the counter but brute force |
| 3149 | * attempts will. |
| 3150 | */ |
| 3151 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3152 | goto return_prx_cond; |
| 3153 | } |
| 3154 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3155 | if (do_stats) { |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3156 | struct stats_admin_rule *stats_admin_rule; |
| 3157 | |
| 3158 | /* We need to provide stats for this request. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3159 | * FIXME!!! that one is rather dangerous, we want to |
| 3160 | * make it follow standard rules (eg: clear req->analysers). |
| 3161 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3162 | |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3163 | /* now check whether we have some admin rules for this request */ |
| 3164 | list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) { |
| 3165 | int ret = 1; |
| 3166 | |
| 3167 | if (stats_admin_rule->cond) { |
| 3168 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ); |
| 3169 | ret = acl_pass(ret); |
| 3170 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3171 | ret = !ret; |
| 3172 | } |
| 3173 | |
| 3174 | if (ret) { |
| 3175 | /* no rule, or the rule matches */ |
| 3176 | s->data_ctx.stats.flags |= STAT_ADMIN; |
| 3177 | break; |
| 3178 | } |
| 3179 | } |
| 3180 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3181 | /* Was the status page requested with a POST ? */ |
| 3182 | if (txn->meth == HTTP_METH_POST) { |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3183 | if (s->data_ctx.stats.flags & STAT_ADMIN) { |
| 3184 | http_process_req_stat_post(s, req); |
| 3185 | } else { |
| 3186 | s->data_ctx.stats.st_code = STAT_STATUS_DENY; |
| 3187 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3188 | } |
| 3189 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3190 | s->logs.tv_request = now; |
| 3191 | s->data_source = DATA_SRC_STATS; |
| 3192 | s->data_state = DATA_ST_INIT; |
| 3193 | s->task->nice = -32; /* small boost for HTTP statistics */ |
| 3194 | stream_int_register_handler(s->rep->prod, http_stats_io_handler); |
| 3195 | s->rep->prod->private = s; |
| 3196 | s->rep->prod->st0 = s->rep->prod->st1 = 0; |
| 3197 | req->analysers = 0; |
| 3198 | |
| 3199 | return 0; |
| 3200 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3201 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3202 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3203 | /* check whether we have some ACLs set to redirect this request */ |
| 3204 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3205 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3206 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3207 | if (rule->cond) { |
| 3208 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
| 3209 | ret = acl_pass(ret); |
| 3210 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3211 | ret = !ret; |
| 3212 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3213 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3214 | if (ret) { |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3215 | struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3216 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3217 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3218 | /* build redirect message */ |
| 3219 | switch(rule->code) { |
| 3220 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3221 | msg_fmt = HTTP_303; |
| 3222 | break; |
| 3223 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3224 | msg_fmt = HTTP_301; |
| 3225 | break; |
| 3226 | case 302: |
| 3227 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3228 | msg_fmt = HTTP_302; |
| 3229 | break; |
| 3230 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3231 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3232 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3233 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3234 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3235 | switch(rule->type) { |
| 3236 | case REDIRECT_TYPE_PREFIX: { |
| 3237 | const char *path; |
| 3238 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3239 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3240 | path = http_get_path(txn); |
| 3241 | /* build message using path */ |
| 3242 | if (path) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3243 | 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] | 3244 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3245 | int qs = 0; |
| 3246 | while (qs < pathlen) { |
| 3247 | if (path[qs] == '?') { |
| 3248 | pathlen = qs; |
| 3249 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3250 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3251 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3252 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3253 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3254 | } else { |
| 3255 | path = "/"; |
| 3256 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3257 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3258 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3259 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3260 | goto return_bad_req; |
| 3261 | |
| 3262 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3263 | * add anything, otherwise it makes it hard for the user to |
| 3264 | * configure a self-redirection. |
| 3265 | */ |
| 3266 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3267 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3268 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3269 | } |
| 3270 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3271 | /* add path */ |
| 3272 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 3273 | rdr.len += pathlen; |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 3274 | |
| 3275 | /* append a slash at the end of the location is needed and missing */ |
| 3276 | if (rdr.len && rdr.str[rdr.len - 1] != '/' && |
| 3277 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3278 | if (rdr.len > rdr.size - 5) |
| 3279 | goto return_bad_req; |
| 3280 | rdr.str[rdr.len] = '/'; |
| 3281 | rdr.len++; |
| 3282 | } |
| 3283 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3284 | break; |
| 3285 | } |
| 3286 | case REDIRECT_TYPE_LOCATION: |
| 3287 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3288 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3289 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3290 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3291 | /* add location */ |
| 3292 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3293 | rdr.len += rule->rdr_len; |
| 3294 | break; |
| 3295 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3296 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3297 | if (rule->cookie_len) { |
| 3298 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 3299 | rdr.len += 14; |
| 3300 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 3301 | rdr.len += rule->cookie_len; |
| 3302 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 3303 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3304 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3305 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3306 | /* add end of headers and the keep-alive/close status. |
| 3307 | * We may choose to set keep-alive if the Location begins |
| 3308 | * with a slash, because the client will come back to the |
| 3309 | * same server. |
| 3310 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3311 | txn->status = rule->code; |
| 3312 | /* let's log the request time */ |
| 3313 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3314 | |
| 3315 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
| 3316 | (txn->flags & TX_REQ_XFER_LEN) && |
| 3317 | !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len && |
| 3318 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3319 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3320 | /* keep-alive possible */ |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3321 | if (!(txn->flags & TX_REQ_VER_11)) { |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3322 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3323 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3324 | rdr.len += 30; |
| 3325 | } else { |
| 3326 | memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24); |
| 3327 | rdr.len += 24; |
| 3328 | } |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3329 | } |
| 3330 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3331 | rdr.len += 4; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3332 | buffer_write(req->prod->ob, rdr.str, rdr.len); |
| 3333 | /* "eat" the request */ |
| 3334 | buffer_ignore(req, msg->sov - msg->som); |
| 3335 | msg->som = msg->sov; |
| 3336 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 3337 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3338 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3339 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3340 | break; |
| 3341 | } else { |
| 3342 | /* keep-alive not possible */ |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3343 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3344 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3345 | rdr.len += 29; |
| 3346 | } else { |
| 3347 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3348 | rdr.len += 23; |
| 3349 | } |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 3350 | stream_int_retnclose(req->prod, &rdr); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3351 | goto return_prx_cond; |
| 3352 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3353 | } |
| 3354 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3355 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3356 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3357 | * If this happens, then the data will not come immediately, so we must |
| 3358 | * send all what we have without waiting. Note that due to the small gain |
| 3359 | * in waiting for the body of the request, it's easier to simply put the |
| 3360 | * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 3361 | * itself once used. |
| 3362 | */ |
| 3363 | req->flags |= BF_SEND_DONTWAIT; |
| 3364 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3365 | /* that's OK for us now, let's move on to next analysers */ |
| 3366 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3367 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3368 | return_bad_req: |
| 3369 | /* We centralize bad requests processing here */ |
| 3370 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3371 | /* we detected a parsing error. We want to archive this request |
| 3372 | * in the dedicated proxy area for later troubleshooting. |
| 3373 | */ |
| 3374 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 3375 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3376 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3377 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3378 | txn->status = 400; |
| 3379 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3380 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3381 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3382 | if (s->listener->counters) |
| 3383 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3384 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3385 | return_prx_cond: |
| 3386 | if (!(s->flags & SN_ERR_MASK)) |
| 3387 | s->flags |= SN_ERR_PRXCOND; |
| 3388 | if (!(s->flags & SN_FINST_MASK)) |
| 3389 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3390 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3391 | req->analysers = 0; |
| 3392 | req->analyse_exp = TICK_ETERNITY; |
| 3393 | return 0; |
| 3394 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3395 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3396 | /* This function performs all the processing enabled for the current request. |
| 3397 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3398 | * needs more data, encounters an error, or wants to immediately abort the |
| 3399 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3400 | */ |
| 3401 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 3402 | { |
| 3403 | struct http_txn *txn = &s->txn; |
| 3404 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3405 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3406 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3407 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3408 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3409 | return 0; |
| 3410 | } |
| 3411 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3412 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 3413 | now_ms, __FUNCTION__, |
| 3414 | s, |
| 3415 | req, |
| 3416 | req->rex, req->wex, |
| 3417 | req->flags, |
| 3418 | req->l, |
| 3419 | req->analysers); |
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 | /* |
| 3422 | * Right now, we know that we have processed the entire headers |
| 3423 | * and that unwanted requests have been filtered out. We can do |
| 3424 | * whatever we want with the remaining request. Also, now we |
| 3425 | * may have separate values for ->fe, ->be. |
| 3426 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3427 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3428 | /* |
| 3429 | * If HTTP PROXY is set we simply get remote server address |
| 3430 | * parsing incoming request. |
| 3431 | */ |
| 3432 | 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] | 3433 | 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] | 3434 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3435 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3436 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3437 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3438 | * Note that doing so might move headers in the request, but |
| 3439 | * the fields will stay coherent and the URI will not move. |
| 3440 | * This should only be performed in the backend. |
| 3441 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3442 | 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] | 3443 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3444 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3445 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3446 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3447 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3448 | * so let's do the same now. |
| 3449 | */ |
| 3450 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3451 | /* It needs to look into the URI unless persistence must be ignored */ |
| 3452 | if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3453 | 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] | 3454 | } |
| 3455 | |
| 3456 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3457 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3458 | * asks for it. |
| 3459 | */ |
| 3460 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
| 3461 | if (s->cli_addr.ss_family == AF_INET) { |
| 3462 | /* Add an X-Forwarded-For header unless the source IP is |
| 3463 | * in the 'except' network range. |
| 3464 | */ |
| 3465 | if ((!s->fe->except_mask.s_addr || |
| 3466 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
| 3467 | != s->fe->except_net.s_addr) && |
| 3468 | (!s->be->except_mask.s_addr || |
| 3469 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr) |
| 3470 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3471 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3472 | unsigned char *pn; |
| 3473 | pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3474 | |
| 3475 | /* Note: we rely on the backend to get the header name to be used for |
| 3476 | * x-forwarded-for, because the header is really meant for the backends. |
| 3477 | * However, if the backend did not specify any option, we have to rely |
| 3478 | * on the frontend's header name. |
| 3479 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3480 | if (s->be->fwdfor_hdr_len) { |
| 3481 | len = s->be->fwdfor_hdr_len; |
| 3482 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3483 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3484 | len = s->fe->fwdfor_hdr_len; |
| 3485 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3486 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3487 | 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] | 3488 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3489 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3490 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3491 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3492 | } |
| 3493 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3494 | else if (s->cli_addr.ss_family == AF_INET6) { |
| 3495 | /* FIXME: for the sake of completeness, we should also support |
| 3496 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3497 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3498 | int len; |
| 3499 | char pn[INET6_ADDRSTRLEN]; |
| 3500 | inet_ntop(AF_INET6, |
| 3501 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 3502 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3503 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3504 | /* Note: we rely on the backend to get the header name to be used for |
| 3505 | * x-forwarded-for, because the header is really meant for the backends. |
| 3506 | * However, if the backend did not specify any option, we have to rely |
| 3507 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3508 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3509 | if (s->be->fwdfor_hdr_len) { |
| 3510 | len = s->be->fwdfor_hdr_len; |
| 3511 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 3512 | } else { |
| 3513 | len = s->fe->fwdfor_hdr_len; |
| 3514 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3515 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3516 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3517 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3518 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3519 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3520 | goto return_bad_req; |
| 3521 | } |
| 3522 | } |
| 3523 | |
| 3524 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3525 | * 10: add X-Original-To if either the frontend or the backend |
| 3526 | * asks for it. |
| 3527 | */ |
| 3528 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3529 | |
| 3530 | /* FIXME: don't know if IPv6 can handle that case too. */ |
| 3531 | if (s->cli_addr.ss_family == AF_INET) { |
| 3532 | /* Add an X-Original-To header unless the destination IP is |
| 3533 | * in the 'except' network range. |
| 3534 | */ |
| 3535 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 3536 | get_frt_addr(s); |
| 3537 | |
| 3538 | if ((!s->fe->except_mask_to.s_addr || |
| 3539 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr) |
| 3540 | != s->fe->except_to.s_addr) && |
| 3541 | (!s->be->except_mask_to.s_addr || |
| 3542 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
| 3543 | != s->be->except_to.s_addr)) { |
| 3544 | int len; |
| 3545 | unsigned char *pn; |
| 3546 | pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr; |
| 3547 | |
| 3548 | /* Note: we rely on the backend to get the header name to be used for |
| 3549 | * x-original-to, because the header is really meant for the backends. |
| 3550 | * However, if the backend did not specify any option, we have to rely |
| 3551 | * on the frontend's header name. |
| 3552 | */ |
| 3553 | if (s->be->orgto_hdr_len) { |
| 3554 | len = s->be->orgto_hdr_len; |
| 3555 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 3556 | } else { |
| 3557 | len = s->fe->orgto_hdr_len; |
| 3558 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3559 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3560 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3561 | |
| 3562 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3563 | &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3564 | goto return_bad_req; |
| 3565 | } |
| 3566 | } |
| 3567 | } |
| 3568 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3569 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */ |
| 3570 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3571 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || |
| 3572 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3573 | unsigned int want_flags = 0; |
| 3574 | |
| 3575 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3576 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3577 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) && |
| 3578 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3579 | want_flags |= TX_CON_CLO_SET; |
| 3580 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3581 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
| 3582 | !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) || |
| 3583 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3584 | want_flags |= TX_CON_KAL_SET; |
| 3585 | } |
| 3586 | |
| 3587 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 3588 | http_change_connection_header(txn, msg, req, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3589 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3590 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3591 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3592 | /* If we have no server assigned yet and we're balancing on url_param |
| 3593 | * with a POST request, we may be interested in checking the body for |
| 3594 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3595 | */ |
| 3596 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3597 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3598 | s->be->url_param_post_limit != 0 && |
| 3599 | (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3600 | 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] | 3601 | buffer_dont_connect(req); |
| 3602 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3603 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3604 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3605 | if (txn->flags & TX_REQ_XFER_LEN) |
| 3606 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3607 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3608 | /************************************************************* |
| 3609 | * OK, that's finished for the headers. We have done what we * |
| 3610 | * could. Let's switch to the DATA state. * |
| 3611 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3612 | req->analyse_exp = TICK_ETERNITY; |
| 3613 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3614 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3615 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3616 | /* OK let's go on with the BODY now */ |
| 3617 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3618 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3619 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3620 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3621 | /* we detected a parsing error. We want to archive this request |
| 3622 | * in the dedicated proxy area for later troubleshooting. |
| 3623 | */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3624 | 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] | 3625 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3626 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3627 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3628 | txn->status = 400; |
| 3629 | req->analysers = 0; |
| 3630 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3631 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3632 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3633 | if (s->listener->counters) |
| 3634 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3635 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3636 | if (!(s->flags & SN_ERR_MASK)) |
| 3637 | s->flags |= SN_ERR_PRXCOND; |
| 3638 | if (!(s->flags & SN_FINST_MASK)) |
| 3639 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3640 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3641 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3642 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3643 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3644 | * returns zero, at the beginning because it prevents any other processing |
| 3645 | * from occurring, and at the end because it terminates the request. |
| 3646 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3647 | 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] | 3648 | { |
| 3649 | struct http_txn *txn = &s->txn; |
| 3650 | |
| 3651 | /* This connection is being tarpitted. The CLIENT side has |
| 3652 | * already set the connect expiration date to the right |
| 3653 | * timeout. We just have to check that the client is still |
| 3654 | * there and that the timeout has not expired. |
| 3655 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3656 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3657 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 3658 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3659 | return 0; |
| 3660 | |
| 3661 | /* We will set the queue timer to the time spent, just for |
| 3662 | * logging purposes. We fake a 500 server error, so that the |
| 3663 | * attacker will not suspect his connection has been tarpitted. |
| 3664 | * It will not cause trouble to the logs because we can exclude |
| 3665 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3666 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3667 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3668 | |
| 3669 | txn->status = 500; |
| 3670 | if (req->flags != BF_READ_ERROR) |
| 3671 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 3672 | |
| 3673 | req->analysers = 0; |
| 3674 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3675 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3676 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3677 | if (s->listener->counters) |
| 3678 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3679 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3680 | if (!(s->flags & SN_ERR_MASK)) |
| 3681 | s->flags |= SN_ERR_PRXCOND; |
| 3682 | if (!(s->flags & SN_FINST_MASK)) |
| 3683 | s->flags |= SN_FINST_T; |
| 3684 | return 0; |
| 3685 | } |
| 3686 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3687 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3688 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3689 | * must only be called after the standard HTTP request processing has occurred, |
| 3690 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3691 | * read more data, or 1 once it has completed its analysis. |
| 3692 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3693 | 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] | 3694 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3695 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3696 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3697 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3698 | |
| 3699 | /* We have to parse the HTTP request body to find any required data. |
| 3700 | * "balance url_param check_post" should have been the only way to get |
| 3701 | * into this. We were brought here after HTTP header analysis, so all |
| 3702 | * related structures are ready. |
| 3703 | */ |
| 3704 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3705 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3706 | goto missing_data; |
| 3707 | |
| 3708 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3709 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3710 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3711 | */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 3712 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3713 | struct hdr_ctx ctx; |
| 3714 | ctx.idx = 0; |
| 3715 | /* Expect is allowed in 1.1, look for it */ |
| 3716 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3717 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3718 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3719 | } |
| 3720 | } |
| 3721 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3722 | } |
| 3723 | |
| 3724 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3725 | /* we have msg->col and msg->sov which both point to the first |
| 3726 | * byte of message body. msg->som still points to the beginning |
| 3727 | * of the message. We must save the body in req->lr because it |
| 3728 | * survives buffer re-alignments. |
| 3729 | */ |
| 3730 | req->lr = req->data + msg->sov; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3731 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3732 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3733 | else |
| 3734 | msg->msg_state = HTTP_MSG_DATA; |
| 3735 | } |
| 3736 | |
| 3737 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3738 | /* read the chunk size and assign it to ->hdr_content_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3739 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3740 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3741 | */ |
| 3742 | int ret = http_parse_chunk_size(req, msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3743 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3744 | if (!ret) |
| 3745 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3746 | else if (ret < 0) { |
| 3747 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3748 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3749 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3750 | } |
| 3751 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3752 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3753 | * We have the first non-header byte in msg->col, which is either the |
| 3754 | * beginning of the chunk size or of the data. The first data byte is in |
| 3755 | * msg->sov, which is equal to msg->col when not using transfer-encoding. |
| 3756 | * 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] | 3757 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3758 | |
| 3759 | if (msg->hdr_content_len < limit) |
| 3760 | limit = msg->hdr_content_len; |
| 3761 | |
Willy Tarreau | 7c96f67 | 2009-12-27 22:47:25 +0100 | [diff] [blame] | 3762 | 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] | 3763 | goto http_end; |
| 3764 | |
| 3765 | missing_data: |
| 3766 | /* we get here if we need to wait for more data */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3767 | if (req->flags & BF_FULL) { |
| 3768 | session_inc_http_err_ctr(s); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3769 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3770 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3771 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3772 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 3773 | txn->status = 408; |
| 3774 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3775 | |
| 3776 | if (!(s->flags & SN_ERR_MASK)) |
| 3777 | s->flags |= SN_ERR_CLITO; |
| 3778 | if (!(s->flags & SN_FINST_MASK)) |
| 3779 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3780 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3781 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3782 | |
| 3783 | /* we get here if we need to wait for more data */ |
| 3784 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3785 | /* Not enough data. We'll re-use the http-request |
| 3786 | * timeout here. Ideally, we should set the timeout |
| 3787 | * relative to the accept() date. We just set the |
| 3788 | * request timeout once at the beginning of the |
| 3789 | * request. |
| 3790 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3791 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3792 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3793 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3794 | return 0; |
| 3795 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3796 | |
| 3797 | http_end: |
| 3798 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3799 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3800 | req->analysers &= ~an_bit; |
| 3801 | req->analyse_exp = TICK_ETERNITY; |
| 3802 | return 1; |
| 3803 | |
| 3804 | return_bad_req: /* let's centralize all bad requests */ |
| 3805 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3806 | txn->status = 400; |
| 3807 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 3808 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3809 | if (!(s->flags & SN_ERR_MASK)) |
| 3810 | s->flags |= SN_ERR_PRXCOND; |
| 3811 | if (!(s->flags & SN_FINST_MASK)) |
| 3812 | s->flags |= SN_FINST_R; |
| 3813 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3814 | return_err_msg: |
| 3815 | req->analysers = 0; |
| 3816 | s->fe->counters.failed_req++; |
| 3817 | if (s->listener->counters) |
| 3818 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3819 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3820 | } |
| 3821 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3822 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3823 | * right now but it works. |
| 3824 | */ |
| 3825 | void http_end_txn_clean_session(struct session *s) |
| 3826 | { |
| 3827 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3828 | * server's connections. We need a safer way to reinitialize buffer |
| 3829 | * flags. We also need a more accurate method for computing per-request |
| 3830 | * data. |
| 3831 | */ |
| 3832 | http_silent_debug(__LINE__, s); |
| 3833 | |
| 3834 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 3835 | s->req->cons->shutr(s->req->cons); |
| 3836 | s->req->cons->shutw(s->req->cons); |
| 3837 | |
| 3838 | http_silent_debug(__LINE__, s); |
| 3839 | |
| 3840 | if (s->flags & SN_BE_ASSIGNED) |
| 3841 | s->be->beconn--; |
| 3842 | |
| 3843 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3844 | session_process_counters(s); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 3845 | session_stop_backend_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3846 | |
| 3847 | if (s->txn.status) { |
| 3848 | int n; |
| 3849 | |
| 3850 | n = s->txn.status / 100; |
| 3851 | if (n < 1 || n > 5) |
| 3852 | n = 0; |
| 3853 | |
| 3854 | if (s->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 3855 | s->fe->counters.fe.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3856 | |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 3857 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3858 | (s->be->mode == PR_MODE_HTTP)) |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 3859 | s->be->counters.be.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3860 | } |
| 3861 | |
| 3862 | /* don't count other requests' data */ |
| 3863 | s->logs.bytes_in -= s->req->l - s->req->send_max; |
| 3864 | s->logs.bytes_out -= s->rep->l - s->rep->send_max; |
| 3865 | |
| 3866 | /* let's do a final log if we need it */ |
| 3867 | if (s->logs.logwait && |
| 3868 | !(s->flags & SN_MONITOR) && |
| 3869 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 3870 | s->do_log(s); |
| 3871 | } |
| 3872 | |
| 3873 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3874 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 3875 | tv_zero(&s->logs.tv_request); |
| 3876 | s->logs.t_queue = -1; |
| 3877 | s->logs.t_connect = -1; |
| 3878 | s->logs.t_data = -1; |
| 3879 | s->logs.t_close = 0; |
| 3880 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3881 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 3882 | |
| 3883 | s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max; |
| 3884 | s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max; |
| 3885 | |
| 3886 | if (s->pend_pos) |
| 3887 | pendconn_free(s->pend_pos); |
| 3888 | |
| 3889 | if (s->srv) { |
| 3890 | if (s->flags & SN_CURR_SESS) { |
| 3891 | s->flags &= ~SN_CURR_SESS; |
| 3892 | s->srv->cur_sess--; |
| 3893 | } |
| 3894 | if (may_dequeue_tasks(s->srv, s->be)) |
| 3895 | process_srv_queue(s->srv); |
| 3896 | } |
| 3897 | |
| 3898 | if (unlikely(s->srv_conn)) |
| 3899 | sess_change_server(s, NULL); |
| 3900 | s->srv = NULL; |
| 3901 | |
| 3902 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
| 3903 | s->req->cons->fd = -1; /* just to help with debugging */ |
| 3904 | s->req->cons->err_type = SI_ET_NONE; |
| 3905 | s->req->cons->err_loc = NULL; |
| 3906 | s->req->cons->exp = TICK_ETERNITY; |
| 3907 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3908 | s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST); |
| 3909 | 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); |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3910 | s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST|SN_IGNORE_PRST); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3911 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 3912 | s->txn.meth = 0; |
| 3913 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 3914 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 3915 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3916 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 3917 | |
| 3918 | /* if the request buffer is not empty, it means we're |
| 3919 | * about to process another request, so send pending |
| 3920 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3921 | * Just don't do this if the buffer is close to be full, |
| 3922 | * because the request will wait for it to flush a little |
| 3923 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3924 | */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3925 | if (s->req->l > s->req->send_max) { |
| 3926 | if (s->rep->send_max && |
| 3927 | !(s->rep->flags & BF_FULL) && |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3928 | s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite) |
| 3929 | s->rep->flags |= BF_EXPECT_MORE; |
| 3930 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3931 | |
| 3932 | /* we're removing the analysers, we MUST re-enable events detection */ |
| 3933 | buffer_auto_read(s->req); |
| 3934 | buffer_auto_close(s->req); |
| 3935 | buffer_auto_read(s->rep); |
| 3936 | buffer_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3937 | |
| 3938 | /* make ->lr point to the first non-forwarded byte */ |
| 3939 | s->req->lr = s->req->w + s->req->send_max; |
| 3940 | if (s->req->lr >= s->req->data + s->req->size) |
| 3941 | s->req->lr -= s->req->size; |
| 3942 | s->rep->lr = s->rep->w + s->rep->send_max; |
| 3943 | if (s->rep->lr >= s->rep->data + s->rep->size) |
| 3944 | s->rep->lr -= s->req->size; |
| 3945 | |
Willy Tarreau | 6df7a0e | 2010-08-03 11:52:10 +0200 | [diff] [blame] | 3946 | s->req->analysers |= s->listener->analysers; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3947 | s->rep->analysers = 0; |
| 3948 | |
| 3949 | http_silent_debug(__LINE__, s); |
| 3950 | } |
| 3951 | |
| 3952 | |
| 3953 | /* This function updates the request state machine according to the response |
| 3954 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3955 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3956 | * it is only used to find when a request/response couple is complete. Both |
| 3957 | * this function and its equivalent should loop until both return zero. It |
| 3958 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3959 | */ |
| 3960 | int http_sync_req_state(struct session *s) |
| 3961 | { |
| 3962 | struct buffer *buf = s->req; |
| 3963 | struct http_txn *txn = &s->txn; |
| 3964 | unsigned int old_flags = buf->flags; |
| 3965 | unsigned int old_state = txn->req.msg_state; |
| 3966 | |
| 3967 | http_silent_debug(__LINE__, s); |
| 3968 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 3969 | return 0; |
| 3970 | |
| 3971 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3972 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 3973 | * We can shut the read side unless we want to abort_on_close, |
| 3974 | * or we have a POST request. The issue with POST requests is |
| 3975 | * that some browsers still send a CRLF after the request, and |
| 3976 | * this CRLF must be read so that it does not remain in the kernel |
| 3977 | * buffers, otherwise a close could cause an RST on some systems |
| 3978 | * (eg: Linux). |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3979 | */ |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 3980 | if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST) |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3981 | buffer_dont_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3982 | |
| 3983 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 3984 | goto wait_other_side; |
| 3985 | |
| 3986 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 3987 | /* The server has not finished to respond, so we |
| 3988 | * don't want to move in order not to upset it. |
| 3989 | */ |
| 3990 | goto wait_other_side; |
| 3991 | } |
| 3992 | |
| 3993 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 3994 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3995 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3996 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 3997 | goto wait_other_side; |
| 3998 | } |
| 3999 | |
| 4000 | /* When we get here, it means that both the request and the |
| 4001 | * response have finished receiving. Depending on the connection |
| 4002 | * mode, we'll have to wait for the last bytes to leave in either |
| 4003 | * direction, and sometimes for a close to be effective. |
| 4004 | */ |
| 4005 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4006 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4007 | /* Server-close mode : queue a connection close to the server */ |
| 4008 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4009 | buffer_shutw_now(buf); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4010 | } |
| 4011 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4012 | /* Option forceclose is set, or either side wants to close, |
| 4013 | * let's enforce it now that we're not expecting any new |
| 4014 | * data to come. The caller knows the session is complete |
| 4015 | * once both states are CLOSED. |
| 4016 | */ |
| 4017 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4018 | buffer_shutr_now(buf); |
| 4019 | buffer_shutw_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4020 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4021 | } |
| 4022 | else { |
| 4023 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4024 | * mode does not set the body analyser, we can't reach this place |
| 4025 | * in tunnel mode, so we're left with keep-alive only. |
| 4026 | * This mode is currently not implemented, we switch to tunnel mode. |
| 4027 | */ |
| 4028 | buffer_auto_read(buf); |
| 4029 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4030 | } |
| 4031 | |
| 4032 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 4033 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4034 | buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 4035 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4036 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 4037 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4038 | goto http_msg_closing; |
| 4039 | } |
| 4040 | else { |
| 4041 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4042 | goto http_msg_closed; |
| 4043 | } |
| 4044 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4045 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4046 | } |
| 4047 | |
| 4048 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4049 | http_msg_closing: |
| 4050 | /* nothing else to forward, just waiting for the output buffer |
| 4051 | * to be empty and for the shutw_now to take effect. |
| 4052 | */ |
| 4053 | if (buf->flags & BF_OUT_EMPTY) { |
| 4054 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4055 | goto http_msg_closed; |
| 4056 | } |
| 4057 | else if (buf->flags & BF_SHUTW) { |
| 4058 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4059 | goto wait_other_side; |
| 4060 | } |
| 4061 | } |
| 4062 | |
| 4063 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4064 | http_msg_closed: |
| 4065 | goto wait_other_side; |
| 4066 | } |
| 4067 | |
| 4068 | wait_other_side: |
| 4069 | http_silent_debug(__LINE__, s); |
| 4070 | return txn->req.msg_state != old_state || buf->flags != old_flags; |
| 4071 | } |
| 4072 | |
| 4073 | |
| 4074 | /* This function updates the response state machine according to the request |
| 4075 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4076 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4077 | * it is only used to find when a request/response couple is complete. Both |
| 4078 | * this function and its equivalent should loop until both return zero. It |
| 4079 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4080 | */ |
| 4081 | int http_sync_res_state(struct session *s) |
| 4082 | { |
| 4083 | struct buffer *buf = s->rep; |
| 4084 | struct http_txn *txn = &s->txn; |
| 4085 | unsigned int old_flags = buf->flags; |
| 4086 | unsigned int old_state = txn->rsp.msg_state; |
| 4087 | |
| 4088 | http_silent_debug(__LINE__, s); |
| 4089 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4090 | return 0; |
| 4091 | |
| 4092 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4093 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4094 | * still monitor the server connection for a possible close |
| 4095 | * while the request is being uploaded, so we don't disable |
| 4096 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4097 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4098 | /* buffer_dont_read(buf); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4099 | |
| 4100 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4101 | goto wait_other_side; |
| 4102 | |
| 4103 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4104 | /* The client seems to still be sending data, probably |
| 4105 | * because we got an error response during an upload. |
| 4106 | * We have the choice of either breaking the connection |
| 4107 | * or letting it pass through. Let's do the later. |
| 4108 | */ |
| 4109 | goto wait_other_side; |
| 4110 | } |
| 4111 | |
| 4112 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4113 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4114 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4115 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4116 | goto wait_other_side; |
| 4117 | } |
| 4118 | |
| 4119 | /* When we get here, it means that both the request and the |
| 4120 | * response have finished receiving. Depending on the connection |
| 4121 | * mode, we'll have to wait for the last bytes to leave in either |
| 4122 | * direction, and sometimes for a close to be effective. |
| 4123 | */ |
| 4124 | |
| 4125 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4126 | /* Server-close mode : shut read and wait for the request |
| 4127 | * side to close its output buffer. The caller will detect |
| 4128 | * when we're in DONE and the other is in CLOSED and will |
| 4129 | * catch that for the final cleanup. |
| 4130 | */ |
| 4131 | if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW))) |
| 4132 | buffer_shutr_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4133 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4134 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4135 | /* Option forceclose is set, or either side wants to close, |
| 4136 | * let's enforce it now that we're not expecting any new |
| 4137 | * data to come. The caller knows the session is complete |
| 4138 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4139 | */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4140 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
| 4141 | buffer_shutr_now(buf); |
| 4142 | buffer_shutw_now(buf); |
| 4143 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4144 | } |
| 4145 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4146 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4147 | * mode does not set the body analyser, we can't reach this place |
| 4148 | * in tunnel mode, so we're left with keep-alive only. |
| 4149 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4150 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4151 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4152 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4153 | } |
| 4154 | |
| 4155 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 4156 | /* if we've just closed an output, let's switch */ |
| 4157 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 4158 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4159 | goto http_msg_closing; |
| 4160 | } |
| 4161 | else { |
| 4162 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4163 | goto http_msg_closed; |
| 4164 | } |
| 4165 | } |
| 4166 | goto wait_other_side; |
| 4167 | } |
| 4168 | |
| 4169 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4170 | http_msg_closing: |
| 4171 | /* nothing else to forward, just waiting for the output buffer |
| 4172 | * to be empty and for the shutw_now to take effect. |
| 4173 | */ |
| 4174 | if (buf->flags & BF_OUT_EMPTY) { |
| 4175 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4176 | goto http_msg_closed; |
| 4177 | } |
| 4178 | else if (buf->flags & BF_SHUTW) { |
| 4179 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 4180 | s->be->counters.cli_aborts++; |
| 4181 | if (s->srv) |
| 4182 | s->srv->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4183 | goto wait_other_side; |
| 4184 | } |
| 4185 | } |
| 4186 | |
| 4187 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4188 | http_msg_closed: |
| 4189 | /* drop any pending data */ |
| 4190 | buffer_ignore(buf, buf->l - buf->send_max); |
| 4191 | buffer_auto_close(buf); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4192 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4193 | goto wait_other_side; |
| 4194 | } |
| 4195 | |
| 4196 | wait_other_side: |
| 4197 | http_silent_debug(__LINE__, s); |
| 4198 | return txn->rsp.msg_state != old_state || buf->flags != old_flags; |
| 4199 | } |
| 4200 | |
| 4201 | |
| 4202 | /* Resync the request and response state machines. Return 1 if either state |
| 4203 | * changes. |
| 4204 | */ |
| 4205 | int http_resync_states(struct session *s) |
| 4206 | { |
| 4207 | struct http_txn *txn = &s->txn; |
| 4208 | int old_req_state = txn->req.msg_state; |
| 4209 | int old_res_state = txn->rsp.msg_state; |
| 4210 | |
| 4211 | http_silent_debug(__LINE__, s); |
| 4212 | http_sync_req_state(s); |
| 4213 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4214 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4215 | if (!http_sync_res_state(s)) |
| 4216 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4217 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4218 | if (!http_sync_req_state(s)) |
| 4219 | break; |
| 4220 | } |
| 4221 | http_silent_debug(__LINE__, s); |
| 4222 | /* OK, both state machines agree on a compatible state. |
| 4223 | * There are a few cases we're interested in : |
| 4224 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4225 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4226 | * directions, so let's simply disable both analysers. |
| 4227 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4228 | * request. |
| 4229 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4230 | * with server-close mode means we've completed one request and we |
| 4231 | * must re-initialize the server connection. |
| 4232 | */ |
| 4233 | |
| 4234 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4235 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4236 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4237 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4238 | s->req->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4239 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4240 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4241 | s->rep->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4242 | buffer_auto_close(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4243 | buffer_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4244 | } |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4245 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4246 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4247 | txn->req.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4248 | (s->rep->flags & BF_SHUTW)) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4249 | s->rep->analysers = 0; |
| 4250 | buffer_auto_close(s->rep); |
| 4251 | buffer_auto_read(s->rep); |
| 4252 | s->req->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4253 | buffer_abort(s->req); |
| 4254 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4255 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4256 | buffer_ignore(s->req, s->req->l - s->req->send_max); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4257 | } |
| 4258 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4259 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 4260 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 4261 | /* server-close: terminate this server connection and |
| 4262 | * reinitialize a fresh-new transaction. |
| 4263 | */ |
| 4264 | http_end_txn_clean_session(s); |
| 4265 | } |
| 4266 | |
| 4267 | http_silent_debug(__LINE__, s); |
| 4268 | return txn->req.msg_state != old_req_state || |
| 4269 | txn->rsp.msg_state != old_res_state; |
| 4270 | } |
| 4271 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4272 | /* This function is an analyser which forwards request body (including chunk |
| 4273 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4274 | * zero byte. The only situation where it must not be called is when we're in |
| 4275 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4276 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4277 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4278 | * read more data, or 1 once we can go on with next request or end the session. |
| 4279 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len |
| 4280 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4281 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4282 | */ |
| 4283 | int http_request_forward_body(struct session *s, struct buffer *req, int an_bit) |
| 4284 | { |
| 4285 | struct http_txn *txn = &s->txn; |
| 4286 | struct http_msg *msg = &s->txn.req; |
| 4287 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4288 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4289 | return 0; |
| 4290 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4291 | if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
| 4292 | ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4293 | /* Output closed while we were sending data. We must abort and |
| 4294 | * wake the other side up. |
| 4295 | */ |
| 4296 | msg->msg_state = HTTP_MSG_ERROR; |
| 4297 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4298 | return 1; |
| 4299 | } |
| 4300 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4301 | /* in most states, we should abort in case of early close */ |
| 4302 | buffer_auto_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4303 | |
| 4304 | /* Note that we don't have to send 100-continue back because we don't |
| 4305 | * need the data to complete our job, and it's up to the server to |
| 4306 | * decide whether to return 100, 417 or anything else in return of |
| 4307 | * an "Expect: 100-continue" header. |
| 4308 | */ |
| 4309 | |
| 4310 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4311 | /* we have msg->col and msg->sov which both point to the first |
| 4312 | * byte of message body. msg->som still points to the beginning |
| 4313 | * of the message. We must save the body in req->lr because it |
| 4314 | * survives buffer re-alignments. |
| 4315 | */ |
| 4316 | req->lr = req->data + msg->sov; |
| 4317 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4318 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4319 | else { |
| 4320 | msg->msg_state = HTTP_MSG_DATA; |
| 4321 | } |
| 4322 | } |
| 4323 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4324 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4325 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4326 | /* we may have some data pending */ |
| 4327 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 4328 | int bytes = msg->sov - msg->som; |
| 4329 | if (bytes < 0) /* sov may have wrapped at the end */ |
| 4330 | bytes += req->size; |
| 4331 | buffer_forward(req, bytes + msg->hdr_content_len); |
| 4332 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 4333 | msg->som = msg->sov; |
| 4334 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4335 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4336 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4337 | /* must still forward */ |
| 4338 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4339 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4340 | |
| 4341 | /* nothing left to forward */ |
| 4342 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4343 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4344 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4345 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4346 | } |
| 4347 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4348 | /* read the chunk size and assign it to ->hdr_content_len, then |
| 4349 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 4350 | * TRAILERS state. |
| 4351 | */ |
| 4352 | int ret = http_parse_chunk_size(req, msg); |
| 4353 | |
| 4354 | if (!ret) |
| 4355 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4356 | else if (ret < 0) { |
| 4357 | session_inc_http_err_ctr(s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4358 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4359 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4360 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4361 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4362 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4363 | /* we want the CRLF after the data */ |
| 4364 | int ret; |
| 4365 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4366 | req->lr = req->w + req->send_max; |
| 4367 | if (req->lr >= req->data + req->size) |
| 4368 | req->lr -= req->size; |
| 4369 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4370 | ret = http_skip_chunk_crlf(req, msg); |
| 4371 | |
| 4372 | if (ret == 0) |
| 4373 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4374 | else if (ret < 0) { |
| 4375 | session_inc_http_err_ctr(s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4376 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4377 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4378 | /* we're in MSG_CHUNK_SIZE now */ |
| 4379 | } |
| 4380 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 4381 | int ret = http_forward_trailers(req, msg); |
| 4382 | |
| 4383 | if (ret == 0) |
| 4384 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4385 | else if (ret < 0) { |
| 4386 | session_inc_http_err_ctr(s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4387 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4388 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4389 | /* we're in HTTP_MSG_DONE now */ |
| 4390 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4391 | else { |
| 4392 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4393 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4394 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4395 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 4396 | buffer_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4397 | if (http_resync_states(s)) { |
| 4398 | /* some state changes occurred, maybe the analyser |
| 4399 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4400 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4401 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 4402 | goto return_bad_req; |
| 4403 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4404 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4405 | |
| 4406 | /* If "option abortonclose" is set on the backend, we |
| 4407 | * want to monitor the client's connection and forward |
| 4408 | * any shutdown notification to the server, which will |
| 4409 | * decide whether to close or to go on processing the |
| 4410 | * request. |
| 4411 | */ |
| 4412 | if (s->be->options & PR_O_ABRT_CLOSE) { |
| 4413 | buffer_auto_read(req); |
| 4414 | buffer_auto_close(req); |
| 4415 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4416 | else if (s->txn.meth == HTTP_METH_POST) { |
| 4417 | /* POST requests may require to read extra CRLF |
| 4418 | * sent by broken browsers and which could cause |
| 4419 | * an RST to be sent upon close on some systems |
| 4420 | * (eg: Linux). |
| 4421 | */ |
| 4422 | buffer_auto_read(req); |
| 4423 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4424 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4425 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4426 | } |
| 4427 | } |
| 4428 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4429 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4430 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4431 | if (req->flags & BF_SHUTR) { |
| 4432 | if (!(s->flags & SN_ERR_MASK)) |
| 4433 | s->flags |= SN_ERR_CLICL; |
| 4434 | if (!(s->flags & SN_FINST_MASK)) |
| 4435 | s->flags |= SN_FINST_D; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4436 | goto return_bad_req; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4437 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4438 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4439 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4440 | if (req->flags & BF_SHUTW) { |
| 4441 | if (!(s->flags & SN_ERR_MASK)) |
| 4442 | s->flags |= SN_ERR_SRVCL; |
| 4443 | if (!(s->flags & SN_FINST_MASK)) |
| 4444 | s->flags |= SN_FINST_D; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4445 | goto return_bad_req; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4446 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4447 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4448 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 4449 | * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE. |
| 4450 | */ |
| 4451 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4452 | buffer_dont_close(req); |
| 4453 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4454 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4455 | return 0; |
| 4456 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4457 | return_bad_req: /* let's centralize all bad requests */ |
| 4458 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4459 | txn->status = 400; |
| 4460 | /* 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] | 4461 | 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] | 4462 | req->analysers = 0; |
| 4463 | s->fe->counters.failed_req++; |
| 4464 | if (s->listener->counters) |
| 4465 | s->listener->counters->failed_req++; |
| 4466 | |
| 4467 | if (!(s->flags & SN_ERR_MASK)) |
| 4468 | s->flags |= SN_ERR_PRXCOND; |
| 4469 | if (!(s->flags & SN_FINST_MASK)) |
| 4470 | s->flags |= SN_FINST_R; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4471 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4472 | return 0; |
| 4473 | } |
| 4474 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4475 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4476 | * processing can continue on next analysers, or zero if it either needs more |
| 4477 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 4478 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 4479 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4480 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4481 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4482 | 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] | 4483 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4484 | struct http_txn *txn = &s->txn; |
| 4485 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4486 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4487 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4488 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4489 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4490 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4491 | 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] | 4492 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4493 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4494 | rep, |
| 4495 | rep->rex, rep->wex, |
| 4496 | rep->flags, |
| 4497 | rep->l, |
| 4498 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4499 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4500 | /* |
| 4501 | * Now parse the partial (or complete) lines. |
| 4502 | * We will check the response syntax, and also join multi-line |
| 4503 | * headers. An index of all the lines will be elaborated while |
| 4504 | * parsing. |
| 4505 | * |
| 4506 | * For the parsing, we use a 28 states FSM. |
| 4507 | * |
| 4508 | * Here is the information we currently have : |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4509 | * rep->data + msg->som = beginning of response |
| 4510 | * rep->data + msg->eoh = end of processed headers / start of current one |
| 4511 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4512 | * rep->lr = first non-visited byte |
| 4513 | * rep->r = end of data |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4514 | * Once we reach MSG_BODY, rep->sol = rep->data + msg->som |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4515 | */ |
| 4516 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4517 | /* There's a protected area at the end of the buffer for rewriting |
| 4518 | * purposes. We don't want to start to parse the request if the |
| 4519 | * protected area is affected, because we may have to move processed |
| 4520 | * data later, which is much more complicated. |
| 4521 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4522 | if (rep->l && msg->msg_state < HTTP_MSG_ERROR) { |
| 4523 | if (unlikely((rep->flags & BF_FULL) || |
| 4524 | rep->r < rep->lr || |
| 4525 | rep->r > rep->data + rep->size - global.tune.maxrewrite)) { |
| 4526 | if (rep->send_max) { |
| 4527 | /* some data has still not left the buffer, wake us once that's done */ |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4528 | if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 4529 | goto abort_response; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4530 | buffer_dont_close(rep); |
| 4531 | rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 4532 | return 0; |
| 4533 | } |
| 4534 | if (rep->l <= rep->size - global.tune.maxrewrite) |
| 4535 | http_buffer_heavy_realign(rep, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4536 | } |
| 4537 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4538 | if (likely(rep->lr < rep->r)) |
| 4539 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4540 | } |
| 4541 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4542 | /* 1: we might have to print this header in debug mode */ |
| 4543 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4544 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 4545 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4546 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4547 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4548 | |
Willy Tarreau | 663308b | 2010-06-07 14:06:08 +0200 | [diff] [blame] | 4549 | sol = rep->data + msg->som; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 4550 | eol = sol + msg->sl.st.l; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4551 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4552 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4553 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4554 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4555 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4556 | while (cur_idx) { |
| 4557 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4558 | debug_hdr("srvhdr", s, sol, eol); |
| 4559 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4560 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4561 | } |
| 4562 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4563 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4564 | /* |
| 4565 | * Now we quickly check if we have found a full valid response. |
| 4566 | * If not so, we check the FD and buffer states before leaving. |
| 4567 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4568 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4569 | * responses are checked first. |
| 4570 | * |
| 4571 | * Depending on whether the client is still there or not, we |
| 4572 | * may send an error response back or not. Note that normally |
| 4573 | * we should only check for HTTP status there, and check I/O |
| 4574 | * errors somewhere else. |
| 4575 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4576 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4577 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4578 | /* Invalid response */ |
| 4579 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4580 | /* we detected a parsing error. We want to archive this response |
| 4581 | * in the dedicated proxy area for later troubleshooting. |
| 4582 | */ |
| 4583 | hdr_response_bad: |
| 4584 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
| 4585 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 4586 | |
| 4587 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4588 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4589 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4590 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 4591 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4592 | abort_response: |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4593 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4594 | rep->analysers = 0; |
| 4595 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4596 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4597 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4598 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 4599 | |
| 4600 | if (!(s->flags & SN_ERR_MASK)) |
| 4601 | s->flags |= SN_ERR_PRXCOND; |
| 4602 | if (!(s->flags & SN_FINST_MASK)) |
| 4603 | s->flags |= SN_FINST_H; |
| 4604 | |
| 4605 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4606 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4607 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4608 | /* too large response does not fit in buffer. */ |
| 4609 | else if (rep->flags & BF_FULL) { |
| 4610 | goto hdr_response_bad; |
| 4611 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4612 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4613 | /* read error */ |
| 4614 | else if (rep->flags & BF_READ_ERROR) { |
| 4615 | if (msg->err_pos >= 0) |
| 4616 | 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] | 4617 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4618 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4619 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4620 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4621 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR); |
| 4622 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4623 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4624 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4625 | rep->analysers = 0; |
| 4626 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4627 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4628 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4629 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4630 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4631 | if (!(s->flags & SN_ERR_MASK)) |
| 4632 | s->flags |= SN_ERR_SRVCL; |
| 4633 | if (!(s->flags & SN_FINST_MASK)) |
| 4634 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4635 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4636 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4637 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4638 | /* read timeout : return a 504 to the client. */ |
| 4639 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 4640 | if (msg->err_pos >= 0) |
| 4641 | 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] | 4642 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4643 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4644 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4645 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4646 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT); |
| 4647 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4648 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4649 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4650 | rep->analysers = 0; |
| 4651 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4652 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4653 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4654 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4655 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4656 | if (!(s->flags & SN_ERR_MASK)) |
| 4657 | s->flags |= SN_ERR_SRVTO; |
| 4658 | if (!(s->flags & SN_FINST_MASK)) |
| 4659 | s->flags |= SN_FINST_H; |
| 4660 | return 0; |
| 4661 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4662 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4663 | /* close from server */ |
| 4664 | else if (rep->flags & BF_SHUTR) { |
| 4665 | if (msg->err_pos >= 0) |
| 4666 | 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] | 4667 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4668 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4669 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4670 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4671 | health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE); |
| 4672 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4673 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4674 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4675 | rep->analysers = 0; |
| 4676 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4677 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4678 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4679 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4680 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4681 | if (!(s->flags & SN_ERR_MASK)) |
| 4682 | s->flags |= SN_ERR_SRVCL; |
| 4683 | if (!(s->flags & SN_FINST_MASK)) |
| 4684 | s->flags |= SN_FINST_H; |
| 4685 | return 0; |
| 4686 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4687 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4688 | /* write error to client (we don't send any message then) */ |
| 4689 | else if (rep->flags & BF_WRITE_ERROR) { |
| 4690 | if (msg->err_pos >= 0) |
| 4691 | 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] | 4692 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4693 | s->be->counters.failed_resp++; |
| 4694 | rep->analysers = 0; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4695 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4696 | |
| 4697 | if (!(s->flags & SN_ERR_MASK)) |
| 4698 | s->flags |= SN_ERR_CLICL; |
| 4699 | if (!(s->flags & SN_FINST_MASK)) |
| 4700 | s->flags |= SN_FINST_H; |
| 4701 | |
| 4702 | /* process_session() will take care of the error */ |
| 4703 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4704 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4705 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4706 | buffer_dont_close(rep); |
| 4707 | return 0; |
| 4708 | } |
| 4709 | |
| 4710 | /* More interesting part now : we know that we have a complete |
| 4711 | * response which at least looks like HTTP. We have an indicator |
| 4712 | * of each header's length, so we can parse them quickly. |
| 4713 | */ |
| 4714 | |
| 4715 | if (unlikely(msg->err_pos >= 0)) |
| 4716 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 4717 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4718 | /* |
| 4719 | * 1: get the status code |
| 4720 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4721 | n = msg->sol[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4722 | if (n < 1 || n > 5) |
| 4723 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4724 | /* when the client triggers a 4xx from the server, it's most often due |
| 4725 | * to a missing object or permission. These events should be tracked |
| 4726 | * because if they happen often, it may indicate a brute force or a |
| 4727 | * vulnerability scan. |
| 4728 | */ |
| 4729 | if (n == 4) |
| 4730 | session_inc_http_err_ctr(s); |
| 4731 | |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4732 | if (s->srv) |
| 4733 | s->srv->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4734 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4735 | /* check if the response is HTTP/1.1 or above */ |
| 4736 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4737 | ((msg->sol[5] > '1') || |
| 4738 | ((msg->sol[5] == '1') && |
| 4739 | (msg->sol[7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4740 | txn->flags |= TX_RES_VER_11; |
| 4741 | |
| 4742 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4743 | 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] | 4744 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4745 | /* transfer length unknown*/ |
| 4746 | txn->flags &= ~TX_RES_XFER_LEN; |
| 4747 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4748 | 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] | 4749 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 4750 | /* Adjust server's health based on status code. Note: status codes 501 |
| 4751 | * and 505 are triggered on demand by client request, so we must not |
| 4752 | * count them as server failures. |
| 4753 | */ |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4754 | if (s->srv) { |
| 4755 | if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505)) |
| 4756 | health_adjust(s->srv, HANA_STATUS_HTTP_OK); |
| 4757 | else |
| 4758 | health_adjust(s->srv, HANA_STATUS_HTTP_STS); |
| 4759 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4760 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4761 | /* |
| 4762 | * 2: check for cacheability. |
| 4763 | */ |
| 4764 | |
| 4765 | switch (txn->status) { |
| 4766 | case 200: |
| 4767 | case 203: |
| 4768 | case 206: |
| 4769 | case 300: |
| 4770 | case 301: |
| 4771 | case 410: |
| 4772 | /* RFC2616 @13.4: |
| 4773 | * "A response received with a status code of |
| 4774 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4775 | * by a cache (...) unless a cache-control |
| 4776 | * directive prohibits caching." |
| 4777 | * |
| 4778 | * RFC2616 @9.5: POST method : |
| 4779 | * "Responses to this method are not cacheable, |
| 4780 | * unless the response includes appropriate |
| 4781 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4782 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4783 | if (likely(txn->meth != HTTP_METH_POST) && |
| 4784 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 4785 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 4786 | break; |
| 4787 | default: |
| 4788 | break; |
| 4789 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4790 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4791 | /* |
| 4792 | * 3: we may need to capture headers |
| 4793 | */ |
| 4794 | s->logs.logwait &= ~LW_RESP; |
| 4795 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4796 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4797 | txn->rsp.cap, s->fe->rsp_cap); |
| 4798 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4799 | /* 4: determine the transfer-length. |
| 4800 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 4801 | * the presence of a message-body in a RESPONSE and its transfer length |
| 4802 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4803 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4804 | * All responses to the HEAD request method MUST NOT include a |
| 4805 | * message-body, even though the presence of entity-header fields |
| 4806 | * might lead one to believe they do. All 1xx (informational), 204 |
| 4807 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 4808 | * message-body. All other responses do include a message-body, |
| 4809 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4810 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4811 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 4812 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 4813 | * always terminated by the first empty line after the header fields, |
| 4814 | * regardless of the entity-header fields present in the message. |
| 4815 | * |
| 4816 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 4817 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 4818 | * transfer-length is defined by the use of this transfer-coding. |
| 4819 | * If a Transfer-Encoding header field is present and the "chunked" |
| 4820 | * transfer-coding is not present, the transfer-length is defined by |
| 4821 | * the sender closing the connection. |
| 4822 | * |
| 4823 | * 3. If a Content-Length header field is present, its decimal value in |
| 4824 | * OCTETs represents both the entity-length and the transfer-length. |
| 4825 | * If a message is received with both a Transfer-Encoding header |
| 4826 | * field and a Content-Length header field, the latter MUST be ignored. |
| 4827 | * |
| 4828 | * 4. If the message uses the media type "multipart/byteranges", and |
| 4829 | * the transfer-length is not otherwise specified, then this self- |
| 4830 | * delimiting media type defines the transfer-length. This media |
| 4831 | * type MUST NOT be used unless the sender knows that the recipient |
| 4832 | * can parse it; the presence in a request of a Range header with |
| 4833 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 4834 | * client can parse multipart/byteranges responses. |
| 4835 | * |
| 4836 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4837 | */ |
| 4838 | |
| 4839 | /* Skip parsing if no content length is possible. The response flags |
| 4840 | * 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] | 4841 | * 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] | 4842 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 4843 | */ |
| 4844 | if (txn->meth == HTTP_METH_HEAD || |
| 4845 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4846 | txn->status == 204 || txn->status == 304) { |
| 4847 | txn->flags |= TX_RES_XFER_LEN; |
| 4848 | goto skip_content_length; |
| 4849 | } |
| 4850 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4851 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4852 | ctx.idx = 0; |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 4853 | while ((txn->flags & TX_RES_VER_11) && |
| 4854 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4855 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 4856 | txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 4857 | else if (txn->flags & TX_RES_TE_CHNK) { |
| 4858 | /* bad transfer-encoding (chunked followed by something else) */ |
| 4859 | use_close_only = 1; |
| 4860 | txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 4861 | break; |
| 4862 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4863 | } |
| 4864 | |
| 4865 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 4866 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4867 | while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only && |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4868 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 4869 | signed long long cl; |
| 4870 | |
| 4871 | if (!ctx.vlen) |
| 4872 | goto hdr_response_bad; |
| 4873 | |
| 4874 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 4875 | goto hdr_response_bad; /* parse failure */ |
| 4876 | |
| 4877 | if (cl < 0) |
| 4878 | goto hdr_response_bad; |
| 4879 | |
| 4880 | if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 4881 | goto hdr_response_bad; /* already specified, was different */ |
| 4882 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4883 | txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4884 | msg->hdr_content_len = cl; |
| 4885 | } |
| 4886 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4887 | /* FIXME: we should also implement the multipart/byterange method. |
| 4888 | * For now on, we resort to close mode in this case (unknown length). |
| 4889 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4890 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4891 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4892 | /* end of job, return OK */ |
| 4893 | rep->analysers &= ~an_bit; |
| 4894 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4895 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4896 | return 1; |
| 4897 | } |
| 4898 | |
| 4899 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4900 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 4901 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 4902 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4903 | */ |
| 4904 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 4905 | { |
| 4906 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4907 | struct http_msg *msg = &txn->rsp; |
| 4908 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4909 | struct cond_wordlist *wl; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4910 | |
| 4911 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 4912 | now_ms, __FUNCTION__, |
| 4913 | t, |
| 4914 | rep, |
| 4915 | rep->rex, rep->wex, |
| 4916 | rep->flags, |
| 4917 | rep->l, |
| 4918 | rep->analysers); |
| 4919 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4920 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4921 | return 0; |
| 4922 | |
| 4923 | rep->analysers &= ~an_bit; |
| 4924 | rep->analyse_exp = TICK_ETERNITY; |
| 4925 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4926 | /* Now we have to check if we need to modify the Connection header. |
| 4927 | * This is more difficult on the response than it is on the request, |
| 4928 | * because we can have two different HTTP versions and we don't know |
| 4929 | * how the client will interprete a response. For instance, let's say |
| 4930 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 4931 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 4932 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 4933 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 4934 | * the lack of header as a keep-alive. Thus we will use two flags |
| 4935 | * indicating how a request MAY be understood by the client. In case |
| 4936 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 4937 | * ambiguous cases such as both close and keepalive are seen, then we |
| 4938 | * will fall back to explicit close. Note that we won't take risks with |
| 4939 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4940 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4941 | */ |
| 4942 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4943 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 4944 | txn->status == 101)) { |
| 4945 | /* Either we've established an explicit tunnel, or we're |
| 4946 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4947 | * to understand the next protocols. We have to switch to tunnel |
| 4948 | * mode, so that we transfer the request and responses then let |
| 4949 | * this protocol pass unmodified. When we later implement specific |
| 4950 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4951 | * header which contains information about that protocol for |
| 4952 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4953 | */ |
| 4954 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 4955 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4956 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 4957 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 4958 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4959 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4960 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4961 | /* on unknown transfer length, we must close */ |
| 4962 | if (!(txn->flags & TX_RES_XFER_LEN) && |
| 4963 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4964 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4965 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4966 | /* now adjust header transformations depending on current state */ |
| 4967 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 4968 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4969 | to_del |= 2; /* remove "keep-alive" on any response */ |
| 4970 | if (!(txn->flags & TX_RES_VER_11)) |
| 4971 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4972 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4973 | else { /* SCL / KAL */ |
| 4974 | to_del |= 1; /* remove "close" on any response */ |
| 4975 | if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11)) |
| 4976 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4977 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4978 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4979 | /* Parse and remove some headers from the connection header */ |
| 4980 | http_parse_connection_header(txn, msg, rep, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4981 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4982 | /* Some keep-alive responses are converted to Server-close if |
| 4983 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4984 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4985 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 4986 | if ((txn->flags & TX_HDR_CONN_CLO) || |
| 4987 | (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0) |
| 4988 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4989 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4990 | } |
| 4991 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4992 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4993 | /* |
| 4994 | * 3: we will have to evaluate the filters. |
| 4995 | * As opposed to version 1.2, now they will be evaluated in the |
| 4996 | * filters order and not in the header order. This means that |
| 4997 | * each filter has to be validated among all headers. |
| 4998 | * |
| 4999 | * Filters are tried with ->be first, then with ->fe if it is |
| 5000 | * different from ->be. |
| 5001 | */ |
| 5002 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5003 | cur_proxy = t->be; |
| 5004 | while (1) { |
| 5005 | struct proxy *rule_set = cur_proxy; |
| 5006 | |
| 5007 | /* try headers filters */ |
| 5008 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5009 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5010 | return_bad_resp: |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5011 | if (t->srv) { |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5012 | t->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5013 | health_adjust(t->srv, HANA_STATUS_HTTP_RSP); |
| 5014 | } |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5015 | cur_proxy->counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5016 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5017 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5018 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5019 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 5020 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 5021 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5022 | if (!(t->flags & SN_ERR_MASK)) |
| 5023 | t->flags |= SN_ERR_PRXCOND; |
| 5024 | if (!(t->flags & SN_FINST_MASK)) |
| 5025 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 5026 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5027 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5028 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5029 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5030 | /* has the response been denied ? */ |
| 5031 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 5032 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5033 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5034 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5035 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5036 | if (t->listener->counters) |
| 5037 | t->listener->counters->denied_resp++; |
| 5038 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5039 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 5040 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5041 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5042 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5043 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5044 | if (txn->status < 200) |
| 5045 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5046 | if (wl->cond) { |
| 5047 | int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR); |
| 5048 | ret = acl_pass(ret); |
| 5049 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5050 | ret = !ret; |
| 5051 | if (!ret) |
| 5052 | continue; |
| 5053 | } |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5054 | 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] | 5055 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5056 | } |
| 5057 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5058 | /* check whether we're already working on the frontend */ |
| 5059 | if (cur_proxy == t->fe) |
| 5060 | break; |
| 5061 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5062 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5063 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5064 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5065 | * We may be facing a 100-continue response, in which case this |
| 5066 | * is not the right response, and we're waiting for the next one. |
| 5067 | * 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] | 5068 | * next one. |
| 5069 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5070 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5071 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5072 | buffer_forward(rep, rep->lr - msg->sol); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5073 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5074 | txn->status = 0; |
| 5075 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 5076 | return 1; |
| 5077 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5078 | else if (unlikely(txn->status < 200)) |
| 5079 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5080 | |
| 5081 | /* we don't have any 1xx status code now */ |
| 5082 | |
| 5083 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5084 | * 4: check for server cookie. |
| 5085 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5086 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 5087 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5088 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5089 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5090 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5091 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5092 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5093 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5094 | 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] | 5095 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5096 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5097 | /* |
| 5098 | * 6: add server cookie in the response if needed |
| 5099 | */ |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5100 | if ((t->srv) && (t->be->options & PR_O_COOK_INS) && |
| 5101 | (!(t->flags & SN_DIRECT) || |
| 5102 | ((t->be->cookie_maxidle || txn->cookie_last_date) && |
| 5103 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5104 | (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5105 | (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5106 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 5107 | !(t->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5108 | int len; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5109 | /* the server is known, it's not the one the client requested, or the |
| 5110 | * cookie's last seen date needs to be refreshed. We have to |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5111 | * insert a set-cookie here, except if we want to insert only on POST |
| 5112 | * requests and this one isn't. Note that servers which don't have cookies |
| 5113 | * (eg: some backup servers) will return a full cookie removal request. |
| 5114 | */ |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5115 | if (!t->srv->cookie) { |
| 5116 | len = sprintf(trash, |
| 5117 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5118 | t->be->cookie_name); |
| 5119 | } |
| 5120 | else { |
| 5121 | len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, t->srv->cookie); |
| 5122 | |
| 5123 | if (t->be->cookie_maxidle || t->be->cookie_maxlife) { |
| 5124 | /* emit last_date, which is mandatory */ |
| 5125 | trash[len++] = COOKIE_DELIM_DATE; |
| 5126 | s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5; |
| 5127 | if (t->be->cookie_maxlife) { |
| 5128 | /* emit first_date, which is either the original one or |
| 5129 | * the current date. |
| 5130 | */ |
| 5131 | trash[len++] = COOKIE_DELIM_DATE; |
| 5132 | s30tob64(txn->cookie_first_date ? |
| 5133 | txn->cookie_first_date >> 2 : |
| 5134 | (date.tv_sec+3) >> 2, trash + len); |
| 5135 | len += 5; |
| 5136 | } |
| 5137 | } |
| 5138 | len += sprintf(trash + len, "; path=/"); |
| 5139 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5140 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5141 | if (t->be->cookie_domain) |
| 5142 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5143 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5144 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5145 | trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5146 | goto return_bad_resp; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5147 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5148 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5149 | if (t->srv->cookie && (t->flags & SN_DIRECT)) |
| 5150 | /* the server did not change, only the date was updated */ |
| 5151 | txn->flags |= TX_SCK_UPDATED; |
| 5152 | else |
| 5153 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5154 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5155 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5156 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5157 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5158 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 5159 | */ |
| 5160 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5161 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5162 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5163 | |
| 5164 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5165 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5166 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5167 | } |
| 5168 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5169 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5170 | /* |
| 5171 | * 7: check if result will be cacheable with a cookie. |
| 5172 | * We'll block the response if security checks have caught |
| 5173 | * nasty things such as a cacheable cookie. |
| 5174 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5175 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5176 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5177 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5178 | |
| 5179 | /* we're in presence of a cacheable response containing |
| 5180 | * a set-cookie header. We'll block it as requested by |
| 5181 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5182 | */ |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 5183 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5184 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5185 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5186 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5187 | if (t->listener->counters) |
| 5188 | t->listener->counters->denied_resp++; |
| 5189 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5190 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5191 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 5192 | send_log(t->be, LOG_ALERT, |
| 5193 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5194 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 5195 | goto return_srv_prx_502; |
| 5196 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5197 | |
| 5198 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5199 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5200 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5201 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 5202 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) { |
| 5203 | unsigned int want_flags = 0; |
| 5204 | |
| 5205 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5206 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5207 | /* we want a keep-alive response here. Keep-alive header |
| 5208 | * required if either side is not 1.1. |
| 5209 | */ |
| 5210 | if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11)) |
| 5211 | want_flags |= TX_CON_KAL_SET; |
| 5212 | } |
| 5213 | else { |
| 5214 | /* we want a close response here. Close header required if |
| 5215 | * the server is 1.1, regardless of the client. |
| 5216 | */ |
| 5217 | if (txn->flags & TX_RES_VER_11) |
| 5218 | want_flags |= TX_CON_CLO_SET; |
| 5219 | } |
| 5220 | |
| 5221 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 5222 | http_change_connection_header(txn, msg, rep, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5223 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5224 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5225 | skip_header_mangling: |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5226 | if ((txn->flags & TX_RES_XFER_LEN) || |
| 5227 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5228 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5229 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5230 | /************************************************************* |
| 5231 | * OK, that's finished for the headers. We have done what we * |
| 5232 | * could. Let's switch to the DATA state. * |
| 5233 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5234 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5235 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5236 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5237 | /* if the user wants to log as soon as possible, without counting |
| 5238 | * bytes from the server, then this is the right moment. We have |
| 5239 | * to temporarily assign bytes_out to log what we currently have. |
| 5240 | */ |
| 5241 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 5242 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 5243 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 5244 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5245 | t->logs.bytes_out = 0; |
| 5246 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5247 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5248 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 5249 | * otherwise we would not let the client side wake up. |
| 5250 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5251 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5252 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5253 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5254 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5255 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5256 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5257 | /* This function is an analyser which forwards response body (including chunk |
| 5258 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5259 | * zero byte. The only situation where it must not be called is when we're in |
| 5260 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5261 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5262 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5263 | * read more data, or 1 once we can go on with next request or end the session. |
| 5264 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len |
| 5265 | * bytes of pending data + the headers if not already done (between som and sov). |
| 5266 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 5267 | */ |
| 5268 | int http_response_forward_body(struct session *s, struct buffer *res, int an_bit) |
| 5269 | { |
| 5270 | struct http_txn *txn = &s->txn; |
| 5271 | struct http_msg *msg = &s->txn.rsp; |
| 5272 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5273 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5274 | return 0; |
| 5275 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5276 | 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] | 5277 | ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5278 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5279 | /* Output closed while we were sending data. We must abort and |
| 5280 | * wake the other side up. |
| 5281 | */ |
| 5282 | msg->msg_state = HTTP_MSG_ERROR; |
| 5283 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5284 | return 1; |
| 5285 | } |
| 5286 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5287 | /* in most states, we should abort in case of early close */ |
| 5288 | buffer_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5289 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5290 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5291 | /* we have msg->col and msg->sov which both point to the first |
| 5292 | * byte of message body. msg->som still points to the beginning |
| 5293 | * of the message. We must save the body in req->lr because it |
| 5294 | * survives buffer re-alignments. |
| 5295 | */ |
| 5296 | res->lr = res->data + msg->sov; |
| 5297 | if (txn->flags & TX_RES_TE_CHNK) |
| 5298 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5299 | else { |
| 5300 | msg->msg_state = HTTP_MSG_DATA; |
| 5301 | } |
| 5302 | } |
| 5303 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5304 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5305 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5306 | /* we may have some data pending */ |
| 5307 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 5308 | int bytes = msg->sov - msg->som; |
| 5309 | if (bytes < 0) /* sov may have wrapped at the end */ |
| 5310 | bytes += res->size; |
| 5311 | buffer_forward(res, bytes + msg->hdr_content_len); |
| 5312 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 5313 | msg->som = msg->sov; |
| 5314 | } |
| 5315 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5316 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5317 | /* must still forward */ |
| 5318 | if (res->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5319 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5320 | |
| 5321 | /* nothing left to forward */ |
| 5322 | if (txn->flags & TX_RES_TE_CHNK) |
| 5323 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 5324 | else |
| 5325 | msg->msg_state = HTTP_MSG_DONE; |
| 5326 | } |
| 5327 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5328 | /* read the chunk size and assign it to ->hdr_content_len, then |
| 5329 | * set ->sov to point to the body and switch to DATA or TRAILERS state. |
| 5330 | */ |
| 5331 | int ret = http_parse_chunk_size(res, msg); |
| 5332 | |
| 5333 | if (!ret) |
| 5334 | goto missing_data; |
| 5335 | else if (ret < 0) |
| 5336 | goto return_bad_res; |
| 5337 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5338 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5339 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 5340 | /* we want the CRLF after the data */ |
| 5341 | int ret; |
| 5342 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 5343 | res->lr = res->w + res->send_max; |
| 5344 | if (res->lr >= res->data + res->size) |
| 5345 | res->lr -= res->size; |
| 5346 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5347 | ret = http_skip_chunk_crlf(res, msg); |
| 5348 | |
| 5349 | if (!ret) |
| 5350 | goto missing_data; |
| 5351 | else if (ret < 0) |
| 5352 | goto return_bad_res; |
| 5353 | /* we're in MSG_CHUNK_SIZE now */ |
| 5354 | } |
| 5355 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 5356 | int ret = http_forward_trailers(res, msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5357 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5358 | if (ret == 0) |
| 5359 | goto missing_data; |
| 5360 | else if (ret < 0) |
| 5361 | goto return_bad_res; |
| 5362 | /* we're in HTTP_MSG_DONE now */ |
| 5363 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5364 | else { |
| 5365 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5366 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5367 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5368 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5369 | buffer_dont_close(res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5370 | if (http_resync_states(s)) { |
| 5371 | http_silent_debug(__LINE__, s); |
| 5372 | /* some state changes occurred, maybe the analyser |
| 5373 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5374 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5375 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 5376 | goto return_bad_res; |
| 5377 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5378 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5379 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5380 | } |
| 5381 | } |
| 5382 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5383 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5384 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5385 | if (res->flags & BF_SHUTR) { |
| 5386 | if (!(s->flags & SN_ERR_MASK)) |
| 5387 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 5388 | s->be->counters.srv_aborts++; |
| 5389 | if (s->srv) |
| 5390 | s->srv->counters.srv_aborts++; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5391 | goto return_bad_res; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5392 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5393 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5394 | /* we need to obey the req analyser, so if it leaves, we must too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5395 | if (!s->req->analysers) |
| 5396 | goto return_bad_res; |
| 5397 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5398 | /* forward the chunk size as well as any pending data */ |
| 5399 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 5400 | buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len); |
| 5401 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 5402 | msg->som = msg->sov; |
| 5403 | } |
| 5404 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5405 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 5406 | * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE. |
| 5407 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 5408 | * close. |
| 5409 | */ |
| 5410 | if ((txn->flags & TX_RES_TE_CHNK) || |
| 5411 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5412 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5413 | buffer_dont_close(res); |
| 5414 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5415 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5416 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5417 | return 0; |
| 5418 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5419 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5420 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 5421 | /* don't send any error message as we're in the body */ |
| 5422 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5423 | res->analysers = 0; |
| 5424 | s->be->counters.failed_resp++; |
| 5425 | if (s->srv) { |
| 5426 | s->srv->counters.failed_resp++; |
| 5427 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 5428 | } |
| 5429 | |
| 5430 | if (!(s->flags & SN_ERR_MASK)) |
| 5431 | s->flags |= SN_ERR_PRXCOND; |
| 5432 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5433 | s->flags |= SN_FINST_D; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5434 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5435 | return 0; |
| 5436 | } |
| 5437 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5438 | /* Iterate the same filter through all request headers. |
| 5439 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5440 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5441 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5442 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5443 | 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] | 5444 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5445 | char term; |
| 5446 | char *cur_ptr, *cur_end, *cur_next; |
| 5447 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5448 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5449 | struct hdr_idx_elem *cur_hdr; |
| 5450 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 5451 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5452 | last_hdr = 0; |
| 5453 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5454 | cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5455 | old_idx = 0; |
| 5456 | |
| 5457 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5458 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5459 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5460 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5461 | (exp->action == ACT_ALLOW || |
| 5462 | exp->action == ACT_DENY || |
| 5463 | exp->action == ACT_TARPIT)) |
| 5464 | return 0; |
| 5465 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5466 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5467 | if (!cur_idx) |
| 5468 | break; |
| 5469 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5470 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5471 | cur_ptr = cur_next; |
| 5472 | cur_end = cur_ptr + cur_hdr->len; |
| 5473 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5474 | |
| 5475 | /* Now we have one header between cur_ptr and cur_end, |
| 5476 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5477 | */ |
| 5478 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5479 | /* The annoying part is that pattern matching needs |
| 5480 | * that we modify the contents to null-terminate all |
| 5481 | * strings before testing them. |
| 5482 | */ |
| 5483 | |
| 5484 | term = *cur_end; |
| 5485 | *cur_end = '\0'; |
| 5486 | |
| 5487 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5488 | switch (exp->action) { |
| 5489 | case ACT_SETBE: |
| 5490 | /* It is not possible to jump a second time. |
| 5491 | * FIXME: should we return an HTTP/500 here so that |
| 5492 | * the admin knows there's a problem ? |
| 5493 | */ |
| 5494 | if (t->be != t->fe) |
| 5495 | break; |
| 5496 | |
| 5497 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5498 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5499 | last_hdr = 1; |
| 5500 | break; |
| 5501 | |
| 5502 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5503 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5504 | last_hdr = 1; |
| 5505 | break; |
| 5506 | |
| 5507 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5508 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5509 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5510 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5511 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5512 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5513 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5514 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5515 | break; |
| 5516 | |
| 5517 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5518 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5519 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5520 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5521 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5522 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5523 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5524 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5525 | break; |
| 5526 | |
| 5527 | case ACT_REPLACE: |
| 5528 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5529 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5530 | /* FIXME: if the user adds a newline in the replacement, the |
| 5531 | * index will not be recalculated for now, and the new line |
| 5532 | * will not be counted as a new header. |
| 5533 | */ |
| 5534 | |
| 5535 | cur_end += delta; |
| 5536 | cur_next += delta; |
| 5537 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5538 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5539 | break; |
| 5540 | |
| 5541 | case ACT_REMOVE: |
| 5542 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 5543 | cur_next += delta; |
| 5544 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5545 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5546 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5547 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5548 | cur_hdr->len = 0; |
| 5549 | cur_end = NULL; /* null-term has been rewritten */ |
| 5550 | break; |
| 5551 | |
| 5552 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5553 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5554 | if (cur_end) |
| 5555 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5556 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5557 | /* keep the link from this header to next one in case of later |
| 5558 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5559 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5560 | old_idx = cur_idx; |
| 5561 | } |
| 5562 | return 0; |
| 5563 | } |
| 5564 | |
| 5565 | |
| 5566 | /* Apply the filter to the request line. |
| 5567 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5568 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5569 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5570 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5571 | */ |
| 5572 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5573 | { |
| 5574 | char term; |
| 5575 | char *cur_ptr, *cur_end; |
| 5576 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5577 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5578 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5579 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5580 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5581 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5582 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5583 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5584 | (exp->action == ACT_ALLOW || |
| 5585 | exp->action == ACT_DENY || |
| 5586 | exp->action == ACT_TARPIT)) |
| 5587 | return 0; |
| 5588 | else if (exp->action == ACT_REMOVE) |
| 5589 | return 0; |
| 5590 | |
| 5591 | done = 0; |
| 5592 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5593 | cur_ptr = txn->req.sol; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5594 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5595 | |
| 5596 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5597 | |
| 5598 | /* The annoying part is that pattern matching needs |
| 5599 | * that we modify the contents to null-terminate all |
| 5600 | * strings before testing them. |
| 5601 | */ |
| 5602 | |
| 5603 | term = *cur_end; |
| 5604 | *cur_end = '\0'; |
| 5605 | |
| 5606 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5607 | switch (exp->action) { |
| 5608 | case ACT_SETBE: |
| 5609 | /* It is not possible to jump a second time. |
| 5610 | * FIXME: should we return an HTTP/500 here so that |
| 5611 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5612 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5613 | if (t->be != t->fe) |
| 5614 | break; |
| 5615 | |
| 5616 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5617 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5618 | done = 1; |
| 5619 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5620 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5621 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5622 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5623 | done = 1; |
| 5624 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5625 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5626 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5627 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5628 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5629 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5630 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5631 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5632 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5633 | done = 1; |
| 5634 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5635 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5636 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5637 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5638 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5639 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5640 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5641 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5642 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5643 | done = 1; |
| 5644 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5645 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5646 | case ACT_REPLACE: |
| 5647 | *cur_end = term; /* restore the string terminator */ |
| 5648 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5649 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5650 | /* FIXME: if the user adds a newline in the replacement, the |
| 5651 | * index will not be recalculated for now, and the new line |
| 5652 | * will not be counted as a new header. |
| 5653 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5654 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5655 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5656 | cur_end += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5657 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5658 | HTTP_MSG_RQMETH, |
| 5659 | cur_ptr, cur_end + 1, |
| 5660 | NULL, NULL); |
| 5661 | if (unlikely(!cur_end)) |
| 5662 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5663 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5664 | /* we have a full request and we know that we have either a CR |
| 5665 | * or an LF at <ptr>. |
| 5666 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5667 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5668 | 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] | 5669 | /* there is no point trying this regex on headers */ |
| 5670 | return 1; |
| 5671 | } |
| 5672 | } |
| 5673 | *cur_end = term; /* restore the string terminator */ |
| 5674 | return done; |
| 5675 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5676 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5677 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5678 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5679 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5680 | * 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] | 5681 | * 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] | 5682 | * unparsable request. Since it can manage the switch to another backend, it |
| 5683 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5684 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5685 | 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] | 5686 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5687 | struct http_txn *txn = &s->txn; |
| 5688 | struct hdr_exp *exp; |
| 5689 | |
| 5690 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5691 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5692 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5693 | /* |
| 5694 | * The interleaving of transformations and verdicts |
| 5695 | * makes it difficult to decide to continue or stop |
| 5696 | * the evaluation. |
| 5697 | */ |
| 5698 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5699 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 5700 | break; |
| 5701 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5702 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5703 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5704 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5705 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5706 | |
| 5707 | /* if this filter had a condition, evaluate it now and skip to |
| 5708 | * next filter if the condition does not match. |
| 5709 | */ |
| 5710 | if (exp->cond) { |
| 5711 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ); |
| 5712 | ret = acl_pass(ret); |
| 5713 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 5714 | ret = !ret; |
| 5715 | |
| 5716 | if (!ret) |
| 5717 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5718 | } |
| 5719 | |
| 5720 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5721 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5722 | if (unlikely(ret < 0)) |
| 5723 | return -1; |
| 5724 | |
| 5725 | if (likely(ret == 0)) { |
| 5726 | /* The filter did not match the request, it can be |
| 5727 | * iterated through all headers. |
| 5728 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5729 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5730 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5731 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5732 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5733 | } |
| 5734 | |
| 5735 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5736 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5737 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5738 | * Try to retrieve the server associated to the appsession. |
| 5739 | * If the server is found, it's assigned to the session. |
| 5740 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5741 | 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] | 5742 | struct http_txn *txn = &t->txn; |
| 5743 | appsess *asession = NULL; |
| 5744 | char *sessid_temp = NULL; |
| 5745 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5746 | if (len > t->be->appsession_len) { |
| 5747 | len = t->be->appsession_len; |
| 5748 | } |
| 5749 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5750 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 5751 | /* 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] | 5752 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5753 | /* 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] | 5754 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5755 | } |
| 5756 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5757 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5758 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5759 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5760 | return; |
| 5761 | } |
| 5762 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5763 | memcpy(txn->sessid, buf, len); |
| 5764 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5765 | } |
| 5766 | |
| 5767 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 5768 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5769 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5770 | return; |
| 5771 | } |
| 5772 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5773 | memcpy(sessid_temp, buf, len); |
| 5774 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5775 | |
| 5776 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 5777 | /* free previously allocated memory */ |
| 5778 | pool_free2(apools.sessid, sessid_temp); |
| 5779 | |
| 5780 | if (asession != NULL) { |
| 5781 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5782 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 5783 | asession->request_count++; |
| 5784 | |
| 5785 | if (asession->serverid != NULL) { |
| 5786 | struct server *srv = t->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5787 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5788 | while (srv) { |
| 5789 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 5790 | if ((srv->state & SRV_RUNNING) || |
| 5791 | (t->be->options & PR_O_PERSIST) || |
| 5792 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5793 | /* we found the server and it's usable */ |
| 5794 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 5795 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5796 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
| 5797 | t->srv = srv; |
| 5798 | break; |
| 5799 | } else { |
| 5800 | txn->flags &= ~TX_CK_MASK; |
| 5801 | txn->flags |= TX_CK_DOWN; |
| 5802 | } |
| 5803 | } |
| 5804 | srv = srv->next; |
| 5805 | } |
| 5806 | } |
| 5807 | } |
| 5808 | } |
| 5809 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5810 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 5811 | * same way as with headers above except that the semi-colon also ends a token. |
| 5812 | * See RFC2965 for more information. Note that it requires a valid header to |
| 5813 | * return a valid result. |
| 5814 | */ |
| 5815 | char *find_cookie_value_end(char *s, const char *e) |
| 5816 | { |
| 5817 | int quoted, qdpair; |
| 5818 | |
| 5819 | quoted = qdpair = 0; |
| 5820 | for (; s < e; s++) { |
| 5821 | if (qdpair) qdpair = 0; |
| 5822 | else if (quoted) { |
| 5823 | if (*s == '\\') qdpair = 1; |
| 5824 | else if (*s == '"') quoted = 0; |
| 5825 | } |
| 5826 | else if (*s == '"') quoted = 1; |
| 5827 | else if (*s == ',' || *s == ';') return s; |
| 5828 | } |
| 5829 | return s; |
| 5830 | } |
| 5831 | |
| 5832 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 5833 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 5834 | * the first delimiter is updated if required. The function tries as much as |
| 5835 | * possible to respect the following principles : |
| 5836 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 5837 | * colon, in which case <next> is simply removed |
| 5838 | * - set exactly one space character after the new first delimiter, unless |
| 5839 | * there are not enough characters in the block being moved to do so. |
| 5840 | * - remove unneeded spaces before the previous delimiter and after the new |
| 5841 | * one. |
| 5842 | * |
| 5843 | * It is the caller's responsibility to ensure that : |
| 5844 | * - <from> points to a valid delimiter or the colon ; |
| 5845 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 5846 | * - there are non-space chars before <from> ; |
| 5847 | * - there is a CR/LF at or after <next>. |
| 5848 | */ |
| 5849 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
| 5850 | { |
| 5851 | char *prev = *from; |
| 5852 | |
| 5853 | if (*prev == ':') { |
| 5854 | /* We're removing the first value, preserve the colon and add a |
| 5855 | * space if possible. |
| 5856 | */ |
| 5857 | if (!http_is_crlf[(unsigned char)*next]) |
| 5858 | next++; |
| 5859 | prev++; |
| 5860 | if (prev < next) |
| 5861 | *prev++ = ' '; |
| 5862 | |
| 5863 | while (http_is_spht[(unsigned char)*next]) |
| 5864 | next++; |
| 5865 | } else { |
| 5866 | /* Remove useless spaces before the old delimiter. */ |
| 5867 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 5868 | prev--; |
| 5869 | *from = prev; |
| 5870 | |
| 5871 | /* copy the delimiter and if possible a space if we're |
| 5872 | * not at the end of the line. |
| 5873 | */ |
| 5874 | if (!http_is_crlf[(unsigned char)*next]) { |
| 5875 | *prev++ = *next++; |
| 5876 | if (prev + 1 < next) |
| 5877 | *prev++ = ' '; |
| 5878 | while (http_is_spht[(unsigned char)*next]) |
| 5879 | next++; |
| 5880 | } |
| 5881 | } |
| 5882 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 5883 | } |
| 5884 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5885 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5886 | * Manage client-side cookie. It can impact performance by about 2% so it is |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5887 | * desirable to call it only when needed. This code is quite complex because |
| 5888 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 5889 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5890 | */ |
| 5891 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 5892 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5893 | struct http_txn *txn = &t->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5894 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5895 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5896 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 5897 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5898 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5899 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 5900 | old_idx = 0; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5901 | hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5902 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5903 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5904 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5905 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5906 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5907 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5908 | hdr_beg = hdr_next; |
| 5909 | hdr_end = hdr_beg + cur_hdr->len; |
| 5910 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5911 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5912 | /* We have one full header between hdr_beg and hdr_end, and the |
| 5913 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5914 | * "Cookie:" headers. |
| 5915 | */ |
| 5916 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5917 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5918 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5919 | old_idx = cur_idx; |
| 5920 | continue; |
| 5921 | } |
| 5922 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5923 | del_from = NULL; /* nothing to be deleted */ |
| 5924 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 5925 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5926 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 5927 | * attributes whose name begin with a '$', and associate them with |
| 5928 | * the right cookie, if we want to delete this cookie. |
| 5929 | * So there are 3 cases for each cookie read : |
| 5930 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 5931 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 5932 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 5933 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 5934 | * "special" cookie. |
| 5935 | * At the end of loop, if a "special" cookie remains, we may have to |
| 5936 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5937 | * *MUST* delete it. |
| 5938 | * |
| 5939 | * Note: RFC2965 is unclear about the processing of spaces around |
| 5940 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 5941 | * the RFC explicitly allows spaces before it, and not within the |
| 5942 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 5943 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 5944 | * after the equal sign too, resulting in some (rare) buggy |
| 5945 | * implementations trying to do that. So let's do what servers do. |
| 5946 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 5947 | * allowed quoted strings in values, with any possible character |
| 5948 | * after a backslash, including control chars and delimitors, which |
| 5949 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 5950 | * within values even without quotes. |
| 5951 | * |
| 5952 | * We have to keep multiple pointers in order to support cookie |
| 5953 | * removal at the beginning, middle or end of header without |
| 5954 | * corrupting the header. All of these headers are valid : |
| 5955 | * |
| 5956 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 5957 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 5958 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 5959 | * | | | | | | | | | |
| 5960 | * | | | | | | | | hdr_end <--+ |
| 5961 | * | | | | | | | +--> next |
| 5962 | * | | | | | | +----> val_end |
| 5963 | * | | | | | +-----------> val_beg |
| 5964 | * | | | | +--------------> equal |
| 5965 | * | | | +----------------> att_end |
| 5966 | * | | +---------------------> att_beg |
| 5967 | * | +--------------------------> prev |
| 5968 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5969 | */ |
| 5970 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5971 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 5972 | /* Iterate through all cookies on this line */ |
| 5973 | |
| 5974 | /* find att_beg */ |
| 5975 | att_beg = prev + 1; |
| 5976 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 5977 | att_beg++; |
| 5978 | |
| 5979 | /* find att_end : this is the first character after the last non |
| 5980 | * space before the equal. It may be equal to hdr_end. |
| 5981 | */ |
| 5982 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5983 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5984 | while (equal < hdr_end) { |
| 5985 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5986 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5987 | if (http_is_spht[(unsigned char)*equal++]) |
| 5988 | continue; |
| 5989 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5990 | } |
| 5991 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5992 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 5993 | * is between <att_beg> and <equal>, both may be identical. |
| 5994 | */ |
| 5995 | |
| 5996 | /* look for end of cookie if there is an equal sign */ |
| 5997 | if (equal < hdr_end && *equal == '=') { |
| 5998 | /* look for the beginning of the value */ |
| 5999 | val_beg = equal + 1; |
| 6000 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6001 | val_beg++; |
| 6002 | |
| 6003 | /* find the end of the value, respecting quotes */ |
| 6004 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6005 | |
| 6006 | /* make val_end point to the first white space or delimitor after the value */ |
| 6007 | val_end = next; |
| 6008 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6009 | val_end--; |
| 6010 | } else { |
| 6011 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6012 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6013 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6014 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6015 | * they will automatically be removed if a header before them is removed, |
| 6016 | * since they're supposed to be linked together. |
| 6017 | */ |
| 6018 | if (*att_beg == '$') |
| 6019 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6020 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6021 | /* Ignore cookies with no equal sign */ |
| 6022 | if (equal == next) { |
| 6023 | /* This is not our cookie, so we must preserve it. But if we already |
| 6024 | * scheduled another cookie for removal, we cannot remove the |
| 6025 | * complete header, but we can remove the previous block itself. |
| 6026 | */ |
| 6027 | preserve_hdr = 1; |
| 6028 | if (del_from != NULL) { |
| 6029 | int delta = del_hdr_value(req, &del_from, prev); |
| 6030 | val_end += delta; |
| 6031 | next += delta; |
| 6032 | hdr_end += delta; |
| 6033 | hdr_next += delta; |
| 6034 | cur_hdr->len += delta; |
| 6035 | http_msg_move_end(&txn->req, delta); |
| 6036 | prev = del_from; |
| 6037 | del_from = NULL; |
| 6038 | } |
| 6039 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6040 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6041 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6042 | /* if there are spaces around the equal sign, we need to |
| 6043 | * strip them otherwise we'll get trouble for cookie captures, |
| 6044 | * or even for rewrites. Since this happens extremely rarely, |
| 6045 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6046 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6047 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6048 | int stripped_before = 0; |
| 6049 | int stripped_after = 0; |
| 6050 | |
| 6051 | if (att_end != equal) { |
| 6052 | stripped_before = buffer_replace2(req, att_end, equal, NULL, 0); |
| 6053 | equal += stripped_before; |
| 6054 | val_beg += stripped_before; |
| 6055 | } |
| 6056 | |
| 6057 | if (val_beg > equal + 1) { |
| 6058 | stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0); |
| 6059 | val_beg += stripped_after; |
| 6060 | stripped_before += stripped_after; |
| 6061 | } |
| 6062 | |
| 6063 | val_end += stripped_before; |
| 6064 | next += stripped_before; |
| 6065 | hdr_end += stripped_before; |
| 6066 | hdr_next += stripped_before; |
| 6067 | cur_hdr->len += stripped_before; |
| 6068 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6069 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6070 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6071 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6072 | /* First, let's see if we want to capture this cookie. We check |
| 6073 | * that we don't already have a client side cookie, because we |
| 6074 | * can only capture one. Also as an optimisation, we ignore |
| 6075 | * cookies shorter than the declared name. |
| 6076 | */ |
| 6077 | if (t->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6078 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6079 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6080 | int log_len = val_end - att_beg; |
| 6081 | |
| 6082 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 6083 | Alert("HTTP logging : out of memory.\n"); |
| 6084 | } else { |
| 6085 | if (log_len > t->fe->capture_len) |
| 6086 | log_len = t->fe->capture_len; |
| 6087 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6088 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6089 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6090 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6091 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6092 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6093 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6094 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6095 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6096 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6097 | * For cookies in prefix mode, the form is : |
| 6098 | * |
| 6099 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6100 | */ |
| 6101 | if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6102 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
| 6103 | struct server *srv = t->be->srv; |
| 6104 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6105 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6106 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6107 | * have the server ID between val_beg and delim, and the original cookie between |
| 6108 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6109 | * |
| 6110 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6111 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6112 | * | || || | |+-> next |
| 6113 | * | || || | +--> val_end |
| 6114 | * | || || +---------> delim |
| 6115 | * | || |+------------> val_beg |
| 6116 | * | || +-------------> att_end = equal |
| 6117 | * | |+-----------------> att_beg |
| 6118 | * | +------------------> prev |
| 6119 | * +-------------------------> hdr_beg |
| 6120 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6121 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6122 | if (t->be->options & PR_O_COOK_PFX) { |
| 6123 | for (delim = val_beg; delim < val_end; delim++) |
| 6124 | if (*delim == COOKIE_DELIM) |
| 6125 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6126 | } else { |
| 6127 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6128 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6129 | /* Now check if the cookie contains a date field, which would |
| 6130 | * appear after a vertical bar ('|') just after the server name |
| 6131 | * and before the delimiter. |
| 6132 | */ |
| 6133 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6134 | if (vbar1) { |
| 6135 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6136 | * right is the last seen date. It is a base64 encoded |
| 6137 | * 30-bit value representing the UNIX date since the |
| 6138 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6139 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6140 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6141 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6142 | if (val_end - vbar1 >= 5) { |
| 6143 | val = b64tos30(vbar1); |
| 6144 | if (val > 0) |
| 6145 | txn->cookie_last_date = val << 2; |
| 6146 | } |
| 6147 | /* look for a second vertical bar */ |
| 6148 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6149 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6150 | val = b64tos30(vbar1 + 1); |
| 6151 | if (val > 0) |
| 6152 | txn->cookie_first_date = val << 2; |
| 6153 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6154 | } |
| 6155 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6156 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6157 | /* if the cookie has an expiration date and the proxy wants to check |
| 6158 | * it, then we do that now. We first check if the cookie is too old, |
| 6159 | * then only if it has expired. We detect strict overflow because the |
| 6160 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6161 | * in the future are ignored if their offset is beyond one day. This |
| 6162 | * allows an admin to fix timezone issues without expiring everyone |
| 6163 | * and at the same time avoids keeping unwanted side effects for too |
| 6164 | * long. |
| 6165 | */ |
| 6166 | if (txn->cookie_first_date && t->be->cookie_maxlife && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6167 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) || |
| 6168 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6169 | txn->flags &= ~TX_CK_MASK; |
| 6170 | txn->flags |= TX_CK_OLD; |
| 6171 | delim = val_beg; // let's pretend we have not found the cookie |
| 6172 | txn->cookie_first_date = 0; |
| 6173 | txn->cookie_last_date = 0; |
| 6174 | } |
| 6175 | else if (txn->cookie_last_date && t->be->cookie_maxidle && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6176 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) || |
| 6177 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6178 | txn->flags &= ~TX_CK_MASK; |
| 6179 | txn->flags |= TX_CK_EXPIRED; |
| 6180 | delim = val_beg; // let's pretend we have not found the cookie |
| 6181 | txn->cookie_first_date = 0; |
| 6182 | txn->cookie_last_date = 0; |
| 6183 | } |
| 6184 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6185 | /* Here, we'll look for the first running server which supports the cookie. |
| 6186 | * This allows to share a same cookie between several servers, for example |
| 6187 | * to dedicate backup servers to specific servers only. |
| 6188 | * However, to prevent clients from sticking to cookie-less backup server |
| 6189 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6190 | * empty cookies and mark them as invalid. |
| 6191 | * The same behaviour is applied when persistence must be ignored. |
| 6192 | */ |
| 6193 | if ((delim == val_beg) || (t->flags & SN_IGNORE_PRST)) |
| 6194 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6195 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6196 | while (srv) { |
| 6197 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6198 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 6199 | if ((srv->state & SRV_RUNNING) || |
| 6200 | (t->be->options & PR_O_PERSIST) || |
| 6201 | (t->flags & SN_FORCE_PRST)) { |
| 6202 | /* we found the server and we can use it */ |
| 6203 | txn->flags &= ~TX_CK_MASK; |
| 6204 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
| 6205 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
| 6206 | t->srv = srv; |
| 6207 | break; |
| 6208 | } else { |
| 6209 | /* we found a server, but it's down, |
| 6210 | * mark it as such and go on in case |
| 6211 | * another one is available. |
| 6212 | */ |
| 6213 | txn->flags &= ~TX_CK_MASK; |
| 6214 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6215 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6216 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6217 | srv = srv->next; |
| 6218 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6219 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6220 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6221 | /* no server matched this cookie */ |
| 6222 | txn->flags &= ~TX_CK_MASK; |
| 6223 | txn->flags |= TX_CK_INVALID; |
| 6224 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6225 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6226 | /* depending on the cookie mode, we may have to either : |
| 6227 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6228 | * the server never sees it ; |
| 6229 | * - remove the server id from the cookie value, and tag the cookie as an |
| 6230 | * application cookie so that it does not get accidentely removed later, |
| 6231 | * if we're in cookie prefix mode |
| 6232 | */ |
| 6233 | if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) { |
| 6234 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6235 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6236 | delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0); |
| 6237 | val_end += delta; |
| 6238 | next += delta; |
| 6239 | hdr_end += delta; |
| 6240 | hdr_next += delta; |
| 6241 | cur_hdr->len += delta; |
| 6242 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6243 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6244 | del_from = NULL; |
| 6245 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6246 | } |
| 6247 | else if (del_from == NULL && |
| 6248 | (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) { |
| 6249 | del_from = prev; |
| 6250 | } |
| 6251 | } else { |
| 6252 | /* This is not our cookie, so we must preserve it. But if we already |
| 6253 | * scheduled another cookie for removal, we cannot remove the |
| 6254 | * complete header, but we can remove the previous block itself. |
| 6255 | */ |
| 6256 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6257 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6258 | if (del_from != NULL) { |
| 6259 | int delta = del_hdr_value(req, &del_from, prev); |
| 6260 | val_end += delta; |
| 6261 | next += delta; |
| 6262 | hdr_end += delta; |
| 6263 | hdr_next += delta; |
| 6264 | cur_hdr->len += delta; |
| 6265 | http_msg_move_end(&txn->req, delta); |
| 6266 | prev = del_from; |
| 6267 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6268 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6269 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6270 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6271 | /* Look for the appsession cookie unless persistence must be ignored */ |
| 6272 | if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
| 6273 | int cmp_len, value_len; |
| 6274 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6275 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6276 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 6277 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6278 | value_begin = att_beg + t->be->appsession_name_len; |
| 6279 | value_len = val_end - att_beg - t->be->appsession_name_len; |
| 6280 | } else { |
| 6281 | cmp_len = att_end - att_beg; |
| 6282 | value_begin = val_beg; |
| 6283 | value_len = val_end - val_beg; |
| 6284 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6285 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6286 | /* let's see if the cookie is our appcookie */ |
| 6287 | if (cmp_len == t->be->appsession_name_len && |
| 6288 | memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) { |
| 6289 | manage_client_side_appsession(t, value_begin, value_len); |
| 6290 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6291 | } |
| 6292 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6293 | /* continue with next cookie on this header line */ |
| 6294 | att_beg = next; |
| 6295 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6296 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6297 | /* There are no more cookies on this line. |
| 6298 | * We may still have one (or several) marked for deletion at the |
| 6299 | * end of the line. We must do this now in two ways : |
| 6300 | * - if some cookies must be preserved, we only delete from the |
| 6301 | * mark to the end of line ; |
| 6302 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6303 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6304 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6305 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6306 | if (preserve_hdr) { |
| 6307 | delta = del_hdr_value(req, &del_from, hdr_end); |
| 6308 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6309 | cur_hdr->len += delta; |
| 6310 | } else { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6311 | delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6312 | |
| 6313 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6314 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6315 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6316 | cur_hdr->len = 0; |
| 6317 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6318 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6319 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6320 | } |
| 6321 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6322 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6323 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6324 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6325 | } |
| 6326 | |
| 6327 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6328 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6329 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6330 | */ |
| 6331 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6332 | { |
| 6333 | char term; |
| 6334 | char *cur_ptr, *cur_end, *cur_next; |
| 6335 | int cur_idx, old_idx, last_hdr; |
| 6336 | struct http_txn *txn = &t->txn; |
| 6337 | struct hdr_idx_elem *cur_hdr; |
| 6338 | int len, delta; |
| 6339 | |
| 6340 | last_hdr = 0; |
| 6341 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6342 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6343 | old_idx = 0; |
| 6344 | |
| 6345 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6346 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6347 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6348 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6349 | (exp->action == ACT_ALLOW || |
| 6350 | exp->action == ACT_DENY)) |
| 6351 | return 0; |
| 6352 | |
| 6353 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 6354 | if (!cur_idx) |
| 6355 | break; |
| 6356 | |
| 6357 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6358 | cur_ptr = cur_next; |
| 6359 | cur_end = cur_ptr + cur_hdr->len; |
| 6360 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6361 | |
| 6362 | /* Now we have one header between cur_ptr and cur_end, |
| 6363 | * and the next header starts at cur_next. |
| 6364 | */ |
| 6365 | |
| 6366 | /* The annoying part is that pattern matching needs |
| 6367 | * that we modify the contents to null-terminate all |
| 6368 | * strings before testing them. |
| 6369 | */ |
| 6370 | |
| 6371 | term = *cur_end; |
| 6372 | *cur_end = '\0'; |
| 6373 | |
| 6374 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6375 | switch (exp->action) { |
| 6376 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6377 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6378 | last_hdr = 1; |
| 6379 | break; |
| 6380 | |
| 6381 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6382 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6383 | last_hdr = 1; |
| 6384 | break; |
| 6385 | |
| 6386 | case ACT_REPLACE: |
| 6387 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6388 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6389 | /* FIXME: if the user adds a newline in the replacement, the |
| 6390 | * index will not be recalculated for now, and the new line |
| 6391 | * will not be counted as a new header. |
| 6392 | */ |
| 6393 | |
| 6394 | cur_end += delta; |
| 6395 | cur_next += delta; |
| 6396 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6397 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6398 | break; |
| 6399 | |
| 6400 | case ACT_REMOVE: |
| 6401 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 6402 | cur_next += delta; |
| 6403 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6404 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6405 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6406 | txn->hdr_idx.used--; |
| 6407 | cur_hdr->len = 0; |
| 6408 | cur_end = NULL; /* null-term has been rewritten */ |
| 6409 | break; |
| 6410 | |
| 6411 | } |
| 6412 | } |
| 6413 | if (cur_end) |
| 6414 | *cur_end = term; /* restore the string terminator */ |
| 6415 | |
| 6416 | /* keep the link from this header to next one in case of later |
| 6417 | * removal of next header. |
| 6418 | */ |
| 6419 | old_idx = cur_idx; |
| 6420 | } |
| 6421 | return 0; |
| 6422 | } |
| 6423 | |
| 6424 | |
| 6425 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 6426 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6427 | * or -1 if a replacement resulted in an invalid status line. |
| 6428 | */ |
| 6429 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6430 | { |
| 6431 | char term; |
| 6432 | char *cur_ptr, *cur_end; |
| 6433 | int done; |
| 6434 | struct http_txn *txn = &t->txn; |
| 6435 | int len, delta; |
| 6436 | |
| 6437 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6438 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6439 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6440 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6441 | (exp->action == ACT_ALLOW || |
| 6442 | exp->action == ACT_DENY)) |
| 6443 | return 0; |
| 6444 | else if (exp->action == ACT_REMOVE) |
| 6445 | return 0; |
| 6446 | |
| 6447 | done = 0; |
| 6448 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6449 | cur_ptr = txn->rsp.sol; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6450 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6451 | |
| 6452 | /* Now we have the status line between cur_ptr and cur_end */ |
| 6453 | |
| 6454 | /* The annoying part is that pattern matching needs |
| 6455 | * that we modify the contents to null-terminate all |
| 6456 | * strings before testing them. |
| 6457 | */ |
| 6458 | |
| 6459 | term = *cur_end; |
| 6460 | *cur_end = '\0'; |
| 6461 | |
| 6462 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6463 | switch (exp->action) { |
| 6464 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6465 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6466 | done = 1; |
| 6467 | break; |
| 6468 | |
| 6469 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6470 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6471 | done = 1; |
| 6472 | break; |
| 6473 | |
| 6474 | case ACT_REPLACE: |
| 6475 | *cur_end = term; /* restore the string terminator */ |
| 6476 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6477 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6478 | /* FIXME: if the user adds a newline in the replacement, the |
| 6479 | * index will not be recalculated for now, and the new line |
| 6480 | * will not be counted as a new header. |
| 6481 | */ |
| 6482 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6483 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6484 | cur_end += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6485 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 6486 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6487 | cur_ptr, cur_end + 1, |
| 6488 | NULL, NULL); |
| 6489 | if (unlikely(!cur_end)) |
| 6490 | return -1; |
| 6491 | |
| 6492 | /* we have a full respnse and we know that we have either a CR |
| 6493 | * or an LF at <ptr>. |
| 6494 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6495 | txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l); |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6496 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r'); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6497 | /* there is no point trying this regex on headers */ |
| 6498 | return 1; |
| 6499 | } |
| 6500 | } |
| 6501 | *cur_end = term; /* restore the string terminator */ |
| 6502 | return done; |
| 6503 | } |
| 6504 | |
| 6505 | |
| 6506 | |
| 6507 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6508 | * 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] | 6509 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 6510 | * unparsable response. |
| 6511 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6512 | 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] | 6513 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6514 | struct http_txn *txn = &s->txn; |
| 6515 | struct hdr_exp *exp; |
| 6516 | |
| 6517 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6518 | int ret; |
| 6519 | |
| 6520 | /* |
| 6521 | * The interleaving of transformations and verdicts |
| 6522 | * makes it difficult to decide to continue or stop |
| 6523 | * the evaluation. |
| 6524 | */ |
| 6525 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6526 | if (txn->flags & TX_SVDENY) |
| 6527 | break; |
| 6528 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6529 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6530 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 6531 | exp->action == ACT_PASS)) { |
| 6532 | exp = exp->next; |
| 6533 | continue; |
| 6534 | } |
| 6535 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6536 | /* if this filter had a condition, evaluate it now and skip to |
| 6537 | * next filter if the condition does not match. |
| 6538 | */ |
| 6539 | if (exp->cond) { |
| 6540 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR); |
| 6541 | ret = acl_pass(ret); |
| 6542 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6543 | ret = !ret; |
| 6544 | if (!ret) |
| 6545 | continue; |
| 6546 | } |
| 6547 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6548 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6549 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6550 | if (unlikely(ret < 0)) |
| 6551 | return -1; |
| 6552 | |
| 6553 | if (likely(ret == 0)) { |
| 6554 | /* The filter did not match the response, it can be |
| 6555 | * iterated through all headers. |
| 6556 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6557 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6558 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6559 | } |
| 6560 | return 0; |
| 6561 | } |
| 6562 | |
| 6563 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6564 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6565 | * Manage server-side cookies. It can impact performance by about 2% so it is |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6566 | * desirable to call it only when needed. This function is also used when we |
| 6567 | * just need to know if there is a cookie (eg: for check-cache). |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6568 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6569 | void manage_server_side_cookies(struct session *t, struct buffer *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6570 | { |
| 6571 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6572 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6573 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6574 | char *hdr_beg, *hdr_end, *hdr_next; |
| 6575 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6576 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6577 | /* Iterate through the headers. |
| 6578 | * we start with the start line. |
| 6579 | */ |
| 6580 | old_idx = 0; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6581 | hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6582 | |
| 6583 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 6584 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6585 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6586 | |
| 6587 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6588 | hdr_beg = hdr_next; |
| 6589 | hdr_end = hdr_beg + cur_hdr->len; |
| 6590 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6591 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6592 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6593 | * next header starts at hdr_next. We're only interested in |
| 6594 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6595 | */ |
| 6596 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6597 | is_cookie2 = 0; |
| 6598 | prev = hdr_beg + 10; |
| 6599 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6600 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6601 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 6602 | if (!val) { |
| 6603 | old_idx = cur_idx; |
| 6604 | continue; |
| 6605 | } |
| 6606 | is_cookie2 = 1; |
| 6607 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6608 | } |
| 6609 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6610 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 6611 | * <prev> points to the colon. |
| 6612 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6613 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6614 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6615 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 6616 | * check-cache is enabled) and we are not interested in checking |
| 6617 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6618 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6619 | if (t->be->cookie_name == NULL && |
| 6620 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6621 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6622 | return; |
| 6623 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6624 | /* OK so now we know we have to process this response cookie. |
| 6625 | * The format of the Set-Cookie header is slightly different |
| 6626 | * from the format of the Cookie header in that it does not |
| 6627 | * support the comma as a cookie delimiter (thus the header |
| 6628 | * cannot be folded) because the Expires attribute described in |
| 6629 | * the original Netscape's spec may contain an unquoted date |
| 6630 | * with a comma inside. We have to live with this because |
| 6631 | * many browsers don't support Max-Age and some browsers don't |
| 6632 | * support quoted strings. However the Set-Cookie2 header is |
| 6633 | * clean. |
| 6634 | * |
| 6635 | * We have to keep multiple pointers in order to support cookie |
| 6636 | * removal at the beginning, middle or end of header without |
| 6637 | * corrupting the header (in case of set-cookie2). A special |
| 6638 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 6639 | * fields after the first semi-colon. The <next> pointer points |
| 6640 | * either to the end of line (set-cookie) or next unquoted comma |
| 6641 | * (set-cookie2). All of these headers are valid : |
| 6642 | * |
| 6643 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 6644 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6645 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6646 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 6647 | * | | | | | | | | | | |
| 6648 | * | | | | | | | | +-> next hdr_end <--+ |
| 6649 | * | | | | | | | +------------> scav |
| 6650 | * | | | | | | +--------------> val_end |
| 6651 | * | | | | | +--------------------> val_beg |
| 6652 | * | | | | +----------------------> equal |
| 6653 | * | | | +------------------------> att_end |
| 6654 | * | | +----------------------------> att_beg |
| 6655 | * | +------------------------------> prev |
| 6656 | * +-----------------------------------------> hdr_beg |
| 6657 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6658 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6659 | for (; prev < hdr_end; prev = next) { |
| 6660 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6661 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6662 | /* find att_beg */ |
| 6663 | att_beg = prev + 1; |
| 6664 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6665 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6666 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6667 | /* find att_end : this is the first character after the last non |
| 6668 | * space before the equal. It may be equal to hdr_end. |
| 6669 | */ |
| 6670 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6671 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6672 | while (equal < hdr_end) { |
| 6673 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 6674 | break; |
| 6675 | if (http_is_spht[(unsigned char)*equal++]) |
| 6676 | continue; |
| 6677 | att_end = equal; |
| 6678 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6679 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6680 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6681 | * is between <att_beg> and <equal>, both may be identical. |
| 6682 | */ |
| 6683 | |
| 6684 | /* look for end of cookie if there is an equal sign */ |
| 6685 | if (equal < hdr_end && *equal == '=') { |
| 6686 | /* look for the beginning of the value */ |
| 6687 | val_beg = equal + 1; |
| 6688 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6689 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6690 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6691 | /* find the end of the value, respecting quotes */ |
| 6692 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6693 | |
| 6694 | /* make val_end point to the first white space or delimitor after the value */ |
| 6695 | val_end = next; |
| 6696 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6697 | val_end--; |
| 6698 | } else { |
| 6699 | /* <equal> points to next comma, semi-colon or EOL */ |
| 6700 | val_beg = val_end = next = equal; |
| 6701 | } |
| 6702 | |
| 6703 | if (next < hdr_end) { |
| 6704 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 6705 | * a colon or semi-colon before the end. So skip all attr-value |
| 6706 | * pairs and look for the next comma. For Set-Cookie, since |
| 6707 | * commas are permitted in values, skip to the end. |
| 6708 | */ |
| 6709 | if (is_cookie2) |
| 6710 | next = find_hdr_value_end(next, hdr_end); |
| 6711 | else |
| 6712 | next = hdr_end; |
| 6713 | } |
| 6714 | |
| 6715 | /* Now everything is as on the diagram above */ |
| 6716 | |
| 6717 | /* Ignore cookies with no equal sign */ |
| 6718 | if (equal == val_end) |
| 6719 | continue; |
| 6720 | |
| 6721 | /* If there are spaces around the equal sign, we need to |
| 6722 | * strip them otherwise we'll get trouble for cookie captures, |
| 6723 | * or even for rewrites. Since this happens extremely rarely, |
| 6724 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6725 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6726 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6727 | int stripped_before = 0; |
| 6728 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6729 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6730 | if (att_end != equal) { |
| 6731 | stripped_before = buffer_replace2(res, att_end, equal, NULL, 0); |
| 6732 | equal += stripped_before; |
| 6733 | val_beg += stripped_before; |
| 6734 | } |
| 6735 | |
| 6736 | if (val_beg > equal + 1) { |
| 6737 | stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0); |
| 6738 | val_beg += stripped_after; |
| 6739 | stripped_before += stripped_after; |
| 6740 | } |
| 6741 | |
| 6742 | val_end += stripped_before; |
| 6743 | next += stripped_before; |
| 6744 | hdr_end += stripped_before; |
| 6745 | hdr_next += stripped_before; |
| 6746 | cur_hdr->len += stripped_before; |
| 6747 | http_msg_move_end(&txn->req, stripped_before); |
| 6748 | } |
| 6749 | |
| 6750 | /* First, let's see if we want to capture this cookie. We check |
| 6751 | * that we don't already have a server side cookie, because we |
| 6752 | * can only capture one. Also as an optimisation, we ignore |
| 6753 | * cookies shorter than the declared name. |
| 6754 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6755 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6756 | txn->srv_cookie == NULL && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6757 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6758 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6759 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 6760 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6761 | Alert("HTTP logging : out of memory.\n"); |
| 6762 | } |
| 6763 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6764 | if (log_len > t->fe->capture_len) |
| 6765 | log_len = t->fe->capture_len; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6766 | memcpy(txn->srv_cookie, att_beg, log_len); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6767 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6768 | } |
| 6769 | |
| 6770 | /* now check if we need to process it for persistence */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6771 | if (!(t->flags & SN_IGNORE_PRST) && |
| 6772 | (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6773 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6774 | /* assume passive cookie by default */ |
| 6775 | txn->flags &= ~TX_SCK_MASK; |
| 6776 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6777 | |
| 6778 | /* If the cookie is in insert mode on a known server, we'll delete |
| 6779 | * this occurrence because we'll insert another one later. |
| 6780 | * We'll delete it too if the "indirect" option is set and we're in |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6781 | * a direct access. |
| 6782 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6783 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 6784 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6785 | /* this cookie must be deleted */ |
| 6786 | if (*prev == ':' && next == hdr_end) { |
| 6787 | /* whole header */ |
| 6788 | delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0); |
| 6789 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6790 | txn->hdr_idx.used--; |
| 6791 | cur_hdr->len = 0; |
| 6792 | hdr_next += delta; |
| 6793 | http_msg_move_end(&txn->rsp, delta); |
| 6794 | /* note: while both invalid now, <next> and <hdr_end> |
| 6795 | * are still equal, so the for() will stop as expected. |
| 6796 | */ |
| 6797 | } else { |
| 6798 | /* just remove the value */ |
| 6799 | int delta = del_hdr_value(res, &prev, next); |
| 6800 | next = prev; |
| 6801 | hdr_end += delta; |
| 6802 | hdr_next += delta; |
| 6803 | cur_hdr->len += delta; |
| 6804 | http_msg_move_end(&txn->rsp, delta); |
| 6805 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6806 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6807 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6808 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6809 | } |
| 6810 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6811 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6812 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6813 | * with this server since we know it. |
| 6814 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6815 | delta = buffer_replace2(res, val_beg, val_end, t->srv->cookie, t->srv->cklen); |
| 6816 | next += delta; |
| 6817 | hdr_end += delta; |
| 6818 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6819 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6820 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6821 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6822 | txn->flags &= ~TX_SCK_MASK; |
| 6823 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6824 | } |
| 6825 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6826 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6827 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6828 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6829 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6830 | delta = buffer_replace2(res, val_beg, val_beg, t->srv->cookie, t->srv->cklen + 1); |
| 6831 | next += delta; |
| 6832 | hdr_end += delta; |
| 6833 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6834 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6835 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6836 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6837 | val_beg[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6838 | txn->flags &= ~TX_SCK_MASK; |
| 6839 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6840 | } |
| 6841 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6842 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
| 6843 | else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6844 | int cmp_len, value_len; |
| 6845 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6846 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6847 | if (t->be->options2 & PR_O2_AS_PFX) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6848 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6849 | value_begin = att_beg + t->be->appsession_name_len; |
| 6850 | value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6851 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6852 | cmp_len = att_end - att_beg; |
| 6853 | value_begin = val_beg; |
| 6854 | value_len = MIN(t->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6855 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6856 | |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 6857 | if ((cmp_len == t->be->appsession_name_len) && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6858 | (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 6859 | /* free a possibly previously allocated memory */ |
| 6860 | pool_free2(apools.sessid, txn->sessid); |
| 6861 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6862 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6863 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6864 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6865 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6866 | return; |
| 6867 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6868 | memcpy(txn->sessid, value_begin, value_len); |
| 6869 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6870 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6871 | } |
| 6872 | /* that's done for this cookie, check the next one on the same |
| 6873 | * line when next != hdr_end (only if is_cookie2). |
| 6874 | */ |
| 6875 | } |
| 6876 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6877 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6878 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6879 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6880 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6881 | appsess *asession = NULL; |
| 6882 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6883 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6884 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 6885 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6886 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 6887 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 6888 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 6889 | return; |
| 6890 | } |
| 6891 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 6892 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6893 | 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] | 6894 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6895 | return; |
| 6896 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6897 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6898 | asession->sessid[t->be->appsession_len] = 0; |
| 6899 | |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 6900 | server_id_len = strlen(t->srv->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6901 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
| 6902 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6903 | 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] | 6904 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6905 | return; |
| 6906 | } |
| 6907 | asession->serverid[0] = '\0'; |
| 6908 | memcpy(asession->serverid, t->srv->id, server_id_len); |
| 6909 | |
| 6910 | asession->request_count = 0; |
| 6911 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 6912 | } |
| 6913 | |
| 6914 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6915 | asession->request_count++; |
| 6916 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6917 | } |
| 6918 | |
| 6919 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6920 | /* |
| 6921 | * Check if response is cacheable or not. Updates t->flags. |
| 6922 | */ |
| 6923 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 6924 | { |
| 6925 | struct http_txn *txn = &t->txn; |
| 6926 | char *p1, *p2; |
| 6927 | |
| 6928 | char *cur_ptr, *cur_end, *cur_next; |
| 6929 | int cur_idx; |
| 6930 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 6931 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6932 | return; |
| 6933 | |
| 6934 | /* Iterate through the headers. |
| 6935 | * we start with the start line. |
| 6936 | */ |
| 6937 | cur_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6938 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6939 | |
| 6940 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6941 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6942 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6943 | |
| 6944 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6945 | cur_ptr = cur_next; |
| 6946 | cur_end = cur_ptr + cur_hdr->len; |
| 6947 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6948 | |
| 6949 | /* We have one full header between cur_ptr and cur_end, and the |
| 6950 | * next header starts at cur_next. We're only interested in |
| 6951 | * "Cookie:" headers. |
| 6952 | */ |
| 6953 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6954 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 6955 | if (val) { |
| 6956 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 6957 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 6958 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 6959 | return; |
| 6960 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6961 | } |
| 6962 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6963 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 6964 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6965 | continue; |
| 6966 | |
| 6967 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 6968 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6969 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6970 | |
| 6971 | if (p1 >= cur_end) /* no more info */ |
| 6972 | continue; |
| 6973 | |
| 6974 | /* p1 is at the beginning of the value */ |
| 6975 | p2 = p1; |
| 6976 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 6977 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6978 | p2++; |
| 6979 | |
| 6980 | /* we have a complete value between p1 and p2 */ |
| 6981 | if (p2 < cur_end && *p2 == '=') { |
| 6982 | /* we have something of the form no-cache="set-cookie" */ |
| 6983 | if ((cur_end - p1 >= 21) && |
| 6984 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 6985 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6986 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6987 | continue; |
| 6988 | } |
| 6989 | |
| 6990 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 6991 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 6992 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 6993 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 6994 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6995 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6996 | return; |
| 6997 | } |
| 6998 | |
| 6999 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7000 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7001 | continue; |
| 7002 | } |
| 7003 | } |
| 7004 | } |
| 7005 | |
| 7006 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7007 | /* |
| 7008 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 7009 | * If the server is found, it's assigned to the session. |
| 7010 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7011 | 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] | 7012 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7013 | char *end_params, *first_param, *cur_param, *next_param; |
| 7014 | char separator; |
| 7015 | int value_len; |
| 7016 | |
| 7017 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7018 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7019 | if (t->be->appsession_name == NULL || |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7020 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST && t->txn.meth != HTTP_METH_HEAD)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7021 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7022 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7023 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7024 | first_param = NULL; |
| 7025 | switch (mode) { |
| 7026 | case PR_O2_AS_M_PP: |
| 7027 | first_param = memchr(begin, ';', len); |
| 7028 | break; |
| 7029 | case PR_O2_AS_M_QS: |
| 7030 | first_param = memchr(begin, '?', len); |
| 7031 | break; |
| 7032 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7033 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7034 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7035 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7036 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7037 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7038 | switch (mode) { |
| 7039 | case PR_O2_AS_M_PP: |
| 7040 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 7041 | end_params = (char *) begin + len; |
| 7042 | } |
| 7043 | separator = ';'; |
| 7044 | break; |
| 7045 | case PR_O2_AS_M_QS: |
| 7046 | end_params = (char *) begin + len; |
| 7047 | separator = '&'; |
| 7048 | break; |
| 7049 | default: |
| 7050 | /* unknown mode, shouldn't happen */ |
| 7051 | return; |
| 7052 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7053 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7054 | cur_param = next_param = end_params; |
| 7055 | while (cur_param > first_param) { |
| 7056 | cur_param--; |
| 7057 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 7058 | /* let's see if this is the appsession parameter */ |
| 7059 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 7060 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 7061 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7062 | /* Cool... it's the right one */ |
| 7063 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 7064 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 7065 | if (value_len > 0) { |
| 7066 | manage_client_side_appsession(t, cur_param, value_len); |
| 7067 | } |
| 7068 | break; |
| 7069 | } |
| 7070 | next_param = cur_param; |
| 7071 | } |
| 7072 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7073 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7074 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 7075 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7076 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7077 | } |
| 7078 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7079 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7080 | * In a GET, HEAD or POST request, check if the requested URI matches the stats uri |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7081 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7082 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7083 | * It is assumed that the request is either a HEAD, GET, or POST and that the |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7084 | * t->be->uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7085 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7086 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7087 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7088 | int stats_check_uri(struct session *t, struct proxy *backend) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7089 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7090 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7091 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7092 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7093 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7094 | if (!uri_auth) |
| 7095 | return 0; |
| 7096 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7097 | if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST) |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7098 | return 0; |
| 7099 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7100 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 7101 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7102 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7103 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7104 | return 0; |
| 7105 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7106 | h = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7107 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 7108 | /* the URI is in h */ |
| 7109 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7110 | return 0; |
| 7111 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7112 | h += uri_auth->uri_len; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7113 | 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] | 7114 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7115 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7116 | break; |
| 7117 | } |
| 7118 | h++; |
| 7119 | } |
| 7120 | |
| 7121 | if (uri_auth->refresh) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7122 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7123 | 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] | 7124 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7125 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7126 | break; |
| 7127 | } |
| 7128 | h++; |
| 7129 | } |
| 7130 | } |
| 7131 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7132 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7133 | 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] | 7134 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7135 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7136 | break; |
| 7137 | } |
| 7138 | h++; |
| 7139 | } |
| 7140 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7141 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7142 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) { |
| 7143 | if (memcmp(h, ";st=", 4) == 0) { |
| 7144 | h += 4; |
| 7145 | |
| 7146 | if (memcmp(h, STAT_STATUS_DONE, 4) == 0) |
| 7147 | t->data_ctx.stats.st_code = STAT_STATUS_DONE; |
| 7148 | else if (memcmp(h, STAT_STATUS_NONE, 4) == 0) |
| 7149 | t->data_ctx.stats.st_code = STAT_STATUS_NONE; |
| 7150 | else if (memcmp(h, STAT_STATUS_EXCD, 4) == 0) |
| 7151 | t->data_ctx.stats.st_code = STAT_STATUS_EXCD; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 7152 | else if (memcmp(h, STAT_STATUS_DENY, 4) == 0) |
| 7153 | t->data_ctx.stats.st_code = STAT_STATUS_DENY; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7154 | else |
| 7155 | t->data_ctx.stats.st_code = STAT_STATUS_UNKN; |
| 7156 | break; |
| 7157 | } |
| 7158 | h++; |
| 7159 | } |
| 7160 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7161 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 7162 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7163 | return 1; |
| 7164 | } |
| 7165 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7166 | /* |
| 7167 | * 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] | 7168 | * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not |
| 7169 | * assume that msg->sol = buf->data + msg->som. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7170 | */ |
| 7171 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
| 7172 | struct buffer *buf, struct http_msg *msg, |
| 7173 | struct proxy *other_end) |
| 7174 | { |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 7175 | es->len = buf->r - (buf->data + msg->som); |
| 7176 | 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] | 7177 | if (msg->err_pos >= 0) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 7178 | es->pos = msg->err_pos - msg->som; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7179 | else |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 7180 | es->pos = buf->lr - (buf->data + msg->som); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7181 | es->when = date; // user-visible date |
| 7182 | es->sid = s->uniq_id; |
| 7183 | es->srv = s->srv; |
| 7184 | es->oe = other_end; |
| 7185 | es->src = s->cli_addr; |
| 7186 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7187 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7188 | /* return the IP address pointed to by occurrence <occ> of header <hname> in |
| 7189 | * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the |
| 7190 | * occurrence number corresponding to this value is returned. If <occ> is |
| 7191 | * strictly negative, the occurrence number before the end corresponding to |
| 7192 | * this value is returned. If <occ> is null, any value is returned, so it is |
| 7193 | * not recommended to use it that way. Negative occurrences are limited to |
| 7194 | * a small value because it is required to keep them in memory while scanning. |
| 7195 | * IP address 0.0.0.0 is returned if no match is found. |
| 7196 | */ |
| 7197 | unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ) |
| 7198 | { |
| 7199 | struct hdr_ctx ctx; |
| 7200 | unsigned int hdr_hist[MAX_HDR_HISTORY]; |
| 7201 | unsigned int hist_ptr; |
| 7202 | int found = 0; |
| 7203 | |
| 7204 | ctx.idx = 0; |
| 7205 | if (occ >= 0) { |
| 7206 | while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) { |
| 7207 | occ--; |
| 7208 | if (occ <= 0) { |
| 7209 | found = 1; |
| 7210 | break; |
| 7211 | } |
| 7212 | } |
| 7213 | if (!found) |
| 7214 | return 0; |
| 7215 | return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen); |
| 7216 | } |
| 7217 | |
| 7218 | /* negative occurrence, we scan all the list then walk back */ |
| 7219 | if (-occ > MAX_HDR_HISTORY) |
| 7220 | return 0; |
| 7221 | |
| 7222 | hist_ptr = 0; |
| 7223 | hdr_hist[hist_ptr] = 0; |
| 7224 | while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) { |
| 7225 | hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen); |
| 7226 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7227 | hist_ptr = 0; |
| 7228 | found++; |
| 7229 | } |
| 7230 | if (-occ > found) |
| 7231 | return 0; |
| 7232 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 7233 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 7234 | */ |
| 7235 | hist_ptr += occ; |
| 7236 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7237 | hist_ptr -= MAX_HDR_HISTORY; |
| 7238 | return hdr_hist[hist_ptr]; |
| 7239 | } |
| 7240 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7241 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7242 | * Print a debug line with a header |
| 7243 | */ |
| 7244 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 7245 | { |
| 7246 | int len, max; |
| 7247 | 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] | 7248 | 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] | 7249 | max = end - start; |
| 7250 | UBOUND(max, sizeof(trash) - len - 1); |
| 7251 | len += strlcpy2(trash + len, start, max + 1); |
| 7252 | trash[len++] = '\n'; |
| 7253 | write(1, trash, len); |
| 7254 | } |
| 7255 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7256 | /* |
| 7257 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 7258 | * the required fields are properly allocated and that we only need to (re)init |
| 7259 | * them. This should be used before processing any new request. |
| 7260 | */ |
| 7261 | void http_init_txn(struct session *s) |
| 7262 | { |
| 7263 | struct http_txn *txn = &s->txn; |
| 7264 | struct proxy *fe = s->fe; |
| 7265 | |
| 7266 | txn->flags = 0; |
| 7267 | txn->status = -1; |
| 7268 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7269 | txn->cookie_first_date = 0; |
| 7270 | txn->cookie_last_date = 0; |
| 7271 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7272 | txn->req.sol = txn->req.eol = NULL; |
| 7273 | txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
| 7274 | txn->rsp.sol = txn->rsp.eol = NULL; |
| 7275 | txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 7276 | txn->req.hdr_content_len = 0LL; |
| 7277 | txn->rsp.hdr_content_len = 0LL; |
| 7278 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7279 | 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] | 7280 | |
| 7281 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7282 | |
| 7283 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7284 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7285 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7286 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7287 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7288 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 7289 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7290 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7291 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 7292 | |
| 7293 | if (txn->hdr_idx.v) |
| 7294 | hdr_idx_init(&txn->hdr_idx); |
| 7295 | } |
| 7296 | |
| 7297 | /* to be used at the end of a transaction */ |
| 7298 | void http_end_txn(struct session *s) |
| 7299 | { |
| 7300 | struct http_txn *txn = &s->txn; |
| 7301 | |
| 7302 | /* these ones will have been dynamically allocated */ |
| 7303 | pool_free2(pool2_requri, txn->uri); |
| 7304 | pool_free2(pool2_capture, txn->cli_cookie); |
| 7305 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7306 | pool_free2(apools.sessid, txn->sessid); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7307 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7308 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7309 | txn->uri = NULL; |
| 7310 | txn->srv_cookie = NULL; |
| 7311 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7312 | |
| 7313 | if (txn->req.cap) { |
| 7314 | struct cap_hdr *h; |
| 7315 | for (h = s->fe->req_cap; h; h = h->next) |
| 7316 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 7317 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 7318 | } |
| 7319 | |
| 7320 | if (txn->rsp.cap) { |
| 7321 | struct cap_hdr *h; |
| 7322 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 7323 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 7324 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 7325 | } |
| 7326 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7327 | } |
| 7328 | |
| 7329 | /* to be used at the end of a transaction to prepare a new one */ |
| 7330 | void http_reset_txn(struct session *s) |
| 7331 | { |
| 7332 | http_end_txn(s); |
| 7333 | http_init_txn(s); |
| 7334 | |
| 7335 | s->be = s->fe; |
| 7336 | s->req->analysers = s->listener->analysers; |
| 7337 | s->logs.logwait = s->fe->to_log; |
| 7338 | s->srv = s->prev_srv = s->srv_conn = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7339 | /* re-init store persistence */ |
| 7340 | s->store_count = 0; |
| 7341 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7342 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7343 | |
| 7344 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
| 7345 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7346 | /* We must trim any excess data from the response buffer, because we |
| 7347 | * may have blocked an invalid response from a server that we don't |
| 7348 | * want to accidentely forward once we disable the analysers, nor do |
| 7349 | * we want those data to come along with next response. A typical |
| 7350 | * example of such data would be from a buggy server responding to |
| 7351 | * a HEAD with some data, or sending more than the advertised |
| 7352 | * content-length. |
| 7353 | */ |
| 7354 | if (unlikely(s->rep->l > s->rep->send_max)) { |
| 7355 | s->rep->l = s->rep->send_max; |
| 7356 | s->rep->r = s->rep->w + s->rep->l; |
| 7357 | if (s->rep->r >= s->rep->data + s->rep->size) |
| 7358 | s->rep->r -= s->rep->size; |
| 7359 | } |
| 7360 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7361 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7362 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7363 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7364 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7365 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7366 | |
| 7367 | s->req->rex = TICK_ETERNITY; |
| 7368 | s->req->wex = TICK_ETERNITY; |
| 7369 | s->req->analyse_exp = TICK_ETERNITY; |
| 7370 | s->rep->rex = TICK_ETERNITY; |
| 7371 | s->rep->wex = TICK_ETERNITY; |
| 7372 | s->rep->analyse_exp = TICK_ETERNITY; |
| 7373 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7374 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7375 | /************************************************************************/ |
| 7376 | /* The code below is dedicated to ACL parsing and matching */ |
| 7377 | /************************************************************************/ |
| 7378 | |
| 7379 | |
| 7380 | |
| 7381 | |
| 7382 | /* 1. Check on METHOD |
| 7383 | * We use the pre-parsed method if it is known, and store its number as an |
| 7384 | * integer. If it is unknown, we use the pointer and the length. |
| 7385 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7386 | 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] | 7387 | { |
| 7388 | int len, meth; |
| 7389 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7390 | len = strlen(*text); |
| 7391 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7392 | |
| 7393 | pattern->val.i = meth; |
| 7394 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7395 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7396 | if (!pattern->ptr.str) |
| 7397 | return 0; |
| 7398 | pattern->len = len; |
| 7399 | } |
| 7400 | return 1; |
| 7401 | } |
| 7402 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7403 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7404 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7405 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7406 | { |
| 7407 | int meth; |
| 7408 | struct http_txn *txn = l7; |
| 7409 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7410 | if (!txn) |
| 7411 | return 0; |
| 7412 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7413 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7414 | return 0; |
| 7415 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7416 | meth = txn->meth; |
| 7417 | test->i = meth; |
| 7418 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7419 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7420 | /* ensure the indexes are not affected */ |
| 7421 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7422 | test->len = txn->req.sl.rq.m_l; |
| 7423 | test->ptr = txn->req.sol; |
| 7424 | } |
| 7425 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7426 | return 1; |
| 7427 | } |
| 7428 | |
| 7429 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 7430 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7431 | int icase; |
| 7432 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7433 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7434 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7435 | |
| 7436 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7437 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7438 | |
| 7439 | /* Other method, we must compare the strings */ |
| 7440 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7441 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7442 | |
| 7443 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 7444 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 7445 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7446 | return ACL_PAT_FAIL; |
| 7447 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7448 | } |
| 7449 | |
| 7450 | /* 2. Check on Request/Status Version |
| 7451 | * We simply compare strings here. |
| 7452 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7453 | 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] | 7454 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7455 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7456 | if (!pattern->ptr.str) |
| 7457 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7458 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7459 | return 1; |
| 7460 | } |
| 7461 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7462 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7463 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7464 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7465 | { |
| 7466 | struct http_txn *txn = l7; |
| 7467 | char *ptr; |
| 7468 | int len; |
| 7469 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7470 | if (!txn) |
| 7471 | return 0; |
| 7472 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7473 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7474 | return 0; |
| 7475 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7476 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7477 | ptr = txn->req.sol + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7478 | |
| 7479 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7480 | if (len <= 0) |
| 7481 | return 0; |
| 7482 | |
| 7483 | test->ptr = ptr; |
| 7484 | test->len = len; |
| 7485 | |
| 7486 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7487 | return 1; |
| 7488 | } |
| 7489 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7490 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7491 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7492 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7493 | { |
| 7494 | struct http_txn *txn = l7; |
| 7495 | char *ptr; |
| 7496 | int len; |
| 7497 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7498 | if (!txn) |
| 7499 | return 0; |
| 7500 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7501 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7502 | return 0; |
| 7503 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7504 | len = txn->rsp.sl.st.v_l; |
| 7505 | ptr = txn->rsp.sol; |
| 7506 | |
| 7507 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7508 | if (len <= 0) |
| 7509 | return 0; |
| 7510 | |
| 7511 | test->ptr = ptr; |
| 7512 | test->len = len; |
| 7513 | |
| 7514 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7515 | return 1; |
| 7516 | } |
| 7517 | |
| 7518 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7519 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7520 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7521 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7522 | { |
| 7523 | struct http_txn *txn = l7; |
| 7524 | char *ptr; |
| 7525 | int len; |
| 7526 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7527 | if (!txn) |
| 7528 | return 0; |
| 7529 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7530 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7531 | return 0; |
| 7532 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7533 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7534 | ptr = txn->rsp.sol + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7535 | |
| 7536 | test->i = __strl2ui(ptr, len); |
| 7537 | test->flags = ACL_TEST_F_VOL_1ST; |
| 7538 | return 1; |
| 7539 | } |
| 7540 | |
| 7541 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7542 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7543 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7544 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7545 | { |
| 7546 | struct http_txn *txn = l7; |
| 7547 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7548 | if (!txn) |
| 7549 | return 0; |
| 7550 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7551 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7552 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7553 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7554 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7555 | /* ensure the indexes are not affected */ |
| 7556 | return 0; |
| 7557 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7558 | test->len = txn->req.sl.rq.u_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7559 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7560 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 7561 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 7562 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7563 | return 1; |
| 7564 | } |
| 7565 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7566 | static int |
| 7567 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7568 | struct acl_expr *expr, struct acl_test *test) |
| 7569 | { |
| 7570 | struct http_txn *txn = l7; |
| 7571 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7572 | if (!txn) |
| 7573 | return 0; |
| 7574 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7575 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7576 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7577 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7578 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7579 | /* ensure the indexes are not affected */ |
| 7580 | return 0; |
| 7581 | |
| 7582 | /* Parse HTTP request */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7583 | 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] | 7584 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 7585 | test->i = AF_INET; |
| 7586 | |
| 7587 | /* |
| 7588 | * If we are parsing url in frontend space, we prepare backend stage |
| 7589 | * to not parse again the same url ! optimization lazyness... |
| 7590 | */ |
| 7591 | if (px->options & PR_O_HTTP_PROXY) |
| 7592 | l4->flags |= SN_ADDR_SET; |
| 7593 | |
| 7594 | test->flags = ACL_TEST_F_READ_ONLY; |
| 7595 | return 1; |
| 7596 | } |
| 7597 | |
| 7598 | static int |
| 7599 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7600 | struct acl_expr *expr, struct acl_test *test) |
| 7601 | { |
| 7602 | struct http_txn *txn = l7; |
| 7603 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7604 | if (!txn) |
| 7605 | return 0; |
| 7606 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7607 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7608 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7609 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7610 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7611 | /* ensure the indexes are not affected */ |
| 7612 | return 0; |
| 7613 | |
| 7614 | /* Same optimization as url_ip */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7615 | 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] | 7616 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 7617 | |
| 7618 | if (px->options & PR_O_HTTP_PROXY) |
| 7619 | l4->flags |= SN_ADDR_SET; |
| 7620 | |
| 7621 | test->flags = ACL_TEST_F_READ_ONLY; |
| 7622 | return 1; |
| 7623 | } |
| 7624 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7625 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 7626 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 7627 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7628 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7629 | 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] | 7630 | struct acl_expr *expr, struct acl_test *test) |
| 7631 | { |
| 7632 | struct http_txn *txn = l7; |
| 7633 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7634 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7635 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7636 | if (!txn) |
| 7637 | return 0; |
| 7638 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7639 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7640 | /* search for header from the beginning */ |
| 7641 | ctx->idx = 0; |
| 7642 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7643 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7644 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7645 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7646 | test->len = ctx->vlen; |
| 7647 | test->ptr = (char *)ctx->line + ctx->val; |
| 7648 | return 1; |
| 7649 | } |
| 7650 | |
| 7651 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7652 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7653 | return 0; |
| 7654 | } |
| 7655 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7656 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7657 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7658 | struct acl_expr *expr, struct acl_test *test) |
| 7659 | { |
| 7660 | struct http_txn *txn = l7; |
| 7661 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7662 | if (!txn) |
| 7663 | return 0; |
| 7664 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7665 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7666 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7667 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7668 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7669 | /* ensure the indexes are not affected */ |
| 7670 | return 0; |
| 7671 | |
| 7672 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 7673 | } |
| 7674 | |
| 7675 | static int |
| 7676 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7677 | struct acl_expr *expr, struct acl_test *test) |
| 7678 | { |
| 7679 | struct http_txn *txn = l7; |
| 7680 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7681 | if (!txn) |
| 7682 | return 0; |
| 7683 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7684 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7685 | return 0; |
| 7686 | |
| 7687 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 7688 | } |
| 7689 | |
| 7690 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 7691 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 7692 | */ |
| 7693 | static int |
| 7694 | 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] | 7695 | struct acl_expr *expr, struct acl_test *test) |
| 7696 | { |
| 7697 | struct http_txn *txn = l7; |
| 7698 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7699 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7700 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7701 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7702 | if (!txn) |
| 7703 | return 0; |
| 7704 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7705 | ctx.idx = 0; |
| 7706 | cnt = 0; |
| 7707 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 7708 | cnt++; |
| 7709 | |
| 7710 | test->i = cnt; |
| 7711 | test->flags = ACL_TEST_F_VOL_HDR; |
| 7712 | return 1; |
| 7713 | } |
| 7714 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7715 | static int |
| 7716 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7717 | struct acl_expr *expr, struct acl_test *test) |
| 7718 | { |
| 7719 | struct http_txn *txn = l7; |
| 7720 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7721 | if (!txn) |
| 7722 | return 0; |
| 7723 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7724 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7725 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7726 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7727 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7728 | /* ensure the indexes are not affected */ |
| 7729 | return 0; |
| 7730 | |
| 7731 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 7732 | } |
| 7733 | |
| 7734 | static int |
| 7735 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7736 | struct acl_expr *expr, struct acl_test *test) |
| 7737 | { |
| 7738 | struct http_txn *txn = l7; |
| 7739 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7740 | if (!txn) |
| 7741 | return 0; |
| 7742 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7743 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7744 | return 0; |
| 7745 | |
| 7746 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 7747 | } |
| 7748 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7749 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 7750 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7751 | * 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] | 7752 | */ |
| 7753 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7754 | 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] | 7755 | struct acl_expr *expr, struct acl_test *test) |
| 7756 | { |
| 7757 | struct http_txn *txn = l7; |
| 7758 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7759 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7760 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7761 | if (!txn) |
| 7762 | return 0; |
| 7763 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7764 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7765 | /* search for header from the beginning */ |
| 7766 | ctx->idx = 0; |
| 7767 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7768 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7769 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7770 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7771 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 7772 | return 1; |
| 7773 | } |
| 7774 | |
| 7775 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7776 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7777 | return 0; |
| 7778 | } |
| 7779 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7780 | static int |
| 7781 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7782 | struct acl_expr *expr, struct acl_test *test) |
| 7783 | { |
| 7784 | struct http_txn *txn = l7; |
| 7785 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7786 | if (!txn) |
| 7787 | return 0; |
| 7788 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7789 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7790 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7791 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7792 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7793 | /* ensure the indexes are not affected */ |
| 7794 | return 0; |
| 7795 | |
| 7796 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 7797 | } |
| 7798 | |
| 7799 | static int |
| 7800 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7801 | struct acl_expr *expr, struct acl_test *test) |
| 7802 | { |
| 7803 | struct http_txn *txn = l7; |
| 7804 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7805 | if (!txn) |
| 7806 | return 0; |
| 7807 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7808 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7809 | return 0; |
| 7810 | |
| 7811 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 7812 | } |
| 7813 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7814 | /* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned. |
| 7815 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 7816 | */ |
| 7817 | static int |
| 7818 | acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 7819 | struct acl_expr *expr, struct acl_test *test) |
| 7820 | { |
| 7821 | struct http_txn *txn = l7; |
| 7822 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7823 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 7824 | |
| 7825 | if (!txn) |
| 7826 | return 0; |
| 7827 | |
| 7828 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7829 | /* search for header from the beginning */ |
| 7830 | ctx->idx = 0; |
| 7831 | |
| 7832 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7833 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7834 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7835 | /* Same optimization as url_ip */ |
| 7836 | memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr)); |
| 7837 | url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr); |
| 7838 | test->ptr = (void *)&l4->srv_addr.sin_addr; |
| 7839 | test->i = AF_INET; |
| 7840 | return 1; |
| 7841 | } |
| 7842 | |
| 7843 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7844 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7845 | return 0; |
| 7846 | } |
| 7847 | |
| 7848 | static int |
| 7849 | acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7850 | struct acl_expr *expr, struct acl_test *test) |
| 7851 | { |
| 7852 | struct http_txn *txn = l7; |
| 7853 | |
| 7854 | if (!txn) |
| 7855 | return 0; |
| 7856 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7857 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7858 | return 0; |
| 7859 | |
| 7860 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7861 | /* ensure the indexes are not affected */ |
| 7862 | return 0; |
| 7863 | |
| 7864 | return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test); |
| 7865 | } |
| 7866 | |
| 7867 | static int |
| 7868 | acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7869 | struct acl_expr *expr, struct acl_test *test) |
| 7870 | { |
| 7871 | struct http_txn *txn = l7; |
| 7872 | |
| 7873 | if (!txn) |
| 7874 | return 0; |
| 7875 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7876 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7877 | return 0; |
| 7878 | |
| 7879 | return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test); |
| 7880 | } |
| 7881 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7882 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 7883 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 7884 | */ |
| 7885 | static int |
| 7886 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7887 | struct acl_expr *expr, struct acl_test *test) |
| 7888 | { |
| 7889 | struct http_txn *txn = l7; |
| 7890 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7891 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7892 | if (!txn) |
| 7893 | return 0; |
| 7894 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7895 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7896 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7897 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7898 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7899 | /* ensure the indexes are not affected */ |
| 7900 | return 0; |
| 7901 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7902 | 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] | 7903 | ptr = http_get_path(txn); |
| 7904 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7905 | return 0; |
| 7906 | |
| 7907 | /* OK, we got the '/' ! */ |
| 7908 | test->ptr = ptr; |
| 7909 | |
| 7910 | while (ptr < end && *ptr != '?') |
| 7911 | ptr++; |
| 7912 | |
| 7913 | test->len = ptr - test->ptr; |
| 7914 | |
| 7915 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 7916 | test->flags = ACL_TEST_F_VOL_1ST; |
| 7917 | return 1; |
| 7918 | } |
| 7919 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7920 | static int |
| 7921 | acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir, |
| 7922 | struct acl_expr *expr, struct acl_test *test) |
| 7923 | { |
| 7924 | struct buffer *req = s->req; |
| 7925 | struct http_txn *txn = &s->txn; |
| 7926 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7927 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7928 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 7929 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 7930 | */ |
| 7931 | |
| 7932 | if (!s || !req) |
| 7933 | return 0; |
| 7934 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7935 | if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7936 | /* Already decoded as OK */ |
| 7937 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 7938 | return 1; |
| 7939 | } |
| 7940 | |
| 7941 | /* Try to decode HTTP request */ |
| 7942 | if (likely(req->lr < req->r)) |
| 7943 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
| 7944 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7945 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7946 | if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) { |
| 7947 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 7948 | return 1; |
| 7949 | } |
| 7950 | /* wait for final state */ |
| 7951 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 7952 | return 0; |
| 7953 | } |
| 7954 | |
| 7955 | /* OK we got a valid HTTP request. We have some minor preparation to |
| 7956 | * perform so that further checks can rely on HTTP tests. |
| 7957 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7958 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7959 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 7960 | s->flags |= SN_REDIRECTABLE; |
| 7961 | |
| 7962 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) { |
| 7963 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 7964 | return 1; |
| 7965 | } |
| 7966 | |
| 7967 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 7968 | return 1; |
| 7969 | } |
| 7970 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7971 | static int |
| 7972 | acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir, |
| 7973 | struct acl_expr *expr, struct acl_test *test) |
| 7974 | { |
| 7975 | |
| 7976 | if (!s) |
| 7977 | return 0; |
| 7978 | |
| 7979 | if (!get_http_auth(s)) |
| 7980 | return 0; |
| 7981 | |
| 7982 | test->ctx.a[0] = expr->arg.ul; |
| 7983 | test->ctx.a[1] = s->txn.auth.user; |
| 7984 | test->ctx.a[2] = s->txn.auth.pass; |
| 7985 | |
| 7986 | test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH; |
| 7987 | |
| 7988 | return 1; |
| 7989 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7990 | |
| 7991 | /************************************************************************/ |
| 7992 | /* All supported keywords must be declared here. */ |
| 7993 | /************************************************************************/ |
| 7994 | |
| 7995 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 7996 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7997 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
| 7998 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7999 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8000 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
| 8001 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8002 | { "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] | 8003 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8004 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8005 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8006 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8007 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8008 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8009 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8010 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 8011 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8012 | { "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] | 8013 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8014 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8015 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8016 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8017 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8018 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8019 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8020 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8021 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8022 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 8023 | { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 8024 | { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8025 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8026 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8027 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 8028 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 8029 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 8030 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 8031 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 8032 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 8033 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 8034 | { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 8035 | { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8036 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8037 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8038 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8039 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8040 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8041 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8042 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8043 | { "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] | 8044 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 8045 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8046 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 8047 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 8048 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 8049 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 8050 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 8051 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 8052 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 8053 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8054 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 8055 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 8056 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 8057 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 8058 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 8059 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 8060 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 8061 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8062 | #endif |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8063 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8064 | { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth }, |
| 8065 | { "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] | 8066 | { NULL, NULL, NULL, NULL }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8067 | }}; |
| 8068 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8069 | /************************************************************************/ |
| 8070 | /* The code below is dedicated to pattern fetching and matching */ |
| 8071 | /************************************************************************/ |
| 8072 | |
| 8073 | /* extract the IP address from the last occurrence of specified header. Note |
| 8074 | * that we should normally first extract the string then convert it to IP, |
| 8075 | * but right now we have all the functions to do this seemlessly, and we will |
| 8076 | * be able to change that later without touching the configuration. |
| 8077 | */ |
| 8078 | static int |
| 8079 | pattern_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8080 | const char *arg, int arg_len, union pattern_data *data) |
| 8081 | { |
| 8082 | struct http_txn *txn = l7; |
| 8083 | |
| 8084 | data->ip.s_addr = htonl(get_ip_from_hdr2(&txn->req, arg, arg_len, &txn->hdr_idx, -1)); |
| 8085 | return data->ip.s_addr != 0; |
| 8086 | } |
| 8087 | |
| 8088 | /************************************************************************/ |
| 8089 | /* All supported keywords must be declared here. */ |
| 8090 | /************************************************************************/ |
| 8091 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 8092 | static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{ |
| 8093 | { "hdr", pattern_fetch_hdr_ip, PATTERN_TYPE_IP, PATTERN_FETCH_REQ }, |
| 8094 | { NULL, NULL, 0, 0 }, |
| 8095 | }}; |
| 8096 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8097 | |
| 8098 | __attribute__((constructor)) |
| 8099 | static void __http_protocol_init(void) |
| 8100 | { |
| 8101 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8102 | pattern_register_fetches(&pattern_fetch_keywords); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8103 | } |
| 8104 | |
| 8105 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8106 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8107 | * Local variables: |
| 8108 | * c-indent-level: 8 |
| 8109 | * c-basic-offset: 8 |
| 8110 | * End: |
| 8111 | */ |