Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4 | * Copyright 2000-2010 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <syslog.h> |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 20 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 26 | #include <common/appsession.h> |
| 27 | #include <common/compat.h> |
| 28 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 29 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/memory.h> |
| 31 | #include <common/mini-clist.h> |
| 32 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 33 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 34 | #include <common/time.h> |
| 35 | #include <common/uri_auth.h> |
| 36 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | |
| 38 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 41 | #include <proto/acl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | #include <proto/backend.h> |
| 43 | #include <proto/buffers.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 44 | #include <proto/checks.h> |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 45 | #include <proto/client.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 46 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | #include <proto/fd.h> |
| 48 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 49 | #include <proto/hdr_idx.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 50 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 52 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/queue.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 54 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 55 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 56 | #include <proto/stream_interface.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 57 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/task.h> |
| 59 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 60 | const char HTTP_100[] = |
| 61 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 62 | |
| 63 | const struct chunk http_100_chunk = { |
| 64 | .str = (char *)&HTTP_100, |
| 65 | .len = sizeof(HTTP_100)-1 |
| 66 | }; |
| 67 | |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 68 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 69 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 70 | "HTTP/1.0 200 OK\r\n" |
| 71 | "Cache-Control: no-cache\r\n" |
| 72 | "Connection: close\r\n" |
| 73 | "Content-Type: text/html\r\n" |
| 74 | "\r\n" |
| 75 | "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n"; |
| 76 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 77 | const struct chunk http_200_chunk = { |
| 78 | .str = (char *)&HTTP_200, |
| 79 | .len = sizeof(HTTP_200)-1 |
| 80 | }; |
| 81 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 82 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 83 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 84 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 85 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 86 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 87 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 88 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 89 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 90 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 91 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 92 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 93 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 94 | |
| 95 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 96 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 97 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 98 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 99 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 100 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 101 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 102 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 103 | const char *HTTP_401_fmt = |
| 104 | "HTTP/1.0 401 Unauthorized\r\n" |
| 105 | "Cache-Control: no-cache\r\n" |
| 106 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 107 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 108 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 109 | "\r\n" |
| 110 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 111 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 112 | |
| 113 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 114 | [HTTP_ERR_400] = 400, |
| 115 | [HTTP_ERR_403] = 403, |
| 116 | [HTTP_ERR_408] = 408, |
| 117 | [HTTP_ERR_500] = 500, |
| 118 | [HTTP_ERR_502] = 502, |
| 119 | [HTTP_ERR_503] = 503, |
| 120 | [HTTP_ERR_504] = 504, |
| 121 | }; |
| 122 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 123 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 124 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 125 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 126 | "Cache-Control: no-cache\r\n" |
| 127 | "Connection: close\r\n" |
| 128 | "Content-Type: text/html\r\n" |
| 129 | "\r\n" |
| 130 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 131 | |
| 132 | [HTTP_ERR_403] = |
| 133 | "HTTP/1.0 403 Forbidden\r\n" |
| 134 | "Cache-Control: no-cache\r\n" |
| 135 | "Connection: close\r\n" |
| 136 | "Content-Type: text/html\r\n" |
| 137 | "\r\n" |
| 138 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 139 | |
| 140 | [HTTP_ERR_408] = |
| 141 | "HTTP/1.0 408 Request Time-out\r\n" |
| 142 | "Cache-Control: no-cache\r\n" |
| 143 | "Connection: close\r\n" |
| 144 | "Content-Type: text/html\r\n" |
| 145 | "\r\n" |
| 146 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 147 | |
| 148 | [HTTP_ERR_500] = |
| 149 | "HTTP/1.0 500 Server Error\r\n" |
| 150 | "Cache-Control: no-cache\r\n" |
| 151 | "Connection: close\r\n" |
| 152 | "Content-Type: text/html\r\n" |
| 153 | "\r\n" |
| 154 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 155 | |
| 156 | [HTTP_ERR_502] = |
| 157 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 158 | "Cache-Control: no-cache\r\n" |
| 159 | "Connection: close\r\n" |
| 160 | "Content-Type: text/html\r\n" |
| 161 | "\r\n" |
| 162 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 163 | |
| 164 | [HTTP_ERR_503] = |
| 165 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 166 | "Cache-Control: no-cache\r\n" |
| 167 | "Connection: close\r\n" |
| 168 | "Content-Type: text/html\r\n" |
| 169 | "\r\n" |
| 170 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 171 | |
| 172 | [HTTP_ERR_504] = |
| 173 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 174 | "Cache-Control: no-cache\r\n" |
| 175 | "Connection: close\r\n" |
| 176 | "Content-Type: text/html\r\n" |
| 177 | "\r\n" |
| 178 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 179 | |
| 180 | }; |
| 181 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 182 | /* We must put the messages here since GCC cannot initialize consts depending |
| 183 | * on strlen(). |
| 184 | */ |
| 185 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 186 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 187 | #define FD_SETS_ARE_BITFIELDS |
| 188 | #ifdef FD_SETS_ARE_BITFIELDS |
| 189 | /* |
| 190 | * This map is used with all the FD_* macros to check whether a particular bit |
| 191 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 192 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 193 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 194 | * exclusively to the macros. |
| 195 | */ |
| 196 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 197 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 198 | |
| 199 | #else |
| 200 | #error "Check if your OS uses bitfields for fd_sets" |
| 201 | #endif |
| 202 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 203 | void init_proto_http() |
| 204 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 205 | int i; |
| 206 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 207 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 208 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 209 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 210 | if (!http_err_msgs[msg]) { |
| 211 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 212 | abort(); |
| 213 | } |
| 214 | |
| 215 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 216 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 217 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 218 | |
| 219 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 220 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 221 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 222 | * printable characters above. |
| 223 | */ |
| 224 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 225 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 226 | for (i = 0; i < 32; i++) { |
| 227 | FD_SET(i, hdr_encode_map); |
| 228 | FD_SET(i, url_encode_map); |
| 229 | } |
| 230 | for (i = 127; i < 256; i++) { |
| 231 | FD_SET(i, hdr_encode_map); |
| 232 | FD_SET(i, url_encode_map); |
| 233 | } |
| 234 | |
| 235 | tmp = "\"#{|}"; |
| 236 | while (*tmp) { |
| 237 | FD_SET(*tmp, hdr_encode_map); |
| 238 | tmp++; |
| 239 | } |
| 240 | |
| 241 | tmp = "\"#"; |
| 242 | while (*tmp) { |
| 243 | FD_SET(*tmp, url_encode_map); |
| 244 | tmp++; |
| 245 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 246 | |
| 247 | /* memory allocations */ |
| 248 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 249 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 250 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 251 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 252 | /* |
| 253 | * We have 26 list of methods (1 per first letter), each of which can have |
| 254 | * up to 3 entries (2 valid, 1 null). |
| 255 | */ |
| 256 | struct http_method_desc { |
| 257 | http_meth_t meth; |
| 258 | int len; |
| 259 | const char text[8]; |
| 260 | }; |
| 261 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 262 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 263 | ['C' - 'A'] = { |
| 264 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 265 | }, |
| 266 | ['D' - 'A'] = { |
| 267 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 268 | }, |
| 269 | ['G' - 'A'] = { |
| 270 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 271 | }, |
| 272 | ['H' - 'A'] = { |
| 273 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 274 | }, |
| 275 | ['P' - 'A'] = { |
| 276 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 277 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 278 | }, |
| 279 | ['T' - 'A'] = { |
| 280 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 281 | }, |
| 282 | /* rest is empty like this : |
| 283 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 284 | */ |
| 285 | }; |
| 286 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 287 | /* It is about twice as fast on recent architectures to lookup a byte in a |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 288 | * table than to perform a boolean AND or OR between two tests. Refer to |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 289 | * RFC2616 for those chars. |
| 290 | */ |
| 291 | |
| 292 | const char http_is_spht[256] = { |
| 293 | [' '] = 1, ['\t'] = 1, |
| 294 | }; |
| 295 | |
| 296 | const char http_is_crlf[256] = { |
| 297 | ['\r'] = 1, ['\n'] = 1, |
| 298 | }; |
| 299 | |
| 300 | const char http_is_lws[256] = { |
| 301 | [' '] = 1, ['\t'] = 1, |
| 302 | ['\r'] = 1, ['\n'] = 1, |
| 303 | }; |
| 304 | |
| 305 | const char http_is_sep[256] = { |
| 306 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 307 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 308 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 309 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 310 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 311 | }; |
| 312 | |
| 313 | const char http_is_ctl[256] = { |
| 314 | [0 ... 31] = 1, |
| 315 | [127] = 1, |
| 316 | }; |
| 317 | |
| 318 | /* |
| 319 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 320 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 321 | * them correctly. Instead, define every non-CTL char's status. |
| 322 | */ |
| 323 | const char http_is_token[256] = { |
| 324 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 325 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 326 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 327 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 328 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 329 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 330 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 331 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 332 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 333 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 334 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 335 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 336 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 337 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 338 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 339 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 340 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 341 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 342 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 343 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 344 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 345 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 346 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 347 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 348 | }; |
| 349 | |
| 350 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 351 | /* |
| 352 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 353 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 354 | */ |
| 355 | const char http_is_ver_token[256] = { |
| 356 | ['.'] = 1, ['/'] = 1, |
| 357 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 358 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 359 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 360 | }; |
| 361 | |
| 362 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 363 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 364 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 365 | */ |
| 366 | #if defined(DEBUG_FSM) |
| 367 | static void http_silent_debug(int line, struct session *s) |
| 368 | { |
| 369 | int size = 0; |
| 370 | size += snprintf(trash + size, sizeof(trash) - size, |
| 371 | "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n", |
| 372 | line, |
| 373 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| 374 | s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags); |
| 375 | write(-1, trash, size); |
| 376 | size = 0; |
| 377 | size += snprintf(trash + size, sizeof(trash) - size, |
| 378 | " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n", |
| 379 | line, |
| 380 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| 381 | s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward); |
| 382 | |
| 383 | write(-1, trash, size); |
| 384 | } |
| 385 | #else |
| 386 | #define http_silent_debug(l,s) do { } while (0) |
| 387 | #endif |
| 388 | |
| 389 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 390 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 391 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 392 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 393 | * of headers is automatically adjusted. The number of bytes added is returned |
| 394 | * on success, otherwise <0 is returned indicating an error. |
| 395 | */ |
| 396 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 397 | struct hdr_idx *hdr_idx, const char *text) |
| 398 | { |
| 399 | int bytes, len; |
| 400 | |
| 401 | len = strlen(text); |
| 402 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 403 | if (!bytes) |
| 404 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 405 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 406 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 411 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 412 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 413 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 414 | * of headers is automatically adjusted. The number of bytes added is returned |
| 415 | * on success, otherwise <0 is returned indicating an error. |
| 416 | */ |
| 417 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 418 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 419 | { |
| 420 | int bytes; |
| 421 | |
| 422 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 423 | if (!bytes) |
| 424 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 425 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 426 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 427 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 428 | |
| 429 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 430 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 431 | * If so, returns the position of the first non-space character relative to |
| 432 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 433 | * to return a pointer to the place after the first space. Returns 0 if the |
| 434 | * header name does not match. Checks are case-insensitive. |
| 435 | */ |
| 436 | int http_header_match2(const char *hdr, const char *end, |
| 437 | const char *name, int len) |
| 438 | { |
| 439 | const char *val; |
| 440 | |
| 441 | if (hdr + len >= end) |
| 442 | return 0; |
| 443 | if (hdr[len] != ':') |
| 444 | return 0; |
| 445 | if (strncasecmp(hdr, name, len) != 0) |
| 446 | return 0; |
| 447 | val = hdr + len + 1; |
| 448 | while (val < end && HTTP_IS_SPHT(*val)) |
| 449 | val++; |
| 450 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 451 | return len + 2; /* we may replace starting from second space */ |
| 452 | return val - hdr; |
| 453 | } |
| 454 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 455 | /* Find the end of the header value contained between <s> and <e>. |
| 456 | * See RFC2616, par 2.2 for more information. Note that it requires |
| 457 | * a valid header to return a valid result. |
| 458 | */ |
| 459 | const char *find_hdr_value_end(const char *s, const char *e) |
| 460 | { |
| 461 | int quoted, qdpair; |
| 462 | |
| 463 | quoted = qdpair = 0; |
| 464 | for (; s < e; s++) { |
| 465 | if (qdpair) qdpair = 0; |
| 466 | else if (quoted && *s == '\\') qdpair = 1; |
| 467 | else if (quoted && *s == '"') quoted = 0; |
| 468 | else if (*s == '"') quoted = 1; |
| 469 | else if (*s == ',') return s; |
| 470 | } |
| 471 | return s; |
| 472 | } |
| 473 | |
| 474 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 475 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 476 | * structure holds everything necessary to use the header and find next |
| 477 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 478 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 479 | * 1 when it finds a value, and 0 when there is no more. |
| 480 | */ |
| 481 | int http_find_header2(const char *name, int len, |
| 482 | const char *sol, struct hdr_idx *idx, |
| 483 | struct hdr_ctx *ctx) |
| 484 | { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 485 | const char *eol, *sov; |
| 486 | int cur_idx; |
| 487 | |
| 488 | if (ctx->idx) { |
| 489 | /* We have previously returned a value, let's search |
| 490 | * another one on the same line. |
| 491 | */ |
| 492 | cur_idx = ctx->idx; |
| 493 | sol = ctx->line; |
| 494 | sov = sol + ctx->val + ctx->vlen; |
| 495 | eol = sol + idx->v[cur_idx].len; |
| 496 | |
| 497 | if (sov >= eol) |
| 498 | /* no more values in this header */ |
| 499 | goto next_hdr; |
| 500 | |
| 501 | /* values remaining for this header, skip the comma */ |
| 502 | sov++; |
| 503 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 504 | sov++; |
| 505 | |
| 506 | goto return_hdr; |
| 507 | } |
| 508 | |
| 509 | /* first request for this header */ |
| 510 | sol += hdr_idx_first_pos(idx); |
| 511 | cur_idx = hdr_idx_first_idx(idx); |
| 512 | |
| 513 | while (cur_idx) { |
| 514 | eol = sol + idx->v[cur_idx].len; |
| 515 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 516 | if (len == 0) { |
| 517 | /* No argument was passed, we want any header. |
| 518 | * To achieve this, we simply build a fake request. */ |
| 519 | while (sol + len < eol && sol[len] != ':') |
| 520 | len++; |
| 521 | name = sol; |
| 522 | } |
| 523 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 524 | if ((len < eol - sol) && |
| 525 | (sol[len] == ':') && |
| 526 | (strncasecmp(sol, name, len) == 0)) { |
| 527 | |
| 528 | sov = sol + len + 1; |
| 529 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 530 | sov++; |
| 531 | return_hdr: |
| 532 | ctx->line = sol; |
| 533 | ctx->idx = cur_idx; |
| 534 | ctx->val = sov - sol; |
| 535 | |
| 536 | eol = find_hdr_value_end(sov, eol); |
| 537 | ctx->vlen = eol - sov; |
| 538 | return 1; |
| 539 | } |
| 540 | next_hdr: |
| 541 | sol = eol + idx->v[cur_idx].cr + 1; |
| 542 | cur_idx = idx->v[cur_idx].next; |
| 543 | } |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | int http_find_header(const char *name, |
| 548 | const char *sol, struct hdr_idx *idx, |
| 549 | struct hdr_ctx *ctx) |
| 550 | { |
| 551 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 552 | } |
| 553 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 554 | /* This function handles a server error at the stream interface level. The |
| 555 | * stream interface is assumed to be already in a closed state. An optional |
| 556 | * message is copied into the input buffer, and an HTTP status code stored. |
| 557 | * 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] | 558 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 559 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 560 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 561 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 562 | { |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 563 | buffer_erase(si->ob); |
| 564 | buffer_erase(si->ib); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 565 | buffer_auto_close(si->ib); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 566 | buffer_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 567 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 568 | t->txn.status = status; |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 569 | buffer_write(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 570 | } |
| 571 | if (!(t->flags & SN_ERR_MASK)) |
| 572 | t->flags |= err; |
| 573 | if (!(t->flags & SN_FINST_MASK)) |
| 574 | t->flags |= finst; |
| 575 | } |
| 576 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 577 | /* This function returns the appropriate error location for the given session |
| 578 | * and message. |
| 579 | */ |
| 580 | |
| 581 | struct chunk *error_message(struct session *s, int msgnum) |
| 582 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 583 | if (s->be->errmsg[msgnum].str) |
| 584 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 585 | else if (s->fe->errmsg[msgnum].str) |
| 586 | return &s->fe->errmsg[msgnum]; |
| 587 | else |
| 588 | return &http_err_chunks[msgnum]; |
| 589 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 590 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 591 | /* |
| 592 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 593 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 594 | */ |
| 595 | static http_meth_t find_http_meth(const char *str, const int len) |
| 596 | { |
| 597 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 598 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 599 | |
| 600 | m = ((unsigned)*str - 'A'); |
| 601 | |
| 602 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 603 | for (h = http_methods[m]; h->len > 0; h++) { |
| 604 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 605 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 606 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 607 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 608 | }; |
| 609 | return HTTP_METH_OTHER; |
| 610 | } |
| 611 | return HTTP_METH_NONE; |
| 612 | |
| 613 | } |
| 614 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 615 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 616 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 617 | * It is returned otherwise. |
| 618 | */ |
| 619 | static char * |
| 620 | http_get_path(struct http_txn *txn) |
| 621 | { |
| 622 | char *ptr, *end; |
| 623 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 624 | ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 625 | end = ptr + txn->req.sl.rq.u_l; |
| 626 | |
| 627 | if (ptr >= end) |
| 628 | return NULL; |
| 629 | |
| 630 | /* RFC2616, par. 5.1.2 : |
| 631 | * Request-URI = "*" | absuri | abspath | authority |
| 632 | */ |
| 633 | |
| 634 | if (*ptr == '*') |
| 635 | return NULL; |
| 636 | |
| 637 | if (isalpha((unsigned char)*ptr)) { |
| 638 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 639 | ptr++; |
| 640 | while (ptr < end && |
| 641 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 642 | ptr++; |
| 643 | /* skip '://' */ |
| 644 | if (ptr == end || *ptr++ != ':') |
| 645 | return NULL; |
| 646 | if (ptr == end || *ptr++ != '/') |
| 647 | return NULL; |
| 648 | if (ptr == end || *ptr++ != '/') |
| 649 | return NULL; |
| 650 | } |
| 651 | /* skip [user[:passwd]@]host[:[port]] */ |
| 652 | |
| 653 | while (ptr < end && *ptr != '/') |
| 654 | ptr++; |
| 655 | |
| 656 | if (ptr == end) |
| 657 | return NULL; |
| 658 | |
| 659 | /* OK, we got the '/' ! */ |
| 660 | return ptr; |
| 661 | } |
| 662 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 663 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 664 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 665 | * left unchanged and will follow normal proxy processing. |
| 666 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 667 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 668 | { |
| 669 | struct http_txn *txn; |
| 670 | struct chunk rdr; |
| 671 | char *path; |
| 672 | int len; |
| 673 | |
| 674 | /* 1: create the response header */ |
| 675 | rdr.len = strlen(HTTP_302); |
| 676 | rdr.str = trash; |
Willy Tarreau | 59e0b0f | 2010-01-09 21:29:23 +0100 | [diff] [blame] | 677 | rdr.size = sizeof(trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 678 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 679 | |
| 680 | /* 2: add the server's prefix */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 681 | if (rdr.len + s->srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 682 | return; |
| 683 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 684 | /* special prefix "/" means don't change URL */ |
| 685 | if (s->srv->rdr_len != 1 || *s->srv->rdr_pfx != '/') { |
| 686 | memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len); |
| 687 | rdr.len += s->srv->rdr_len; |
| 688 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 689 | |
| 690 | /* 3: add the request URI */ |
| 691 | txn = &s->txn; |
| 692 | path = http_get_path(txn); |
| 693 | if (!path) |
| 694 | return; |
| 695 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 696 | 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] | 697 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 698 | return; |
| 699 | |
| 700 | memcpy(rdr.str + rdr.len, path, len); |
| 701 | rdr.len += len; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 702 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 703 | rdr.len += 23; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 704 | |
| 705 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 706 | si->shutr(si); |
| 707 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 708 | si->err_type = SI_ET_NONE; |
| 709 | si->err_loc = NULL; |
| 710 | si->state = SI_ST_CLO; |
| 711 | |
| 712 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 713 | 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] | 714 | |
| 715 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 716 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 717 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 718 | } |
| 719 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 720 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 721 | * that the server side is closed. Note that err_type is actually a |
| 722 | * bitmask, where almost only aborts may be cumulated with other |
| 723 | * values. We consider that aborted operations are more important |
| 724 | * than timeouts or errors due to the fact that nobody else in the |
| 725 | * logs might explain incomplete retries. All others should avoid |
| 726 | * being cumulated. It should normally not be possible to have multiple |
| 727 | * aborts at once, but just in case, the first one in sequence is reported. |
| 728 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 729 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 730 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 731 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 732 | |
| 733 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 734 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 735 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 736 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 737 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 738 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 739 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 740 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 741 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 742 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 743 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 744 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 745 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 746 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 747 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 748 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 749 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 750 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 751 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 752 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 753 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 754 | } |
| 755 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 756 | extern const char sess_term_cond[8]; |
| 757 | extern const char sess_fin_state[8]; |
| 758 | extern const char *monthname[12]; |
| 759 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 760 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 761 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 762 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 763 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 764 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 765 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 766 | void http_sess_clflog(struct session *s) |
| 767 | { |
| 768 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 769 | struct proxy *fe = s->fe; |
| 770 | struct proxy *be = s->be; |
| 771 | struct proxy *prx_log; |
| 772 | struct http_txn *txn = &s->txn; |
| 773 | int tolog, level, err; |
| 774 | char *uri, *h; |
| 775 | char *svid; |
| 776 | struct tm tm; |
| 777 | static char tmpline[MAX_SYSLOG_LEN]; |
| 778 | int hdr; |
| 779 | size_t w; |
| 780 | int t_request; |
| 781 | |
| 782 | prx_log = fe; |
| 783 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
| 784 | (s->conn_retries != be->conn_retries) || |
| 785 | txn->status >= 500; |
| 786 | |
| 787 | if (s->cli_addr.ss_family == AF_INET) |
| 788 | inet_ntop(AF_INET, |
| 789 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 790 | pn, sizeof(pn)); |
| 791 | else |
| 792 | inet_ntop(AF_INET6, |
| 793 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 794 | pn, sizeof(pn)); |
| 795 | |
| 796 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
| 797 | |
| 798 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 799 | tolog = fe->to_log; |
| 800 | |
| 801 | h = tmpline; |
| 802 | |
| 803 | w = snprintf(h, sizeof(tmpline), |
| 804 | "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]", |
| 805 | pn, |
| 806 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 807 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 808 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 809 | goto trunc; |
| 810 | h += w; |
| 811 | |
| 812 | if (h >= tmpline + sizeof(tmpline) - 4) |
| 813 | goto trunc; |
| 814 | |
| 815 | *(h++) = ' '; |
| 816 | *(h++) = '\"'; |
| 817 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 818 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 819 | '#', url_encode_map, uri); |
| 820 | *(h++) = '\"'; |
| 821 | |
| 822 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out); |
| 823 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 824 | goto trunc; |
| 825 | h += w; |
| 826 | |
| 827 | if (h >= tmpline + sizeof(tmpline) - 9) |
| 828 | goto trunc; |
| 829 | memcpy(h, " \"-\" \"-\"", 8); |
| 830 | h += 8; |
| 831 | |
| 832 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 833 | " %d %03d", |
| 834 | (s->cli_addr.ss_family == AF_INET) ? |
| 835 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 836 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 837 | (int)s->logs.accept_date.tv_usec/1000); |
| 838 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 839 | goto trunc; |
| 840 | h += w; |
| 841 | |
| 842 | w = strlen(fe->id); |
| 843 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 844 | goto trunc; |
| 845 | *(h++) = ' '; |
| 846 | *(h++) = '\"'; |
| 847 | memcpy(h, fe->id, w); |
| 848 | h += w; |
| 849 | *(h++) = '\"'; |
| 850 | |
| 851 | w = strlen(be->id); |
| 852 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 853 | goto trunc; |
| 854 | *(h++) = ' '; |
| 855 | *(h++) = '\"'; |
| 856 | memcpy(h, be->id, w); |
| 857 | h += w; |
| 858 | *(h++) = '\"'; |
| 859 | |
| 860 | svid = (tolog & LW_SVID) ? |
| 861 | (s->data_source != DATA_SRC_STATS) ? |
| 862 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 863 | |
| 864 | w = strlen(svid); |
| 865 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 866 | goto trunc; |
| 867 | *(h++) = ' '; |
| 868 | *(h++) = '\"'; |
| 869 | memcpy(h, svid, w); |
| 870 | h += w; |
| 871 | *(h++) = '\"'; |
| 872 | |
| 873 | t_request = -1; |
| 874 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 875 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 876 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 877 | " %d %ld %ld %ld %ld", |
| 878 | t_request, |
| 879 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
| 880 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 881 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 882 | s->logs.t_close); |
| 883 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 884 | goto trunc; |
| 885 | h += w; |
| 886 | |
| 887 | if (h >= tmpline + sizeof(tmpline) - 8) |
| 888 | goto trunc; |
| 889 | *(h++) = ' '; |
| 890 | *(h++) = '\"'; |
| 891 | *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]; |
| 892 | *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]; |
| 893 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 894 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-'; |
| 895 | *(h++) = '\"'; |
| 896 | |
| 897 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 898 | " %d %d %d %d %d %ld %ld", |
| 899 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
| 900 | (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries, |
| 901 | s->logs.srv_queue_size, s->logs.prx_queue_size); |
| 902 | |
| 903 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 904 | goto trunc; |
| 905 | h += w; |
| 906 | |
| 907 | if (txn->cli_cookie) { |
| 908 | w = strlen(txn->cli_cookie); |
| 909 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 910 | goto trunc; |
| 911 | *(h++) = ' '; |
| 912 | *(h++) = '\"'; |
| 913 | memcpy(h, txn->cli_cookie, w); |
| 914 | h += w; |
| 915 | *(h++) = '\"'; |
| 916 | } else { |
| 917 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 918 | goto trunc; |
| 919 | memcpy(h, " \"-\"", 4); |
| 920 | h += 4; |
| 921 | } |
| 922 | |
| 923 | if (txn->srv_cookie) { |
| 924 | w = strlen(txn->srv_cookie); |
| 925 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 926 | goto trunc; |
| 927 | *(h++) = ' '; |
| 928 | *(h++) = '\"'; |
| 929 | memcpy(h, txn->srv_cookie, w); |
| 930 | h += w; |
| 931 | *(h++) = '\"'; |
| 932 | } else { |
| 933 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 934 | goto trunc; |
| 935 | memcpy(h, " \"-\"", 4); |
| 936 | h += 4; |
| 937 | } |
| 938 | |
| 939 | if ((fe->to_log & LW_REQHDR) && txn->req.cap) { |
| 940 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 941 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 942 | goto trunc; |
| 943 | *(h++) = ' '; |
| 944 | *(h++) = '\"'; |
| 945 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 946 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 947 | *(h++) = '\"'; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) { |
| 952 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 953 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 954 | goto trunc; |
| 955 | *(h++) = ' '; |
| 956 | *(h++) = '\"'; |
| 957 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 958 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 959 | *(h++) = '\"'; |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | trunc: |
| 964 | *h = '\0'; |
| 965 | |
| 966 | level = LOG_INFO; |
| 967 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 968 | level = LOG_ERR; |
| 969 | |
| 970 | send_log(prx_log, level, "%s\n", tmpline); |
| 971 | |
| 972 | s->logs.logwait = 0; |
| 973 | } |
| 974 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 975 | /* |
| 976 | * send a log for the session when we have enough info about it. |
| 977 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 978 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 979 | void http_sess_log(struct session *s) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 980 | { |
| 981 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 982 | struct proxy *fe = s->fe; |
| 983 | struct proxy *be = s->be; |
| 984 | struct proxy *prx_log; |
| 985 | struct http_txn *txn = &s->txn; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 986 | int tolog, level, err; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 987 | char *uri, *h; |
| 988 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 989 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 990 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 991 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 992 | int hdr; |
| 993 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 994 | /* if we don't want to log normal traffic, return now */ |
| 995 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
| 996 | (s->conn_retries != be->conn_retries) || |
| 997 | txn->status >= 500; |
| 998 | if (!err && (fe->options2 & PR_O2_NOLOGNORM)) |
| 999 | return; |
| 1000 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1001 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 1002 | return; |
| 1003 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1004 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1005 | if (prx_log->options2 & PR_O2_CLFLOG) |
| 1006 | return http_sess_clflog(s); |
| 1007 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1008 | if (s->cli_addr.ss_family == AF_INET) |
| 1009 | inet_ntop(AF_INET, |
| 1010 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 1011 | pn, sizeof(pn)); |
| 1012 | else |
| 1013 | inet_ntop(AF_INET6, |
| 1014 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 1015 | pn, sizeof(pn)); |
| 1016 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1017 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1018 | |
| 1019 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 1020 | tolog = fe->to_log; |
| 1021 | |
| 1022 | h = tmpline; |
| 1023 | if (fe->to_log & LW_REQHDR && |
| 1024 | txn->req.cap && |
| 1025 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 1026 | *(h++) = ' '; |
| 1027 | *(h++) = '{'; |
| 1028 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1029 | if (hdr) |
| 1030 | *(h++) = '|'; |
| 1031 | if (txn->req.cap[hdr] != NULL) |
| 1032 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1033 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1034 | } |
| 1035 | *(h++) = '}'; |
| 1036 | } |
| 1037 | |
| 1038 | if (fe->to_log & LW_RSPHDR && |
| 1039 | txn->rsp.cap && |
| 1040 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1041 | *(h++) = ' '; |
| 1042 | *(h++) = '{'; |
| 1043 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1044 | if (hdr) |
| 1045 | *(h++) = '|'; |
| 1046 | if (txn->rsp.cap[hdr] != NULL) |
| 1047 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1048 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1049 | } |
| 1050 | *(h++) = '}'; |
| 1051 | } |
| 1052 | |
| 1053 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1054 | *(h++) = ' '; |
| 1055 | *(h++) = '"'; |
| 1056 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1057 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1058 | '#', url_encode_map, uri); |
| 1059 | *(h++) = '"'; |
| 1060 | } |
| 1061 | *h = '\0'; |
| 1062 | |
| 1063 | svid = (tolog & LW_SVID) ? |
| 1064 | (s->data_source != DATA_SRC_STATS) ? |
| 1065 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 1066 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1067 | t_request = -1; |
| 1068 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1069 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1070 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1071 | level = LOG_INFO; |
| 1072 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1073 | level = LOG_ERR; |
| 1074 | |
| 1075 | send_log(prx_log, level, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1076 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1077 | " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld" |
| 1078 | " %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] | 1079 | pn, |
| 1080 | (s->cli_addr.ss_family == AF_INET) ? |
| 1081 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 1082 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1083 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1084 | 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] | 1085 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1086 | t_request, |
| 1087 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1088 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1089 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1090 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1091 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1092 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1093 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1094 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1095 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1096 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1097 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1098 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 1099 | 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] | 1100 | (s->flags & SN_REDISP)?"+":"", |
| 1101 | (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1102 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1103 | |
| 1104 | s->logs.logwait = 0; |
| 1105 | } |
| 1106 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1107 | |
| 1108 | /* |
| 1109 | * Capture headers from message starting at <som> according to header list |
| 1110 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1111 | */ |
| 1112 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1113 | char **cap, struct cap_hdr *cap_hdr) |
| 1114 | { |
| 1115 | char *eol, *sol, *col, *sov; |
| 1116 | int cur_idx; |
| 1117 | struct cap_hdr *h; |
| 1118 | int len; |
| 1119 | |
| 1120 | sol = som + hdr_idx_first_pos(idx); |
| 1121 | cur_idx = hdr_idx_first_idx(idx); |
| 1122 | |
| 1123 | while (cur_idx) { |
| 1124 | eol = sol + idx->v[cur_idx].len; |
| 1125 | |
| 1126 | col = sol; |
| 1127 | while (col < eol && *col != ':') |
| 1128 | col++; |
| 1129 | |
| 1130 | sov = col + 1; |
| 1131 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1132 | sov++; |
| 1133 | |
| 1134 | for (h = cap_hdr; h; h = h->next) { |
| 1135 | if ((h->namelen == col - sol) && |
| 1136 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1137 | if (cap[h->index] == NULL) |
| 1138 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1139 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1140 | |
| 1141 | if (cap[h->index] == NULL) { |
| 1142 | Alert("HTTP capture : out of memory.\n"); |
| 1143 | continue; |
| 1144 | } |
| 1145 | |
| 1146 | len = eol - sov; |
| 1147 | if (len > h->len) |
| 1148 | len = h->len; |
| 1149 | |
| 1150 | memcpy(cap[h->index], sov, len); |
| 1151 | cap[h->index][len]=0; |
| 1152 | } |
| 1153 | } |
| 1154 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1155 | cur_idx = idx->v[cur_idx].next; |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1160 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1161 | */ |
| 1162 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1163 | |
| 1164 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1165 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1166 | */ |
| 1167 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1168 | ptr++; \ |
| 1169 | if (likely(ptr < end)) \ |
| 1170 | goto good; \ |
| 1171 | else { \ |
| 1172 | state = (st); \ |
| 1173 | goto http_msg_ood; \ |
| 1174 | } \ |
| 1175 | } while (0) |
| 1176 | |
| 1177 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1178 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1179 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1180 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1181 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1182 | * will give undefined results. |
| 1183 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1184 | * and that msg->sol points to the beginning of the response. |
| 1185 | * If a complete line is found (which implies that at least one CR or LF is |
| 1186 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1187 | * returned indicating an incomplete line (which does not mean that parts have |
| 1188 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1189 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1190 | * upon next call. |
| 1191 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1192 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1193 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1194 | * 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] | 1195 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1196 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1197 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1198 | unsigned int state, const char *ptr, const char *end, |
| 1199 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1200 | { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1201 | switch (state) { |
| 1202 | http_msg_rpver: |
| 1203 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1204 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1205 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1206 | |
| 1207 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1208 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1209 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1210 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1211 | state = HTTP_MSG_ERROR; |
| 1212 | break; |
| 1213 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1214 | http_msg_rpver_sp: |
| 1215 | case HTTP_MSG_RPVER_SP: |
| 1216 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1217 | msg->sl.st.c = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1218 | goto http_msg_rpcode; |
| 1219 | } |
| 1220 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1221 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1222 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1223 | state = HTTP_MSG_ERROR; |
| 1224 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1225 | |
| 1226 | http_msg_rpcode: |
| 1227 | case HTTP_MSG_RPCODE: |
| 1228 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1229 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1230 | |
| 1231 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1232 | 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] | 1233 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1234 | } |
| 1235 | |
| 1236 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1237 | 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] | 1238 | http_msg_rsp_reason: |
| 1239 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1240 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1241 | msg->sl.st.r_l = 0; |
| 1242 | goto http_msg_rpline_eol; |
| 1243 | |
| 1244 | http_msg_rpcode_sp: |
| 1245 | case HTTP_MSG_RPCODE_SP: |
| 1246 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1247 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1248 | goto http_msg_rpreason; |
| 1249 | } |
| 1250 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1251 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1252 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1253 | goto http_msg_rsp_reason; |
| 1254 | |
| 1255 | http_msg_rpreason: |
| 1256 | case HTTP_MSG_RPREASON: |
| 1257 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1258 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1259 | 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] | 1260 | http_msg_rpline_eol: |
| 1261 | /* We have seen the end of line. Note that we do not |
| 1262 | * necessarily have the \n yet, but at least we know that we |
| 1263 | * have EITHER \r OR \n, otherwise the response would not be |
| 1264 | * complete. We can then record the response length and return |
| 1265 | * to the caller which will be able to register it. |
| 1266 | */ |
| 1267 | msg->sl.st.l = ptr - msg->sol; |
| 1268 | return ptr; |
| 1269 | |
| 1270 | #ifdef DEBUG_FULL |
| 1271 | default: |
| 1272 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1273 | exit(1); |
| 1274 | #endif |
| 1275 | } |
| 1276 | |
| 1277 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1278 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1279 | if (ret_state) |
| 1280 | *ret_state = state; |
| 1281 | if (ret_ptr) |
| 1282 | *ret_ptr = (char *)ptr; |
| 1283 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | |
| 1287 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1288 | * This function parses a request line between <ptr> and <end>, starting with |
| 1289 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1290 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1291 | * will give undefined results. |
| 1292 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1293 | * and that msg->sol points to the beginning of the request. |
| 1294 | * If a complete line is found (which implies that at least one CR or LF is |
| 1295 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1296 | * returned indicating an incomplete line (which does not mean that parts have |
| 1297 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1298 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1299 | * upon next call. |
| 1300 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1301 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1302 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1303 | * 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] | 1304 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1305 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1306 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1307 | unsigned int state, const char *ptr, const char *end, |
| 1308 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1309 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1310 | switch (state) { |
| 1311 | http_msg_rqmeth: |
| 1312 | case HTTP_MSG_RQMETH: |
| 1313 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1314 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1315 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1316 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1317 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1318 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1319 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1320 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1321 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1322 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1323 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1324 | http_msg_req09_uri: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1325 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1326 | http_msg_req09_uri_e: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1327 | 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] | 1328 | http_msg_req09_ver: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1329 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1330 | msg->sl.rq.v_l = 0; |
| 1331 | goto http_msg_rqline_eol; |
| 1332 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1333 | state = HTTP_MSG_ERROR; |
| 1334 | break; |
| 1335 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1336 | http_msg_rqmeth_sp: |
| 1337 | case HTTP_MSG_RQMETH_SP: |
| 1338 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1339 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1340 | goto http_msg_rquri; |
| 1341 | } |
| 1342 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1343 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1344 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1345 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1346 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1347 | http_msg_rquri: |
| 1348 | case HTTP_MSG_RQURI: |
| 1349 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1350 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1351 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1352 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1353 | 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] | 1354 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1355 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1356 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1357 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1358 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1359 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1360 | http_msg_rquri_sp: |
| 1361 | case HTTP_MSG_RQURI_SP: |
| 1362 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1363 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1364 | goto http_msg_rqver; |
| 1365 | } |
| 1366 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1367 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1368 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1369 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1370 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1371 | http_msg_rqver: |
| 1372 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1373 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1374 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1375 | |
| 1376 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1377 | 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] | 1378 | http_msg_rqline_eol: |
| 1379 | /* We have seen the end of line. Note that we do not |
| 1380 | * necessarily have the \n yet, but at least we know that we |
| 1381 | * have EITHER \r OR \n, otherwise the request would not be |
| 1382 | * complete. We can then record the request length and return |
| 1383 | * to the caller which will be able to register it. |
| 1384 | */ |
| 1385 | msg->sl.rq.l = ptr - msg->sol; |
| 1386 | return ptr; |
| 1387 | } |
| 1388 | |
| 1389 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1390 | state = HTTP_MSG_ERROR; |
| 1391 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1392 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1393 | #ifdef DEBUG_FULL |
| 1394 | default: |
| 1395 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1396 | exit(1); |
| 1397 | #endif |
| 1398 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1399 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1400 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1401 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1402 | if (ret_state) |
| 1403 | *ret_state = state; |
| 1404 | if (ret_ptr) |
| 1405 | *ret_ptr = (char *)ptr; |
| 1406 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1407 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1408 | |
| 1409 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1410 | /* |
| 1411 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1412 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1413 | * when data are missing and recalled at the exact same location with no |
| 1414 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1415 | * 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] | 1416 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1417 | * the first state, so that none of the msg pointers has to be initialized |
| 1418 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1419 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1420 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1421 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1422 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1423 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1424 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1425 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1426 | ptr = buf->lr; |
| 1427 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1428 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1429 | if (unlikely(ptr >= end)) |
| 1430 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1431 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1432 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1433 | /* |
| 1434 | * First, states that are specific to the response only. |
| 1435 | * We check them first so that request and headers are |
| 1436 | * closer to each other (accessed more often). |
| 1437 | */ |
| 1438 | http_msg_rpbefore: |
| 1439 | case HTTP_MSG_RPBEFORE: |
| 1440 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1441 | /* we have a start of message, but we have to check |
| 1442 | * first if we need to remove some CRLF. We can only |
| 1443 | * do this when send_max=0. |
| 1444 | */ |
| 1445 | char *beg = buf->w + buf->send_max; |
| 1446 | if (beg >= buf->data + buf->size) |
| 1447 | beg -= buf->size; |
| 1448 | if (unlikely(ptr != beg)) { |
| 1449 | if (buf->send_max) |
| 1450 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1451 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1452 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1453 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1454 | msg->som = ptr - buf->data; |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1455 | msg->sol = ptr; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1456 | hdr_idx_init(idx); |
| 1457 | state = HTTP_MSG_RPVER; |
| 1458 | goto http_msg_rpver; |
| 1459 | } |
| 1460 | |
| 1461 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1462 | goto http_msg_invalid; |
| 1463 | |
| 1464 | if (unlikely(*ptr == '\n')) |
| 1465 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1466 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1467 | /* stop here */ |
| 1468 | |
| 1469 | http_msg_rpbefore_cr: |
| 1470 | case HTTP_MSG_RPBEFORE_CR: |
| 1471 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1472 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1473 | /* stop here */ |
| 1474 | |
| 1475 | http_msg_rpver: |
| 1476 | case HTTP_MSG_RPVER: |
| 1477 | case HTTP_MSG_RPVER_SP: |
| 1478 | case HTTP_MSG_RPCODE: |
| 1479 | case HTTP_MSG_RPCODE_SP: |
| 1480 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1481 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1482 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1483 | if (unlikely(!ptr)) |
| 1484 | return; |
| 1485 | |
| 1486 | /* we have a full response and we know that we have either a CR |
| 1487 | * or an LF at <ptr>. |
| 1488 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1489 | //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] | 1490 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1491 | |
| 1492 | msg->sol = ptr; |
| 1493 | if (likely(*ptr == '\r')) |
| 1494 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1495 | goto http_msg_rpline_end; |
| 1496 | |
| 1497 | http_msg_rpline_end: |
| 1498 | case HTTP_MSG_RPLINE_END: |
| 1499 | /* msg->sol must point to the first of CR or LF. */ |
| 1500 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1501 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1502 | /* stop here */ |
| 1503 | |
| 1504 | /* |
| 1505 | * Second, states that are specific to the request only |
| 1506 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1507 | http_msg_rqbefore: |
| 1508 | case HTTP_MSG_RQBEFORE: |
| 1509 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1510 | /* we have a start of message, but we have to check |
| 1511 | * first if we need to remove some CRLF. We can only |
| 1512 | * do this when send_max=0. |
| 1513 | */ |
| 1514 | char *beg = buf->w + buf->send_max; |
| 1515 | if (beg >= buf->data + buf->size) |
| 1516 | beg -= buf->size; |
| 1517 | if (likely(ptr != beg)) { |
| 1518 | if (buf->send_max) |
| 1519 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1520 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1521 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1522 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1523 | msg->som = ptr - buf->data; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1524 | msg->sol = ptr; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1525 | /* we will need this when keep-alive will be supported |
| 1526 | hdr_idx_init(idx); |
| 1527 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1528 | state = HTTP_MSG_RQMETH; |
| 1529 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1530 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1531 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1532 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1533 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1534 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1535 | if (unlikely(*ptr == '\n')) |
| 1536 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1537 | 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] | 1538 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1539 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1540 | http_msg_rqbefore_cr: |
| 1541 | case HTTP_MSG_RQBEFORE_CR: |
| 1542 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1543 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1544 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1545 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1546 | http_msg_rqmeth: |
| 1547 | case HTTP_MSG_RQMETH: |
| 1548 | case HTTP_MSG_RQMETH_SP: |
| 1549 | case HTTP_MSG_RQURI: |
| 1550 | case HTTP_MSG_RQURI_SP: |
| 1551 | case HTTP_MSG_RQVER: |
| 1552 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1553 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1554 | if (unlikely(!ptr)) |
| 1555 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1556 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1557 | /* we have a full request and we know that we have either a CR |
| 1558 | * or an LF at <ptr>. |
| 1559 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1560 | //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] | 1561 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1562 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1563 | msg->sol = ptr; |
| 1564 | if (likely(*ptr == '\r')) |
| 1565 | 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] | 1566 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1567 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1568 | http_msg_rqline_end: |
| 1569 | case HTTP_MSG_RQLINE_END: |
| 1570 | /* check for HTTP/0.9 request : no version information available. |
| 1571 | * msg->sol must point to the first of CR or LF. |
| 1572 | */ |
| 1573 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1574 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1575 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1576 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1577 | 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] | 1578 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1579 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1580 | /* |
| 1581 | * Common states below |
| 1582 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1583 | http_msg_hdr_first: |
| 1584 | case HTTP_MSG_HDR_FIRST: |
| 1585 | msg->sol = ptr; |
| 1586 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1587 | goto http_msg_hdr_name; |
| 1588 | } |
| 1589 | |
| 1590 | if (likely(*ptr == '\r')) |
| 1591 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1592 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1593 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1594 | http_msg_hdr_name: |
| 1595 | case HTTP_MSG_HDR_NAME: |
| 1596 | /* assumes msg->sol points to the first char */ |
| 1597 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1598 | 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] | 1599 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1600 | if (likely(*ptr == ':')) { |
| 1601 | msg->col = ptr - buf->data; |
| 1602 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1603 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1604 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1605 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1606 | goto http_msg_invalid; |
| 1607 | |
| 1608 | if (msg->err_pos == -1) /* capture error pointer */ |
| 1609 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 1610 | |
| 1611 | /* and we still accept this non-token character */ |
| 1612 | 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] | 1613 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1614 | http_msg_hdr_l1_sp: |
| 1615 | case HTTP_MSG_HDR_L1_SP: |
| 1616 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1617 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1618 | 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] | 1619 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1620 | /* header value can be basically anything except CR/LF */ |
| 1621 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1622 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1623 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1624 | goto http_msg_hdr_val; |
| 1625 | } |
| 1626 | |
| 1627 | if (likely(*ptr == '\r')) |
| 1628 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1629 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1630 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1631 | http_msg_hdr_l1_lf: |
| 1632 | case HTTP_MSG_HDR_L1_LF: |
| 1633 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1634 | 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] | 1635 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1636 | http_msg_hdr_l1_lws: |
| 1637 | case HTTP_MSG_HDR_L1_LWS: |
| 1638 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1639 | /* replace HT,CR,LF with spaces */ |
| 1640 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1641 | buf->data[msg->sov] = ' '; |
| 1642 | goto http_msg_hdr_l1_sp; |
| 1643 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1644 | /* we had a header consisting only in spaces ! */ |
| 1645 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1646 | goto http_msg_complete_header; |
| 1647 | |
| 1648 | http_msg_hdr_val: |
| 1649 | case HTTP_MSG_HDR_VAL: |
| 1650 | /* assumes msg->sol points to the first char, msg->col to the |
| 1651 | * colon, and msg->sov points to the first character of the |
| 1652 | * value. |
| 1653 | */ |
| 1654 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1655 | 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] | 1656 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1657 | msg->eol = ptr; |
| 1658 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1659 | * real header end in case it ends with lots of LWS, but is this |
| 1660 | * really needed ? |
| 1661 | */ |
| 1662 | if (likely(*ptr == '\r')) |
| 1663 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1664 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1665 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1666 | http_msg_hdr_l2_lf: |
| 1667 | case HTTP_MSG_HDR_L2_LF: |
| 1668 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1669 | 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] | 1670 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1671 | http_msg_hdr_l2_lws: |
| 1672 | case HTTP_MSG_HDR_L2_LWS: |
| 1673 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1674 | /* LWS: replace HT,CR,LF with spaces */ |
| 1675 | for (; msg->eol < ptr; msg->eol++) |
| 1676 | *msg->eol = ' '; |
| 1677 | goto http_msg_hdr_val; |
| 1678 | } |
| 1679 | http_msg_complete_header: |
| 1680 | /* |
| 1681 | * It was a new header, so the last one is finished. |
| 1682 | * Assumes msg->sol points to the first char, msg->col to the |
| 1683 | * colon, msg->sov points to the first character of the value |
| 1684 | * and msg->eol to the first CR or LF so we know how the line |
| 1685 | * ends. We insert last header into the index. |
| 1686 | */ |
| 1687 | /* |
| 1688 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1689 | write(2, msg->sol, msg->eol-msg->sol); |
| 1690 | fprintf(stderr,"\n"); |
| 1691 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1692 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1693 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1694 | idx, idx->tail) < 0)) |
| 1695 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1696 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1697 | msg->sol = ptr; |
| 1698 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1699 | goto http_msg_hdr_name; |
| 1700 | } |
| 1701 | |
| 1702 | if (likely(*ptr == '\r')) |
| 1703 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1704 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1705 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1706 | http_msg_last_lf: |
| 1707 | case HTTP_MSG_LAST_LF: |
| 1708 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1709 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1710 | ptr++; |
| 1711 | buf->lr = ptr; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1712 | msg->col = msg->sov = buf->lr - buf->data; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1713 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1714 | msg->sol = buf->data + msg->som; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1715 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1716 | return; |
| 1717 | #ifdef DEBUG_FULL |
| 1718 | default: |
| 1719 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1720 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1721 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1722 | } |
| 1723 | http_msg_ood: |
| 1724 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1725 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1726 | buf->lr = ptr; |
| 1727 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1728 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1729 | http_msg_invalid: |
| 1730 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1731 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1732 | buf->lr = ptr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1733 | return; |
| 1734 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1735 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1736 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1737 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1738 | * nothing is done and 1 is returned. |
| 1739 | */ |
| 1740 | static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn) |
| 1741 | { |
| 1742 | int delta; |
| 1743 | char *cur_end; |
| 1744 | |
| 1745 | if (msg->sl.rq.v_l != 0) |
| 1746 | return 1; |
| 1747 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1748 | cur_end = msg->sol + msg->sl.rq.l; |
| 1749 | delta = 0; |
| 1750 | |
| 1751 | if (msg->sl.rq.u_l == 0) { |
| 1752 | /* if no URI was set, add "/" */ |
| 1753 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1754 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1755 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1756 | } |
| 1757 | /* add HTTP version */ |
| 1758 | 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] | 1759 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1760 | cur_end += delta; |
| 1761 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1762 | HTTP_MSG_RQMETH, |
| 1763 | msg->sol, cur_end + 1, |
| 1764 | NULL, NULL); |
| 1765 | if (unlikely(!cur_end)) |
| 1766 | return 0; |
| 1767 | |
| 1768 | /* we have a full HTTP/1.0 request now and we know that |
| 1769 | * we have either a CR or an LF at <ptr>. |
| 1770 | */ |
| 1771 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1772 | return 1; |
| 1773 | } |
| 1774 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1775 | /* Parse the Connection: headaer of an HTTP request, and set the transaction |
| 1776 | * flag TX_REQ_CONN_CLO if a "close" mode is expected. The TX_CON_HDR_PARS flag |
| 1777 | * is also set so that we don't parse a second time. If some dangerous values |
| 1778 | * are encountered, we leave the status to indicate that the request might be |
| 1779 | * interpreted as keep-alive, but we also set the connection flags to indicate |
| 1780 | * that we WANT it to be a close, so that the header will be fixed. This |
| 1781 | * function should only be called when we know we're interested in checking |
| 1782 | * the request (not a CONNECT, and FE or BE mangles the header). |
| 1783 | */ |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1784 | void http_req_parse_connection_header(struct http_txn *txn) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1785 | { |
| 1786 | struct http_msg *msg = &txn->req; |
| 1787 | struct hdr_ctx ctx; |
| 1788 | int conn_cl, conn_ka; |
| 1789 | |
| 1790 | if (txn->flags & TX_CON_HDR_PARS) |
| 1791 | return; |
| 1792 | |
| 1793 | conn_cl = 0; |
| 1794 | conn_ka = 0; |
| 1795 | ctx.idx = 0; |
| 1796 | |
| 1797 | while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) { |
| 1798 | if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0) |
| 1799 | conn_cl = 1; |
| 1800 | else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0) |
| 1801 | conn_ka = 1; |
| 1802 | } |
| 1803 | |
| 1804 | /* Determine if the client wishes keep-alive or close. |
| 1805 | * RFC2616 #8.1.2 and #14.10 state that HTTP/1.1 and above connections |
| 1806 | * are persistent unless "Connection: close" is explicitly specified. |
| 1807 | * RFC2616 #19.6.2 refers to RFC2068 for HTTP/1.0 persistent connections. |
| 1808 | * RFC2068 #19.7.1 states that HTTP/1.0 clients are not persistent unless |
| 1809 | * they explicitly specify "Connection: Keep-Alive", regardless of any |
| 1810 | * optional "Keep-Alive" header. |
| 1811 | * Note that if we find a request with both "Connection: close" and |
| 1812 | * "Connection: Keep-Alive", we indicate we want a close but don't have |
| 1813 | * it, so that it can be enforced later. |
| 1814 | */ |
| 1815 | |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1816 | if (conn_cl && conn_ka) { |
| 1817 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 1818 | } |
| 1819 | else if (txn->flags & TX_REQ_VER_11) { /* HTTP/1.1 */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1820 | if (conn_cl) { |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1821 | txn->flags |= TX_REQ_CONN_CLO; |
| 1822 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 1823 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1824 | } |
| 1825 | } else { /* HTTP/1.0 */ |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1826 | if (!conn_ka) { |
| 1827 | txn->flags |= TX_REQ_CONN_CLO; |
| 1828 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 1829 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 1830 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1831 | } |
| 1832 | txn->flags |= TX_CON_HDR_PARS; |
| 1833 | } |
| 1834 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1835 | /* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the |
| 1836 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
| 1837 | * so that we know we can forward between ->som and ->sov. Note that due to |
| 1838 | * possible wrapping at the end of the buffer, it is possible that msg->sov is |
| 1839 | * lower than msg->som. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1840 | * 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] | 1841 | * 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] | 1842 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1843 | int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1844 | { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1845 | char *ptr = buf->lr; |
| 1846 | char *end = buf->data + buf->size; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1847 | unsigned int chunk = 0; |
| 1848 | |
| 1849 | /* The chunk size is in the following form, though we are only |
| 1850 | * interested in the size and CRLF : |
| 1851 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1852 | */ |
| 1853 | while (1) { |
| 1854 | int c; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1855 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1856 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1857 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1858 | if (c < 0) /* not a hex digit anymore */ |
| 1859 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1860 | if (++ptr >= end) |
| 1861 | ptr = buf->data; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1862 | if (chunk & 0xF000000) /* overflow will occur */ |
| 1863 | return -1; |
| 1864 | chunk = (chunk << 4) + c; |
| 1865 | } |
| 1866 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1867 | /* empty size not allowed */ |
| 1868 | if (ptr == buf->lr) |
| 1869 | return -1; |
| 1870 | |
| 1871 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1872 | if (++ptr >= end) |
| 1873 | ptr = buf->data; |
| 1874 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1875 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1876 | } |
| 1877 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1878 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1879 | * for the end of chunk size. |
| 1880 | */ |
| 1881 | while (1) { |
| 1882 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1883 | /* we now have a CR or an LF at ptr */ |
| 1884 | if (likely(*ptr == '\r')) { |
| 1885 | if (++ptr >= end) |
| 1886 | ptr = buf->data; |
| 1887 | if (ptr == buf->r) |
| 1888 | return 0; |
| 1889 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1890 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1891 | if (*ptr != '\n') |
| 1892 | return -1; |
| 1893 | if (++ptr >= end) |
| 1894 | ptr = buf->data; |
| 1895 | /* done */ |
| 1896 | break; |
| 1897 | } |
| 1898 | else if (*ptr == ';') { |
| 1899 | /* chunk extension, ends at next CRLF */ |
| 1900 | if (++ptr >= end) |
| 1901 | ptr = buf->data; |
| 1902 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1903 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1904 | |
| 1905 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1906 | if (++ptr >= end) |
| 1907 | ptr = buf->data; |
| 1908 | if (ptr == buf->r) |
| 1909 | return 0; |
| 1910 | } |
| 1911 | /* we have a CRLF now, loop above */ |
| 1912 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1913 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1914 | else |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1915 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1916 | } |
| 1917 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1918 | /* OK we found our CRLF and now <ptr> points to the next byte, |
| 1919 | * which may or may not be present. We save that into ->lr and |
| 1920 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1921 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1922 | msg->sov += ptr - buf->lr; |
| 1923 | buf->lr = ptr; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1924 | msg->hdr_content_len = chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1925 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1926 | return 1; |
| 1927 | } |
| 1928 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1929 | /* This function skips trailers in the buffer <buf> associated with HTTP |
| 1930 | * message <msg>. The first visited position is buf->lr. If the end of |
| 1931 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 1932 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 1933 | * If not enough data are available, the function does not change anything |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1934 | * except maybe buf->lr and msg->sov if it could parse some lines, and returns |
| 1935 | * zero. If a parse error is encountered, the function returns < 0 and does not |
| 1936 | * change anything except maybe buf->lr and msg->sov. Note that the message |
| 1937 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 1938 | * which implies that all non-trailers data have already been scheduled for |
| 1939 | * forwarding, and that the difference between msg->som and msg->sov exactly |
| 1940 | * matches the length of trailers already parsed and not forwarded. It is also |
| 1941 | * important to note that this function is designed to be able to parse wrapped |
| 1942 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1943 | */ |
| 1944 | int http_forward_trailers(struct buffer *buf, struct http_msg *msg) |
| 1945 | { |
| 1946 | /* we have buf->lr which points to next line. Look for CRLF. */ |
| 1947 | while (1) { |
| 1948 | char *p1 = NULL, *p2 = NULL; |
| 1949 | char *ptr = buf->lr; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1950 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1951 | |
| 1952 | /* scan current line and stop at LF or CRLF */ |
| 1953 | while (1) { |
| 1954 | if (ptr == buf->r) |
| 1955 | return 0; |
| 1956 | |
| 1957 | if (*ptr == '\n') { |
| 1958 | if (!p1) |
| 1959 | p1 = ptr; |
| 1960 | p2 = ptr; |
| 1961 | break; |
| 1962 | } |
| 1963 | |
| 1964 | if (*ptr == '\r') { |
| 1965 | if (p1) |
| 1966 | return -1; |
| 1967 | p1 = ptr; |
| 1968 | } |
| 1969 | |
| 1970 | ptr++; |
| 1971 | if (ptr >= buf->data + buf->size) |
| 1972 | ptr = buf->data; |
| 1973 | } |
| 1974 | |
| 1975 | /* after LF; point to beginning of next line */ |
| 1976 | p2++; |
| 1977 | if (p2 >= buf->data + buf->size) |
| 1978 | p2 = buf->data; |
| 1979 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1980 | bytes = p2 - buf->lr; |
| 1981 | if (bytes < 0) |
| 1982 | bytes += buf->size; |
| 1983 | |
| 1984 | /* schedule this line for forwarding */ |
| 1985 | msg->sov += bytes; |
| 1986 | if (msg->sov >= buf->size) |
| 1987 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1988 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1989 | if (p1 == buf->lr) { |
| 1990 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 1991 | * Everything was scheduled for forwarding, there's nothing |
| 1992 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 1993 | */ |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1994 | buf->lr = p2; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1995 | msg->msg_state = HTTP_MSG_DONE; |
| 1996 | return 1; |
| 1997 | } |
| 1998 | /* OK, next line then */ |
| 1999 | buf->lr = p2; |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 2004 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
| 2005 | * ->som, buf->lr in order to include this part into the next forwarding phase. |
| 2006 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2007 | * not enough data are available, the function does not change anything and |
| 2008 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2009 | * does not change anything. Note: this function is designed to parse wrapped |
| 2010 | * CRLF at the end of the buffer. |
| 2011 | */ |
| 2012 | int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg) |
| 2013 | { |
| 2014 | char *ptr; |
| 2015 | int bytes; |
| 2016 | |
| 2017 | /* NB: we'll check data availabilty at the end. It's not a |
| 2018 | * problem because whatever we match first will be checked |
| 2019 | * against the correct length. |
| 2020 | */ |
| 2021 | bytes = 1; |
| 2022 | ptr = buf->lr; |
| 2023 | if (*ptr == '\r') { |
| 2024 | bytes++; |
| 2025 | ptr++; |
| 2026 | if (ptr >= buf->data + buf->size) |
| 2027 | ptr = buf->data; |
| 2028 | } |
| 2029 | |
| 2030 | if (buf->l < bytes) |
| 2031 | return 0; |
| 2032 | |
| 2033 | if (*ptr != '\n') |
| 2034 | return -1; |
| 2035 | |
| 2036 | ptr++; |
| 2037 | if (ptr >= buf->data + buf->size) |
| 2038 | ptr = buf->data; |
| 2039 | buf->lr = ptr; |
| 2040 | /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */ |
| 2041 | msg->sov = ptr - buf->data; |
| 2042 | msg->som = msg->sov - bytes; |
| 2043 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2044 | return 1; |
| 2045 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2046 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2047 | void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg) |
| 2048 | { |
| 2049 | char *end = buf->data + buf->size; |
| 2050 | int off = buf->data + buf->size - buf->w; |
| 2051 | |
| 2052 | /* two possible cases : |
| 2053 | * - the buffer is in one contiguous block, we move it in-place |
| 2054 | * - the buffer is in two blocks, we move it via the trash |
| 2055 | */ |
| 2056 | if (buf->l) { |
| 2057 | int block1 = buf->l; |
| 2058 | int block2 = 0; |
| 2059 | if (buf->r <= buf->w) { |
| 2060 | /* non-contiguous block */ |
| 2061 | block1 = buf->data + buf->size - buf->w; |
| 2062 | block2 = buf->r - buf->data; |
| 2063 | } |
| 2064 | if (block2) |
| 2065 | memcpy(trash, buf->data, block2); |
| 2066 | memmove(buf->data, buf->w, block1); |
| 2067 | if (block2) |
| 2068 | memcpy(buf->data + block1, trash, block2); |
| 2069 | } |
| 2070 | |
| 2071 | /* adjust all known pointers */ |
| 2072 | buf->w = buf->data; |
| 2073 | buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size; |
| 2074 | buf->r += off; if (buf->r >= end) buf->r -= buf->size; |
| 2075 | msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size; |
| 2076 | msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size; |
| 2077 | |
| 2078 | /* adjust relative pointers */ |
| 2079 | msg->som = 0; |
| 2080 | msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size; |
| 2081 | msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size; |
| 2082 | msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size; |
| 2083 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2084 | if (msg->err_pos >= 0) { |
| 2085 | msg->err_pos += off; |
| 2086 | if (msg->err_pos >= buf->size) |
| 2087 | msg->err_pos -= buf->size; |
| 2088 | } |
| 2089 | |
| 2090 | buf->flags &= ~BF_FULL; |
| 2091 | if (buf->l >= buffer_max_len(buf)) |
| 2092 | buf->flags |= BF_FULL; |
| 2093 | } |
| 2094 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2095 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2096 | * processing can continue on next analysers, or zero if it either needs more |
| 2097 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2098 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2099 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2100 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2101 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2102 | 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] | 2103 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2104 | /* |
| 2105 | * We will parse the partial (or complete) lines. |
| 2106 | * We will check the request syntax, and also join multi-line |
| 2107 | * headers. An index of all the lines will be elaborated while |
| 2108 | * parsing. |
| 2109 | * |
| 2110 | * For the parsing, we use a 28 states FSM. |
| 2111 | * |
| 2112 | * Here is the information we currently have : |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 2113 | * req->data + msg->som = beginning of request |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2114 | * req->data + msg->eoh = end of processed headers / start of current one |
| 2115 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2116 | * req->lr = first non-visited byte |
| 2117 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2118 | * |
| 2119 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2120 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2121 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2122 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2123 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2124 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2125 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2126 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2127 | struct http_txn *txn = &s->txn; |
| 2128 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2129 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2130 | |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2131 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2132 | now_ms, __FUNCTION__, |
| 2133 | s, |
| 2134 | req, |
| 2135 | req->rex, req->wex, |
| 2136 | req->flags, |
| 2137 | req->l, |
| 2138 | req->analysers); |
| 2139 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2140 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2141 | s->srv_error = http_return_srv_error; |
| 2142 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2143 | /* There's a protected area at the end of the buffer for rewriting |
| 2144 | * purposes. We don't want to start to parse the request if the |
| 2145 | * protected area is affected, because we may have to move processed |
| 2146 | * data later, which is much more complicated. |
| 2147 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2148 | if (req->l && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2149 | if ((txn->flags & TX_NOT_FIRST) && |
| 2150 | unlikely((req->flags & BF_FULL) || |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2151 | req->r < req->lr || |
| 2152 | req->r > req->data + req->size - global.tune.maxrewrite)) { |
| 2153 | if (req->send_max) { |
| 2154 | /* some data has still not left the buffer, wake us once that's done */ |
| 2155 | buffer_dont_connect(req); |
| 2156 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2157 | return 0; |
| 2158 | } |
| 2159 | if (req->l <= req->size - global.tune.maxrewrite) |
| 2160 | http_buffer_heavy_realign(req, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2161 | } |
| 2162 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2163 | /* Note that we have the same problem with the response ; we |
| 2164 | * may want to send a redirect, error or anything which requires |
| 2165 | * some spare space. So we'll ensure that we have at least |
| 2166 | * maxrewrite bytes available in the response buffer before |
| 2167 | * processing that one. This will only affect pipelined |
| 2168 | * keep-alive requests. |
| 2169 | */ |
| 2170 | if ((txn->flags & TX_NOT_FIRST) && |
| 2171 | unlikely((s->rep->flags & BF_FULL) || |
| 2172 | s->rep->r < s->rep->lr || |
| 2173 | s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) { |
| 2174 | if (s->rep->send_max) { |
| 2175 | /* don't let a connection request be initiated */ |
| 2176 | buffer_dont_connect(req); |
| 2177 | return 0; |
| 2178 | } |
| 2179 | } |
| 2180 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2181 | if (likely(req->lr < req->r)) |
| 2182 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2183 | } |
| 2184 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2185 | /* 1: we might have to print this header in debug mode */ |
| 2186 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2187 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2188 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2189 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2190 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2191 | sol = msg->sol; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2192 | eol = sol + msg->sl.rq.l; |
| 2193 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2194 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2195 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2196 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2197 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2198 | while (cur_idx) { |
| 2199 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2200 | debug_hdr("clihdr", s, sol, eol); |
| 2201 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2202 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2203 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2204 | } |
| 2205 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2206 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2207 | /* |
| 2208 | * Now we quickly check if we have found a full valid request. |
| 2209 | * If not so, we check the FD and buffer states before leaving. |
| 2210 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2211 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2212 | * requests are checked first. |
| 2213 | * |
| 2214 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2215 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2216 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2217 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2218 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2219 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2220 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 2221 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2222 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2223 | /* 1: Since we are in header mode, if there's no space |
| 2224 | * left for headers, we won't be able to free more |
| 2225 | * later, so the session will never terminate. We |
| 2226 | * must terminate it now. |
| 2227 | */ |
| 2228 | if (unlikely(req->flags & BF_FULL)) { |
| 2229 | /* FIXME: check if URI is set and return Status |
| 2230 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2231 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2232 | goto return_bad_req; |
| 2233 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2234 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2235 | /* 2: have we encountered a read error ? */ |
| 2236 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2237 | if (txn->flags & TX_NOT_FIRST) |
| 2238 | goto failed_keep_alive; |
| 2239 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2240 | /* we cannot return any message on error */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2241 | if (msg->err_pos >= 0) |
| 2242 | 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] | 2243 | msg->msg_state = HTTP_MSG_ERROR; |
| 2244 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2245 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2246 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2247 | if (s->listener->counters) |
| 2248 | s->listener->counters->failed_req++; |
| 2249 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2250 | if (!(s->flags & SN_ERR_MASK)) |
| 2251 | s->flags |= SN_ERR_CLICL; |
| 2252 | if (!(s->flags & SN_FINST_MASK)) |
| 2253 | s->flags |= SN_FINST_R; |
| 2254 | return 0; |
| 2255 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2256 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2257 | /* 3: has the read timeout expired ? */ |
| 2258 | else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2259 | if (txn->flags & TX_NOT_FIRST) |
| 2260 | goto failed_keep_alive; |
| 2261 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2262 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2263 | if (msg->err_pos >= 0) |
| 2264 | 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] | 2265 | txn->status = 408; |
| 2266 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2267 | msg->msg_state = HTTP_MSG_ERROR; |
| 2268 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2269 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2270 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2271 | if (s->listener->counters) |
| 2272 | s->listener->counters->failed_req++; |
| 2273 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2274 | if (!(s->flags & SN_ERR_MASK)) |
| 2275 | s->flags |= SN_ERR_CLITO; |
| 2276 | if (!(s->flags & SN_FINST_MASK)) |
| 2277 | s->flags |= SN_FINST_R; |
| 2278 | return 0; |
| 2279 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2280 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2281 | /* 4: have we encountered a close ? */ |
| 2282 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2283 | if (txn->flags & TX_NOT_FIRST) |
| 2284 | goto failed_keep_alive; |
| 2285 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2286 | if (msg->err_pos >= 0) |
| 2287 | 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] | 2288 | txn->status = 400; |
| 2289 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2290 | msg->msg_state = HTTP_MSG_ERROR; |
| 2291 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2292 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2293 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2294 | if (s->listener->counters) |
| 2295 | s->listener->counters->failed_req++; |
| 2296 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2297 | if (!(s->flags & SN_ERR_MASK)) |
| 2298 | s->flags |= SN_ERR_CLICL; |
| 2299 | if (!(s->flags & SN_FINST_MASK)) |
| 2300 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2301 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2302 | } |
| 2303 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2304 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2305 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2306 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2307 | /* just set the request timeout once at the beginning of the request */ |
| 2308 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 2309 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2310 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2311 | /* we're not ready yet */ |
| 2312 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2313 | |
| 2314 | failed_keep_alive: |
| 2315 | /* Here we process low-level errors for keep-alive requests. In |
| 2316 | * short, if the request is not the first one and it experiences |
| 2317 | * a timeout, read error or shutdown, we just silently close so |
| 2318 | * that the client can try again. |
| 2319 | */ |
| 2320 | txn->status = 0; |
| 2321 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2322 | req->analysers = 0; |
| 2323 | s->logs.logwait = 0; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame^] | 2324 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2325 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2326 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2327 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2328 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2329 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2330 | * later. At this point, we have the last CRLF at req->data + msg->eoh. |
| 2331 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
| 2332 | * points to the CRLF of the request line. req->lr points to the first |
| 2333 | * byte after the last LF. msg->col and msg->sov point to the first |
| 2334 | * byte of data. msg->eol cannot be trusted because it may have been |
| 2335 | * left uninitialized (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2336 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2337 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2338 | /* Maybe we found in invalid header name while we were configured not |
| 2339 | * to block on that, so we have to capture it now. |
| 2340 | */ |
| 2341 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2342 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2343 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2344 | /* |
| 2345 | * 1: identify the method |
| 2346 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2347 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2348 | |
| 2349 | /* we can make use of server redirect on GET and HEAD */ |
| 2350 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2351 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2352 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2353 | /* |
| 2354 | * 2: check if the URI matches the monitor_uri. |
| 2355 | * We have to do this for every request which gets in, because |
| 2356 | * the monitor-uri is defined by the frontend. |
| 2357 | */ |
| 2358 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2359 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2360 | !memcmp(msg->sol + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2361 | s->fe->monitor_uri, |
| 2362 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2363 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2364 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2365 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2366 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2367 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2368 | s->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2369 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2370 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2371 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| 2372 | 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] | 2373 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2374 | ret = acl_pass(ret); |
| 2375 | if (cond->pol == ACL_COND_UNLESS) |
| 2376 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2377 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2378 | if (ret) { |
| 2379 | /* we fail this request, let's return 503 service unavail */ |
| 2380 | txn->status = 503; |
| 2381 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2382 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2383 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2384 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2385 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2386 | /* nothing to fail, let's reply normaly */ |
| 2387 | txn->status = 200; |
| 2388 | stream_int_retnclose(req->prod, &http_200_chunk); |
| 2389 | goto return_prx_cond; |
| 2390 | } |
| 2391 | |
| 2392 | /* |
| 2393 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2394 | * Note: we cannot log anymore if the request has been |
| 2395 | * classified as invalid. |
| 2396 | */ |
| 2397 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2398 | /* we have a complete HTTP request that we must log */ |
| 2399 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2400 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2401 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2402 | if (urilen >= REQURI_LEN) |
| 2403 | urilen = REQURI_LEN - 1; |
| 2404 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2405 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2406 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2407 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2408 | s->do_log(s); |
| 2409 | } else { |
| 2410 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2411 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2412 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2413 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2414 | /* 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] | 2415 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) |
| 2416 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2417 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2418 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2419 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2420 | ((msg->sol[msg->sl.rq.v + 5] > '1') || |
| 2421 | ((msg->sol[msg->sl.rq.v + 5] == '1') && |
| 2422 | (msg->sol[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2423 | txn->flags |= TX_REQ_VER_11; |
| 2424 | |
| 2425 | /* "connection" has not been parsed yet */ |
| 2426 | txn->flags &= ~TX_CON_HDR_PARS; |
| 2427 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2428 | /* transfer length unknown*/ |
| 2429 | txn->flags &= ~TX_REQ_XFER_LEN; |
| 2430 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2431 | /* 5: we may need to capture headers */ |
| 2432 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2433 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2434 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2435 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2436 | /* 6: determine the transfer-length. |
| 2437 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2438 | * the presence of a message-body in a REQUEST and its transfer length |
| 2439 | * must be determined that way (in order of precedence) : |
| 2440 | * 1. The presence of a message-body in a request is signaled by the |
| 2441 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2442 | * in the request's header fields. When a request message contains |
| 2443 | * both a message-body of non-zero length and a method that does |
| 2444 | * not define any semantics for that request message-body, then an |
| 2445 | * origin server SHOULD either ignore the message-body or respond |
| 2446 | * with an appropriate error message (e.g., 413). A proxy or |
| 2447 | * gateway, when presented the same request, SHOULD either forward |
| 2448 | * the request inbound with the message- body or ignore the |
| 2449 | * message-body when determining a response. |
| 2450 | * |
| 2451 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2452 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2453 | * transfer-length is defined by the use of this transfer-coding. |
| 2454 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2455 | * transfer-coding is not present, the transfer-length is defined |
| 2456 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2457 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2458 | * 3. If a Content-Length header field is present, its decimal value in |
| 2459 | * OCTETs represents both the entity-length and the transfer-length. |
| 2460 | * If a message is received with both a Transfer-Encoding header |
| 2461 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2462 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2463 | * 4. By the server closing the connection. (Closing the connection |
| 2464 | * cannot be used to indicate the end of a request body, since that |
| 2465 | * would leave no possibility for the server to send back a response.) |
| 2466 | * |
| 2467 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2468 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2469 | * it is terminated by closing the connection. When the "chunked" |
| 2470 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2471 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2472 | */ |
| 2473 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2474 | /* CONNECT sets a tunnel and ignores everything else */ |
| 2475 | if (txn->meth == HTTP_METH_CONNECT) |
| 2476 | goto skip_xfer_len; |
| 2477 | |
| 2478 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2479 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2480 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 2481 | while ((txn->flags & TX_REQ_VER_11) && |
| 2482 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2483 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 2484 | txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2485 | else if (txn->flags & TX_REQ_TE_CHNK) { |
| 2486 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2487 | use_close_only = 1; |
| 2488 | txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2489 | break; |
| 2490 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2491 | } |
| 2492 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2493 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2494 | while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only && |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2495 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2496 | signed long long cl; |
| 2497 | |
| 2498 | if (!ctx.vlen) |
| 2499 | goto return_bad_req; |
| 2500 | |
| 2501 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 2502 | goto return_bad_req; /* parse failure */ |
| 2503 | |
| 2504 | if (cl < 0) |
| 2505 | goto return_bad_req; |
| 2506 | |
| 2507 | if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 2508 | goto return_bad_req; /* already specified, was different */ |
| 2509 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2510 | txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2511 | msg->hdr_content_len = cl; |
| 2512 | } |
| 2513 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2514 | /* bodyless requests have a known length */ |
| 2515 | if (!use_close_only) |
| 2516 | txn->flags |= TX_REQ_XFER_LEN; |
| 2517 | |
| 2518 | skip_xfer_len: |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2519 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2520 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2521 | req->analyse_exp = TICK_ETERNITY; |
| 2522 | return 1; |
| 2523 | |
| 2524 | return_bad_req: |
| 2525 | /* We centralize bad requests processing here */ |
| 2526 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2527 | /* we detected a parsing error. We want to archive this request |
| 2528 | * in the dedicated proxy area for later troubleshooting. |
| 2529 | */ |
| 2530 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2531 | } |
| 2532 | |
| 2533 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2534 | txn->status = 400; |
| 2535 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2536 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2537 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2538 | if (s->listener->counters) |
| 2539 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2540 | |
| 2541 | return_prx_cond: |
| 2542 | if (!(s->flags & SN_ERR_MASK)) |
| 2543 | s->flags |= SN_ERR_PRXCOND; |
| 2544 | if (!(s->flags & SN_FINST_MASK)) |
| 2545 | s->flags |= SN_FINST_R; |
| 2546 | |
| 2547 | req->analysers = 0; |
| 2548 | req->analyse_exp = TICK_ETERNITY; |
| 2549 | return 0; |
| 2550 | } |
| 2551 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2552 | /* This stream analyser runs all HTTP request processing which is common to |
| 2553 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2554 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2555 | * 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] | 2556 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2557 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2558 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2559 | 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] | 2560 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2561 | struct http_txn *txn = &s->txn; |
| 2562 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2563 | struct acl_cond *cond; |
| 2564 | struct redirect_rule *rule; |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 2565 | struct wordlist *wl; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2566 | int cur_idx; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2567 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2568 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2569 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2570 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2571 | return 0; |
| 2572 | } |
| 2573 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2574 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2575 | req->analyse_exp = TICK_ETERNITY; |
| 2576 | |
| 2577 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2578 | now_ms, __FUNCTION__, |
| 2579 | s, |
| 2580 | req, |
| 2581 | req->rex, req->wex, |
| 2582 | req->flags, |
| 2583 | req->l, |
| 2584 | req->analysers); |
| 2585 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2586 | /* first check whether we have some ACLs set to block this request */ |
| 2587 | list_for_each_entry(cond, &px->block_cond, list) { |
| 2588 | int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2589 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2590 | ret = acl_pass(ret); |
| 2591 | if (cond->pol == ACL_COND_UNLESS) |
| 2592 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2593 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2594 | if (ret) { |
| 2595 | txn->status = 403; |
| 2596 | /* let's log the request time */ |
| 2597 | s->logs.tv_request = now; |
| 2598 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2599 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2600 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2601 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2602 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2603 | /* try headers filters */ |
| 2604 | if (px->req_exp != NULL) { |
| 2605 | if (apply_filters_to_request(s, req, px->req_exp) < 0) |
| 2606 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2607 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2608 | /* has the request been denied ? */ |
| 2609 | if (txn->flags & TX_CLDENY) { |
| 2610 | /* no need to go further */ |
| 2611 | txn->status = 403; |
| 2612 | /* let's log the request time */ |
| 2613 | s->logs.tv_request = now; |
| 2614 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2615 | goto return_prx_cond; |
| 2616 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2617 | |
| 2618 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2619 | * which may be the same as the connect timeout if unspecified. |
| 2620 | * If unset, then set it to zero because we really want it to |
| 2621 | * eventually expire. We build the tarpit as an analyser. |
| 2622 | */ |
| 2623 | if (txn->flags & TX_CLTARPIT) { |
| 2624 | buffer_erase(s->req); |
| 2625 | /* wipe the request out so that we can drop the connection early |
| 2626 | * if the client closes first. |
| 2627 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2628 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2629 | req->analysers = 0; /* remove switching rules etc... */ |
| 2630 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2631 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2632 | if (!req->analyse_exp) |
| 2633 | req->analyse_exp = tick_add(now_ms, 0); |
| 2634 | return 1; |
| 2635 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2636 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2637 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2638 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 2639 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2640 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 2641 | * in which headers are mangled. However, if another mode is set, it will |
| 2642 | * affect it (eg: server-close/keep-alive + httpclose = close). |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 2643 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2644 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2645 | if ((txn->meth != HTTP_METH_CONNECT) && |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2646 | ((s->fe->options|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] | 2647 | int tmp = TX_CON_WANT_TUN; |
| 2648 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE) |
| 2649 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2650 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 2651 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2652 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2653 | tmp = TX_CON_WANT_CLO; |
| 2654 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2655 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2656 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2657 | |
Willy Tarreau | 8db1c17 | 2010-01-05 23:12:12 +0100 | [diff] [blame] | 2658 | if (!(txn->flags & TX_CON_HDR_PARS)) |
| 2659 | http_req_parse_connection_header(txn); |
| 2660 | |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2661 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 2662 | if ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) |
| 2663 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2664 | if (!(txn->flags & TX_REQ_XFER_LEN)) |
| 2665 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2666 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2667 | } |
| 2668 | |
| 2669 | /* We're really certain of the connection mode (tunnel, close, keep-alive) |
| 2670 | * once we know the backend, because the tunnel mode can be implied by the |
| 2671 | * lack of any close/keepalive options in both the FE and the BE. Since |
| 2672 | * this information can evolve with time, we proceed by trying to make the |
| 2673 | * header status match the desired status. For this, we'll have to adjust |
| 2674 | * the "Connection" header. The test for persistent connections has already |
| 2675 | * been performed, so we only enter here if there is a risk the connection |
| 2676 | * is considered as persistent and we want it to be closed on the server |
| 2677 | * side. It would be nice if we could enter this place only when a |
| 2678 | * Connection header exists. Note that a CONNECT method will not enter |
| 2679 | * here. |
| 2680 | */ |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2681 | if (!(txn->flags & TX_REQ_CONN_CLO) && |
| 2682 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 2683 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2684 | char *cur_ptr, *cur_end, *cur_next; |
| 2685 | int old_idx, delta, val; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2686 | int must_delete; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2687 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2688 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2689 | must_delete = !(txn->flags & TX_REQ_VER_11); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2690 | cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2691 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2692 | for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) { |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2693 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 2694 | cur_ptr = cur_next; |
| 2695 | cur_end = cur_ptr + cur_hdr->len; |
| 2696 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2697 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2698 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2699 | if (!val) |
| 2700 | continue; |
| 2701 | |
| 2702 | /* 3 possibilities : |
| 2703 | * - we have already set "Connection: close" or we're in |
| 2704 | * HTTP/1.0, so we remove this line. |
| 2705 | * - we have not yet set "Connection: close", but this line |
| 2706 | * indicates close. We leave it untouched and set the flag. |
| 2707 | * - we have not yet set "Connection: close", and this line |
| 2708 | * indicates non-close. We replace it and set the flag. |
| 2709 | */ |
| 2710 | if (must_delete) { |
| 2711 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 2712 | http_msg_move_end(&txn->req, delta); |
| 2713 | cur_next += delta; |
| 2714 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2715 | txn->hdr_idx.used--; |
| 2716 | cur_hdr->len = 0; |
| 2717 | txn->flags |= TX_REQ_CONN_CLO; |
| 2718 | } else { |
| 2719 | if (cur_end - cur_ptr - val != 5 || |
| 2720 | strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2721 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2722 | "close", 5); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2723 | cur_next += delta; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2724 | cur_hdr->len += delta; |
| 2725 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2726 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2727 | txn->flags |= TX_REQ_CONN_CLO; |
| 2728 | must_delete = 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2729 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2730 | } /* for loop */ |
| 2731 | } /* if must close keep-alive */ |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2732 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2733 | /* add request headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 2734 | list_for_each_entry(wl, &px->req_add, list) { |
| 2735 | 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] | 2736 | goto return_bad_req; |
| 2737 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2738 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2739 | /* check if stats URI was requested, and if an auth is needed */ |
| 2740 | if (px->uri_auth != NULL && |
| 2741 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
| 2742 | /* we have to check the URI and auth for this request. |
| 2743 | * FIXME!!! that one is rather dangerous, we want to |
| 2744 | * make it follow standard rules (eg: clear req->analysers). |
| 2745 | */ |
| 2746 | if (stats_check_uri_auth(s, px)) { |
| 2747 | req->analysers = 0; |
| 2748 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2749 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2750 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2751 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2752 | /* check whether we have some ACLs set to redirect this request */ |
| 2753 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 2754 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2755 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 2756 | if (rule->cond) { |
| 2757 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
| 2758 | ret = acl_pass(ret); |
| 2759 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2760 | ret = !ret; |
| 2761 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2762 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2763 | if (ret) { |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 2764 | struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2765 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2766 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2767 | /* build redirect message */ |
| 2768 | switch(rule->code) { |
| 2769 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2770 | msg_fmt = HTTP_303; |
| 2771 | break; |
| 2772 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2773 | msg_fmt = HTTP_301; |
| 2774 | break; |
| 2775 | case 302: |
| 2776 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2777 | msg_fmt = HTTP_302; |
| 2778 | break; |
| 2779 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2780 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 2781 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2782 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2783 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2784 | switch(rule->type) { |
| 2785 | case REDIRECT_TYPE_PREFIX: { |
| 2786 | const char *path; |
| 2787 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2788 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2789 | path = http_get_path(txn); |
| 2790 | /* build message using path */ |
| 2791 | if (path) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2792 | 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] | 2793 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2794 | int qs = 0; |
| 2795 | while (qs < pathlen) { |
| 2796 | if (path[qs] == '?') { |
| 2797 | pathlen = qs; |
| 2798 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2799 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2800 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2801 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2802 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2803 | } else { |
| 2804 | path = "/"; |
| 2805 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2806 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2807 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 2808 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2809 | goto return_bad_req; |
| 2810 | |
| 2811 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2812 | * add anything, otherwise it makes it hard for the user to |
| 2813 | * configure a self-redirection. |
| 2814 | */ |
| 2815 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2816 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2817 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2818 | } |
| 2819 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2820 | /* add path */ |
| 2821 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 2822 | rdr.len += pathlen; |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 2823 | |
| 2824 | /* append a slash at the end of the location is needed and missing */ |
| 2825 | if (rdr.len && rdr.str[rdr.len - 1] != '/' && |
| 2826 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2827 | if (rdr.len > rdr.size - 5) |
| 2828 | goto return_bad_req; |
| 2829 | rdr.str[rdr.len] = '/'; |
| 2830 | rdr.len++; |
| 2831 | } |
| 2832 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2833 | break; |
| 2834 | } |
| 2835 | case REDIRECT_TYPE_LOCATION: |
| 2836 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 2837 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2838 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2839 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2840 | /* add location */ |
| 2841 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2842 | rdr.len += rule->rdr_len; |
| 2843 | break; |
| 2844 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2845 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2846 | if (rule->cookie_len) { |
| 2847 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 2848 | rdr.len += 14; |
| 2849 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 2850 | rdr.len += rule->cookie_len; |
| 2851 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 2852 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2853 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2854 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 2855 | /* add end of headers and the keep-alive/close status. |
| 2856 | * We may choose to set keep-alive if the Location begins |
| 2857 | * with a slash, because the client will come back to the |
| 2858 | * same server. |
| 2859 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2860 | txn->status = rule->code; |
| 2861 | /* let's log the request time */ |
| 2862 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 2863 | |
| 2864 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
| 2865 | (txn->flags & TX_REQ_XFER_LEN) && |
| 2866 | !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len && |
| 2867 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 2868 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 2869 | /* keep-alive possible */ |
| 2870 | memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive\r\n\r\n", 28); |
| 2871 | rdr.len += 28; |
| 2872 | buffer_write(req->prod->ob, rdr.str, rdr.len); |
| 2873 | /* "eat" the request */ |
| 2874 | buffer_ignore(req, msg->sov - msg->som); |
| 2875 | msg->som = msg->sov; |
| 2876 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 2877 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 2878 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 2879 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 2880 | break; |
| 2881 | } else { |
| 2882 | /* keep-alive not possible */ |
| 2883 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 2884 | rdr.len += 23; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame^] | 2885 | stream_int_retnclose(req->prod, &rdr); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 2886 | goto return_prx_cond; |
| 2887 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2888 | } |
| 2889 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2890 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 2891 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 2892 | * If this happens, then the data will not come immediately, so we must |
| 2893 | * send all what we have without waiting. Note that due to the small gain |
| 2894 | * in waiting for the body of the request, it's easier to simply put the |
| 2895 | * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 2896 | * itself once used. |
| 2897 | */ |
| 2898 | req->flags |= BF_SEND_DONTWAIT; |
| 2899 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2900 | /* that's OK for us now, let's move on to next analysers */ |
| 2901 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2902 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2903 | return_bad_req: |
| 2904 | /* We centralize bad requests processing here */ |
| 2905 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2906 | /* we detected a parsing error. We want to archive this request |
| 2907 | * in the dedicated proxy area for later troubleshooting. |
| 2908 | */ |
| 2909 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2910 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2911 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2912 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2913 | txn->status = 400; |
| 2914 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2915 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2916 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2917 | if (s->listener->counters) |
| 2918 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2919 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2920 | return_prx_cond: |
| 2921 | if (!(s->flags & SN_ERR_MASK)) |
| 2922 | s->flags |= SN_ERR_PRXCOND; |
| 2923 | if (!(s->flags & SN_FINST_MASK)) |
| 2924 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2925 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2926 | req->analysers = 0; |
| 2927 | req->analyse_exp = TICK_ETERNITY; |
| 2928 | return 0; |
| 2929 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2930 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2931 | /* This function performs all the processing enabled for the current request. |
| 2932 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 2933 | * needs more data, encounters an error, or wants to immediately abort the |
| 2934 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 2935 | */ |
| 2936 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 2937 | { |
| 2938 | struct http_txn *txn = &s->txn; |
| 2939 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2940 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2941 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2942 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2943 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2944 | return 0; |
| 2945 | } |
| 2946 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2947 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2948 | now_ms, __FUNCTION__, |
| 2949 | s, |
| 2950 | req, |
| 2951 | req->rex, req->wex, |
| 2952 | req->flags, |
| 2953 | req->l, |
| 2954 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2955 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2956 | /* |
| 2957 | * Right now, we know that we have processed the entire headers |
| 2958 | * and that unwanted requests have been filtered out. We can do |
| 2959 | * whatever we want with the remaining request. Also, now we |
| 2960 | * may have separate values for ->fe, ->be. |
| 2961 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2962 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2963 | /* |
| 2964 | * If HTTP PROXY is set we simply get remote server address |
| 2965 | * parsing incoming request. |
| 2966 | */ |
| 2967 | 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] | 2968 | 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] | 2969 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2970 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2971 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 2972 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2973 | * Note that doing so might move headers in the request, but |
| 2974 | * the fields will stay coherent and the URI will not move. |
| 2975 | * This should only be performed in the backend. |
| 2976 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 2977 | 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] | 2978 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 2979 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2980 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2981 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 2982 | * 8: the appsession cookie was looked up very early in 1.2, |
| 2983 | * so let's do the same now. |
| 2984 | */ |
| 2985 | |
| 2986 | /* It needs to look into the URI */ |
| 2987 | if ((s->sessid == NULL) && s->be->appsession_name) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2988 | 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] | 2989 | } |
| 2990 | |
| 2991 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2992 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2993 | * asks for it. |
| 2994 | */ |
| 2995 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
| 2996 | if (s->cli_addr.ss_family == AF_INET) { |
| 2997 | /* Add an X-Forwarded-For header unless the source IP is |
| 2998 | * in the 'except' network range. |
| 2999 | */ |
| 3000 | if ((!s->fe->except_mask.s_addr || |
| 3001 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
| 3002 | != s->fe->except_net.s_addr) && |
| 3003 | (!s->be->except_mask.s_addr || |
| 3004 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr) |
| 3005 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3006 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3007 | unsigned char *pn; |
| 3008 | pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3009 | |
| 3010 | /* Note: we rely on the backend to get the header name to be used for |
| 3011 | * x-forwarded-for, because the header is really meant for the backends. |
| 3012 | * However, if the backend did not specify any option, we have to rely |
| 3013 | * on the frontend's header name. |
| 3014 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3015 | if (s->be->fwdfor_hdr_len) { |
| 3016 | len = s->be->fwdfor_hdr_len; |
| 3017 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3018 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3019 | len = s->fe->fwdfor_hdr_len; |
| 3020 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3021 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3022 | 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] | 3023 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3024 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3025 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3026 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3027 | } |
| 3028 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3029 | else if (s->cli_addr.ss_family == AF_INET6) { |
| 3030 | /* FIXME: for the sake of completeness, we should also support |
| 3031 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3032 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3033 | int len; |
| 3034 | char pn[INET6_ADDRSTRLEN]; |
| 3035 | inet_ntop(AF_INET6, |
| 3036 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 3037 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3038 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3039 | /* Note: we rely on the backend to get the header name to be used for |
| 3040 | * x-forwarded-for, because the header is really meant for the backends. |
| 3041 | * However, if the backend did not specify any option, we have to rely |
| 3042 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3043 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3044 | if (s->be->fwdfor_hdr_len) { |
| 3045 | len = s->be->fwdfor_hdr_len; |
| 3046 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 3047 | } else { |
| 3048 | len = s->fe->fwdfor_hdr_len; |
| 3049 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3050 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3051 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3052 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3053 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3054 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3055 | goto return_bad_req; |
| 3056 | } |
| 3057 | } |
| 3058 | |
| 3059 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3060 | * 10: add X-Original-To if either the frontend or the backend |
| 3061 | * asks for it. |
| 3062 | */ |
| 3063 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3064 | |
| 3065 | /* FIXME: don't know if IPv6 can handle that case too. */ |
| 3066 | if (s->cli_addr.ss_family == AF_INET) { |
| 3067 | /* Add an X-Original-To header unless the destination IP is |
| 3068 | * in the 'except' network range. |
| 3069 | */ |
| 3070 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 3071 | get_frt_addr(s); |
| 3072 | |
| 3073 | if ((!s->fe->except_mask_to.s_addr || |
| 3074 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr) |
| 3075 | != s->fe->except_to.s_addr) && |
| 3076 | (!s->be->except_mask_to.s_addr || |
| 3077 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
| 3078 | != s->be->except_to.s_addr)) { |
| 3079 | int len; |
| 3080 | unsigned char *pn; |
| 3081 | pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr; |
| 3082 | |
| 3083 | /* Note: we rely on the backend to get the header name to be used for |
| 3084 | * x-original-to, because the header is really meant for the backends. |
| 3085 | * However, if the backend did not specify any option, we have to rely |
| 3086 | * on the frontend's header name. |
| 3087 | */ |
| 3088 | if (s->be->orgto_hdr_len) { |
| 3089 | len = s->be->orgto_hdr_len; |
| 3090 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 3091 | } else { |
| 3092 | len = s->fe->orgto_hdr_len; |
| 3093 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3094 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3095 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3096 | |
| 3097 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3098 | &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3099 | goto return_bad_req; |
| 3100 | } |
| 3101 | } |
| 3102 | } |
| 3103 | |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3104 | /* 11: add "Connection: close" if needed and not yet set. */ |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3105 | if (!(txn->flags & TX_REQ_CONN_CLO) && |
| 3106 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3107 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3108 | if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3109 | "Connection: close", 17) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3110 | goto return_bad_req; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3111 | txn->flags |= TX_REQ_CONN_CLO; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3112 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3113 | |
| 3114 | /* If we have no server assigned yet and we're balancing on url_param |
| 3115 | * with a POST request, we may be interested in checking the body for |
| 3116 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3117 | */ |
| 3118 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3119 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3120 | s->be->url_param_post_limit != 0 && |
| 3121 | (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3122 | 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] | 3123 | buffer_dont_connect(req); |
| 3124 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3125 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3126 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3127 | if (txn->flags & TX_REQ_XFER_LEN) |
| 3128 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3129 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3130 | /************************************************************* |
| 3131 | * OK, that's finished for the headers. We have done what we * |
| 3132 | * could. Let's switch to the DATA state. * |
| 3133 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3134 | req->analyse_exp = TICK_ETERNITY; |
| 3135 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3136 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3137 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3138 | /* OK let's go on with the BODY now */ |
| 3139 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3140 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3141 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3142 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3143 | /* we detected a parsing error. We want to archive this request |
| 3144 | * in the dedicated proxy area for later troubleshooting. |
| 3145 | */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3146 | 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] | 3147 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3148 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3149 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3150 | txn->status = 400; |
| 3151 | req->analysers = 0; |
| 3152 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3153 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3154 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3155 | if (s->listener->counters) |
| 3156 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3157 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3158 | if (!(s->flags & SN_ERR_MASK)) |
| 3159 | s->flags |= SN_ERR_PRXCOND; |
| 3160 | if (!(s->flags & SN_FINST_MASK)) |
| 3161 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3162 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3163 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3164 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3165 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3166 | * returns zero, at the beginning because it prevents any other processing |
| 3167 | * from occurring, and at the end because it terminates the request. |
| 3168 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3169 | 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] | 3170 | { |
| 3171 | struct http_txn *txn = &s->txn; |
| 3172 | |
| 3173 | /* This connection is being tarpitted. The CLIENT side has |
| 3174 | * already set the connect expiration date to the right |
| 3175 | * timeout. We just have to check that the client is still |
| 3176 | * there and that the timeout has not expired. |
| 3177 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3178 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3179 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 3180 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3181 | return 0; |
| 3182 | |
| 3183 | /* We will set the queue timer to the time spent, just for |
| 3184 | * logging purposes. We fake a 500 server error, so that the |
| 3185 | * attacker will not suspect his connection has been tarpitted. |
| 3186 | * It will not cause trouble to the logs because we can exclude |
| 3187 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3188 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3189 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3190 | |
| 3191 | txn->status = 500; |
| 3192 | if (req->flags != BF_READ_ERROR) |
| 3193 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 3194 | |
| 3195 | req->analysers = 0; |
| 3196 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3197 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3198 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3199 | if (s->listener->counters) |
| 3200 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3201 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3202 | if (!(s->flags & SN_ERR_MASK)) |
| 3203 | s->flags |= SN_ERR_PRXCOND; |
| 3204 | if (!(s->flags & SN_FINST_MASK)) |
| 3205 | s->flags |= SN_FINST_T; |
| 3206 | return 0; |
| 3207 | } |
| 3208 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3209 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3210 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3211 | * must only be called after the standard HTTP request processing has occurred, |
| 3212 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3213 | * read more data, or 1 once it has completed its analysis. |
| 3214 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3215 | 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] | 3216 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3217 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3218 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3219 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3220 | |
| 3221 | /* We have to parse the HTTP request body to find any required data. |
| 3222 | * "balance url_param check_post" should have been the only way to get |
| 3223 | * into this. We were brought here after HTTP header analysis, so all |
| 3224 | * related structures are ready. |
| 3225 | */ |
| 3226 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3227 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3228 | goto missing_data; |
| 3229 | |
| 3230 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3231 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3232 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3233 | */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 3234 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3235 | struct hdr_ctx ctx; |
| 3236 | ctx.idx = 0; |
| 3237 | /* Expect is allowed in 1.1, look for it */ |
| 3238 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3239 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3240 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3241 | } |
| 3242 | } |
| 3243 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3244 | } |
| 3245 | |
| 3246 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3247 | /* we have msg->col and msg->sov which both point to the first |
| 3248 | * byte of message body. msg->som still points to the beginning |
| 3249 | * of the message. We must save the body in req->lr because it |
| 3250 | * survives buffer re-alignments. |
| 3251 | */ |
| 3252 | req->lr = req->data + msg->sov; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3253 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3254 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3255 | else |
| 3256 | msg->msg_state = HTTP_MSG_DATA; |
| 3257 | } |
| 3258 | |
| 3259 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3260 | /* read the chunk size and assign it to ->hdr_content_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3261 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3262 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3263 | */ |
| 3264 | int ret = http_parse_chunk_size(req, msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3265 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3266 | if (!ret) |
| 3267 | goto missing_data; |
| 3268 | else if (ret < 0) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3269 | goto return_bad_req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3270 | } |
| 3271 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3272 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3273 | * We have the first non-header byte in msg->col, which is either the |
| 3274 | * beginning of the chunk size or of the data. The first data byte is in |
| 3275 | * msg->sov, which is equal to msg->col when not using transfer-encoding. |
| 3276 | * 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] | 3277 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3278 | |
| 3279 | if (msg->hdr_content_len < limit) |
| 3280 | limit = msg->hdr_content_len; |
| 3281 | |
Willy Tarreau | 7c96f67 | 2009-12-27 22:47:25 +0100 | [diff] [blame] | 3282 | 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] | 3283 | goto http_end; |
| 3284 | |
| 3285 | missing_data: |
| 3286 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3287 | if (req->flags & BF_FULL) |
| 3288 | goto return_bad_req; |
| 3289 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3290 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 3291 | txn->status = 408; |
| 3292 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 3293 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3294 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3295 | |
| 3296 | /* we get here if we need to wait for more data */ |
| 3297 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3298 | /* Not enough data. We'll re-use the http-request |
| 3299 | * timeout here. Ideally, we should set the timeout |
| 3300 | * relative to the accept() date. We just set the |
| 3301 | * request timeout once at the beginning of the |
| 3302 | * request. |
| 3303 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3304 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3305 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3306 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3307 | return 0; |
| 3308 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3309 | |
| 3310 | http_end: |
| 3311 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3312 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3313 | req->analysers &= ~an_bit; |
| 3314 | req->analyse_exp = TICK_ETERNITY; |
| 3315 | return 1; |
| 3316 | |
| 3317 | return_bad_req: /* let's centralize all bad requests */ |
| 3318 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3319 | txn->status = 400; |
| 3320 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 3321 | |
| 3322 | return_err_msg: |
| 3323 | req->analysers = 0; |
| 3324 | s->fe->counters.failed_req++; |
| 3325 | if (s->listener->counters) |
| 3326 | s->listener->counters->failed_req++; |
| 3327 | |
| 3328 | if (!(s->flags & SN_ERR_MASK)) |
| 3329 | s->flags |= SN_ERR_PRXCOND; |
| 3330 | if (!(s->flags & SN_FINST_MASK)) |
| 3331 | s->flags |= SN_FINST_R; |
| 3332 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3333 | } |
| 3334 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3335 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3336 | * right now but it works. |
| 3337 | */ |
| 3338 | void http_end_txn_clean_session(struct session *s) |
| 3339 | { |
| 3340 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3341 | * server's connections. We need a safer way to reinitialize buffer |
| 3342 | * flags. We also need a more accurate method for computing per-request |
| 3343 | * data. |
| 3344 | */ |
| 3345 | http_silent_debug(__LINE__, s); |
| 3346 | |
| 3347 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 3348 | s->req->cons->shutr(s->req->cons); |
| 3349 | s->req->cons->shutw(s->req->cons); |
| 3350 | |
| 3351 | http_silent_debug(__LINE__, s); |
| 3352 | |
| 3353 | if (s->flags & SN_BE_ASSIGNED) |
| 3354 | s->be->beconn--; |
| 3355 | |
| 3356 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3357 | session_process_counters(s); |
| 3358 | |
| 3359 | if (s->txn.status) { |
| 3360 | int n; |
| 3361 | |
| 3362 | n = s->txn.status / 100; |
| 3363 | if (n < 1 || n > 5) |
| 3364 | n = 0; |
| 3365 | |
| 3366 | if (s->fe->mode == PR_MODE_HTTP) |
| 3367 | s->fe->counters.p.http.rsp[n]++; |
| 3368 | |
| 3369 | if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) && |
| 3370 | (s->be->mode == PR_MODE_HTTP)) |
| 3371 | s->be->counters.p.http.rsp[n]++; |
| 3372 | } |
| 3373 | |
| 3374 | /* don't count other requests' data */ |
| 3375 | s->logs.bytes_in -= s->req->l - s->req->send_max; |
| 3376 | s->logs.bytes_out -= s->rep->l - s->rep->send_max; |
| 3377 | |
| 3378 | /* let's do a final log if we need it */ |
| 3379 | if (s->logs.logwait && |
| 3380 | !(s->flags & SN_MONITOR) && |
| 3381 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 3382 | s->do_log(s); |
| 3383 | } |
| 3384 | |
| 3385 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3386 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 3387 | tv_zero(&s->logs.tv_request); |
| 3388 | s->logs.t_queue = -1; |
| 3389 | s->logs.t_connect = -1; |
| 3390 | s->logs.t_data = -1; |
| 3391 | s->logs.t_close = 0; |
| 3392 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3393 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 3394 | |
| 3395 | s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max; |
| 3396 | s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max; |
| 3397 | |
| 3398 | if (s->pend_pos) |
| 3399 | pendconn_free(s->pend_pos); |
| 3400 | |
| 3401 | if (s->srv) { |
| 3402 | if (s->flags & SN_CURR_SESS) { |
| 3403 | s->flags &= ~SN_CURR_SESS; |
| 3404 | s->srv->cur_sess--; |
| 3405 | } |
| 3406 | if (may_dequeue_tasks(s->srv, s->be)) |
| 3407 | process_srv_queue(s->srv); |
| 3408 | } |
| 3409 | |
| 3410 | if (unlikely(s->srv_conn)) |
| 3411 | sess_change_server(s, NULL); |
| 3412 | s->srv = NULL; |
| 3413 | |
| 3414 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
| 3415 | s->req->cons->fd = -1; /* just to help with debugging */ |
| 3416 | s->req->cons->err_type = SI_ET_NONE; |
| 3417 | s->req->cons->err_loc = NULL; |
| 3418 | s->req->cons->exp = TICK_ETERNITY; |
| 3419 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3420 | s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST); |
| 3421 | s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_WRITE_PARTIAL); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3422 | s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED); |
| 3423 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 3424 | s->txn.meth = 0; |
| 3425 | http_reset_txn(s); |
| 3426 | s->txn.flags |= TX_NOT_FIRST; |
| 3427 | if (s->be->options2 & PR_O2_INDEPSTR) |
| 3428 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 3429 | |
| 3430 | /* if the request buffer is not empty, it means we're |
| 3431 | * about to process another request, so send pending |
| 3432 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3433 | * Just don't do this if the buffer is close to be full, |
| 3434 | * because the request will wait for it to flush a little |
| 3435 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3436 | */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3437 | if (s->req->l > s->req->send_max) { |
| 3438 | if (s->rep->send_max && |
| 3439 | !(s->rep->flags & BF_FULL) && |
| 3440 | s->rep->lr <= s->rep->r && |
| 3441 | s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite) |
| 3442 | s->rep->flags |= BF_EXPECT_MORE; |
| 3443 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3444 | |
| 3445 | /* we're removing the analysers, we MUST re-enable events detection */ |
| 3446 | buffer_auto_read(s->req); |
| 3447 | buffer_auto_close(s->req); |
| 3448 | buffer_auto_read(s->rep); |
| 3449 | buffer_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3450 | |
| 3451 | /* make ->lr point to the first non-forwarded byte */ |
| 3452 | s->req->lr = s->req->w + s->req->send_max; |
| 3453 | if (s->req->lr >= s->req->data + s->req->size) |
| 3454 | s->req->lr -= s->req->size; |
| 3455 | s->rep->lr = s->rep->w + s->rep->send_max; |
| 3456 | if (s->rep->lr >= s->rep->data + s->rep->size) |
| 3457 | s->rep->lr -= s->req->size; |
| 3458 | |
| 3459 | s->req->analysers |= s->fe->fe_req_ana; |
| 3460 | s->rep->analysers = 0; |
| 3461 | |
| 3462 | http_silent_debug(__LINE__, s); |
| 3463 | } |
| 3464 | |
| 3465 | |
| 3466 | /* This function updates the request state machine according to the response |
| 3467 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3468 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3469 | * it is only used to find when a request/response couple is complete. Both |
| 3470 | * this function and its equivalent should loop until both return zero. It |
| 3471 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3472 | */ |
| 3473 | int http_sync_req_state(struct session *s) |
| 3474 | { |
| 3475 | struct buffer *buf = s->req; |
| 3476 | struct http_txn *txn = &s->txn; |
| 3477 | unsigned int old_flags = buf->flags; |
| 3478 | unsigned int old_state = txn->req.msg_state; |
| 3479 | |
| 3480 | http_silent_debug(__LINE__, s); |
| 3481 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 3482 | return 0; |
| 3483 | |
| 3484 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3485 | /* No need to read anymore, the request was completely parsed. |
| 3486 | * We can shut the read side unless we want to abort_on_close. |
| 3487 | */ |
| 3488 | if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE)) |
| 3489 | buffer_dont_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3490 | |
| 3491 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 3492 | goto wait_other_side; |
| 3493 | |
| 3494 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 3495 | /* The server has not finished to respond, so we |
| 3496 | * don't want to move in order not to upset it. |
| 3497 | */ |
| 3498 | goto wait_other_side; |
| 3499 | } |
| 3500 | |
| 3501 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 3502 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3503 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3504 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 3505 | goto wait_other_side; |
| 3506 | } |
| 3507 | |
| 3508 | /* When we get here, it means that both the request and the |
| 3509 | * response have finished receiving. Depending on the connection |
| 3510 | * mode, we'll have to wait for the last bytes to leave in either |
| 3511 | * direction, and sometimes for a close to be effective. |
| 3512 | */ |
| 3513 | |
| 3514 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
| 3515 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3516 | /* Server-close mode : queue a connection close to the server */ |
| 3517 | buffer_shutw_now(buf); |
| 3518 | buf->cons->flags |= SI_FL_NOLINGER; |
| 3519 | } |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3520 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3521 | /* Option forceclose is set, let's enforce it now |
| 3522 | * that we're not expecting any new data to come. |
| 3523 | */ |
| 3524 | buffer_shutr_now(buf); |
| 3525 | buffer_shutw_now(buf); |
| 3526 | buf->cons->flags |= SI_FL_NOLINGER; |
| 3527 | } |
| 3528 | /* other modes include httpclose (no action) and keepalive (not implemented) */ |
| 3529 | } |
| 3530 | |
| 3531 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3532 | /* if we've just closed an output, let's switch */ |
| 3533 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3534 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 3535 | goto http_msg_closing; |
| 3536 | } |
| 3537 | else { |
| 3538 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3539 | goto http_msg_closed; |
| 3540 | } |
| 3541 | } |
| 3542 | else { |
| 3543 | /* other modes are used as a tunnel right now */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3544 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3545 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 3546 | goto wait_other_side; |
| 3547 | } |
| 3548 | } |
| 3549 | |
| 3550 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 3551 | http_msg_closing: |
| 3552 | /* nothing else to forward, just waiting for the output buffer |
| 3553 | * to be empty and for the shutw_now to take effect. |
| 3554 | */ |
| 3555 | if (buf->flags & BF_OUT_EMPTY) { |
| 3556 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3557 | goto http_msg_closed; |
| 3558 | } |
| 3559 | else if (buf->flags & BF_SHUTW) { |
| 3560 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3561 | goto wait_other_side; |
| 3562 | } |
| 3563 | } |
| 3564 | |
| 3565 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 3566 | http_msg_closed: |
| 3567 | goto wait_other_side; |
| 3568 | } |
| 3569 | |
| 3570 | wait_other_side: |
| 3571 | http_silent_debug(__LINE__, s); |
| 3572 | return txn->req.msg_state != old_state || buf->flags != old_flags; |
| 3573 | } |
| 3574 | |
| 3575 | |
| 3576 | /* This function updates the response state machine according to the request |
| 3577 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3578 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3579 | * it is only used to find when a request/response couple is complete. Both |
| 3580 | * this function and its equivalent should loop until both return zero. It |
| 3581 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3582 | */ |
| 3583 | int http_sync_res_state(struct session *s) |
| 3584 | { |
| 3585 | struct buffer *buf = s->rep; |
| 3586 | struct http_txn *txn = &s->txn; |
| 3587 | unsigned int old_flags = buf->flags; |
| 3588 | unsigned int old_state = txn->rsp.msg_state; |
| 3589 | |
| 3590 | http_silent_debug(__LINE__, s); |
| 3591 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 3592 | return 0; |
| 3593 | |
| 3594 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 3595 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3596 | * still monitor the server connection for a possible close |
| 3597 | * while the request is being uploaded, so we don't disable |
| 3598 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3599 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3600 | /* buffer_dont_read(buf); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3601 | |
| 3602 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 3603 | goto wait_other_side; |
| 3604 | |
| 3605 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 3606 | /* The client seems to still be sending data, probably |
| 3607 | * because we got an error response during an upload. |
| 3608 | * We have the choice of either breaking the connection |
| 3609 | * or letting it pass through. Let's do the later. |
| 3610 | */ |
| 3611 | goto wait_other_side; |
| 3612 | } |
| 3613 | |
| 3614 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 3615 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3616 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3617 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 3618 | goto wait_other_side; |
| 3619 | } |
| 3620 | |
| 3621 | /* When we get here, it means that both the request and the |
| 3622 | * response have finished receiving. Depending on the connection |
| 3623 | * mode, we'll have to wait for the last bytes to leave in either |
| 3624 | * direction, and sometimes for a close to be effective. |
| 3625 | */ |
| 3626 | |
| 3627 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3628 | /* Server-close mode : shut read and wait for the request |
| 3629 | * side to close its output buffer. The caller will detect |
| 3630 | * when we're in DONE and the other is in CLOSED and will |
| 3631 | * catch that for the final cleanup. |
| 3632 | */ |
| 3633 | if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW))) |
| 3634 | buffer_shutr_now(buf); |
| 3635 | goto wait_other_side; |
| 3636 | } |
| 3637 | else if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) && |
| 3638 | ((s->fe->options | s->be->options) & PR_O_FORCE_CLO)) { |
| 3639 | /* Option forceclose is set, let's enforce it now |
| 3640 | * that we're not expecting any new data to come. |
| 3641 | * The caller knows the session is complete once |
| 3642 | * both states are CLOSED. |
| 3643 | */ |
| 3644 | buffer_shutr_now(buf); |
| 3645 | buffer_shutw_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3646 | } |
| 3647 | else { |
| 3648 | /* other modes include httpclose (no action) and keepalive |
| 3649 | * (not implemented). These modes are used as a tunnel right |
| 3650 | * now. |
| 3651 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3652 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3653 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 3654 | goto wait_other_side; |
| 3655 | } |
| 3656 | |
| 3657 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3658 | /* if we've just closed an output, let's switch */ |
| 3659 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3660 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 3661 | goto http_msg_closing; |
| 3662 | } |
| 3663 | else { |
| 3664 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 3665 | goto http_msg_closed; |
| 3666 | } |
| 3667 | } |
| 3668 | goto wait_other_side; |
| 3669 | } |
| 3670 | |
| 3671 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 3672 | http_msg_closing: |
| 3673 | /* nothing else to forward, just waiting for the output buffer |
| 3674 | * to be empty and for the shutw_now to take effect. |
| 3675 | */ |
| 3676 | if (buf->flags & BF_OUT_EMPTY) { |
| 3677 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 3678 | goto http_msg_closed; |
| 3679 | } |
| 3680 | else if (buf->flags & BF_SHUTW) { |
| 3681 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 3682 | goto wait_other_side; |
| 3683 | } |
| 3684 | } |
| 3685 | |
| 3686 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 3687 | http_msg_closed: |
| 3688 | /* drop any pending data */ |
| 3689 | buffer_ignore(buf, buf->l - buf->send_max); |
| 3690 | buffer_auto_close(buf); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3691 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3692 | goto wait_other_side; |
| 3693 | } |
| 3694 | |
| 3695 | wait_other_side: |
| 3696 | http_silent_debug(__LINE__, s); |
| 3697 | return txn->rsp.msg_state != old_state || buf->flags != old_flags; |
| 3698 | } |
| 3699 | |
| 3700 | |
| 3701 | /* Resync the request and response state machines. Return 1 if either state |
| 3702 | * changes. |
| 3703 | */ |
| 3704 | int http_resync_states(struct session *s) |
| 3705 | { |
| 3706 | struct http_txn *txn = &s->txn; |
| 3707 | int old_req_state = txn->req.msg_state; |
| 3708 | int old_res_state = txn->rsp.msg_state; |
| 3709 | |
| 3710 | http_silent_debug(__LINE__, s); |
| 3711 | http_sync_req_state(s); |
| 3712 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3713 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3714 | if (!http_sync_res_state(s)) |
| 3715 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3716 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3717 | if (!http_sync_req_state(s)) |
| 3718 | break; |
| 3719 | } |
| 3720 | http_silent_debug(__LINE__, s); |
| 3721 | /* OK, both state machines agree on a compatible state. |
| 3722 | * There are a few cases we're interested in : |
| 3723 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 3724 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 3725 | * directions, so let's simply disable both analysers. |
| 3726 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 3727 | * request. |
| 3728 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 3729 | * with server-close mode means we've completed one request and we |
| 3730 | * must re-initialize the server connection. |
| 3731 | */ |
| 3732 | |
| 3733 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 3734 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 3735 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 3736 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 3737 | s->req->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 3738 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3739 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3740 | s->rep->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 3741 | buffer_auto_close(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3742 | buffer_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3743 | } |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 3744 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 3745 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
| 3746 | (s->rep->flags & BF_SHUTW)) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3747 | s->rep->analysers = 0; |
| 3748 | buffer_auto_close(s->rep); |
| 3749 | buffer_auto_read(s->rep); |
| 3750 | s->req->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3751 | buffer_abort(s->req); |
| 3752 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3753 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3754 | buffer_ignore(s->req, s->req->l - s->req->send_max); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3755 | } |
| 3756 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 3757 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 3758 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 3759 | /* server-close: terminate this server connection and |
| 3760 | * reinitialize a fresh-new transaction. |
| 3761 | */ |
| 3762 | http_end_txn_clean_session(s); |
| 3763 | } |
| 3764 | |
| 3765 | http_silent_debug(__LINE__, s); |
| 3766 | return txn->req.msg_state != old_req_state || |
| 3767 | txn->rsp.msg_state != old_res_state; |
| 3768 | } |
| 3769 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3770 | /* This function is an analyser which forwards request body (including chunk |
| 3771 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 3772 | * zero byte. The only situation where it must not be called is when we're in |
| 3773 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 3774 | * remaining data and to resync after end of body. It expects the msg_state to |
| 3775 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 3776 | * read more data, or 1 once we can go on with next request or end the session. |
| 3777 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len |
| 3778 | * bytes of pending data + the headers if not already done (between som and sov). |
| 3779 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 3780 | */ |
| 3781 | int http_request_forward_body(struct session *s, struct buffer *req, int an_bit) |
| 3782 | { |
| 3783 | struct http_txn *txn = &s->txn; |
| 3784 | struct http_msg *msg = &s->txn.req; |
| 3785 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3786 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3787 | return 0; |
| 3788 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 3789 | if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
| 3790 | ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) { |
| 3791 | /* Output closed while we were sending data. We must abort. */ |
| 3792 | buffer_ignore(req, req->l - req->send_max); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3793 | buffer_auto_read(req); |
| 3794 | buffer_auto_close(req); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 3795 | req->analysers &= ~an_bit; |
| 3796 | return 1; |
| 3797 | } |
| 3798 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 3799 | buffer_dont_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3800 | |
| 3801 | /* Note that we don't have to send 100-continue back because we don't |
| 3802 | * need the data to complete our job, and it's up to the server to |
| 3803 | * decide whether to return 100, 417 or anything else in return of |
| 3804 | * an "Expect: 100-continue" header. |
| 3805 | */ |
| 3806 | |
| 3807 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 3808 | /* we have msg->col and msg->sov which both point to the first |
| 3809 | * byte of message body. msg->som still points to the beginning |
| 3810 | * of the message. We must save the body in req->lr because it |
| 3811 | * survives buffer re-alignments. |
| 3812 | */ |
| 3813 | req->lr = req->data + msg->sov; |
| 3814 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3815 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3816 | else { |
| 3817 | msg->msg_state = HTTP_MSG_DATA; |
| 3818 | } |
| 3819 | } |
| 3820 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3821 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3822 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 3823 | /* we may have some data pending */ |
| 3824 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 3825 | int bytes = msg->sov - msg->som; |
| 3826 | if (bytes < 0) /* sov may have wrapped at the end */ |
| 3827 | bytes += req->size; |
| 3828 | buffer_forward(req, bytes + msg->hdr_content_len); |
| 3829 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 3830 | msg->som = msg->sov; |
| 3831 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 3832 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 3833 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 3834 | /* must still forward */ |
| 3835 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3836 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 3837 | |
| 3838 | /* nothing left to forward */ |
| 3839 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3840 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3841 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 3842 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 3843 | } |
| 3844 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3845 | /* read the chunk size and assign it to ->hdr_content_len, then |
| 3846 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3847 | * TRAILERS state. |
| 3848 | */ |
| 3849 | int ret = http_parse_chunk_size(req, msg); |
| 3850 | |
| 3851 | if (!ret) |
| 3852 | goto missing_data; |
| 3853 | else if (ret < 0) |
| 3854 | goto return_bad_req; |
| 3855 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3856 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3857 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 3858 | /* we want the CRLF after the data */ |
| 3859 | int ret; |
| 3860 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 3861 | req->lr = req->w + req->send_max; |
| 3862 | if (req->lr >= req->data + req->size) |
| 3863 | req->lr -= req->size; |
| 3864 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3865 | ret = http_skip_chunk_crlf(req, msg); |
| 3866 | |
| 3867 | if (ret == 0) |
| 3868 | goto missing_data; |
| 3869 | else if (ret < 0) |
| 3870 | goto return_bad_req; |
| 3871 | /* we're in MSG_CHUNK_SIZE now */ |
| 3872 | } |
| 3873 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 3874 | int ret = http_forward_trailers(req, msg); |
| 3875 | |
| 3876 | if (ret == 0) |
| 3877 | goto missing_data; |
| 3878 | else if (ret < 0) |
| 3879 | goto return_bad_req; |
| 3880 | /* we're in HTTP_MSG_DONE now */ |
| 3881 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3882 | else { |
| 3883 | /* other states, DONE...TUNNEL */ |
| 3884 | if (http_resync_states(s)) { |
| 3885 | /* some state changes occurred, maybe the analyser |
| 3886 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 3887 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3888 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 3889 | goto return_bad_req; |
| 3890 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 3891 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3892 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3893 | } |
| 3894 | } |
| 3895 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3896 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3897 | /* stop waiting for data if the input is closed before the end */ |
| 3898 | if (req->flags & BF_SHUTR) |
| 3899 | goto return_bad_req; |
| 3900 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3901 | /* waiting for the last bits to leave the buffer */ |
| 3902 | if (req->flags & BF_SHUTW) |
| 3903 | goto return_bad_req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3904 | |
| 3905 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3906 | return 0; |
| 3907 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3908 | return_bad_req: /* let's centralize all bad requests */ |
| 3909 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3910 | txn->status = 400; |
| 3911 | /* 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^] | 3912 | 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] | 3913 | req->analysers = 0; |
| 3914 | s->fe->counters.failed_req++; |
| 3915 | if (s->listener->counters) |
| 3916 | s->listener->counters->failed_req++; |
| 3917 | |
| 3918 | if (!(s->flags & SN_ERR_MASK)) |
| 3919 | s->flags |= SN_ERR_PRXCOND; |
| 3920 | if (!(s->flags & SN_FINST_MASK)) |
| 3921 | s->flags |= SN_FINST_R; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3922 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3923 | return 0; |
| 3924 | } |
| 3925 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3926 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 3927 | * processing can continue on next analysers, or zero if it either needs more |
| 3928 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 3929 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 3930 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 3931 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3932 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3933 | 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] | 3934 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3935 | struct http_txn *txn = &s->txn; |
| 3936 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 3937 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 3938 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3939 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 3940 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3941 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3942 | 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] | 3943 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3944 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3945 | rep, |
| 3946 | rep->rex, rep->wex, |
| 3947 | rep->flags, |
| 3948 | rep->l, |
| 3949 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 3950 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3951 | /* |
| 3952 | * Now parse the partial (or complete) lines. |
| 3953 | * We will check the response syntax, and also join multi-line |
| 3954 | * headers. An index of all the lines will be elaborated while |
| 3955 | * parsing. |
| 3956 | * |
| 3957 | * For the parsing, we use a 28 states FSM. |
| 3958 | * |
| 3959 | * Here is the information we currently have : |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 3960 | * rep->data + msg->som = beginning of response |
| 3961 | * rep->data + msg->eoh = end of processed headers / start of current one |
| 3962 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3963 | * rep->lr = first non-visited byte |
| 3964 | * rep->r = end of data |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3965 | * Once we reach MSG_BODY, rep->sol = rep->data + msg->som |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3966 | */ |
| 3967 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 3968 | /* There's a protected area at the end of the buffer for rewriting |
| 3969 | * purposes. We don't want to start to parse the request if the |
| 3970 | * protected area is affected, because we may have to move processed |
| 3971 | * data later, which is much more complicated. |
| 3972 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 3973 | if (rep->l && msg->msg_state < HTTP_MSG_ERROR) { |
| 3974 | if (unlikely((rep->flags & BF_FULL) || |
| 3975 | rep->r < rep->lr || |
| 3976 | rep->r > rep->data + rep->size - global.tune.maxrewrite)) { |
| 3977 | if (rep->send_max) { |
| 3978 | /* some data has still not left the buffer, wake us once that's done */ |
| 3979 | buffer_dont_close(rep); |
| 3980 | rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 3981 | return 0; |
| 3982 | } |
| 3983 | if (rep->l <= rep->size - global.tune.maxrewrite) |
| 3984 | http_buffer_heavy_realign(rep, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 3985 | } |
| 3986 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 3987 | if (likely(rep->lr < rep->r)) |
| 3988 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 3989 | } |
| 3990 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3991 | /* 1: we might have to print this header in debug mode */ |
| 3992 | if (unlikely((global.mode & MODE_DEBUG) && |
| 3993 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3994 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3995 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3996 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3997 | sol = msg->sol; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 3998 | eol = sol + msg->sl.rq.l; |
| 3999 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4000 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4001 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4002 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4003 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4004 | while (cur_idx) { |
| 4005 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4006 | debug_hdr("srvhdr", s, sol, eol); |
| 4007 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4008 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4009 | } |
| 4010 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4011 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4012 | /* |
| 4013 | * Now we quickly check if we have found a full valid response. |
| 4014 | * If not so, we check the FD and buffer states before leaving. |
| 4015 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4016 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4017 | * responses are checked first. |
| 4018 | * |
| 4019 | * Depending on whether the client is still there or not, we |
| 4020 | * may send an error response back or not. Note that normally |
| 4021 | * we should only check for HTTP status there, and check I/O |
| 4022 | * errors somewhere else. |
| 4023 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4024 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4025 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4026 | /* Invalid response */ |
| 4027 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4028 | /* we detected a parsing error. We want to archive this response |
| 4029 | * in the dedicated proxy area for later troubleshooting. |
| 4030 | */ |
| 4031 | hdr_response_bad: |
| 4032 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
| 4033 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 4034 | |
| 4035 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4036 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4037 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4038 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 4039 | } |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4040 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4041 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4042 | rep->analysers = 0; |
| 4043 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4044 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4045 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 4046 | |
| 4047 | if (!(s->flags & SN_ERR_MASK)) |
| 4048 | s->flags |= SN_ERR_PRXCOND; |
| 4049 | if (!(s->flags & SN_FINST_MASK)) |
| 4050 | s->flags |= SN_FINST_H; |
| 4051 | |
| 4052 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4053 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4054 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4055 | /* too large response does not fit in buffer. */ |
| 4056 | else if (rep->flags & BF_FULL) { |
| 4057 | goto hdr_response_bad; |
| 4058 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4059 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4060 | /* read error */ |
| 4061 | else if (rep->flags & BF_READ_ERROR) { |
| 4062 | if (msg->err_pos >= 0) |
| 4063 | 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] | 4064 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4065 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4066 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4067 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4068 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR); |
| 4069 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4070 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4071 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4072 | rep->analysers = 0; |
| 4073 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4074 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4075 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4076 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4077 | if (!(s->flags & SN_ERR_MASK)) |
| 4078 | s->flags |= SN_ERR_SRVCL; |
| 4079 | if (!(s->flags & SN_FINST_MASK)) |
| 4080 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4081 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4082 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4083 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4084 | /* read timeout : return a 504 to the client. */ |
| 4085 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 4086 | if (msg->err_pos >= 0) |
| 4087 | 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] | 4088 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4089 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4090 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4091 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4092 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT); |
| 4093 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4094 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4095 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4096 | rep->analysers = 0; |
| 4097 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4098 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4099 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4100 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4101 | if (!(s->flags & SN_ERR_MASK)) |
| 4102 | s->flags |= SN_ERR_SRVTO; |
| 4103 | if (!(s->flags & SN_FINST_MASK)) |
| 4104 | s->flags |= SN_FINST_H; |
| 4105 | return 0; |
| 4106 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4107 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4108 | /* close from server */ |
| 4109 | else if (rep->flags & BF_SHUTR) { |
| 4110 | if (msg->err_pos >= 0) |
| 4111 | 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] | 4112 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4113 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4114 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4115 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4116 | health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE); |
| 4117 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4118 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4119 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4120 | rep->analysers = 0; |
| 4121 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4122 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4123 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4124 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4125 | if (!(s->flags & SN_ERR_MASK)) |
| 4126 | s->flags |= SN_ERR_SRVCL; |
| 4127 | if (!(s->flags & SN_FINST_MASK)) |
| 4128 | s->flags |= SN_FINST_H; |
| 4129 | return 0; |
| 4130 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4131 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4132 | /* write error to client (we don't send any message then) */ |
| 4133 | else if (rep->flags & BF_WRITE_ERROR) { |
| 4134 | if (msg->err_pos >= 0) |
| 4135 | 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] | 4136 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4137 | s->be->counters.failed_resp++; |
| 4138 | rep->analysers = 0; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4139 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4140 | |
| 4141 | if (!(s->flags & SN_ERR_MASK)) |
| 4142 | s->flags |= SN_ERR_CLICL; |
| 4143 | if (!(s->flags & SN_FINST_MASK)) |
| 4144 | s->flags |= SN_FINST_H; |
| 4145 | |
| 4146 | /* process_session() will take care of the error */ |
| 4147 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4148 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4149 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4150 | buffer_dont_close(rep); |
| 4151 | return 0; |
| 4152 | } |
| 4153 | |
| 4154 | /* More interesting part now : we know that we have a complete |
| 4155 | * response which at least looks like HTTP. We have an indicator |
| 4156 | * of each header's length, so we can parse them quickly. |
| 4157 | */ |
| 4158 | |
| 4159 | if (unlikely(msg->err_pos >= 0)) |
| 4160 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 4161 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4162 | /* |
| 4163 | * 1: get the status code |
| 4164 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4165 | n = msg->sol[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4166 | if (n < 1 || n > 5) |
| 4167 | n = 0; |
| 4168 | s->srv->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4169 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4170 | /* check if the response is HTTP/1.1 or above */ |
| 4171 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4172 | ((msg->sol[5] > '1') || |
| 4173 | ((msg->sol[5] == '1') && |
| 4174 | (msg->sol[7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4175 | txn->flags |= TX_RES_VER_11; |
| 4176 | |
| 4177 | /* "connection" has not been parsed yet */ |
| 4178 | txn->flags &= ~TX_CON_HDR_PARS; |
| 4179 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4180 | /* transfer length unknown*/ |
| 4181 | txn->flags &= ~TX_RES_XFER_LEN; |
| 4182 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4183 | 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] | 4184 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4185 | if (txn->status >= 100 && txn->status < 500) |
| 4186 | health_adjust(s->srv, HANA_STATUS_HTTP_OK); |
| 4187 | else |
| 4188 | health_adjust(s->srv, HANA_STATUS_HTTP_STS); |
| 4189 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4190 | /* |
| 4191 | * 2: check for cacheability. |
| 4192 | */ |
| 4193 | |
| 4194 | switch (txn->status) { |
| 4195 | case 200: |
| 4196 | case 203: |
| 4197 | case 206: |
| 4198 | case 300: |
| 4199 | case 301: |
| 4200 | case 410: |
| 4201 | /* RFC2616 @13.4: |
| 4202 | * "A response received with a status code of |
| 4203 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4204 | * by a cache (...) unless a cache-control |
| 4205 | * directive prohibits caching." |
| 4206 | * |
| 4207 | * RFC2616 @9.5: POST method : |
| 4208 | * "Responses to this method are not cacheable, |
| 4209 | * unless the response includes appropriate |
| 4210 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4211 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4212 | if (likely(txn->meth != HTTP_METH_POST) && |
| 4213 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 4214 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 4215 | break; |
| 4216 | default: |
| 4217 | break; |
| 4218 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4219 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4220 | /* |
| 4221 | * 3: we may need to capture headers |
| 4222 | */ |
| 4223 | s->logs.logwait &= ~LW_RESP; |
| 4224 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4225 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4226 | txn->rsp.cap, s->fe->rsp_cap); |
| 4227 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4228 | /* 4: determine the transfer-length. |
| 4229 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 4230 | * the presence of a message-body in a RESPONSE and its transfer length |
| 4231 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4232 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4233 | * All responses to the HEAD request method MUST NOT include a |
| 4234 | * message-body, even though the presence of entity-header fields |
| 4235 | * might lead one to believe they do. All 1xx (informational), 204 |
| 4236 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 4237 | * message-body. All other responses do include a message-body, |
| 4238 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4239 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4240 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 4241 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 4242 | * always terminated by the first empty line after the header fields, |
| 4243 | * regardless of the entity-header fields present in the message. |
| 4244 | * |
| 4245 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 4246 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 4247 | * transfer-length is defined by the use of this transfer-coding. |
| 4248 | * If a Transfer-Encoding header field is present and the "chunked" |
| 4249 | * transfer-coding is not present, the transfer-length is defined by |
| 4250 | * the sender closing the connection. |
| 4251 | * |
| 4252 | * 3. If a Content-Length header field is present, its decimal value in |
| 4253 | * OCTETs represents both the entity-length and the transfer-length. |
| 4254 | * If a message is received with both a Transfer-Encoding header |
| 4255 | * field and a Content-Length header field, the latter MUST be ignored. |
| 4256 | * |
| 4257 | * 4. If the message uses the media type "multipart/byteranges", and |
| 4258 | * the transfer-length is not otherwise specified, then this self- |
| 4259 | * delimiting media type defines the transfer-length. This media |
| 4260 | * type MUST NOT be used unless the sender knows that the recipient |
| 4261 | * can parse it; the presence in a request of a Range header with |
| 4262 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 4263 | * client can parse multipart/byteranges responses. |
| 4264 | * |
| 4265 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4266 | */ |
| 4267 | |
| 4268 | /* Skip parsing if no content length is possible. The response flags |
| 4269 | * 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] | 4270 | * 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] | 4271 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 4272 | */ |
| 4273 | if (txn->meth == HTTP_METH_HEAD || |
| 4274 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4275 | txn->status == 204 || txn->status == 304) { |
| 4276 | txn->flags |= TX_RES_XFER_LEN; |
| 4277 | goto skip_content_length; |
| 4278 | } |
| 4279 | |
| 4280 | if (txn->meth == HTTP_METH_CONNECT) |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4281 | goto skip_content_length; |
| 4282 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4283 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4284 | ctx.idx = 0; |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 4285 | while ((txn->flags & TX_RES_VER_11) && |
| 4286 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4287 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 4288 | txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 4289 | else if (txn->flags & TX_RES_TE_CHNK) { |
| 4290 | /* bad transfer-encoding (chunked followed by something else) */ |
| 4291 | use_close_only = 1; |
| 4292 | txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 4293 | break; |
| 4294 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4295 | } |
| 4296 | |
| 4297 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 4298 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4299 | while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only && |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4300 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 4301 | signed long long cl; |
| 4302 | |
| 4303 | if (!ctx.vlen) |
| 4304 | goto hdr_response_bad; |
| 4305 | |
| 4306 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 4307 | goto hdr_response_bad; /* parse failure */ |
| 4308 | |
| 4309 | if (cl < 0) |
| 4310 | goto hdr_response_bad; |
| 4311 | |
| 4312 | if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 4313 | goto hdr_response_bad; /* already specified, was different */ |
| 4314 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4315 | txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4316 | msg->hdr_content_len = cl; |
| 4317 | } |
| 4318 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4319 | /* FIXME: we should also implement the multipart/byterange method. |
| 4320 | * For now on, we resort to close mode in this case (unknown length). |
| 4321 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4322 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4323 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4324 | /* end of job, return OK */ |
| 4325 | rep->analysers &= ~an_bit; |
| 4326 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4327 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4328 | return 1; |
| 4329 | } |
| 4330 | |
| 4331 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4332 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 4333 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 4334 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4335 | */ |
| 4336 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 4337 | { |
| 4338 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4339 | struct http_msg *msg = &txn->rsp; |
| 4340 | struct proxy *cur_proxy; |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4341 | struct wordlist *wl; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4342 | int conn_ka = 0, conn_cl = 0; |
| 4343 | int must_close = 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4344 | int must_del_close = 0, must_keep = 0; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4345 | |
| 4346 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 4347 | now_ms, __FUNCTION__, |
| 4348 | t, |
| 4349 | rep, |
| 4350 | rep->rex, rep->wex, |
| 4351 | rep->flags, |
| 4352 | rep->l, |
| 4353 | rep->analysers); |
| 4354 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4355 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4356 | return 0; |
| 4357 | |
| 4358 | rep->analysers &= ~an_bit; |
| 4359 | rep->analyse_exp = TICK_ETERNITY; |
| 4360 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4361 | /* Now we have to check if we need to modify the Connection header. |
| 4362 | * This is more difficult on the response than it is on the request, |
| 4363 | * because we can have two different HTTP versions and we don't know |
| 4364 | * how the client will interprete a response. For instance, let's say |
| 4365 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 4366 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 4367 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 4368 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 4369 | * the lack of header as a keep-alive. Thus we will use two flags |
| 4370 | * indicating how a request MAY be understood by the client. In case |
| 4371 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 4372 | * ambiguous cases such as both close and keepalive are seen, then we |
| 4373 | * will fall back to explicit close. Note that we won't take risks with |
| 4374 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
| 4375 | */ |
| 4376 | |
| 4377 | if ((txn->meth != HTTP_METH_CONNECT) && |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 4378 | (txn->status >= 200) && !(txn->flags & TX_CON_HDR_PARS) && |
| 4379 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 4380 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4381 | int may_keep = 0, may_close = 0; /* how it may be understood */ |
| 4382 | struct hdr_ctx ctx; |
| 4383 | |
| 4384 | ctx.idx = 0; |
| 4385 | while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) { |
| 4386 | if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0) |
| 4387 | conn_cl = 1; |
| 4388 | else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0) |
| 4389 | conn_ka = 1; |
| 4390 | } |
| 4391 | |
| 4392 | if (conn_cl) { |
| 4393 | /* close header present */ |
| 4394 | may_close = 1; |
| 4395 | if (conn_ka) /* we have both close and keep-alive */ |
| 4396 | may_keep = 1; |
| 4397 | } |
| 4398 | else if (conn_ka) { |
| 4399 | /* keep-alive alone */ |
| 4400 | may_keep = 1; |
| 4401 | } |
| 4402 | else { |
| 4403 | /* no close nor keep-alive header */ |
| 4404 | if (txn->flags & TX_RES_VER_11) |
| 4405 | may_keep = 1; |
| 4406 | else |
| 4407 | may_close = 1; |
| 4408 | |
| 4409 | if (txn->flags & TX_REQ_VER_11) |
| 4410 | may_keep = 1; |
| 4411 | else |
| 4412 | may_close = 1; |
| 4413 | } |
| 4414 | |
| 4415 | /* let's update the transaction status to reflect any close. |
| 4416 | * Note that ambiguous cases with keep & close will also be |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4417 | * handled. We also explicitly state that we will close in |
| 4418 | * case of an ambiguous response having no content-length. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4419 | */ |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 4420 | if ((may_close && ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) && |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4421 | (may_keep || ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL))) || |
| 4422 | !(txn->flags & TX_RES_XFER_LEN)) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4423 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 4424 | |
| 4425 | /* Now we must adjust the response header : |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 4426 | * - set "close" if may_keep and (WANT_CLO | httpclose) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4427 | * - remove "close" if WANT_SCL and REQ_1.1 and may_close and (content-length or TE_CHNK) |
| 4428 | * - add "keep-alive" if WANT_SCL and REQ_1.0 and may_close and content-length |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4429 | */ |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 4430 | if (may_keep && |
| 4431 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO || |
| 4432 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4433 | must_close = 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4434 | else if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 4435 | may_close && (txn->flags & TX_RES_XFER_LEN)) { |
| 4436 | must_del_close = 1; |
| 4437 | if (!(txn->flags & TX_REQ_VER_11)) |
| 4438 | must_keep = 1; |
| 4439 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4440 | |
| 4441 | txn->flags |= TX_CON_HDR_PARS; |
| 4442 | } |
| 4443 | |
| 4444 | /* We might have to check for "Connection:" if the server |
| 4445 | * returns a connection status that is not compatible with |
| 4446 | * the client's or with the config. |
| 4447 | */ |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4448 | if ((txn->status >= 200) && (must_del_close|must_close) && (conn_cl|conn_ka)) { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4449 | char *cur_ptr, *cur_end, *cur_next; |
| 4450 | int cur_idx, old_idx, delta, val; |
| 4451 | int must_delete; |
| 4452 | struct hdr_idx_elem *cur_hdr; |
| 4453 | |
| 4454 | /* we just have to remove the headers if both sides are 1.0 */ |
| 4455 | must_delete = !(txn->flags & TX_REQ_VER_11) && !(txn->flags & TX_RES_VER_11); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4456 | |
| 4457 | /* same if we want to re-enable keep-alive on 1.1 */ |
| 4458 | must_delete |= must_del_close; |
| 4459 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4460 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4461 | |
| 4462 | for (old_idx = 0; (cur_idx = txn->hdr_idx.v[old_idx].next); old_idx = cur_idx) { |
| 4463 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4464 | cur_ptr = cur_next; |
| 4465 | cur_end = cur_ptr + cur_hdr->len; |
| 4466 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4467 | |
| 4468 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 4469 | if (!val) |
| 4470 | continue; |
| 4471 | |
| 4472 | /* 3 possibilities : |
| 4473 | * - we have already set "Connection: close" or we're in |
| 4474 | * HTTP/1.0, so we remove this line. |
| 4475 | * - we have not yet set "Connection: close", but this line |
| 4476 | * indicates close. We leave it untouched and set the flag. |
| 4477 | * - we have not yet set "Connection: close", and this line |
| 4478 | * indicates non-close. We replace it and set the flag. |
| 4479 | */ |
| 4480 | if (must_delete) { |
| 4481 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 4482 | http_msg_move_end(&txn->rsp, delta); |
| 4483 | cur_next += delta; |
| 4484 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4485 | txn->hdr_idx.used--; |
| 4486 | cur_hdr->len = 0; |
| 4487 | must_close = 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4488 | must_del_close = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4489 | } else { |
| 4490 | if (cur_end - cur_ptr - val != 5 || |
| 4491 | strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 4492 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 4493 | "close", 5); |
| 4494 | cur_next += delta; |
| 4495 | cur_hdr->len += delta; |
| 4496 | http_msg_move_end(&txn->rsp, delta); |
| 4497 | } |
| 4498 | must_delete = 1; |
| 4499 | must_close = 0; |
| 4500 | } |
| 4501 | } /* for loop */ |
| 4502 | } /* if must close keep-alive */ |
| 4503 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4504 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4505 | /* |
| 4506 | * 3: we will have to evaluate the filters. |
| 4507 | * As opposed to version 1.2, now they will be evaluated in the |
| 4508 | * filters order and not in the header order. This means that |
| 4509 | * each filter has to be validated among all headers. |
| 4510 | * |
| 4511 | * Filters are tried with ->be first, then with ->fe if it is |
| 4512 | * different from ->be. |
| 4513 | */ |
| 4514 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4515 | cur_proxy = t->be; |
| 4516 | while (1) { |
| 4517 | struct proxy *rule_set = cur_proxy; |
| 4518 | |
| 4519 | /* try headers filters */ |
| 4520 | if (rule_set->rsp_exp != NULL) { |
| 4521 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 4522 | return_bad_resp: |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4523 | if (t->srv) { |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4524 | t->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4525 | health_adjust(t->srv, HANA_STATUS_HTTP_RSP); |
| 4526 | } |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4527 | cur_proxy->counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4528 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 4529 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4530 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4531 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 4532 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4533 | if (!(t->flags & SN_ERR_MASK)) |
| 4534 | t->flags |= SN_ERR_PRXCOND; |
| 4535 | if (!(t->flags & SN_FINST_MASK)) |
| 4536 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4537 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4538 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4539 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4540 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4541 | /* has the response been denied ? */ |
| 4542 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 4543 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4544 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4545 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4546 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4547 | if (t->listener->counters) |
| 4548 | t->listener->counters->denied_resp++; |
| 4549 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4550 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 4551 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4552 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4553 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4554 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4555 | if (txn->status < 200) |
| 4556 | break; |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4557 | 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] | 4558 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 4559 | } |
| 4560 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4561 | /* check whether we're already working on the frontend */ |
| 4562 | if (cur_proxy == t->fe) |
| 4563 | break; |
| 4564 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4565 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4566 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4567 | /* |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4568 | * We may be facing a 1xx response (100 continue, 101 switching protocols), |
| 4569 | * in which case this is not the right response, and we're waiting for the |
| 4570 | * next one. Let's allow this response to go to the client and wait for the |
| 4571 | * next one. |
| 4572 | */ |
| 4573 | if (txn->status < 200) { |
| 4574 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4575 | buffer_forward(rep, rep->lr - msg->sol); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4576 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 4577 | txn->status = 0; |
| 4578 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 4579 | return 1; |
| 4580 | } |
| 4581 | |
| 4582 | /* we don't have any 1xx status code now */ |
| 4583 | |
| 4584 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4585 | * 4: check for server cookie. |
| 4586 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4587 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 4588 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4589 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4590 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4591 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4592 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4593 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4594 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4595 | 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] | 4596 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4597 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4598 | /* |
| 4599 | * 6: add server cookie in the response if needed |
| 4600 | */ |
| 4601 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4602 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4603 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4604 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4605 | /* the server is known, it's not the one the client requested, we have to |
| 4606 | * insert a set-cookie here, except if we want to insert only on POST |
| 4607 | * requests and this one isn't. Note that servers which don't have cookies |
| 4608 | * (eg: some backup servers) will return a full cookie removal request. |
| 4609 | */ |
| 4610 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 4611 | t->be->cookie_name, |
| 4612 | t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4613 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4614 | if (t->be->cookie_domain) |
| 4615 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4616 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4617 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4618 | trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4619 | goto return_bad_resp; |
| 4620 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4621 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4622 | /* Here, we will tell an eventual cache on the client side that we don't |
| 4623 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 4624 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 4625 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 4626 | */ |
| 4627 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4628 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4629 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4630 | |
| 4631 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4632 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4633 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4634 | } |
| 4635 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4636 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4637 | /* |
| 4638 | * 7: check if result will be cacheable with a cookie. |
| 4639 | * We'll block the response if security checks have caught |
| 4640 | * nasty things such as a cacheable cookie. |
| 4641 | */ |
| 4642 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 4643 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4644 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4645 | |
| 4646 | /* we're in presence of a cacheable response containing |
| 4647 | * a set-cookie header. We'll block it as requested by |
| 4648 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4649 | */ |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 4650 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4651 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4652 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4653 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4654 | if (t->listener->counters) |
| 4655 | t->listener->counters->denied_resp++; |
| 4656 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4657 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 4658 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 4659 | send_log(t->be, LOG_ALERT, |
| 4660 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 4661 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 4662 | goto return_srv_prx_502; |
| 4663 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4664 | |
| 4665 | /* |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4666 | * 8: add "Connection: close" if needed and not yet set. This is |
| 4667 | * only needed for 1.1 responses since we know there is no other |
| 4668 | * Connection header. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4669 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4670 | if (must_close && (txn->flags & TX_RES_VER_11)) { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4671 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4672 | "Connection: close", 17) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4673 | goto return_bad_resp; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4674 | must_close = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4675 | } |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4676 | else if (must_keep && !(txn->flags & TX_REQ_VER_11)) { |
| 4677 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 4678 | "Connection: keep-alive", 22) < 0)) |
| 4679 | goto return_bad_resp; |
| 4680 | must_keep = 0; |
| 4681 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4682 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4683 | if (txn->flags & TX_RES_XFER_LEN) |
| 4684 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4685 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4686 | /************************************************************* |
| 4687 | * OK, that's finished for the headers. We have done what we * |
| 4688 | * could. Let's switch to the DATA state. * |
| 4689 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4690 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4691 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4692 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4693 | /* if the user wants to log as soon as possible, without counting |
| 4694 | * bytes from the server, then this is the right moment. We have |
| 4695 | * to temporarily assign bytes_out to log what we currently have. |
| 4696 | */ |
| 4697 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 4698 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 4699 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 4700 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4701 | t->logs.bytes_out = 0; |
| 4702 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4703 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4704 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 4705 | * otherwise we would not let the client side wake up. |
| 4706 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4707 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4708 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4709 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4710 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4711 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4712 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4713 | /* This function is an analyser which forwards response body (including chunk |
| 4714 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4715 | * zero byte. The only situation where it must not be called is when we're in |
| 4716 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4717 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4718 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4719 | * read more data, or 1 once we can go on with next request or end the session. |
| 4720 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len |
| 4721 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4722 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4723 | */ |
| 4724 | int http_response_forward_body(struct session *s, struct buffer *res, int an_bit) |
| 4725 | { |
| 4726 | struct http_txn *txn = &s->txn; |
| 4727 | struct http_msg *msg = &s->txn.rsp; |
| 4728 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4729 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4730 | return 0; |
| 4731 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4732 | 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] | 4733 | ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4734 | !s->req->analysers) { |
| 4735 | /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */ |
| 4736 | buffer_ignore(res, res->l - res->send_max); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4737 | buffer_auto_read(res); |
| 4738 | buffer_auto_close(res); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4739 | res->analysers &= ~an_bit; |
| 4740 | return 1; |
| 4741 | } |
| 4742 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4743 | buffer_dont_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4744 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4745 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4746 | /* we have msg->col and msg->sov which both point to the first |
| 4747 | * byte of message body. msg->som still points to the beginning |
| 4748 | * of the message. We must save the body in req->lr because it |
| 4749 | * survives buffer re-alignments. |
| 4750 | */ |
| 4751 | res->lr = res->data + msg->sov; |
| 4752 | if (txn->flags & TX_RES_TE_CHNK) |
| 4753 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4754 | else { |
| 4755 | msg->msg_state = HTTP_MSG_DATA; |
| 4756 | } |
| 4757 | } |
| 4758 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4759 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4760 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4761 | /* we may have some data pending */ |
| 4762 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 4763 | int bytes = msg->sov - msg->som; |
| 4764 | if (bytes < 0) /* sov may have wrapped at the end */ |
| 4765 | bytes += res->size; |
| 4766 | buffer_forward(res, bytes + msg->hdr_content_len); |
| 4767 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 4768 | msg->som = msg->sov; |
| 4769 | } |
| 4770 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4771 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4772 | /* must still forward */ |
| 4773 | if (res->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4774 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4775 | |
| 4776 | /* nothing left to forward */ |
| 4777 | if (txn->flags & TX_RES_TE_CHNK) |
| 4778 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 4779 | else |
| 4780 | msg->msg_state = HTTP_MSG_DONE; |
| 4781 | } |
| 4782 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4783 | /* read the chunk size and assign it to ->hdr_content_len, then |
| 4784 | * set ->sov to point to the body and switch to DATA or TRAILERS state. |
| 4785 | */ |
| 4786 | int ret = http_parse_chunk_size(res, msg); |
| 4787 | |
| 4788 | if (!ret) |
| 4789 | goto missing_data; |
| 4790 | else if (ret < 0) |
| 4791 | goto return_bad_res; |
| 4792 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4793 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4794 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4795 | /* we want the CRLF after the data */ |
| 4796 | int ret; |
| 4797 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4798 | res->lr = res->w + res->send_max; |
| 4799 | if (res->lr >= res->data + res->size) |
| 4800 | res->lr -= res->size; |
| 4801 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4802 | ret = http_skip_chunk_crlf(res, msg); |
| 4803 | |
| 4804 | if (!ret) |
| 4805 | goto missing_data; |
| 4806 | else if (ret < 0) |
| 4807 | goto return_bad_res; |
| 4808 | /* we're in MSG_CHUNK_SIZE now */ |
| 4809 | } |
| 4810 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 4811 | int ret = http_forward_trailers(res, msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4812 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4813 | if (ret == 0) |
| 4814 | goto missing_data; |
| 4815 | else if (ret < 0) |
| 4816 | goto return_bad_res; |
| 4817 | /* we're in HTTP_MSG_DONE now */ |
| 4818 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4819 | else { |
| 4820 | /* other states, DONE...TUNNEL */ |
| 4821 | if (http_resync_states(s)) { |
| 4822 | http_silent_debug(__LINE__, s); |
| 4823 | /* some state changes occurred, maybe the analyser |
| 4824 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4825 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4826 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 4827 | goto return_bad_res; |
| 4828 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4829 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4830 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4831 | } |
| 4832 | } |
| 4833 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4834 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4835 | /* stop waiting for data if the input is closed before the end */ |
| 4836 | if (res->flags & BF_SHUTR) |
| 4837 | goto return_bad_res; |
| 4838 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4839 | if (!s->req->analysers) |
| 4840 | goto return_bad_res; |
| 4841 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4842 | /* forward the chunk size as well as any pending data */ |
| 4843 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 4844 | buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len); |
| 4845 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 4846 | msg->som = msg->sov; |
| 4847 | } |
| 4848 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4849 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4850 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4851 | return 0; |
| 4852 | |
| 4853 | return_bad_res: /* let's centralize all bad resuests */ |
| 4854 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 4855 | txn->status = 502; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame^] | 4856 | /* don't send any error message as we're in the body */ |
| 4857 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4858 | res->analysers = 0; |
| 4859 | s->be->counters.failed_resp++; |
| 4860 | if (s->srv) { |
| 4861 | s->srv->counters.failed_resp++; |
| 4862 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 4863 | } |
| 4864 | |
| 4865 | if (!(s->flags & SN_ERR_MASK)) |
| 4866 | s->flags |= SN_ERR_PRXCOND; |
| 4867 | if (!(s->flags & SN_FINST_MASK)) |
| 4868 | s->flags |= SN_FINST_R; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4869 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4870 | return 0; |
| 4871 | } |
| 4872 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4873 | /* Iterate the same filter through all request headers. |
| 4874 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4875 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4876 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4877 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4878 | 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] | 4879 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4880 | char term; |
| 4881 | char *cur_ptr, *cur_end, *cur_next; |
| 4882 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4883 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4884 | struct hdr_idx_elem *cur_hdr; |
| 4885 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 4886 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4887 | last_hdr = 0; |
| 4888 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4889 | cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4890 | old_idx = 0; |
| 4891 | |
| 4892 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4893 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4894 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4895 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4896 | (exp->action == ACT_ALLOW || |
| 4897 | exp->action == ACT_DENY || |
| 4898 | exp->action == ACT_TARPIT)) |
| 4899 | return 0; |
| 4900 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4901 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4902 | if (!cur_idx) |
| 4903 | break; |
| 4904 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4905 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4906 | cur_ptr = cur_next; |
| 4907 | cur_end = cur_ptr + cur_hdr->len; |
| 4908 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4909 | |
| 4910 | /* Now we have one header between cur_ptr and cur_end, |
| 4911 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4912 | */ |
| 4913 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4914 | /* The annoying part is that pattern matching needs |
| 4915 | * that we modify the contents to null-terminate all |
| 4916 | * strings before testing them. |
| 4917 | */ |
| 4918 | |
| 4919 | term = *cur_end; |
| 4920 | *cur_end = '\0'; |
| 4921 | |
| 4922 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4923 | switch (exp->action) { |
| 4924 | case ACT_SETBE: |
| 4925 | /* It is not possible to jump a second time. |
| 4926 | * FIXME: should we return an HTTP/500 here so that |
| 4927 | * the admin knows there's a problem ? |
| 4928 | */ |
| 4929 | if (t->be != t->fe) |
| 4930 | break; |
| 4931 | |
| 4932 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4933 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4934 | last_hdr = 1; |
| 4935 | break; |
| 4936 | |
| 4937 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4938 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4939 | last_hdr = 1; |
| 4940 | break; |
| 4941 | |
| 4942 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4943 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4944 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4945 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4946 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4947 | if (t->listener->counters) |
| 4948 | t->listener->counters->denied_resp++; |
| 4949 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4950 | break; |
| 4951 | |
| 4952 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4953 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4954 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4955 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4956 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4957 | if (t->listener->counters) |
| 4958 | t->listener->counters->denied_resp++; |
| 4959 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4960 | break; |
| 4961 | |
| 4962 | case ACT_REPLACE: |
| 4963 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4964 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4965 | /* FIXME: if the user adds a newline in the replacement, the |
| 4966 | * index will not be recalculated for now, and the new line |
| 4967 | * will not be counted as a new header. |
| 4968 | */ |
| 4969 | |
| 4970 | cur_end += delta; |
| 4971 | cur_next += delta; |
| 4972 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 4973 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4974 | break; |
| 4975 | |
| 4976 | case ACT_REMOVE: |
| 4977 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 4978 | cur_next += delta; |
| 4979 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 4980 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4981 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4982 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4983 | cur_hdr->len = 0; |
| 4984 | cur_end = NULL; /* null-term has been rewritten */ |
| 4985 | break; |
| 4986 | |
| 4987 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4988 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4989 | if (cur_end) |
| 4990 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4991 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4992 | /* keep the link from this header to next one in case of later |
| 4993 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4994 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4995 | old_idx = cur_idx; |
| 4996 | } |
| 4997 | return 0; |
| 4998 | } |
| 4999 | |
| 5000 | |
| 5001 | /* Apply the filter to the request line. |
| 5002 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5003 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5004 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5005 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5006 | */ |
| 5007 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5008 | { |
| 5009 | char term; |
| 5010 | char *cur_ptr, *cur_end; |
| 5011 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5012 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5013 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5014 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5015 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5016 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5017 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5018 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5019 | (exp->action == ACT_ALLOW || |
| 5020 | exp->action == ACT_DENY || |
| 5021 | exp->action == ACT_TARPIT)) |
| 5022 | return 0; |
| 5023 | else if (exp->action == ACT_REMOVE) |
| 5024 | return 0; |
| 5025 | |
| 5026 | done = 0; |
| 5027 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5028 | cur_ptr = txn->req.sol; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5029 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5030 | |
| 5031 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5032 | |
| 5033 | /* The annoying part is that pattern matching needs |
| 5034 | * that we modify the contents to null-terminate all |
| 5035 | * strings before testing them. |
| 5036 | */ |
| 5037 | |
| 5038 | term = *cur_end; |
| 5039 | *cur_end = '\0'; |
| 5040 | |
| 5041 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5042 | switch (exp->action) { |
| 5043 | case ACT_SETBE: |
| 5044 | /* It is not possible to jump a second time. |
| 5045 | * FIXME: should we return an HTTP/500 here so that |
| 5046 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5047 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5048 | if (t->be != t->fe) |
| 5049 | break; |
| 5050 | |
| 5051 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5052 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5053 | done = 1; |
| 5054 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5055 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5056 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5057 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5058 | done = 1; |
| 5059 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5060 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5061 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5062 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5063 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5064 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5065 | if (t->listener->counters) |
| 5066 | t->listener->counters->denied_resp++; |
| 5067 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5068 | done = 1; |
| 5069 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5070 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5071 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5072 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5073 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5074 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5075 | if (t->listener->counters) |
| 5076 | t->listener->counters->denied_resp++; |
| 5077 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5078 | done = 1; |
| 5079 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5080 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5081 | case ACT_REPLACE: |
| 5082 | *cur_end = term; /* restore the string terminator */ |
| 5083 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5084 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5085 | /* FIXME: if the user adds a newline in the replacement, the |
| 5086 | * index will not be recalculated for now, and the new line |
| 5087 | * will not be counted as a new header. |
| 5088 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5089 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5090 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5091 | cur_end += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5092 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5093 | HTTP_MSG_RQMETH, |
| 5094 | cur_ptr, cur_end + 1, |
| 5095 | NULL, NULL); |
| 5096 | if (unlikely(!cur_end)) |
| 5097 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5098 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5099 | /* we have a full request and we know that we have either a CR |
| 5100 | * or an LF at <ptr>. |
| 5101 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5102 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5103 | 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] | 5104 | /* there is no point trying this regex on headers */ |
| 5105 | return 1; |
| 5106 | } |
| 5107 | } |
| 5108 | *cur_end = term; /* restore the string terminator */ |
| 5109 | return done; |
| 5110 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5111 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5112 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5113 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5114 | /* |
| 5115 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 5116 | * 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] | 5117 | * unparsable request. Since it can manage the switch to another backend, it |
| 5118 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5119 | */ |
| 5120 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5121 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5122 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5123 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5124 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5125 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5126 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5127 | /* |
| 5128 | * The interleaving of transformations and verdicts |
| 5129 | * makes it difficult to decide to continue or stop |
| 5130 | * the evaluation. |
| 5131 | */ |
| 5132 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5133 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5134 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 5135 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 5136 | exp = exp->next; |
| 5137 | continue; |
| 5138 | } |
| 5139 | |
| 5140 | /* Apply the filter to the request line. */ |
| 5141 | ret = apply_filter_to_req_line(t, req, exp); |
| 5142 | if (unlikely(ret < 0)) |
| 5143 | return -1; |
| 5144 | |
| 5145 | if (likely(ret == 0)) { |
| 5146 | /* The filter did not match the request, it can be |
| 5147 | * iterated through all headers. |
| 5148 | */ |
| 5149 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5150 | } |
| 5151 | exp = exp->next; |
| 5152 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5153 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5154 | } |
| 5155 | |
| 5156 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5157 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5158 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5159 | * Try to retrieve the server associated to the appsession. |
| 5160 | * If the server is found, it's assigned to the session. |
| 5161 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5162 | 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] | 5163 | struct http_txn *txn = &t->txn; |
| 5164 | appsess *asession = NULL; |
| 5165 | char *sessid_temp = NULL; |
| 5166 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5167 | if (len > t->be->appsession_len) { |
| 5168 | len = t->be->appsession_len; |
| 5169 | } |
| 5170 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5171 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 5172 | /* request-learn option is enabled : store the sessid in the session for future use */ |
| 5173 | if (t->sessid != NULL) { |
| 5174 | /* free previously allocated memory as we don't need the session id found in the URL anymore */ |
| 5175 | pool_free2(apools.sessid, t->sessid); |
| 5176 | } |
| 5177 | |
| 5178 | if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 5179 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5180 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5181 | return; |
| 5182 | } |
| 5183 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5184 | memcpy(t->sessid, buf, len); |
| 5185 | t->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5186 | } |
| 5187 | |
| 5188 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 5189 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5190 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5191 | return; |
| 5192 | } |
| 5193 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5194 | memcpy(sessid_temp, buf, len); |
| 5195 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5196 | |
| 5197 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 5198 | /* free previously allocated memory */ |
| 5199 | pool_free2(apools.sessid, sessid_temp); |
| 5200 | |
| 5201 | if (asession != NULL) { |
| 5202 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5203 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 5204 | asession->request_count++; |
| 5205 | |
| 5206 | if (asession->serverid != NULL) { |
| 5207 | struct server *srv = t->be->srv; |
| 5208 | while (srv) { |
| 5209 | if (strcmp(srv->id, asession->serverid) == 0) { |
| 5210 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
| 5211 | /* we found the server and it's usable */ |
| 5212 | txn->flags &= ~TX_CK_MASK; |
| 5213 | txn->flags |= TX_CK_VALID; |
| 5214 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
| 5215 | t->srv = srv; |
| 5216 | break; |
| 5217 | } else { |
| 5218 | txn->flags &= ~TX_CK_MASK; |
| 5219 | txn->flags |= TX_CK_DOWN; |
| 5220 | } |
| 5221 | } |
| 5222 | srv = srv->next; |
| 5223 | } |
| 5224 | } |
| 5225 | } |
| 5226 | } |
| 5227 | |
| 5228 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5229 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 5230 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5231 | */ |
| 5232 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 5233 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5234 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5235 | char *p1, *p2, *p3, *p4, *p5; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5236 | char *del_colon, *del_cookie, *colon; |
| 5237 | int app_cookies; |
| 5238 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5239 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5240 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5241 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 5242 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5243 | * we start with the start line. |
| 5244 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 5245 | old_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5246 | cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5247 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5248 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5249 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5250 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5251 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5252 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5253 | cur_ptr = cur_next; |
| 5254 | cur_end = cur_ptr + cur_hdr->len; |
| 5255 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5256 | |
| 5257 | /* We have one full header between cur_ptr and cur_end, and the |
| 5258 | * next header starts at cur_next. We're only interested in |
| 5259 | * "Cookie:" headers. |
| 5260 | */ |
| 5261 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5262 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 5263 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5264 | old_idx = cur_idx; |
| 5265 | continue; |
| 5266 | } |
| 5267 | |
| 5268 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 5269 | * attributes whose name begin with a '$', and associate them with |
| 5270 | * the right cookie, if we want to delete this cookie. |
| 5271 | * So there are 3 cases for each cookie read : |
| 5272 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 5273 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 5274 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 5275 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 5276 | * "special" cookie. |
| 5277 | * At the end of loop, if a "special" cookie remains, we may have to |
| 5278 | * remove it. If no application cookie persists in the header, we |
| 5279 | * *MUST* delete it |
| 5280 | */ |
| 5281 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5282 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5283 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5284 | /* del_cookie == NULL => nothing to be deleted */ |
| 5285 | del_colon = del_cookie = NULL; |
| 5286 | app_cookies = 0; |
| 5287 | |
| 5288 | while (p1 < cur_end) { |
| 5289 | /* skip spaces and colons, but keep an eye on these ones */ |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5290 | resync_name: |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5291 | while (p1 < cur_end) { |
| 5292 | if (*p1 == ';' || *p1 == ',') |
| 5293 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5294 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5295 | break; |
| 5296 | p1++; |
| 5297 | } |
| 5298 | |
| 5299 | if (p1 == cur_end) |
| 5300 | break; |
| 5301 | |
| 5302 | /* p1 is at the beginning of the cookie name */ |
| 5303 | p2 = p1; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5304 | while (p2 < cur_end && *p2 != '=') { |
| 5305 | if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) { |
| 5306 | /* oops, the cookie name was truncated, resync */ |
| 5307 | p1 = p2; |
| 5308 | goto resync_name; |
| 5309 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5310 | p2++; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5311 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5312 | |
| 5313 | if (p2 == cur_end) |
| 5314 | break; |
| 5315 | |
| 5316 | p3 = p2 + 1; /* skips the '=' sign */ |
| 5317 | if (p3 == cur_end) |
| 5318 | break; |
| 5319 | |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5320 | /* parse the value, stripping leading and trailing spaces but keeping insiders. */ |
| 5321 | p5 = p4 = p3; |
| 5322 | while (p5 < cur_end && *p5 != ';' && *p5 != ',') { |
| 5323 | if (!isspace((unsigned char)*p5)) |
| 5324 | p4 = p5 + 1; |
| 5325 | p5++; |
| 5326 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5327 | |
| 5328 | /* here, we have the cookie name between p1 and p2, |
| 5329 | * and its value between p3 and p4. |
| 5330 | * we can process it : |
| 5331 | * |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5332 | * Cookie: NAME=VALUE ; |
| 5333 | * | || || |+-> p5 |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5334 | * | || || +--> p4 |
| 5335 | * | || |+-------> p3 |
| 5336 | * | || +--------> p2 |
| 5337 | * | |+------------> p1 |
| 5338 | * | +-------------> colon |
| 5339 | * +--------------------> cur_ptr |
| 5340 | */ |
| 5341 | |
| 5342 | if (*p1 == '$') { |
| 5343 | /* skip this one */ |
| 5344 | } |
| 5345 | else { |
| 5346 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5347 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5348 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5349 | (p4 - p1 >= t->fe->capture_namelen) && |
| 5350 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5351 | int log_len = p4 - p1; |
| 5352 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 5353 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5354 | Alert("HTTP logging : out of memory.\n"); |
| 5355 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5356 | if (log_len > t->fe->capture_len) |
| 5357 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5358 | memcpy(txn->cli_cookie, p1, log_len); |
| 5359 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5360 | } |
| 5361 | } |
| 5362 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5363 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5364 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5365 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5366 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5367 | char *delim; |
| 5368 | |
| 5369 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 5370 | * have the server ID betweek p3 and delim, and the original cookie between |
| 5371 | * delim+1 and p4. Otherwise, delim==p4 : |
| 5372 | * |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5373 | * Cookie: NAME=SRV~VALUE ; |
| 5374 | * | || || | |+-> p5 |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5375 | * | || || | +--> p4 |
| 5376 | * | || || +--------> delim |
| 5377 | * | || |+-----------> p3 |
| 5378 | * | || +------------> p2 |
| 5379 | * | |+----------------> p1 |
| 5380 | * | +-----------------> colon |
| 5381 | * +------------------------> cur_ptr |
| 5382 | */ |
| 5383 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5384 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5385 | for (delim = p3; delim < p4; delim++) |
| 5386 | if (*delim == COOKIE_DELIM) |
| 5387 | break; |
| 5388 | } |
| 5389 | else |
| 5390 | delim = p4; |
| 5391 | |
| 5392 | |
| 5393 | /* Here, we'll look for the first running server which supports the cookie. |
| 5394 | * This allows to share a same cookie between several servers, for example |
| 5395 | * to dedicate backup servers to specific servers only. |
| 5396 | * However, to prevent clients from sticking to cookie-less backup server |
| 5397 | * when they have incidentely learned an empty cookie, we simply ignore |
| 5398 | * empty cookies and mark them as invalid. |
| 5399 | */ |
| 5400 | if (delim == p3) |
| 5401 | srv = NULL; |
| 5402 | |
| 5403 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 5404 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 5405 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5406 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5407 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5408 | txn->flags &= ~TX_CK_MASK; |
| 5409 | txn->flags |= TX_CK_VALID; |
| 5410 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5411 | t->srv = srv; |
| 5412 | break; |
| 5413 | } else { |
| 5414 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5415 | txn->flags &= ~TX_CK_MASK; |
| 5416 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5417 | } |
| 5418 | } |
| 5419 | srv = srv->next; |
| 5420 | } |
| 5421 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5422 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5423 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5424 | txn->flags &= ~TX_CK_MASK; |
| 5425 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5426 | } |
| 5427 | |
| 5428 | /* depending on the cookie mode, we may have to either : |
| 5429 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 5430 | * the server never sees it ; |
| 5431 | * - remove the server id from the cookie value, and tag the cookie as an |
| 5432 | * application cookie so that it does not get accidentely removed later, |
| 5433 | * if we're in cookie prefix mode |
| 5434 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5435 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5436 | int delta; /* negative */ |
| 5437 | |
| 5438 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 5439 | p4 += delta; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5440 | p5 += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5441 | cur_end += delta; |
| 5442 | cur_next += delta; |
| 5443 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5444 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5445 | |
| 5446 | del_cookie = del_colon = NULL; |
| 5447 | app_cookies++; /* protect the header from deletion */ |
| 5448 | } |
| 5449 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5450 | (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5451 | del_cookie = p1; |
| 5452 | del_colon = colon; |
| 5453 | } |
| 5454 | } else { |
| 5455 | /* now we know that we must keep this cookie since it's |
| 5456 | * not ours. But if we wanted to delete our cookie |
| 5457 | * earlier, we cannot remove the complete header, but we |
| 5458 | * can remove the previous block itself. |
| 5459 | */ |
| 5460 | app_cookies++; |
| 5461 | |
| 5462 | if (del_cookie != NULL) { |
| 5463 | int delta; /* negative */ |
| 5464 | |
| 5465 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 5466 | p4 += delta; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5467 | p5 += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5468 | cur_end += delta; |
| 5469 | cur_next += delta; |
| 5470 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5471 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5472 | del_cookie = del_colon = NULL; |
| 5473 | } |
| 5474 | } |
| 5475 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5476 | if (t->be->appsession_name != NULL) { |
| 5477 | int cmp_len, value_len; |
| 5478 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5479 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5480 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 5481 | cmp_len = MIN(p4 - p1, t->be->appsession_name_len); |
| 5482 | value_begin = p1 + t->be->appsession_name_len; |
| 5483 | value_len = p4 - p1 - t->be->appsession_name_len; |
| 5484 | } else { |
| 5485 | cmp_len = p2 - p1; |
| 5486 | value_begin = p3; |
| 5487 | value_len = p4 - p3; |
| 5488 | } |
| 5489 | |
| 5490 | /* let's see if the cookie is our appcookie */ |
| 5491 | if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) { |
| 5492 | /* Cool... it's the right one */ |
| 5493 | manage_client_side_appsession(t, value_begin, value_len); |
| 5494 | } |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5495 | #if defined(DEBUG_HASH) |
| 5496 | Alert("manage_client_side_cookies\n"); |
| 5497 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 5498 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5499 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5500 | } |
| 5501 | |
| 5502 | /* we'll have to look for another cookie ... */ |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5503 | p1 = p5; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5504 | } /* while (p1 < cur_end) */ |
| 5505 | |
| 5506 | /* There's no more cookie on this line. |
| 5507 | * We may have marked the last one(s) for deletion. |
| 5508 | * We must do this now in two ways : |
| 5509 | * - if there is no app cookie, we simply delete the header ; |
| 5510 | * - if there are app cookies, we must delete the end of the |
| 5511 | * string properly, including the colon/semi-colon before |
| 5512 | * the cookie name. |
| 5513 | */ |
| 5514 | if (del_cookie != NULL) { |
| 5515 | int delta; |
| 5516 | if (app_cookies) { |
| 5517 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 5518 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5519 | cur_hdr->len += delta; |
| 5520 | } else { |
| 5521 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5522 | |
| 5523 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5524 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5525 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5526 | cur_hdr->len = 0; |
| 5527 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 5528 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5529 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5530 | } |
| 5531 | |
| 5532 | /* keep the link from this header to next one */ |
| 5533 | old_idx = cur_idx; |
| 5534 | } /* end of cookie processing on this header */ |
| 5535 | } |
| 5536 | |
| 5537 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5538 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 5539 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 5540 | */ |
| 5541 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5542 | { |
| 5543 | char term; |
| 5544 | char *cur_ptr, *cur_end, *cur_next; |
| 5545 | int cur_idx, old_idx, last_hdr; |
| 5546 | struct http_txn *txn = &t->txn; |
| 5547 | struct hdr_idx_elem *cur_hdr; |
| 5548 | int len, delta; |
| 5549 | |
| 5550 | last_hdr = 0; |
| 5551 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5552 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5553 | old_idx = 0; |
| 5554 | |
| 5555 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5556 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5557 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5558 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5559 | (exp->action == ACT_ALLOW || |
| 5560 | exp->action == ACT_DENY)) |
| 5561 | return 0; |
| 5562 | |
| 5563 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 5564 | if (!cur_idx) |
| 5565 | break; |
| 5566 | |
| 5567 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5568 | cur_ptr = cur_next; |
| 5569 | cur_end = cur_ptr + cur_hdr->len; |
| 5570 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5571 | |
| 5572 | /* Now we have one header between cur_ptr and cur_end, |
| 5573 | * and the next header starts at cur_next. |
| 5574 | */ |
| 5575 | |
| 5576 | /* The annoying part is that pattern matching needs |
| 5577 | * that we modify the contents to null-terminate all |
| 5578 | * strings before testing them. |
| 5579 | */ |
| 5580 | |
| 5581 | term = *cur_end; |
| 5582 | *cur_end = '\0'; |
| 5583 | |
| 5584 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5585 | switch (exp->action) { |
| 5586 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5587 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5588 | last_hdr = 1; |
| 5589 | break; |
| 5590 | |
| 5591 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5592 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5593 | last_hdr = 1; |
| 5594 | break; |
| 5595 | |
| 5596 | case ACT_REPLACE: |
| 5597 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5598 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5599 | /* FIXME: if the user adds a newline in the replacement, the |
| 5600 | * index will not be recalculated for now, and the new line |
| 5601 | * will not be counted as a new header. |
| 5602 | */ |
| 5603 | |
| 5604 | cur_end += delta; |
| 5605 | cur_next += delta; |
| 5606 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5607 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5608 | break; |
| 5609 | |
| 5610 | case ACT_REMOVE: |
| 5611 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5612 | cur_next += delta; |
| 5613 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5614 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5615 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5616 | txn->hdr_idx.used--; |
| 5617 | cur_hdr->len = 0; |
| 5618 | cur_end = NULL; /* null-term has been rewritten */ |
| 5619 | break; |
| 5620 | |
| 5621 | } |
| 5622 | } |
| 5623 | if (cur_end) |
| 5624 | *cur_end = term; /* restore the string terminator */ |
| 5625 | |
| 5626 | /* keep the link from this header to next one in case of later |
| 5627 | * removal of next header. |
| 5628 | */ |
| 5629 | old_idx = cur_idx; |
| 5630 | } |
| 5631 | return 0; |
| 5632 | } |
| 5633 | |
| 5634 | |
| 5635 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 5636 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5637 | * or -1 if a replacement resulted in an invalid status line. |
| 5638 | */ |
| 5639 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5640 | { |
| 5641 | char term; |
| 5642 | char *cur_ptr, *cur_end; |
| 5643 | int done; |
| 5644 | struct http_txn *txn = &t->txn; |
| 5645 | int len, delta; |
| 5646 | |
| 5647 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5648 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5649 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5650 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5651 | (exp->action == ACT_ALLOW || |
| 5652 | exp->action == ACT_DENY)) |
| 5653 | return 0; |
| 5654 | else if (exp->action == ACT_REMOVE) |
| 5655 | return 0; |
| 5656 | |
| 5657 | done = 0; |
| 5658 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5659 | cur_ptr = txn->rsp.sol; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5660 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 5661 | |
| 5662 | /* Now we have the status line between cur_ptr and cur_end */ |
| 5663 | |
| 5664 | /* The annoying part is that pattern matching needs |
| 5665 | * that we modify the contents to null-terminate all |
| 5666 | * strings before testing them. |
| 5667 | */ |
| 5668 | |
| 5669 | term = *cur_end; |
| 5670 | *cur_end = '\0'; |
| 5671 | |
| 5672 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5673 | switch (exp->action) { |
| 5674 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5675 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5676 | done = 1; |
| 5677 | break; |
| 5678 | |
| 5679 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5680 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5681 | done = 1; |
| 5682 | break; |
| 5683 | |
| 5684 | case ACT_REPLACE: |
| 5685 | *cur_end = term; /* restore the string terminator */ |
| 5686 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5687 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5688 | /* FIXME: if the user adds a newline in the replacement, the |
| 5689 | * index will not be recalculated for now, and the new line |
| 5690 | * will not be counted as a new header. |
| 5691 | */ |
| 5692 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5693 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5694 | cur_end += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5695 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 5696 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5697 | cur_ptr, cur_end + 1, |
| 5698 | NULL, NULL); |
| 5699 | if (unlikely(!cur_end)) |
| 5700 | return -1; |
| 5701 | |
| 5702 | /* we have a full respnse and we know that we have either a CR |
| 5703 | * or an LF at <ptr>. |
| 5704 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5705 | txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5706 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 5707 | /* there is no point trying this regex on headers */ |
| 5708 | return 1; |
| 5709 | } |
| 5710 | } |
| 5711 | *cur_end = term; /* restore the string terminator */ |
| 5712 | return done; |
| 5713 | } |
| 5714 | |
| 5715 | |
| 5716 | |
| 5717 | /* |
| 5718 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 5719 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 5720 | * unparsable response. |
| 5721 | */ |
| 5722 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5723 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5724 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5725 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5726 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5727 | int ret; |
| 5728 | |
| 5729 | /* |
| 5730 | * The interleaving of transformations and verdicts |
| 5731 | * makes it difficult to decide to continue or stop |
| 5732 | * the evaluation. |
| 5733 | */ |
| 5734 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5735 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5736 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 5737 | exp->action == ACT_PASS)) { |
| 5738 | exp = exp->next; |
| 5739 | continue; |
| 5740 | } |
| 5741 | |
| 5742 | /* Apply the filter to the status line. */ |
| 5743 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 5744 | if (unlikely(ret < 0)) |
| 5745 | return -1; |
| 5746 | |
| 5747 | if (likely(ret == 0)) { |
| 5748 | /* The filter did not match the response, it can be |
| 5749 | * iterated through all headers. |
| 5750 | */ |
| 5751 | apply_filter_to_resp_headers(t, rtr, exp); |
| 5752 | } |
| 5753 | exp = exp->next; |
| 5754 | } |
| 5755 | return 0; |
| 5756 | } |
| 5757 | |
| 5758 | |
| 5759 | |
| 5760 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5761 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 5762 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5763 | */ |
| 5764 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 5765 | { |
| 5766 | struct http_txn *txn = &t->txn; |
| 5767 | char *p1, *p2, *p3, *p4; |
| 5768 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5769 | char *cur_ptr, *cur_end, *cur_next; |
| 5770 | int cur_idx, old_idx, delta; |
| 5771 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5772 | /* Iterate through the headers. |
| 5773 | * we start with the start line. |
| 5774 | */ |
| 5775 | old_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5776 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5777 | |
| 5778 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 5779 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5780 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5781 | |
| 5782 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5783 | cur_ptr = cur_next; |
| 5784 | cur_end = cur_ptr + cur_hdr->len; |
| 5785 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5786 | |
| 5787 | /* We have one full header between cur_ptr and cur_end, and the |
| 5788 | * next header starts at cur_next. We're only interested in |
| 5789 | * "Cookie:" headers. |
| 5790 | */ |
| 5791 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5792 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 5793 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5794 | old_idx = cur_idx; |
| 5795 | continue; |
| 5796 | } |
| 5797 | |
| 5798 | /* OK, right now we know we have a set-cookie at cur_ptr */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5799 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5800 | |
| 5801 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5802 | /* maybe we only wanted to see if there was a set-cookie. Note that |
| 5803 | * the cookie capture is declared in the fronend. |
| 5804 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5805 | if (t->be->cookie_name == NULL && |
| 5806 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5807 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5808 | return; |
| 5809 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5810 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5811 | |
| 5812 | while (p1 < cur_end) { /* in fact, we'll break after the first cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5813 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 5814 | break; |
| 5815 | |
| 5816 | /* p1 is at the beginning of the cookie name */ |
| 5817 | p2 = p1; |
| 5818 | |
| 5819 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 5820 | p2++; |
| 5821 | |
| 5822 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 5823 | break; |
| 5824 | |
| 5825 | p3 = p2 + 1; /* skip the '=' sign */ |
| 5826 | if (p3 == cur_end) |
| 5827 | break; |
| 5828 | |
| 5829 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5830 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5831 | p4++; |
| 5832 | |
| 5833 | /* here, we have the cookie name between p1 and p2, |
| 5834 | * and its value between p3 and p4. |
| 5835 | * we can process it. |
| 5836 | */ |
| 5837 | |
| 5838 | /* first, let's see if we want to capture it */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5839 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5840 | txn->srv_cookie == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5841 | (p4 - p1 >= t->fe->capture_namelen) && |
| 5842 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5843 | int log_len = p4 - p1; |
| 5844 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 5845 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5846 | Alert("HTTP logging : out of memory.\n"); |
| 5847 | } |
| 5848 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5849 | if (log_len > t->fe->capture_len) |
| 5850 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5851 | memcpy(txn->srv_cookie, p1, log_len); |
| 5852 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5853 | } |
| 5854 | |
| 5855 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5856 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5857 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5858 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5859 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5860 | |
| 5861 | /* If the cookie is in insert mode on a known server, we'll delete |
| 5862 | * this occurrence because we'll insert another one later. |
| 5863 | * We'll delete it too if the "indirect" option is set and we're in |
| 5864 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5865 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 5866 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5867 | /* this header must be deleted */ |
| 5868 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5869 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5870 | txn->hdr_idx.used--; |
| 5871 | cur_hdr->len = 0; |
| 5872 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5873 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5874 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5875 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5876 | } |
| 5877 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5878 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5879 | /* replace bytes p3->p4 with the cookie name associated |
| 5880 | * with this server since we know it. |
| 5881 | */ |
| 5882 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 5883 | cur_hdr->len += delta; |
| 5884 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5885 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5886 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5887 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5888 | } |
| 5889 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5890 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5891 | /* insert the cookie name associated with this server |
| 5892 | * before existing cookie, and insert a delimitor between them.. |
| 5893 | */ |
| 5894 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 5895 | cur_hdr->len += delta; |
| 5896 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5897 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5898 | |
| 5899 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5900 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5901 | } |
| 5902 | } |
| 5903 | /* next, let's see if the cookie is our appcookie */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5904 | else if (t->be->appsession_name != NULL) { |
| 5905 | int cmp_len, value_len; |
| 5906 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5907 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5908 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 5909 | cmp_len = MIN(p4 - p1, t->be->appsession_name_len); |
| 5910 | value_begin = p1 + t->be->appsession_name_len; |
| 5911 | value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len); |
| 5912 | } else { |
| 5913 | cmp_len = p2 - p1; |
| 5914 | value_begin = p3; |
| 5915 | value_len = MIN(t->be->appsession_len, p4 - p3); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5916 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5917 | |
| 5918 | if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) { |
| 5919 | /* Cool... it's the right one */ |
| 5920 | if (t->sessid != NULL) { |
| 5921 | /* free previously allocated memory as we don't need it anymore */ |
| 5922 | pool_free2(apools.sessid, t->sessid); |
| 5923 | } |
| 5924 | /* Store the sessid in the session for future use */ |
| 5925 | if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 5926 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5927 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5928 | return; |
| 5929 | } |
| 5930 | memcpy(t->sessid, value_begin, value_len); |
| 5931 | t->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5932 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5933 | } /* end if ((t->be->appsession_name != NULL) ... */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5934 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 5935 | } /* we're now at the end of the cookie value */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5936 | /* keep the link from this header to next one */ |
| 5937 | old_idx = cur_idx; |
| 5938 | } /* end of cookie processing on this header */ |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5939 | |
| 5940 | if (t->sessid != NULL) { |
| 5941 | appsess *asession = NULL; |
| 5942 | /* only do insert, if lookup fails */ |
| 5943 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid); |
| 5944 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 5945 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5946 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 5947 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 5948 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 5949 | return; |
| 5950 | } |
| 5951 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 5952 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5953 | 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] | 5954 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5955 | return; |
| 5956 | } |
| 5957 | memcpy(asession->sessid, t->sessid, t->be->appsession_len); |
| 5958 | asession->sessid[t->be->appsession_len] = 0; |
| 5959 | |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 5960 | server_id_len = strlen(t->srv->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5961 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
| 5962 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5963 | 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] | 5964 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5965 | return; |
| 5966 | } |
| 5967 | asession->serverid[0] = '\0'; |
| 5968 | memcpy(asession->serverid, t->srv->id, server_id_len); |
| 5969 | |
| 5970 | asession->request_count = 0; |
| 5971 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 5972 | } |
| 5973 | |
| 5974 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5975 | asession->request_count++; |
| 5976 | } |
| 5977 | |
| 5978 | #if defined(DEBUG_HASH) |
| 5979 | Alert("manage_server_side_cookies\n"); |
| 5980 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 5981 | #endif |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5982 | } |
| 5983 | |
| 5984 | |
| 5985 | |
| 5986 | /* |
| 5987 | * Check if response is cacheable or not. Updates t->flags. |
| 5988 | */ |
| 5989 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 5990 | { |
| 5991 | struct http_txn *txn = &t->txn; |
| 5992 | char *p1, *p2; |
| 5993 | |
| 5994 | char *cur_ptr, *cur_end, *cur_next; |
| 5995 | int cur_idx; |
| 5996 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 5997 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5998 | return; |
| 5999 | |
| 6000 | /* Iterate through the headers. |
| 6001 | * we start with the start line. |
| 6002 | */ |
| 6003 | cur_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6004 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6005 | |
| 6006 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6007 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6008 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6009 | |
| 6010 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6011 | cur_ptr = cur_next; |
| 6012 | cur_end = cur_ptr + cur_hdr->len; |
| 6013 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6014 | |
| 6015 | /* We have one full header between cur_ptr and cur_end, and the |
| 6016 | * next header starts at cur_next. We're only interested in |
| 6017 | * "Cookie:" headers. |
| 6018 | */ |
| 6019 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6020 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 6021 | if (val) { |
| 6022 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 6023 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 6024 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 6025 | return; |
| 6026 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6027 | } |
| 6028 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6029 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 6030 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6031 | continue; |
| 6032 | |
| 6033 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 6034 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6035 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6036 | |
| 6037 | if (p1 >= cur_end) /* no more info */ |
| 6038 | continue; |
| 6039 | |
| 6040 | /* p1 is at the beginning of the value */ |
| 6041 | p2 = p1; |
| 6042 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 6043 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6044 | p2++; |
| 6045 | |
| 6046 | /* we have a complete value between p1 and p2 */ |
| 6047 | if (p2 < cur_end && *p2 == '=') { |
| 6048 | /* we have something of the form no-cache="set-cookie" */ |
| 6049 | if ((cur_end - p1 >= 21) && |
| 6050 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 6051 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6052 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6053 | continue; |
| 6054 | } |
| 6055 | |
| 6056 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 6057 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 6058 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 6059 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 6060 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6061 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6062 | return; |
| 6063 | } |
| 6064 | |
| 6065 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6066 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6067 | continue; |
| 6068 | } |
| 6069 | } |
| 6070 | } |
| 6071 | |
| 6072 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6073 | /* |
| 6074 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 6075 | * If the server is found, it's assigned to the session. |
| 6076 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6077 | 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] | 6078 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6079 | char *end_params, *first_param, *cur_param, *next_param; |
| 6080 | char separator; |
| 6081 | int value_len; |
| 6082 | |
| 6083 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6084 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6085 | if (t->be->appsession_name == NULL || |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6086 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6087 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6088 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6089 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6090 | first_param = NULL; |
| 6091 | switch (mode) { |
| 6092 | case PR_O2_AS_M_PP: |
| 6093 | first_param = memchr(begin, ';', len); |
| 6094 | break; |
| 6095 | case PR_O2_AS_M_QS: |
| 6096 | first_param = memchr(begin, '?', len); |
| 6097 | break; |
| 6098 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6099 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6100 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6101 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6102 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6103 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6104 | switch (mode) { |
| 6105 | case PR_O2_AS_M_PP: |
| 6106 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 6107 | end_params = (char *) begin + len; |
| 6108 | } |
| 6109 | separator = ';'; |
| 6110 | break; |
| 6111 | case PR_O2_AS_M_QS: |
| 6112 | end_params = (char *) begin + len; |
| 6113 | separator = '&'; |
| 6114 | break; |
| 6115 | default: |
| 6116 | /* unknown mode, shouldn't happen */ |
| 6117 | return; |
| 6118 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6119 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6120 | cur_param = next_param = end_params; |
| 6121 | while (cur_param > first_param) { |
| 6122 | cur_param--; |
| 6123 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 6124 | /* let's see if this is the appsession parameter */ |
| 6125 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 6126 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 6127 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 6128 | /* Cool... it's the right one */ |
| 6129 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 6130 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 6131 | if (value_len > 0) { |
| 6132 | manage_client_side_appsession(t, cur_param, value_len); |
| 6133 | } |
| 6134 | break; |
| 6135 | } |
| 6136 | next_param = cur_param; |
| 6137 | } |
| 6138 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6139 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6140 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 6141 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6142 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6143 | } |
| 6144 | |
| 6145 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6146 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6147 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 6148 | * for the current backend, and if an authorization has been passed and is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6149 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6150 | * It is assumed that the request is either a HEAD or GET and that the |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6151 | * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or |
Willy Tarreau | b0c9bc4 | 2009-10-04 15:56:38 +0200 | [diff] [blame] | 6152 | * the stats I/O handler will be registered to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6153 | * |
| 6154 | * Returns 1 if the session's state changes, otherwise 0. |
| 6155 | */ |
| 6156 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 6157 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6158 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6159 | struct uri_auth *uri_auth = backend->uri_auth; |
| 6160 | struct user_auth *user; |
| 6161 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6162 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6163 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6164 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 6165 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6166 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6167 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6168 | return 0; |
| 6169 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6170 | h = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6171 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6172 | /* the URI is in h */ |
| 6173 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6174 | return 0; |
| 6175 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6176 | h += uri_auth->uri_len; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6177 | 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] | 6178 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6179 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6180 | break; |
| 6181 | } |
| 6182 | h++; |
| 6183 | } |
| 6184 | |
| 6185 | if (uri_auth->refresh) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6186 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 6187 | 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] | 6188 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6189 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6190 | break; |
| 6191 | } |
| 6192 | h++; |
| 6193 | } |
| 6194 | } |
| 6195 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6196 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 6197 | 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] | 6198 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6199 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 6200 | break; |
| 6201 | } |
| 6202 | h++; |
| 6203 | } |
| 6204 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6205 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 6206 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6207 | /* we are in front of a interceptable URI. Let's check |
| 6208 | * if there's an authentication and if it's valid. |
| 6209 | */ |
| 6210 | user = uri_auth->users; |
| 6211 | if (!user) { |
| 6212 | /* no user auth required, it's OK */ |
| 6213 | authenticated = 1; |
| 6214 | } else { |
| 6215 | authenticated = 0; |
| 6216 | |
| 6217 | /* a user list is defined, we have to check. |
| 6218 | * skip 21 chars for "Authorization: Basic ". |
| 6219 | */ |
| 6220 | |
| 6221 | /* FIXME: this should move to an earlier place */ |
| 6222 | cur_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6223 | h = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6224 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6225 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6226 | if (len > 14 && |
| 6227 | !strncasecmp("Authorization:", h, 14)) { |
Krzysztof Piotr Oledzki | 6f61b21 | 2009-10-04 23:34:15 +0200 | [diff] [blame] | 6228 | chunk_initlen(&txn->auth_hdr, h, 0, len); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6229 | break; |
| 6230 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6231 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6232 | } |
| 6233 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6234 | if (txn->auth_hdr.len < 21 || |
| 6235 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6236 | user = NULL; |
| 6237 | |
| 6238 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6239 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 6240 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6241 | user->user_pwd, user->user_len)) { |
| 6242 | authenticated = 1; |
| 6243 | break; |
| 6244 | } |
| 6245 | user = user->next; |
| 6246 | } |
| 6247 | } |
| 6248 | |
| 6249 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 6250 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6251 | |
| 6252 | /* no need to go further */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 6253 | sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
| 6254 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6255 | txn->status = 401; |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 6256 | stream_int_retnclose(t->req->prod, &msg); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 6257 | t->req->analysers = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6258 | if (!(t->flags & SN_ERR_MASK)) |
| 6259 | t->flags |= SN_ERR_PRXCOND; |
| 6260 | if (!(t->flags & SN_FINST_MASK)) |
| 6261 | t->flags |= SN_FINST_R; |
| 6262 | return 1; |
| 6263 | } |
| 6264 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6265 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6266 | * data. |
| 6267 | */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 6268 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6269 | t->data_source = DATA_SRC_STATS; |
| 6270 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 6271 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b0c9bc4 | 2009-10-04 15:56:38 +0200 | [diff] [blame] | 6272 | stream_int_register_handler(t->rep->prod, http_stats_io_handler); |
| 6273 | t->rep->prod->private = t; |
| 6274 | t->rep->prod->st0 = t->rep->prod->st1 = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6275 | return 1; |
| 6276 | } |
| 6277 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6278 | /* |
| 6279 | * 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] | 6280 | * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not |
| 6281 | * assume that msg->sol = buf->data + msg->som. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6282 | */ |
| 6283 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
| 6284 | struct buffer *buf, struct http_msg *msg, |
| 6285 | struct proxy *other_end) |
| 6286 | { |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6287 | es->len = buf->r - (buf->data + msg->som); |
| 6288 | 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] | 6289 | if (msg->err_pos >= 0) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6290 | es->pos = msg->err_pos - msg->som; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6291 | else |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6292 | es->pos = buf->lr - (buf->data + msg->som); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6293 | es->when = date; // user-visible date |
| 6294 | es->sid = s->uniq_id; |
| 6295 | es->srv = s->srv; |
| 6296 | es->oe = other_end; |
| 6297 | es->src = s->cli_addr; |
| 6298 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6299 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6300 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6301 | * Print a debug line with a header |
| 6302 | */ |
| 6303 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 6304 | { |
| 6305 | int len, max; |
| 6306 | 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] | 6307 | 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] | 6308 | max = end - start; |
| 6309 | UBOUND(max, sizeof(trash) - len - 1); |
| 6310 | len += strlcpy2(trash + len, start, max + 1); |
| 6311 | trash[len++] = '\n'; |
| 6312 | write(1, trash, len); |
| 6313 | } |
| 6314 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6315 | /* |
| 6316 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 6317 | * the required fields are properly allocated and that we only need to (re)init |
| 6318 | * them. This should be used before processing any new request. |
| 6319 | */ |
| 6320 | void http_init_txn(struct session *s) |
| 6321 | { |
| 6322 | struct http_txn *txn = &s->txn; |
| 6323 | struct proxy *fe = s->fe; |
| 6324 | |
| 6325 | txn->flags = 0; |
| 6326 | txn->status = -1; |
| 6327 | |
| 6328 | txn->req.sol = txn->req.eol = NULL; |
| 6329 | txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
| 6330 | txn->rsp.sol = txn->rsp.eol = NULL; |
| 6331 | txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 6332 | txn->req.hdr_content_len = 0LL; |
| 6333 | txn->rsp.hdr_content_len = 0LL; |
| 6334 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 6335 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 6336 | chunk_reset(&txn->auth_hdr); |
| 6337 | |
| 6338 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 6339 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 6340 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 6341 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6342 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6343 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 6344 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6345 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6346 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 6347 | |
| 6348 | if (txn->hdr_idx.v) |
| 6349 | hdr_idx_init(&txn->hdr_idx); |
| 6350 | } |
| 6351 | |
| 6352 | /* to be used at the end of a transaction */ |
| 6353 | void http_end_txn(struct session *s) |
| 6354 | { |
| 6355 | struct http_txn *txn = &s->txn; |
| 6356 | |
| 6357 | /* these ones will have been dynamically allocated */ |
| 6358 | pool_free2(pool2_requri, txn->uri); |
| 6359 | pool_free2(pool2_capture, txn->cli_cookie); |
| 6360 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | 762a236 | 2010-01-09 13:57:26 +0100 | [diff] [blame] | 6361 | pool_free2(apools.sessid, s->sessid); |
| 6362 | s->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6363 | txn->uri = NULL; |
| 6364 | txn->srv_cookie = NULL; |
| 6365 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6366 | |
| 6367 | if (txn->req.cap) { |
| 6368 | struct cap_hdr *h; |
| 6369 | for (h = s->fe->req_cap; h; h = h->next) |
| 6370 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 6371 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 6372 | } |
| 6373 | |
| 6374 | if (txn->rsp.cap) { |
| 6375 | struct cap_hdr *h; |
| 6376 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 6377 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 6378 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 6379 | } |
| 6380 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6381 | } |
| 6382 | |
| 6383 | /* to be used at the end of a transaction to prepare a new one */ |
| 6384 | void http_reset_txn(struct session *s) |
| 6385 | { |
| 6386 | http_end_txn(s); |
| 6387 | http_init_txn(s); |
| 6388 | |
| 6389 | s->be = s->fe; |
| 6390 | s->req->analysers = s->listener->analysers; |
| 6391 | s->logs.logwait = s->fe->to_log; |
| 6392 | s->srv = s->prev_srv = s->srv_conn = NULL; |
| 6393 | s->pend_pos = NULL; |
| 6394 | s->conn_retries = s->be->conn_retries; |
| 6395 | |
| 6396 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
| 6397 | |
| 6398 | s->req->rto = s->fe->timeout.client; |
| 6399 | s->req->wto = s->be->timeout.server; |
| 6400 | s->req->cto = s->be->timeout.connect; |
| 6401 | |
| 6402 | s->rep->rto = s->be->timeout.server; |
| 6403 | s->rep->wto = s->fe->timeout.client; |
| 6404 | s->rep->cto = TICK_ETERNITY; |
| 6405 | |
| 6406 | s->req->rex = TICK_ETERNITY; |
| 6407 | s->req->wex = TICK_ETERNITY; |
| 6408 | s->req->analyse_exp = TICK_ETERNITY; |
| 6409 | s->rep->rex = TICK_ETERNITY; |
| 6410 | s->rep->wex = TICK_ETERNITY; |
| 6411 | s->rep->analyse_exp = TICK_ETERNITY; |
| 6412 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6413 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6414 | /************************************************************************/ |
| 6415 | /* The code below is dedicated to ACL parsing and matching */ |
| 6416 | /************************************************************************/ |
| 6417 | |
| 6418 | |
| 6419 | |
| 6420 | |
| 6421 | /* 1. Check on METHOD |
| 6422 | * We use the pre-parsed method if it is known, and store its number as an |
| 6423 | * integer. If it is unknown, we use the pointer and the length. |
| 6424 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6425 | 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] | 6426 | { |
| 6427 | int len, meth; |
| 6428 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6429 | len = strlen(*text); |
| 6430 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6431 | |
| 6432 | pattern->val.i = meth; |
| 6433 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6434 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6435 | if (!pattern->ptr.str) |
| 6436 | return 0; |
| 6437 | pattern->len = len; |
| 6438 | } |
| 6439 | return 1; |
| 6440 | } |
| 6441 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6442 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6443 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6444 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6445 | { |
| 6446 | int meth; |
| 6447 | struct http_txn *txn = l7; |
| 6448 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6449 | if (!txn) |
| 6450 | return 0; |
| 6451 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6452 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6453 | return 0; |
| 6454 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6455 | meth = txn->meth; |
| 6456 | test->i = meth; |
| 6457 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6458 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6459 | /* ensure the indexes are not affected */ |
| 6460 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6461 | test->len = txn->req.sl.rq.m_l; |
| 6462 | test->ptr = txn->req.sol; |
| 6463 | } |
| 6464 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6465 | return 1; |
| 6466 | } |
| 6467 | |
| 6468 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 6469 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6470 | int icase; |
| 6471 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6472 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6473 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6474 | |
| 6475 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6476 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6477 | |
| 6478 | /* Other method, we must compare the strings */ |
| 6479 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6480 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6481 | |
| 6482 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 6483 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 6484 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6485 | return ACL_PAT_FAIL; |
| 6486 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6487 | } |
| 6488 | |
| 6489 | /* 2. Check on Request/Status Version |
| 6490 | * We simply compare strings here. |
| 6491 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6492 | 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] | 6493 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6494 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6495 | if (!pattern->ptr.str) |
| 6496 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6497 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6498 | return 1; |
| 6499 | } |
| 6500 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6501 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6502 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6503 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6504 | { |
| 6505 | struct http_txn *txn = l7; |
| 6506 | char *ptr; |
| 6507 | int len; |
| 6508 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6509 | if (!txn) |
| 6510 | return 0; |
| 6511 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6512 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6513 | return 0; |
| 6514 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6515 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6516 | ptr = txn->req.sol + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6517 | |
| 6518 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6519 | if (len <= 0) |
| 6520 | return 0; |
| 6521 | |
| 6522 | test->ptr = ptr; |
| 6523 | test->len = len; |
| 6524 | |
| 6525 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6526 | return 1; |
| 6527 | } |
| 6528 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6529 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6530 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6531 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6532 | { |
| 6533 | struct http_txn *txn = l7; |
| 6534 | char *ptr; |
| 6535 | int len; |
| 6536 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6537 | if (!txn) |
| 6538 | return 0; |
| 6539 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6540 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6541 | return 0; |
| 6542 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6543 | len = txn->rsp.sl.st.v_l; |
| 6544 | ptr = txn->rsp.sol; |
| 6545 | |
| 6546 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6547 | if (len <= 0) |
| 6548 | return 0; |
| 6549 | |
| 6550 | test->ptr = ptr; |
| 6551 | test->len = len; |
| 6552 | |
| 6553 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6554 | return 1; |
| 6555 | } |
| 6556 | |
| 6557 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6558 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6559 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6560 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6561 | { |
| 6562 | struct http_txn *txn = l7; |
| 6563 | char *ptr; |
| 6564 | int len; |
| 6565 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6566 | if (!txn) |
| 6567 | return 0; |
| 6568 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6569 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6570 | return 0; |
| 6571 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6572 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6573 | ptr = txn->rsp.sol + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6574 | |
| 6575 | test->i = __strl2ui(ptr, len); |
| 6576 | test->flags = ACL_TEST_F_VOL_1ST; |
| 6577 | return 1; |
| 6578 | } |
| 6579 | |
| 6580 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6581 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6582 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6583 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6584 | { |
| 6585 | struct http_txn *txn = l7; |
| 6586 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6587 | if (!txn) |
| 6588 | return 0; |
| 6589 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6590 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6591 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6592 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6593 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6594 | /* ensure the indexes are not affected */ |
| 6595 | return 0; |
| 6596 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6597 | test->len = txn->req.sl.rq.u_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6598 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6599 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 6600 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 6601 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6602 | return 1; |
| 6603 | } |
| 6604 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6605 | static int |
| 6606 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6607 | struct acl_expr *expr, struct acl_test *test) |
| 6608 | { |
| 6609 | struct http_txn *txn = l7; |
| 6610 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6611 | if (!txn) |
| 6612 | return 0; |
| 6613 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6614 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6615 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6616 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6617 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6618 | /* ensure the indexes are not affected */ |
| 6619 | return 0; |
| 6620 | |
| 6621 | /* Parse HTTP request */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6622 | 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] | 6623 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 6624 | test->i = AF_INET; |
| 6625 | |
| 6626 | /* |
| 6627 | * If we are parsing url in frontend space, we prepare backend stage |
| 6628 | * to not parse again the same url ! optimization lazyness... |
| 6629 | */ |
| 6630 | if (px->options & PR_O_HTTP_PROXY) |
| 6631 | l4->flags |= SN_ADDR_SET; |
| 6632 | |
| 6633 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6634 | return 1; |
| 6635 | } |
| 6636 | |
| 6637 | static int |
| 6638 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6639 | struct acl_expr *expr, struct acl_test *test) |
| 6640 | { |
| 6641 | struct http_txn *txn = l7; |
| 6642 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6643 | if (!txn) |
| 6644 | return 0; |
| 6645 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6646 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6647 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6648 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6649 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6650 | /* ensure the indexes are not affected */ |
| 6651 | return 0; |
| 6652 | |
| 6653 | /* Same optimization as url_ip */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6654 | 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] | 6655 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 6656 | |
| 6657 | if (px->options & PR_O_HTTP_PROXY) |
| 6658 | l4->flags |= SN_ADDR_SET; |
| 6659 | |
| 6660 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6661 | return 1; |
| 6662 | } |
| 6663 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6664 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 6665 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 6666 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6667 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6668 | 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] | 6669 | struct acl_expr *expr, struct acl_test *test) |
| 6670 | { |
| 6671 | struct http_txn *txn = l7; |
| 6672 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6673 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6674 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6675 | if (!txn) |
| 6676 | return 0; |
| 6677 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6678 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6679 | /* search for header from the beginning */ |
| 6680 | ctx->idx = 0; |
| 6681 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6682 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6683 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6684 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6685 | test->len = ctx->vlen; |
| 6686 | test->ptr = (char *)ctx->line + ctx->val; |
| 6687 | return 1; |
| 6688 | } |
| 6689 | |
| 6690 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6691 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6692 | return 0; |
| 6693 | } |
| 6694 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6695 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6696 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6697 | struct acl_expr *expr, struct acl_test *test) |
| 6698 | { |
| 6699 | struct http_txn *txn = l7; |
| 6700 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6701 | if (!txn) |
| 6702 | return 0; |
| 6703 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6704 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6705 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6706 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6707 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6708 | /* ensure the indexes are not affected */ |
| 6709 | return 0; |
| 6710 | |
| 6711 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 6712 | } |
| 6713 | |
| 6714 | static int |
| 6715 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6716 | struct acl_expr *expr, struct acl_test *test) |
| 6717 | { |
| 6718 | struct http_txn *txn = l7; |
| 6719 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6720 | if (!txn) |
| 6721 | return 0; |
| 6722 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6723 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6724 | return 0; |
| 6725 | |
| 6726 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 6727 | } |
| 6728 | |
| 6729 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 6730 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 6731 | */ |
| 6732 | static int |
| 6733 | 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] | 6734 | struct acl_expr *expr, struct acl_test *test) |
| 6735 | { |
| 6736 | struct http_txn *txn = l7; |
| 6737 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6738 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6739 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6740 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6741 | if (!txn) |
| 6742 | return 0; |
| 6743 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6744 | ctx.idx = 0; |
| 6745 | cnt = 0; |
| 6746 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 6747 | cnt++; |
| 6748 | |
| 6749 | test->i = cnt; |
| 6750 | test->flags = ACL_TEST_F_VOL_HDR; |
| 6751 | return 1; |
| 6752 | } |
| 6753 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6754 | static int |
| 6755 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6756 | struct acl_expr *expr, struct acl_test *test) |
| 6757 | { |
| 6758 | struct http_txn *txn = l7; |
| 6759 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6760 | if (!txn) |
| 6761 | return 0; |
| 6762 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6763 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6764 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6765 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6766 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6767 | /* ensure the indexes are not affected */ |
| 6768 | return 0; |
| 6769 | |
| 6770 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 6771 | } |
| 6772 | |
| 6773 | static int |
| 6774 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6775 | struct acl_expr *expr, struct acl_test *test) |
| 6776 | { |
| 6777 | struct http_txn *txn = l7; |
| 6778 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6779 | if (!txn) |
| 6780 | return 0; |
| 6781 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6782 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6783 | return 0; |
| 6784 | |
| 6785 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 6786 | } |
| 6787 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6788 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 6789 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6790 | * 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] | 6791 | */ |
| 6792 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6793 | 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] | 6794 | struct acl_expr *expr, struct acl_test *test) |
| 6795 | { |
| 6796 | struct http_txn *txn = l7; |
| 6797 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6798 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6799 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6800 | if (!txn) |
| 6801 | return 0; |
| 6802 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6803 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6804 | /* search for header from the beginning */ |
| 6805 | ctx->idx = 0; |
| 6806 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6807 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6808 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6809 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6810 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 6811 | return 1; |
| 6812 | } |
| 6813 | |
| 6814 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6815 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6816 | return 0; |
| 6817 | } |
| 6818 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6819 | static int |
| 6820 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6821 | struct acl_expr *expr, struct acl_test *test) |
| 6822 | { |
| 6823 | struct http_txn *txn = l7; |
| 6824 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6825 | if (!txn) |
| 6826 | return 0; |
| 6827 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6828 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6829 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6830 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6831 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6832 | /* ensure the indexes are not affected */ |
| 6833 | return 0; |
| 6834 | |
| 6835 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 6836 | } |
| 6837 | |
| 6838 | static int |
| 6839 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6840 | struct acl_expr *expr, struct acl_test *test) |
| 6841 | { |
| 6842 | struct http_txn *txn = l7; |
| 6843 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6844 | if (!txn) |
| 6845 | return 0; |
| 6846 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6847 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6848 | return 0; |
| 6849 | |
| 6850 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 6851 | } |
| 6852 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 6853 | /* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned. |
| 6854 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 6855 | */ |
| 6856 | static int |
| 6857 | acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 6858 | struct acl_expr *expr, struct acl_test *test) |
| 6859 | { |
| 6860 | struct http_txn *txn = l7; |
| 6861 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6862 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 6863 | |
| 6864 | if (!txn) |
| 6865 | return 0; |
| 6866 | |
| 6867 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6868 | /* search for header from the beginning */ |
| 6869 | ctx->idx = 0; |
| 6870 | |
| 6871 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6872 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6873 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6874 | /* Same optimization as url_ip */ |
| 6875 | memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr)); |
| 6876 | url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr); |
| 6877 | test->ptr = (void *)&l4->srv_addr.sin_addr; |
| 6878 | test->i = AF_INET; |
| 6879 | return 1; |
| 6880 | } |
| 6881 | |
| 6882 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6883 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6884 | return 0; |
| 6885 | } |
| 6886 | |
| 6887 | static int |
| 6888 | acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6889 | struct acl_expr *expr, struct acl_test *test) |
| 6890 | { |
| 6891 | struct http_txn *txn = l7; |
| 6892 | |
| 6893 | if (!txn) |
| 6894 | return 0; |
| 6895 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6896 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 6897 | return 0; |
| 6898 | |
| 6899 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6900 | /* ensure the indexes are not affected */ |
| 6901 | return 0; |
| 6902 | |
| 6903 | return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test); |
| 6904 | } |
| 6905 | |
| 6906 | static int |
| 6907 | acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6908 | struct acl_expr *expr, struct acl_test *test) |
| 6909 | { |
| 6910 | struct http_txn *txn = l7; |
| 6911 | |
| 6912 | if (!txn) |
| 6913 | return 0; |
| 6914 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6915 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 6916 | return 0; |
| 6917 | |
| 6918 | return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test); |
| 6919 | } |
| 6920 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6921 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 6922 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 6923 | */ |
| 6924 | static int |
| 6925 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6926 | struct acl_expr *expr, struct acl_test *test) |
| 6927 | { |
| 6928 | struct http_txn *txn = l7; |
| 6929 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6930 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6931 | if (!txn) |
| 6932 | return 0; |
| 6933 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6934 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6935 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6936 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6937 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6938 | /* ensure the indexes are not affected */ |
| 6939 | return 0; |
| 6940 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6941 | 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] | 6942 | ptr = http_get_path(txn); |
| 6943 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6944 | return 0; |
| 6945 | |
| 6946 | /* OK, we got the '/' ! */ |
| 6947 | test->ptr = ptr; |
| 6948 | |
| 6949 | while (ptr < end && *ptr != '?') |
| 6950 | ptr++; |
| 6951 | |
| 6952 | test->len = ptr - test->ptr; |
| 6953 | |
| 6954 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 6955 | test->flags = ACL_TEST_F_VOL_1ST; |
| 6956 | return 1; |
| 6957 | } |
| 6958 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6959 | static int |
| 6960 | acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir, |
| 6961 | struct acl_expr *expr, struct acl_test *test) |
| 6962 | { |
| 6963 | struct buffer *req = s->req; |
| 6964 | struct http_txn *txn = &s->txn; |
| 6965 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6966 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6967 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 6968 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 6969 | */ |
| 6970 | |
| 6971 | if (!s || !req) |
| 6972 | return 0; |
| 6973 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6974 | if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6975 | /* Already decoded as OK */ |
| 6976 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 6977 | return 1; |
| 6978 | } |
| 6979 | |
| 6980 | /* Try to decode HTTP request */ |
| 6981 | if (likely(req->lr < req->r)) |
| 6982 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
| 6983 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6984 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6985 | if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) { |
| 6986 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 6987 | return 1; |
| 6988 | } |
| 6989 | /* wait for final state */ |
| 6990 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 6991 | return 0; |
| 6992 | } |
| 6993 | |
| 6994 | /* OK we got a valid HTTP request. We have some minor preparation to |
| 6995 | * perform so that further checks can rely on HTTP tests. |
| 6996 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6997 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6998 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 6999 | s->flags |= SN_REDIRECTABLE; |
| 7000 | |
| 7001 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) { |
| 7002 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 7003 | return 1; |
| 7004 | } |
| 7005 | |
| 7006 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 7007 | return 1; |
| 7008 | } |
| 7009 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7010 | |
| 7011 | /************************************************************************/ |
| 7012 | /* All supported keywords must be declared here. */ |
| 7013 | /************************************************************************/ |
| 7014 | |
| 7015 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 7016 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7017 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
| 7018 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7019 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 7020 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7021 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 7022 | { "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] | 7023 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7024 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7025 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7026 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7027 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7028 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7029 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 7030 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7031 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 7032 | { "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] | 7033 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7034 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 7035 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7036 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7037 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7038 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7039 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7040 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7041 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 7042 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 7043 | { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7044 | { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7045 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7046 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 7047 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 7048 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 7049 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 7050 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 7051 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 7052 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 7053 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 7054 | { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7055 | { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7056 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7057 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7058 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7059 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7060 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7061 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7062 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7063 | { "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] | 7064 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 7065 | { NULL, NULL, NULL, NULL }, |
| 7066 | |
| 7067 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7068 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 7069 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 7070 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 7071 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 7072 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 7073 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 7074 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 7075 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7076 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 7077 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 7078 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 7079 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 7080 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 7081 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 7082 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 7083 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 7084 | |
| 7085 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 7086 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 7087 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 7088 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 7089 | { NULL, NULL, NULL, NULL }, |
| 7090 | #endif |
| 7091 | }}; |
| 7092 | |
| 7093 | |
| 7094 | __attribute__((constructor)) |
| 7095 | static void __http_protocol_init(void) |
| 7096 | { |
| 7097 | acl_register_keywords(&acl_kws); |
| 7098 | } |
| 7099 | |
| 7100 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7101 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7102 | * Local variables: |
| 7103 | * c-indent-level: 8 |
| 7104 | * c-basic-offset: 8 |
| 7105 | * End: |
| 7106 | */ |