Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4 | * Copyright 2000-2011 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 | b05405a | 2012-01-23 15:35:52 +0100 | [diff] [blame] | 26 | #include <netinet/tcp.h> |
| 27 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 28 | #include <common/base64.h> |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 29 | #include <common/cfgparse.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 30 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 31 | #include <common/compat.h> |
| 32 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 33 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 34 | #include <common/memory.h> |
| 35 | #include <common/mini-clist.h> |
| 36 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 37 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 38 | #include <common/time.h> |
| 39 | #include <common/uri_auth.h> |
| 40 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | |
| 42 | #include <types/capture.h> |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 43 | #include <types/cli.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 44 | #include <types/filters.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | #include <types/global.h> |
William Lallemand | 71bd11a | 2017-11-20 19:13:14 +0100 | [diff] [blame] | 46 | #include <types/cache.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 47 | #include <types/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 49 | #include <proto/acl.h> |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 50 | #include <proto/action.h> |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 51 | #include <proto/arg.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 52 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/backend.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 54 | #include <proto/channel.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 55 | #include <proto/checks.h> |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 56 | #include <proto/cli.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 57 | #include <proto/compression.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 58 | #include <proto/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 59 | #include <proto/fd.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 60 | #include <proto/filters.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 61 | #include <proto/frontend.h> |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 62 | #include <proto/h1.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 63 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 64 | #include <proto/hdr_idx.h> |
Patrick Hemmer | e3faf02 | 2018-08-22 10:02:00 -0400 | [diff] [blame] | 65 | #include <proto/hlua.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 66 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 67 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 69 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 70 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 71 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 72 | #include <proto/server.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 73 | #include <proto/stream.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 74 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 75 | #include <proto/task.h> |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 76 | #include <proto/pattern.h> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 77 | #include <proto/vars.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 78 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 79 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 80 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 81 | [STAT_STATUS_DENY] = "DENY", |
| 82 | [STAT_STATUS_DONE] = "DONE", |
| 83 | [STAT_STATUS_ERRP] = "ERRP", |
| 84 | [STAT_STATUS_EXCD] = "EXCD", |
| 85 | [STAT_STATUS_NONE] = "NONE", |
| 86 | [STAT_STATUS_PART] = "PART", |
| 87 | [STAT_STATUS_UNKN] = "UNKN", |
| 88 | }; |
| 89 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 90 | void init_proto_http() |
| 91 | { |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 92 | /* memory allocations */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 93 | pool_head_http_txn = create_pool("http_txn", sizeof(struct http_txn), MEM_F_SHARED); |
| 94 | pool_head_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 95 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 96 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 97 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 98 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
Willy Tarreau | 4d893d4 | 2018-07-12 15:43:32 +0200 | [diff] [blame] | 99 | * the last CRLF. <len> bytes are copied, not counting the CRLF. |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 100 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 101 | * of headers is automatically adjusted. The number of bytes added is returned |
| 102 | * on success, otherwise <0 is returned indicating an error. |
| 103 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 104 | int http_header_add_tail2(struct http_msg *msg, |
| 105 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 106 | { |
| 107 | int bytes; |
| 108 | |
Willy Tarreau | 4d893d4 | 2018-07-12 15:43:32 +0200 | [diff] [blame] | 109 | bytes = ci_insert_line2(msg->chn, msg->eoh, text, len); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 110 | if (!bytes) |
| 111 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 112 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 113 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 114 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 115 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 116 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 117 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 118 | * structure holds everything necessary to use the header and find next |
| 119 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 120 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 121 | * 1 when it finds a value, and 0 when there is no more. It is very similar to |
| 122 | * http_find_header2() except that it is designed to work with full-line headers |
| 123 | * whose comma is not a delimiter but is part of the syntax. As a special case, |
| 124 | * if ctx->val is NULL when searching for a new values of a header, the current |
| 125 | * header is rescanned. This allows rescanning after a header deletion. |
| 126 | */ |
| 127 | int http_find_full_header2(const char *name, int len, |
| 128 | char *sol, struct hdr_idx *idx, |
| 129 | struct hdr_ctx *ctx) |
| 130 | { |
| 131 | char *eol, *sov; |
| 132 | int cur_idx, old_idx; |
| 133 | |
| 134 | cur_idx = ctx->idx; |
| 135 | if (cur_idx) { |
| 136 | /* We have previously returned a header, let's search another one */ |
| 137 | sol = ctx->line; |
| 138 | eol = sol + idx->v[cur_idx].len; |
| 139 | goto next_hdr; |
| 140 | } |
| 141 | |
| 142 | /* first request for this header */ |
| 143 | sol += hdr_idx_first_pos(idx); |
| 144 | old_idx = 0; |
| 145 | cur_idx = hdr_idx_first_idx(idx); |
| 146 | while (cur_idx) { |
| 147 | eol = sol + idx->v[cur_idx].len; |
| 148 | |
| 149 | if (len == 0) { |
| 150 | /* No argument was passed, we want any header. |
| 151 | * To achieve this, we simply build a fake request. */ |
| 152 | while (sol + len < eol && sol[len] != ':') |
| 153 | len++; |
| 154 | name = sol; |
| 155 | } |
| 156 | |
| 157 | if ((len < eol - sol) && |
| 158 | (sol[len] == ':') && |
| 159 | (strncasecmp(sol, name, len) == 0)) { |
| 160 | ctx->del = len; |
| 161 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 162 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 163 | sov++; |
| 164 | |
| 165 | ctx->line = sol; |
| 166 | ctx->prev = old_idx; |
| 167 | ctx->idx = cur_idx; |
| 168 | ctx->val = sov - sol; |
| 169 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 170 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 171 | eol--; |
| 172 | ctx->tws++; |
| 173 | } |
| 174 | ctx->vlen = eol - sov; |
| 175 | return 1; |
| 176 | } |
| 177 | next_hdr: |
| 178 | sol = eol + idx->v[cur_idx].cr + 1; |
| 179 | old_idx = cur_idx; |
| 180 | cur_idx = idx->v[cur_idx].next; |
| 181 | } |
| 182 | return 0; |
| 183 | } |
| 184 | |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 185 | /* Find the first or next header field in message buffer <sol> using headers |
| 186 | * index <idx>, and return it in the <ctx> structure. This structure holds |
| 187 | * everything necessary to use the header and find next occurrence. If its |
| 188 | * <idx> member is 0, the first header is retrieved. Otherwise, the next |
| 189 | * occurrence is returned. The function returns 1 when it finds a value, and |
| 190 | * 0 when there is no more. It is equivalent to http_find_full_header2() with |
| 191 | * no header name. |
| 192 | */ |
| 193 | int http_find_next_header(char *sol, struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 194 | { |
| 195 | char *eol, *sov; |
| 196 | int cur_idx, old_idx; |
| 197 | int len; |
| 198 | |
| 199 | cur_idx = ctx->idx; |
| 200 | if (cur_idx) { |
| 201 | /* We have previously returned a header, let's search another one */ |
| 202 | sol = ctx->line; |
| 203 | eol = sol + idx->v[cur_idx].len; |
| 204 | goto next_hdr; |
| 205 | } |
| 206 | |
| 207 | /* first request for this header */ |
| 208 | sol += hdr_idx_first_pos(idx); |
| 209 | old_idx = 0; |
| 210 | cur_idx = hdr_idx_first_idx(idx); |
| 211 | while (cur_idx) { |
| 212 | eol = sol + idx->v[cur_idx].len; |
| 213 | |
| 214 | len = 0; |
| 215 | while (1) { |
| 216 | if (len >= eol - sol) |
| 217 | goto next_hdr; |
| 218 | if (sol[len] == ':') |
| 219 | break; |
| 220 | len++; |
| 221 | } |
| 222 | |
| 223 | ctx->del = len; |
| 224 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 225 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 226 | sov++; |
| 227 | |
| 228 | ctx->line = sol; |
| 229 | ctx->prev = old_idx; |
| 230 | ctx->idx = cur_idx; |
| 231 | ctx->val = sov - sol; |
| 232 | ctx->tws = 0; |
| 233 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 234 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | c90dc23 | 2015-02-20 13:51:36 +0100 | [diff] [blame] | 235 | eol--; |
| 236 | ctx->tws++; |
| 237 | } |
| 238 | ctx->vlen = eol - sov; |
| 239 | return 1; |
| 240 | |
| 241 | next_hdr: |
| 242 | sol = eol + idx->v[cur_idx].cr + 1; |
| 243 | old_idx = cur_idx; |
| 244 | cur_idx = idx->v[cur_idx].next; |
| 245 | } |
| 246 | return 0; |
| 247 | } |
| 248 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 249 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 250 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 251 | * structure holds everything necessary to use the header and find next |
| 252 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 253 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 254 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 255 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 256 | * is NULL when searching for a new values of a header, the current header is |
| 257 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 258 | */ |
| 259 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 260 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 261 | struct hdr_ctx *ctx) |
| 262 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 263 | char *eol, *sov; |
| 264 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 265 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 266 | cur_idx = ctx->idx; |
| 267 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 268 | /* We have previously returned a value, let's search |
| 269 | * another one on the same line. |
| 270 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 271 | sol = ctx->line; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 272 | ctx->del = ctx->val + ctx->vlen + ctx->tws; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 273 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 274 | eol = sol + idx->v[cur_idx].len; |
| 275 | |
| 276 | if (sov >= eol) |
| 277 | /* no more values in this header */ |
| 278 | goto next_hdr; |
| 279 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 280 | /* values remaining for this header, skip the comma but save it |
| 281 | * for later use (eg: for header deletion). |
| 282 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 283 | sov++; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 284 | while (sov < eol && HTTP_IS_LWS((*sov))) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 285 | sov++; |
| 286 | |
| 287 | goto return_hdr; |
| 288 | } |
| 289 | |
| 290 | /* first request for this header */ |
| 291 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 292 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 293 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 294 | while (cur_idx) { |
| 295 | eol = sol + idx->v[cur_idx].len; |
| 296 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 297 | if (len == 0) { |
| 298 | /* No argument was passed, we want any header. |
| 299 | * To achieve this, we simply build a fake request. */ |
| 300 | while (sol + len < eol && sol[len] != ':') |
| 301 | len++; |
| 302 | name = sol; |
| 303 | } |
| 304 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 305 | if ((len < eol - sol) && |
| 306 | (sol[len] == ':') && |
| 307 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 308 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 309 | sov = sol + len + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 310 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 311 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 312 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 313 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 314 | ctx->prev = old_idx; |
| 315 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 316 | ctx->idx = cur_idx; |
| 317 | ctx->val = sov - sol; |
| 318 | |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 319 | eol = http_find_hdr_value_end(sov, eol); |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 320 | ctx->tws = 0; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 321 | while (eol > sov && HTTP_IS_LWS(*(eol - 1))) { |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 322 | eol--; |
| 323 | ctx->tws++; |
| 324 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 325 | ctx->vlen = eol - sov; |
| 326 | return 1; |
| 327 | } |
| 328 | next_hdr: |
| 329 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 330 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 331 | cur_idx = idx->v[cur_idx].next; |
| 332 | } |
| 333 | return 0; |
| 334 | } |
| 335 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 336 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 337 | * the http_find_header functions. The value is removed, as well as surrounding |
| 338 | * commas if any. If the removed value was alone, the whole header is removed. |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 339 | * The ctx is always updated accordingly, as well as the buffer and HTTP |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 340 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 341 | * no more header, so any processing may stop. The ctx is always left in a form |
| 342 | * that can be handled by http_find_header2() to find next occurrence. |
| 343 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 344 | int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx) |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 345 | { |
| 346 | int cur_idx = ctx->idx; |
| 347 | char *sol = ctx->line; |
| 348 | struct hdr_idx_elem *hdr; |
| 349 | int delta, skip_comma; |
| 350 | |
| 351 | if (!cur_idx) |
| 352 | return 0; |
| 353 | |
| 354 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 355 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 356 | /* This was the only value of the header, we must now remove it entirely. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 357 | delta = b_rep_blk(&msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 358 | http_msg_move_end(msg, delta); |
| 359 | idx->used--; |
| 360 | hdr->len = 0; /* unused entry */ |
| 361 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 362 | if (idx->tail == ctx->idx) |
| 363 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 364 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
Willy Tarreau | 7c1c217 | 2015-01-07 17:23:50 +0100 | [diff] [blame] | 365 | ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 366 | ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */ |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 367 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 368 | return ctx->idx; |
| 369 | } |
| 370 | |
| 371 | /* This was not the only value of this header. We have to remove between |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 372 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 373 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 374 | */ |
| 375 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 376 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 377 | delta = b_rep_blk(&msg->chn->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 378 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 379 | NULL, 0); |
| 380 | hdr->len += delta; |
| 381 | http_msg_move_end(msg, delta); |
| 382 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 383 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 384 | return ctx->idx; |
| 385 | } |
| 386 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 387 | /* This function handles a server error at the stream interface level. The |
| 388 | * stream interface is assumed to be already in a closed state. An optional |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 389 | * message is copied into the input buffer. |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 390 | * 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] | 391 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 392 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 393 | static void http_server_error(struct stream *s, struct stream_interface *si, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 394 | int err, int finst, const struct buffer *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 395 | { |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 396 | FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg)); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 397 | channel_auto_read(si_oc(si)); |
| 398 | channel_abort(si_oc(si)); |
| 399 | channel_auto_close(si_oc(si)); |
| 400 | channel_erase(si_oc(si)); |
| 401 | channel_auto_close(si_ic(si)); |
| 402 | channel_auto_read(si_ic(si)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 403 | if (msg) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 404 | co_inject(si_ic(si), msg->area, msg->data); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 405 | if (!(s->flags & SF_ERR_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 406 | s->flags |= err; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 407 | if (!(s->flags & SF_FINST_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 408 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 409 | } |
| 410 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 411 | /* This function returns the appropriate error location for the given stream |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 412 | * and message. |
| 413 | */ |
| 414 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 415 | struct buffer *http_error_message(struct stream *s) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 416 | { |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 417 | const int msgnum = http_get_status_idx(s->txn->status); |
| 418 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 419 | if (s->be->errmsg[msgnum].area) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 420 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 421 | else if (strm_fe(s)->errmsg[msgnum].area) |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 422 | return &strm_fe(s)->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 423 | else |
| 424 | return &http_err_chunks[msgnum]; |
| 425 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 426 | |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 427 | void |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 428 | http_reply_and_close(struct stream *s, short status, struct buffer *msg) |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 429 | { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 430 | s->txn->flags &= ~TX_WAIT_NEXT_RQ; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 431 | FLT_STRM_CB(s, flt_http_reply(s, status, msg)); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 432 | stream_int_retnclose(&s->si[0], msg); |
| 433 | } |
| 434 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 435 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 436 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 437 | * It is returned otherwise. |
| 438 | */ |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 439 | char *http_txn_get_path(const struct http_txn *txn) |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 440 | { |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 441 | struct ist ret; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 442 | |
Willy Tarreau | 9d9ccdb | 2018-10-28 20:13:12 +0100 | [diff] [blame] | 443 | if (!txn->req.chn->buf.size) |
| 444 | return NULL; |
| 445 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 446 | ret = http_get_path(ist2(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l)); |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 447 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 448 | return ret.ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 449 | } |
| 450 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 451 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 452 | * in redirect mode. This may only be called just after the stream interface |
| 453 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 454 | * follow normal proxy processing. NOTE: this function is designed to support |
| 455 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 456 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 457 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 458 | { |
| 459 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 460 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 461 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 462 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 463 | |
| 464 | /* 1: create the response header */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 465 | trash.data = strlen(HTTP_302); |
| 466 | memcpy(trash.area, HTTP_302, trash.data); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 467 | |
Willy Tarreau | b05e48a | 2018-09-20 11:12:58 +0200 | [diff] [blame] | 468 | srv = __objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 469 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 470 | /* 2: add the server's prefix */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 471 | if (trash.data + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 472 | return; |
| 473 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 474 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 475 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 476 | memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len); |
| 477 | trash.data += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 478 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 479 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 480 | /* 3: add the request URI. Since it was already forwarded, we need |
| 481 | * to temporarily rewind the buffer. |
| 482 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 483 | txn = s->txn; |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 484 | c_rew(&s->req, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 485 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 486 | path = http_txn_get_path(txn); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 487 | len = b_dist(&s->req.buf, path, c_ptr(&s->req, txn->req.sl.rq.u + txn->req.sl.rq.u_l)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 488 | |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 489 | c_adv(&s->req, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 490 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 491 | if (!path) |
| 492 | return; |
| 493 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 494 | if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 495 | return; |
| 496 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 497 | memcpy(trash.area + trash.data, path, len); |
| 498 | trash.data += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 499 | |
| 500 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 501 | memcpy(trash.area + trash.data, |
| 502 | "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 503 | trash.data += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 504 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 505 | memcpy(trash.area + trash.data, |
| 506 | "\r\nConnection: close\r\n\r\n", 23); |
| 507 | trash.data += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 508 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 509 | |
| 510 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 511 | si_shutr(si); |
| 512 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 513 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 514 | si->state = SI_ST_CLO; |
| 515 | |
| 516 | /* send the message */ |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 517 | txn->status = 302; |
| 518 | http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 519 | |
| 520 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
Willy Tarreau | 4521ba6 | 2013-01-24 01:25:25 +0100 | [diff] [blame] | 521 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 522 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 523 | } |
| 524 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 525 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 526 | * that the server side is closed. Note that err_type is actually a |
| 527 | * bitmask, where almost only aborts may be cumulated with other |
| 528 | * values. We consider that aborted operations are more important |
| 529 | * than timeouts or errors due to the fact that nobody else in the |
| 530 | * logs might explain incomplete retries. All others should avoid |
| 531 | * being cumulated. It should normally not be possible to have multiple |
| 532 | * aborts at once, but just in case, the first one in sequence is reported. |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 533 | * Note that connection errors appearing on the second request of a keep-alive |
| 534 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 535 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 536 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 537 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 538 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 539 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 540 | /* set s->txn->status for http_error_message(s) */ |
| 541 | s->txn->status = 503; |
| 542 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 543 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 544 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 545 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 546 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 547 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 548 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 549 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 550 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 551 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 552 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 553 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 554 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 555 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 556 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 557 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 558 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 559 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 560 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 561 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 562 | (s->flags & SF_SRV_REUSED) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 563 | http_error_message(s)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 564 | else if (err_type & SI_ET_CONN_RES) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 565 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 566 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 567 | http_error_message(s)); |
| 568 | else { /* SI_ET_CONN_OTHER and others */ |
| 569 | s->txn->status = 500; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 570 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 571 | http_error_message(s)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 572 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 573 | } |
| 574 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 575 | extern const char sess_term_cond[8]; |
| 576 | extern const char sess_fin_state[8]; |
| 577 | extern const char *monthname[12]; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 578 | struct pool_head *pool_head_http_txn; |
| 579 | struct pool_head *pool_head_requri; |
| 580 | struct pool_head *pool_head_capture = NULL; |
| 581 | struct pool_head *pool_head_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 582 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 583 | /* |
| 584 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 585 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 586 | */ |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 587 | void http_capture_headers(char *som, struct hdr_idx *idx, |
| 588 | char **cap, struct cap_hdr *cap_hdr) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 589 | { |
| 590 | char *eol, *sol, *col, *sov; |
| 591 | int cur_idx; |
| 592 | struct cap_hdr *h; |
| 593 | int len; |
| 594 | |
| 595 | sol = som + hdr_idx_first_pos(idx); |
| 596 | cur_idx = hdr_idx_first_idx(idx); |
| 597 | |
| 598 | while (cur_idx) { |
| 599 | eol = sol + idx->v[cur_idx].len; |
| 600 | |
| 601 | col = sol; |
| 602 | while (col < eol && *col != ':') |
| 603 | col++; |
| 604 | |
| 605 | sov = col + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 606 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 607 | sov++; |
| 608 | |
| 609 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 610 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 611 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 612 | if (cap[h->index] == NULL) |
| 613 | cap[h->index] = |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 614 | pool_alloc(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 615 | |
| 616 | if (cap[h->index] == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 617 | ha_alert("HTTP capture : out of memory.\n"); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 618 | continue; |
| 619 | } |
| 620 | |
| 621 | len = eol - sov; |
| 622 | if (len > h->len) |
| 623 | len = h->len; |
| 624 | |
| 625 | memcpy(cap[h->index], sov, len); |
| 626 | cap[h->index][len]=0; |
| 627 | } |
| 628 | } |
| 629 | sol = eol + idx->v[cur_idx].cr + 1; |
| 630 | cur_idx = idx->v[cur_idx].next; |
| 631 | } |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 632 | } |
| 633 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 634 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 635 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 636 | * nothing is done and 1 is returned. |
| 637 | */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 638 | int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 639 | { |
| 640 | int delta; |
| 641 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 642 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 643 | |
| 644 | if (msg->sl.rq.v_l != 0) |
| 645 | return 1; |
| 646 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 647 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 648 | if (txn->meth != HTTP_METH_GET) |
| 649 | return 0; |
| 650 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 651 | cur_end = ci_head(msg->chn) + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 652 | |
| 653 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 654 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 655 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 656 | } |
| 657 | /* add HTTP version */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 658 | delta = b_rep_blk(&msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 659 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 660 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 661 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 662 | HTTP_MSG_RQMETH, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 663 | ci_head(msg->chn), cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 664 | NULL, NULL); |
| 665 | if (unlikely(!cur_end)) |
| 666 | return 0; |
| 667 | |
| 668 | /* we have a full HTTP/1.0 request now and we know that |
| 669 | * we have either a CR or an LF at <ptr>. |
| 670 | */ |
| 671 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 672 | return 1; |
| 673 | } |
| 674 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 675 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 676 | * and "keep-alive" values. If we already know that some headers may safely |
| 677 | * be removed, we remove them now. The <to_del> flags are used for that : |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 678 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 679 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 680 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 681 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 682 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 683 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 684 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 685 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 686 | void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 687 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 688 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 689 | const char *hdr_val = "Connection"; |
| 690 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 691 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 692 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 693 | return; |
| 694 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 695 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 696 | hdr_val = "Proxy-Connection"; |
| 697 | hdr_len = 16; |
| 698 | } |
| 699 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 700 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 701 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 702 | while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 703 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 704 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 705 | if (to_del & 2) |
| 706 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 707 | else |
| 708 | txn->flags |= TX_CON_KAL_SET; |
| 709 | } |
| 710 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 711 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 712 | if (to_del & 1) |
| 713 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 714 | else |
| 715 | txn->flags |= TX_CON_CLO_SET; |
| 716 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 717 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 718 | txn->flags |= TX_HDR_CONN_UPG; |
| 719 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 720 | } |
| 721 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 722 | txn->flags |= TX_HDR_CONN_PRS; |
| 723 | return; |
| 724 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 725 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 726 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 727 | * added depending on the <wanted> flags, which are exclusively composed of |
| 728 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 729 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 730 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 731 | void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 732 | { |
| 733 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 734 | const char *hdr_val = "Connection"; |
| 735 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 736 | |
| 737 | ctx.idx = 0; |
| 738 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 739 | |
| 740 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 741 | hdr_val = "Proxy-Connection"; |
| 742 | hdr_len = 16; |
| 743 | } |
| 744 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 745 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 746 | while (http_find_header2(hdr_val, hdr_len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 747 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 748 | if (wanted & TX_CON_KAL_SET) |
| 749 | txn->flags |= TX_CON_KAL_SET; |
| 750 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 751 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 752 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 753 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 754 | if (wanted & TX_CON_CLO_SET) |
| 755 | txn->flags |= TX_CON_CLO_SET; |
| 756 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 757 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 758 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 759 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 760 | |
| 761 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 762 | return; |
| 763 | |
| 764 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 765 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 766 | hdr_val = "Connection: close"; |
| 767 | hdr_len = 17; |
| 768 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 769 | hdr_val = "Proxy-Connection: close"; |
| 770 | hdr_len = 23; |
| 771 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 772 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 776 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 777 | hdr_val = "Connection: keep-alive"; |
| 778 | hdr_len = 22; |
| 779 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 780 | hdr_val = "Proxy-Connection: keep-alive"; |
| 781 | hdr_len = 28; |
| 782 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 783 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 784 | } |
| 785 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 786 | } |
| 787 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 788 | void http_adjust_conn_mode(struct stream *s, struct http_txn *txn, struct http_msg *msg) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 789 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 790 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 791 | int tmp = TX_CON_WANT_KAL; |
| 792 | |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 793 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
| 794 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 795 | tmp = TX_CON_WANT_TUN; |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 796 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 797 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 798 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 799 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 800 | |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 801 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO || |
| 802 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 803 | tmp = TX_CON_WANT_CLO; |
| 804 | |
| 805 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 806 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 807 | |
| 808 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 809 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 810 | /* parse the Connection header and possibly clean it */ |
| 811 | int to_del = 0; |
| 812 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 813 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 814 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 815 | to_del |= 2; /* remove "keep-alive" */ |
| 816 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 817 | to_del |= 1; /* remove "close" */ |
| 818 | http_parse_connection_header(txn, msg, to_del); |
| 819 | } |
| 820 | |
| 821 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 822 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 823 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 824 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 825 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 826 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 827 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 828 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 829 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 830 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 831 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 832 | * processing can continue on next analysers, or zero if it either needs more |
| 833 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 834 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 835 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 836 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 837 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 838 | int http_wait_for_request(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 839 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 840 | /* |
| 841 | * We will parse the partial (or complete) lines. |
| 842 | * We will check the request syntax, and also join multi-line |
| 843 | * headers. An index of all the lines will be elaborated while |
| 844 | * parsing. |
| 845 | * |
| 846 | * For the parsing, we use a 28 states FSM. |
| 847 | * |
| 848 | * Here is the information we currently have : |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 849 | * ci_head(req) = beginning of request |
| 850 | * ci_head(req) + msg->eoh = end of processed headers / start of current one |
| 851 | * ci_tail(req) = end of input data |
| 852 | * msg->eol = end of current header or line (LF or CRLF) |
| 853 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 854 | * |
| 855 | * At end of parsing, we may perform a capture of the error (if any), and |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 856 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 857 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 858 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 859 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 860 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 861 | int cur_idx; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 862 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 863 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 864 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 865 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 866 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 867 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 868 | now_ms, __FUNCTION__, |
| 869 | s, |
| 870 | req, |
| 871 | req->rex, req->wex, |
| 872 | req->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 873 | ci_data(req), |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 874 | req->analysers); |
| 875 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 876 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 877 | s->srv_error = http_return_srv_error; |
| 878 | |
Rian McGuire | 89fcb7d | 2018-04-24 11:19:21 -0300 | [diff] [blame] | 879 | /* If there is data available for analysis, log the end of the idle time. */ |
Willy Tarreau | d760eec | 2018-07-10 09:50:25 +0200 | [diff] [blame] | 880 | if (c_data(req) && s->logs.t_idle == -1) |
Rian McGuire | 89fcb7d | 2018-04-24 11:19:21 -0300 | [diff] [blame] | 881 | s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake; |
| 882 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 883 | /* There's a protected area at the end of the buffer for rewriting |
| 884 | * purposes. We don't want to start to parse the request if the |
| 885 | * protected area is affected, because we may have to move processed |
| 886 | * data later, which is much more complicated. |
| 887 | */ |
Willy Tarreau | d760eec | 2018-07-10 09:50:25 +0200 | [diff] [blame] | 888 | if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 889 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 890 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 891 | if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 892 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 893 | /* some data has still not left the buffer, wake us once that's done */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 894 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 895 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 896 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 897 | return 0; |
| 898 | } |
Willy Tarreau | 188e230 | 2018-06-15 11:11:53 +0200 | [diff] [blame] | 899 | if (unlikely(ci_tail(req) < c_ptr(req, msg->next) || |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 900 | ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 901 | channel_slow_realign(req, trash.area); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 902 | } |
| 903 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 904 | if (likely(msg->next < ci_data(req))) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 905 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 906 | } |
| 907 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 908 | /* 1: we might have to print this header in debug mode */ |
| 909 | if (unlikely((global.mode & MODE_DEBUG) && |
| 910 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 911 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 912 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 913 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 914 | sol = ci_head(req); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 915 | /* this is a bit complex : in case of error on the request line, |
| 916 | * we know that rq.l is still zero, so we display only the part |
| 917 | * up to the end of the line (truncated by debug_hdr). |
| 918 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 919 | eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : ci_data(req)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 920 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 921 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 922 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 923 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 924 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 925 | while (cur_idx) { |
| 926 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 927 | debug_hdr("clihdr", s, sol, eol); |
| 928 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 929 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 930 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 931 | } |
| 932 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 933 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 934 | /* |
| 935 | * Now we quickly check if we have found a full valid request. |
| 936 | * If not so, we check the FD and buffer states before leaving. |
| 937 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 938 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 939 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 940 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 941 | * we note the error in the stream flags but don't set any state. |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 942 | * Since the error will be noted there, it will not be counted by |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 943 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 944 | * Last, we may increase some tracked counters' http request errors on |
| 945 | * the cases that are deliberately the client's fault. For instance, |
| 946 | * a timeout or connection reset is not counted as an error. However |
| 947 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 948 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 949 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 950 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 951 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 952 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 953 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 954 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 955 | stream_inc_http_req_ctr(s); |
| 956 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 957 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 958 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 959 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 960 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 961 | /* 1: Since we are in header mode, if there's no space |
| 962 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 963 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 964 | * must terminate it now. |
| 965 | */ |
Willy Tarreau | 2375233 | 2018-06-15 14:54:53 +0200 | [diff] [blame] | 966 | if (unlikely(channel_full(req, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 967 | /* FIXME: check if URI is set and return Status |
| 968 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 969 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 970 | stream_inc_http_req_ctr(s); |
| 971 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 972 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 973 | if (msg->err_pos < 0) |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 974 | msg->err_pos = ci_data(req); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 975 | goto return_bad_req; |
| 976 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 977 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 978 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 979 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 980 | if (!(s->flags & SF_ERR_MASK)) |
| 981 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 982 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 983 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 984 | goto failed_keep_alive; |
| 985 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 986 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 987 | goto failed_keep_alive; |
| 988 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 989 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 990 | if (msg->err_pos >= 0) { |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 991 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 992 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 993 | } |
| 994 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 995 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 996 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 997 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 998 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 999 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1000 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1001 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1002 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1003 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1004 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1005 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1006 | if (!(s->flags & SF_FINST_MASK)) |
| 1007 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1008 | return 0; |
| 1009 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1010 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1011 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1012 | else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1013 | if (!(s->flags & SF_ERR_MASK)) |
| 1014 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1015 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1016 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1017 | goto failed_keep_alive; |
| 1018 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1019 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1020 | goto failed_keep_alive; |
| 1021 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1022 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1023 | if (msg->err_pos >= 0) { |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 1024 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1025 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 1026 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1027 | txn->status = 408; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1028 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1029 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1030 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1031 | req->analysers &= AN_REQ_FLT_END; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1032 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1033 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1034 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1035 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1036 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1037 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1038 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1039 | if (!(s->flags & SF_FINST_MASK)) |
| 1040 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1041 | return 0; |
| 1042 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1043 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1044 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1045 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1046 | if (!(s->flags & SF_ERR_MASK)) |
| 1047 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 1048 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1049 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1050 | goto failed_keep_alive; |
| 1051 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 1052 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 1053 | goto failed_keep_alive; |
| 1054 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 1055 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 1056 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1057 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1058 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1059 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1060 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1061 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1062 | stream_inc_http_err_ctr(s); |
| 1063 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1064 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1065 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1066 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1067 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1068 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1069 | if (!(s->flags & SF_FINST_MASK)) |
| 1070 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1071 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1072 | } |
| 1073 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 1074 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 1075 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1076 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 1077 | #ifdef TCP_QUICKACK |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1078 | if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req) && |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1079 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 1080 | /* We need more data, we have to re-enable quick-ack in case we |
| 1081 | * previously disabled it, otherwise we might cause the client |
| 1082 | * to delay next data. |
| 1083 | */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 1084 | setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 1085 | } |
| 1086 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 1087 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1088 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 1089 | /* If the client starts to talk, let's fall back to |
| 1090 | * request timeout processing. |
| 1091 | */ |
| 1092 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1093 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 1094 | } |
| 1095 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1096 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1097 | if (!tick_isset(req->analyse_exp)) { |
| 1098 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 1099 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 1100 | tick_isset(s->be->timeout.httpka)) |
| 1101 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 1102 | else |
| 1103 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 1104 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1105 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1106 | /* we're not ready yet */ |
| 1107 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1108 | |
| 1109 | failed_keep_alive: |
| 1110 | /* Here we process low-level errors for keep-alive requests. In |
| 1111 | * short, if the request is not the first one and it experiences |
| 1112 | * a timeout, read error or shutdown, we just silently close so |
| 1113 | * that the client can try again. |
| 1114 | */ |
| 1115 | txn->status = 0; |
| 1116 | msg->msg_state = HTTP_MSG_RQBEFORE; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1117 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1118 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 1119 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1120 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1121 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 1122 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1123 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1124 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1125 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 1126 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 1127 | * later. At this point, we have the last CRLF at req->buf.data + msg->eoh. |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1128 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1129 | * points to the CRLF of the request line. msg->next points to the first |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1130 | * byte after the last LF. msg->sov points to the first byte of data. |
| 1131 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 1132 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1133 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1134 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1135 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1136 | proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */ |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 1137 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 1138 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 1139 | /* kill the pending keep-alive timeout */ |
| 1140 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 1141 | req->analyse_exp = TICK_ETERNITY; |
| 1142 | } |
| 1143 | |
| 1144 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1145 | /* Maybe we found in invalid header name while we were configured not |
| 1146 | * to block on that, so we have to capture it now. |
| 1147 | */ |
| 1148 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 1149 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 1150 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1151 | /* |
| 1152 | * 1: identify the method |
| 1153 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1154 | txn->meth = find_http_meth(ci_head(req), msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1155 | |
| 1156 | /* we can make use of server redirect on GET and HEAD */ |
| 1157 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1158 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 9165979 | 2017-11-10 19:38:10 +0100 | [diff] [blame] | 1159 | else if (txn->meth == HTTP_METH_OTHER && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1160 | msg->sl.rq.m_l == 3 && memcmp(ci_head(req), "PRI", 3) == 0) { |
Willy Tarreau | 9165979 | 2017-11-10 19:38:10 +0100 | [diff] [blame] | 1161 | /* PRI is reserved for the HTTP/2 preface */ |
| 1162 | msg->err_pos = 0; |
| 1163 | goto return_bad_req; |
| 1164 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1165 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1166 | /* |
| 1167 | * 2: check if the URI matches the monitor_uri. |
| 1168 | * We have to do this for every request which gets in, because |
| 1169 | * the monitor-uri is defined by the frontend. |
| 1170 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1171 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 1172 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1173 | !memcmp(ci_head(req) + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1174 | sess->fe->monitor_uri, |
| 1175 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1176 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1177 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1178 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1179 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1180 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1181 | s->flags |= SF_MONITOR; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1182 | HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1183 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1184 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1185 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1186 | int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1187 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1188 | ret = acl_pass(ret); |
| 1189 | if (cond->pol == ACL_COND_UNLESS) |
| 1190 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1191 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1192 | if (ret) { |
| 1193 | /* we fail this request, let's return 503 service unavail */ |
| 1194 | txn->status = 503; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1195 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1196 | if (!(s->flags & SF_ERR_MASK)) |
| 1197 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1198 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1199 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1200 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 1201 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1202 | /* nothing to fail, let's reply normaly */ |
| 1203 | txn->status = 200; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1204 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1205 | if (!(s->flags & SF_ERR_MASK)) |
| 1206 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1207 | goto return_prx_cond; |
| 1208 | } |
| 1209 | |
| 1210 | /* |
| 1211 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1212 | * Note: we cannot log anymore if the request has been |
| 1213 | * classified as invalid. |
| 1214 | */ |
| 1215 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 1216 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1217 | if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1218 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1219 | |
Stéphane Cottin | 23e9e93 | 2017-05-18 08:58:41 +0200 | [diff] [blame] | 1220 | if (urilen >= global.tune.requri_len ) |
| 1221 | urilen = global.tune.requri_len - 1; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1222 | memcpy(txn->uri, ci_head(req), urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1223 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1224 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1225 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1226 | s->do_log(s); |
| 1227 | } else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1228 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1229 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1230 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1231 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 1232 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 1233 | * exactly one digit "." one digit. This check may be disabled using |
| 1234 | * option accept-invalid-http-request. |
| 1235 | */ |
| 1236 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 1237 | if (msg->sl.rq.v_l != 8) { |
| 1238 | msg->err_pos = msg->sl.rq.v; |
| 1239 | goto return_bad_req; |
| 1240 | } |
| 1241 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1242 | if (ci_head(req)[msg->sl.rq.v + 4] != '/' || |
| 1243 | !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) || |
| 1244 | ci_head(req)[msg->sl.rq.v + 6] != '.' || |
| 1245 | !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) { |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 1246 | msg->err_pos = msg->sl.rq.v + 4; |
| 1247 | goto return_bad_req; |
| 1248 | } |
| 1249 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 1250 | else { |
| 1251 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 1252 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 1253 | goto return_bad_req; |
| 1254 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 1255 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1256 | /* ... and check if the request is HTTP/1.1 or above */ |
| 1257 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1258 | ((ci_head(req)[msg->sl.rq.v + 5] > '1') || |
| 1259 | ((ci_head(req)[msg->sl.rq.v + 5] == '1') && |
| 1260 | (ci_head(req)[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 1261 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1262 | |
| 1263 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1264 | txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1265 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1266 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 1267 | * we have what looks like a proxied connection instead of a connection, |
| 1268 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 1269 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 1270 | * happen to do that despite being non-standard :-( |
| 1271 | * We consider that a request not beginning with either '/' or '*' is |
| 1272 | * a proxied connection, which covers both "scheme://location" and |
| 1273 | * CONNECT ip:port. |
| 1274 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1275 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1276 | ci_head(req)[msg->sl.rq.u] != '/' && ci_head(req)[msg->sl.rq.u] != '*') |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1277 | txn->flags |= TX_USE_PX_CONN; |
| 1278 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1279 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 1280 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1281 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1282 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1283 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 1284 | http_capture_headers(ci_head(req), &txn->hdr_idx, |
| 1285 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1286 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1287 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 1288 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1289 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1290 | * The length of a message body is determined by one of the following |
| 1291 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1292 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1293 | * 1. Any response to a HEAD request and any response with a 1xx |
| 1294 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 1295 | * code is always terminated by the first empty line after the |
| 1296 | * header fields, regardless of the header fields present in the |
| 1297 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1298 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1299 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 1300 | * the connection will become a tunnel immediately after the empty |
| 1301 | * line that concludes the header fields. A client MUST ignore any |
| 1302 | * Content-Length or Transfer-Encoding header fields received in |
| 1303 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1304 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1305 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 1306 | * transfer coding (Section 4.1) is the final encoding, the message |
| 1307 | * body length is determined by reading and decoding the chunked |
| 1308 | * data until the transfer coding indicates the data is complete. |
| 1309 | * |
| 1310 | * If a Transfer-Encoding header field is present in a response and |
| 1311 | * the chunked transfer coding is not the final encoding, the |
| 1312 | * message body length is determined by reading the connection until |
| 1313 | * it is closed by the server. If a Transfer-Encoding header field |
| 1314 | * is present in a request and the chunked transfer coding is not |
| 1315 | * the final encoding, the message body length cannot be determined |
| 1316 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 1317 | * status code and then close the connection. |
| 1318 | * |
| 1319 | * If a message is received with both a Transfer-Encoding and a |
| 1320 | * Content-Length header field, the Transfer-Encoding overrides the |
| 1321 | * Content-Length. Such a message might indicate an attempt to |
| 1322 | * perform request smuggling (Section 9.5) or response splitting |
| 1323 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 1324 | * remove the received Content-Length field prior to forwarding such |
| 1325 | * a message downstream. |
| 1326 | * |
| 1327 | * 4. If a message is received without Transfer-Encoding and with |
| 1328 | * either multiple Content-Length header fields having differing |
| 1329 | * field-values or a single Content-Length header field having an |
| 1330 | * invalid value, then the message framing is invalid and the |
| 1331 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 1332 | * request message, the server MUST respond with a 400 (Bad Request) |
| 1333 | * status code and then close the connection. If this is a response |
| 1334 | * message received by a proxy, the proxy MUST close the connection |
| 1335 | * to the server, discard the received response, and send a 502 (Bad |
| 1336 | * Gateway) response to the client. If this is a response message |
| 1337 | * received by a user agent, the user agent MUST close the |
| 1338 | * connection to the server and discard the received response. |
| 1339 | * |
| 1340 | * 5. If a valid Content-Length header field is present without |
| 1341 | * Transfer-Encoding, its decimal value defines the expected message |
| 1342 | * body length in octets. If the sender closes the connection or |
| 1343 | * the recipient times out before the indicated number of octets are |
| 1344 | * received, the recipient MUST consider the message to be |
| 1345 | * incomplete and close the connection. |
| 1346 | * |
| 1347 | * 6. If this is a request message and none of the above are true, then |
| 1348 | * the message body length is zero (no message body is present). |
| 1349 | * |
| 1350 | * 7. Otherwise, this is a response message without a declared message |
| 1351 | * body length, so the message body length is determined by the |
| 1352 | * number of octets received prior to the server closing the |
| 1353 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1354 | */ |
| 1355 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1356 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1357 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1358 | while (http_find_header2("Transfer-Encoding", 17, ci_head(req), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1359 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 1360 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 1361 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 1362 | /* chunked not last, return badreq */ |
| 1363 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1364 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1365 | } |
| 1366 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 1367 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1368 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 1369 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1370 | while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 1371 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 1372 | } |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1373 | else while (http_find_header2("Content-Length", 14, ci_head(req), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1374 | signed long long cl; |
| 1375 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1376 | if (!ctx.vlen) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1377 | msg->err_pos = ctx.line + ctx.val - ci_head(req); |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1378 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1379 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1380 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1381 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1382 | msg->err_pos = ctx.line + ctx.val - ci_head(req); |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1383 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1384 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1385 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1386 | if (cl < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1387 | msg->err_pos = ctx.line + ctx.val - ci_head(req); |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1388 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1389 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1390 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 1391 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1392 | msg->err_pos = ctx.line + ctx.val - ci_head(req); |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1393 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1394 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1395 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 1396 | msg->flags |= HTTP_MSGF_CNT_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 1397 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1398 | } |
| 1399 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 1400 | /* even bodyless requests have a known length */ |
| 1401 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1402 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 1403 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 1404 | * only change if both the request and the config reference something else. |
| 1405 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 1406 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 1407 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 1408 | * if FE and BE have the same settings (common). The method consists in |
| 1409 | * checking if options changed between the two calls (implying that either |
| 1410 | * one is non-null, or one of them is non-null and we are there for the first |
| 1411 | * time. |
| 1412 | */ |
| 1413 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1414 | ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 1415 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 1416 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 1417 | /* we may have to wait for the request's body */ |
| 1418 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 1419 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 1420 | req->analysers |= AN_REQ_HTTP_BODY; |
| 1421 | |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 1422 | /* |
| 1423 | * RFC7234#4: |
| 1424 | * A cache MUST write through requests with methods |
| 1425 | * that are unsafe (Section 4.2.1 of [RFC7231]) to |
| 1426 | * the origin server; i.e., a cache is not allowed |
| 1427 | * to generate a reply to such a request before |
| 1428 | * having forwarded the request and having received |
| 1429 | * a corresponding response. |
| 1430 | * |
| 1431 | * RFC7231#4.2.1: |
| 1432 | * Of the request methods defined by this |
| 1433 | * specification, the GET, HEAD, OPTIONS, and TRACE |
| 1434 | * methods are defined to be safe. |
| 1435 | */ |
| 1436 | if (likely(txn->meth == HTTP_METH_GET || |
| 1437 | txn->meth == HTTP_METH_HEAD || |
| 1438 | txn->meth == HTTP_METH_OPTIONS || |
| 1439 | txn->meth == HTTP_METH_TRACE)) |
| 1440 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 1441 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1442 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 1443 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1444 | req->analyse_exp = TICK_ETERNITY; |
| 1445 | return 1; |
| 1446 | |
| 1447 | return_bad_req: |
| 1448 | /* We centralize bad requests processing here */ |
| 1449 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 1450 | /* we detected a parsing error. We want to archive this request |
| 1451 | * in the dedicated proxy area for later troubleshooting. |
| 1452 | */ |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 1453 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1454 | } |
| 1455 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1456 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1457 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 1458 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1459 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1460 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1461 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1462 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1463 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1464 | |
| 1465 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1466 | if (!(s->flags & SF_ERR_MASK)) |
| 1467 | s->flags |= SF_ERR_PRXCOND; |
| 1468 | if (!(s->flags & SF_FINST_MASK)) |
| 1469 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1470 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1471 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1472 | req->analyse_exp = TICK_ETERNITY; |
| 1473 | return 0; |
| 1474 | } |
| 1475 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 1476 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 1477 | /* This function prepares an applet to handle the stats. It can deal with the |
| 1478 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 1479 | * and program a response message if something was unexpected. It cannot fail |
| 1480 | * and always relies on the stats applet to complete the job. It does not touch |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1481 | * analysers nor counters, which are left to the caller. It does not touch |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1482 | * s->target which is supposed to already point to the stats applet. The caller |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1483 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1484 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1485 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1486 | { |
| 1487 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1488 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1489 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1490 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1491 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1492 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 1493 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1494 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1495 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1496 | appctx = si_appctx(si); |
| 1497 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 1498 | appctx->st1 = appctx->st2 = 0; |
| 1499 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 1500 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1501 | if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn->meth != HTTP_METH_HEAD)) |
Willy Tarreau | af3cf70 | 2014-04-22 22:19:53 +0200 | [diff] [blame] | 1502 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1503 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1504 | uri = ci_head(msg->chn) + msg->sl.rq.u; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1505 | lookup = uri + uri_auth->uri_len; |
| 1506 | |
| 1507 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 1508 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1509 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1510 | break; |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | if (uri_auth->refresh) { |
| 1515 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 1516 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1517 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1518 | break; |
| 1519 | } |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 1524 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1525 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1526 | break; |
| 1527 | } |
| 1528 | } |
| 1529 | |
Willy Tarreau | 1e62df9 | 2016-01-11 18:57:53 +0100 | [diff] [blame] | 1530 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) { |
| 1531 | if (memcmp(h, ";typed", 6) == 0) { |
| 1532 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
| 1533 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
| 1534 | break; |
| 1535 | } |
| 1536 | } |
| 1537 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1538 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 1539 | if (memcmp(h, ";st=", 4) == 0) { |
| 1540 | int i; |
| 1541 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1542 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1543 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 1544 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1545 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1546 | break; |
| 1547 | } |
| 1548 | } |
| 1549 | break; |
| 1550 | } |
| 1551 | } |
| 1552 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1553 | appctx->ctx.stats.scope_str = 0; |
| 1554 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1555 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 1556 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 1557 | int itx = 0; |
| 1558 | const char *h2; |
| 1559 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 1560 | const char *err; |
| 1561 | |
| 1562 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 1563 | h2 = h; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1564 | appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn); |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1565 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 1566 | itx++; |
| 1567 | h++; |
| 1568 | } |
| 1569 | |
| 1570 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 1571 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1572 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1573 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1574 | /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1575 | memcpy(scope_txt, h2, itx); |
| 1576 | scope_txt[itx] = '\0'; |
| 1577 | err = invalid_char(scope_txt); |
| 1578 | if (err) { |
| 1579 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1580 | appctx->ctx.stats.scope_str = 0; |
| 1581 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1582 | } |
| 1583 | break; |
| 1584 | } |
| 1585 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1586 | |
| 1587 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1588 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1589 | int ret = 1; |
| 1590 | |
| 1591 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1592 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1593 | ret = acl_pass(ret); |
| 1594 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 1595 | ret = !ret; |
| 1596 | } |
| 1597 | |
| 1598 | if (ret) { |
| 1599 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1600 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1601 | break; |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 1606 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1607 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 1608 | /* we'll need the request body, possibly after sending 100-continue */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 1609 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) |
| 1610 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1611 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1612 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 1613 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1614 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 1615 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 1616 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1617 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 1618 | else { |
| 1619 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1620 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1621 | } |
| 1622 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1623 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1624 | return 1; |
| 1625 | } |
| 1626 | |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 1627 | /* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets |
| 1628 | * (as per RFC3260 #4 and BCP37 #4.2 and #5.2). |
| 1629 | */ |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 1630 | void inet_set_tos(int fd, const struct sockaddr_storage *from, int tos) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 1631 | { |
| 1632 | #ifdef IP_TOS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 1633 | if (from->ss_family == AF_INET) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 1634 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 1635 | #endif |
| 1636 | #ifdef IPV6_TCLASS |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 1637 | if (from->ss_family == AF_INET6) { |
| 1638 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr)) |
Lukas Tribus | 67db8df | 2013-06-23 17:37:13 +0200 | [diff] [blame] | 1639 | /* v4-mapped addresses need IP_TOS */ |
| 1640 | setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 1641 | else |
| 1642 | setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)); |
| 1643 | } |
| 1644 | #endif |
| 1645 | } |
| 1646 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1647 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1648 | const char* name, unsigned int name_len, |
| 1649 | const char *str, struct my_regex *re, |
| 1650 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1651 | { |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1652 | struct hdr_ctx ctx; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1653 | char *buf = ci_head(msg->chn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1654 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1655 | int (*http_find_hdr_func)(const char *name, int len, char *sol, |
| 1656 | struct hdr_idx *idx, struct hdr_ctx *ctx); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1657 | struct buffer *output = get_trash_chunk(); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1658 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1659 | ctx.idx = 0; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1660 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1661 | /* Choose the header browsing function. */ |
| 1662 | switch (action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1663 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1664 | http_find_hdr_func = http_find_header2; |
| 1665 | break; |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1666 | case ACT_HTTP_REPLACE_HDR: |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1667 | http_find_hdr_func = http_find_full_header2; |
| 1668 | break; |
| 1669 | default: /* impossible */ |
| 1670 | return -1; |
| 1671 | } |
| 1672 | |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1673 | while (http_find_hdr_func(name, name_len, buf, idx, &ctx)) { |
| 1674 | struct hdr_idx_elem *hdr = idx->v + ctx.idx; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 1675 | int delta, len; |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1676 | char *val = ctx.line + ctx.val; |
| 1677 | char* val_end = val + ctx.vlen; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1678 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1679 | if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch, 0)) |
| 1680 | continue; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1681 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 1682 | len = exp_replace(output->area, output->size, val, str, pmatch); |
| 1683 | if (len == -1) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1684 | return -1; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1685 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 1686 | delta = b_rep_blk(&msg->chn->buf, val, val_end, output->area, len); |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1687 | |
| 1688 | hdr->len += delta; |
| 1689 | http_msg_move_end(msg, delta); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1690 | |
| 1691 | /* Adjust the length of the current value of the index. */ |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1692 | ctx.vlen += delta; |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | return 0; |
| 1696 | } |
| 1697 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1698 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1699 | const char* name, unsigned int name_len, |
| 1700 | struct list *fmt, struct my_regex *re, |
| 1701 | int action) |
| 1702 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1703 | struct buffer *replace; |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 1704 | int ret = -1; |
| 1705 | |
| 1706 | replace = alloc_trash_chunk(); |
| 1707 | if (!replace) |
| 1708 | goto leave; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1709 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1710 | replace->data = build_logline(s, replace->area, replace->size, fmt); |
| 1711 | if (replace->data >= replace->size - 1) |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 1712 | goto leave; |
| 1713 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1714 | ret = http_transform_header_str(s, msg, name, name_len, replace->area, |
| 1715 | re, action); |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1716 | |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 1717 | leave: |
| 1718 | free_trash_chunk(replace); |
| 1719 | return ret; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1720 | } |
| 1721 | |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1722 | /* |
| 1723 | * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value |
| 1724 | * built according to <fmt> log line format. |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1725 | * If <early_hints> is NULL, it is allocated and the HTTP 103 response first |
| 1726 | * line is inserted before the header. If an error occurred <early_hints> is |
| 1727 | * released and NULL is returned. On success the updated buffer is returned. |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1728 | */ |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1729 | static struct buffer *http_apply_early_hint_rule(struct stream* s, struct buffer *early_hints, |
| 1730 | const char* name, unsigned int name_len, |
| 1731 | struct list *fmt) |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1732 | { |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1733 | if (!early_hints) { |
| 1734 | early_hints = alloc_trash_chunk(); |
| 1735 | if (!early_hints) |
| 1736 | goto fail; |
| 1737 | if (!chunk_memcat(early_hints, HTTP_103.ptr, HTTP_103.len)) |
| 1738 | goto fail; |
| 1739 | } |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1740 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1741 | if (!chunk_memcat(early_hints, name, name_len) || !chunk_memcat(early_hints, ": ", 2)) |
| 1742 | goto fail; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1743 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1744 | early_hints->data += build_logline(s, b_tail(early_hints), b_room(early_hints), fmt); |
| 1745 | if (!chunk_memcat(early_hints, "\r\n", 2)) |
| 1746 | goto fail; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1747 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1748 | return early_hints; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1749 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1750 | fail: |
| 1751 | free_trash_chunk(early_hints); |
| 1752 | return NULL; |
| 1753 | } |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1754 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1755 | /* Sends an HTTP 103 response. Before sending it, the last CRLF finishing the |
| 1756 | * response is added. If an error occurred or if another response was already |
| 1757 | * sent, this function does nothing. |
| 1758 | */ |
| 1759 | static void http_send_early_hints(struct stream *s, struct buffer *early_hints) |
| 1760 | { |
| 1761 | struct channel *chn = s->txn->rsp.chn; |
| 1762 | char *cur_ptr = ci_head(chn); |
| 1763 | int ret; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1764 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1765 | /* If a response was already sent, skip early hints */ |
| 1766 | if (s->txn->status > 0) |
| 1767 | return; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1768 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1769 | if (!chunk_memcat(early_hints, "\r\n", 2)) |
| 1770 | return; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1771 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1772 | ret = b_rep_blk(&chn->buf, cur_ptr, cur_ptr, b_head(early_hints), b_data(early_hints)); |
| 1773 | c_adv(chn, ret); |
| 1774 | chn->total += ret; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1775 | } |
| 1776 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1777 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 1778 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 1779 | * further processing of the request (auth, deny, ...), and defaults to |
| 1780 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 1781 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 1782 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 1783 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 1784 | * status. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1785 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 1786 | enum rule_result |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 1787 | http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1788 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1789 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 1790 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1791 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 1792 | struct act_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 1793 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 1794 | const char *auth_realm; |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1795 | struct buffer *early_hints = NULL; |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1796 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 1797 | int act_flags = 0; |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 1798 | int len; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1799 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 1800 | /* If "the current_rule_list" match the executed rule list, we are in |
| 1801 | * resume condition. If a resume is needed it is always in the action |
| 1802 | * and never in the ACL or converters. In this case, we initialise the |
| 1803 | * current rule, and go to the action execution point. |
| 1804 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 1805 | if (s->current_rule) { |
| 1806 | rule = s->current_rule; |
| 1807 | s->current_rule = NULL; |
| 1808 | if (s->current_rule_list == rules) |
| 1809 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 1810 | } |
| 1811 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 1812 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 1813 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1814 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1815 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 1816 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1817 | int ret; |
| 1818 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1819 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1820 | ret = acl_pass(ret); |
| 1821 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 1822 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1823 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1824 | |
| 1825 | if (!ret) /* condition not matched */ |
| 1826 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1827 | } |
| 1828 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 1829 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 1830 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1831 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1832 | case ACT_ACTION_ALLOW: |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1833 | rule_ret = HTTP_RULE_RES_STOP; |
| 1834 | goto end; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1835 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1836 | case ACT_ACTION_DENY: |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 1837 | if (deny_status) |
| 1838 | *deny_status = rule->deny_status; |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1839 | rule_ret = HTTP_RULE_RES_DENY; |
| 1840 | goto end; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1841 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1842 | case ACT_HTTP_REQ_TARPIT: |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 1843 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 1844 | if (deny_status) |
| 1845 | *deny_status = rule->deny_status; |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1846 | rule_ret = HTTP_RULE_RES_DENY; |
| 1847 | goto end; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 1848 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1849 | case ACT_HTTP_REQ_AUTH: |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1850 | /* Be sure to send any pending HTTP 103 response first */ |
| 1851 | if (early_hints) { |
| 1852 | http_send_early_hints(s, early_hints); |
| 1853 | free_trash_chunk(early_hints); |
| 1854 | early_hints = NULL; |
| 1855 | } |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 1856 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 1857 | auth_realm = rule->arg.auth.realm; |
| 1858 | if (!auth_realm) { |
| 1859 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 1860 | auth_realm = STATS_DEFAULT_REALM; |
| 1861 | else |
| 1862 | auth_realm = px->id; |
| 1863 | } |
| 1864 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 1865 | * count one error, because normal browsing won't significantly |
| 1866 | * increase the counter but brute force attempts will. |
| 1867 | */ |
| 1868 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 1869 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1870 | http_reply_and_close(s, txn->status, &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1871 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1872 | rule_ret = HTTP_RULE_RES_ABRT; |
| 1873 | goto end; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1874 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1875 | case ACT_HTTP_REDIR: |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1876 | /* Be sure to send any pending HTTP 103 response first */ |
| 1877 | if (early_hints) { |
| 1878 | http_send_early_hints(s, early_hints); |
| 1879 | free_trash_chunk(early_hints); |
| 1880 | early_hints = NULL; |
| 1881 | } |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1882 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 1883 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1884 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1885 | goto end; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 1886 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1887 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 1888 | s->task->nice = rule->arg.nice; |
| 1889 | break; |
| 1890 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1891 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1892 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 1893 | inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 1894 | break; |
| 1895 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1896 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 1897 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1898 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 1899 | setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark)); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 1900 | #endif |
| 1901 | break; |
| 1902 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1903 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 1904 | s->logs.level = rule->arg.loglevel; |
| 1905 | break; |
| 1906 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1907 | case ACT_HTTP_REPLACE_HDR: |
| 1908 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1909 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 1910 | rule->arg.hdr_add.name_len, |
| 1911 | &rule->arg.hdr_add.fmt, |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1912 | &rule->arg.hdr_add.re, rule->action)) { |
| 1913 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1914 | goto end; |
| 1915 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1916 | break; |
| 1917 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1918 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1919 | ctx.idx = 0; |
| 1920 | /* remove all occurrences of the header */ |
| 1921 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1922 | ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1923 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 1924 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1925 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1926 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1927 | case ACT_HTTP_SET_HDR: |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1928 | case ACT_HTTP_ADD_HDR: { |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 1929 | /* The scope of the trash buffer must be limited to this function. The |
| 1930 | * build_logline() function can execute a lot of other function which |
| 1931 | * can use the trash buffer. So for limiting the scope of this global |
| 1932 | * buffer, we build first the header value using build_logline, and |
| 1933 | * after we store the header name. |
| 1934 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1935 | struct buffer *replace; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1936 | |
| 1937 | replace = alloc_trash_chunk(); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1938 | if (!replace) { |
| 1939 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1940 | goto end; |
| 1941 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1942 | |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 1943 | len = rule->arg.hdr_add.name_len + 2, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1944 | len += build_logline(s, replace->area + len, |
| 1945 | replace->size - len, |
| 1946 | &rule->arg.hdr_add.fmt); |
| 1947 | memcpy(replace->area, rule->arg.hdr_add.name, |
| 1948 | rule->arg.hdr_add.name_len); |
| 1949 | replace->area[rule->arg.hdr_add.name_len] = ':'; |
| 1950 | replace->area[rule->arg.hdr_add.name_len + 1] = ' '; |
| 1951 | replace->data = len; |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1952 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1953 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1954 | /* remove all occurrences of the header */ |
| 1955 | ctx.idx = 0; |
| 1956 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1957 | ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1958 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 1959 | } |
| 1960 | } |
| 1961 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1962 | if (http_header_add_tail2(&txn->req, &txn->hdr_idx, replace->area, replace->data) < 0) { |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 1963 | static unsigned char rate_limit = 0; |
| 1964 | |
| 1965 | if ((rate_limit++ & 255) == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1966 | replace->area[rule->arg.hdr_add.name_len] = 0; |
| 1967 | send_log(px, LOG_WARNING, "Proxy %s failed to add or set the request header '%s' for request #%u. You might need to increase tune.maxrewrite.", px->id, |
| 1968 | replace->area, s->uniq_id); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1); |
| 1972 | if (sess->fe != s->be) |
| 1973 | HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1); |
| 1974 | if (sess->listener->counters) |
| 1975 | HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1); |
| 1976 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1977 | |
| 1978 | free_trash_chunk(replace); |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1979 | break; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1980 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1981 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1982 | case ACT_HTTP_DEL_ACL: |
| 1983 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1984 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1985 | struct buffer *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1986 | |
| 1987 | /* collect reference */ |
| 1988 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 1989 | if (!ref) |
| 1990 | continue; |
| 1991 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1992 | /* allocate key */ |
| 1993 | key = alloc_trash_chunk(); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1994 | if (!key) { |
| 1995 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1996 | goto end; |
| 1997 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1998 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1999 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2000 | key->data = build_logline(s, key->area, key->size, |
| 2001 | &rule->arg.map.key); |
| 2002 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2003 | |
| 2004 | /* perform update */ |
| 2005 | /* returned code: 1=ok, 0=ko */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2006 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2007 | pat_ref_delete(ref, key->area); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2008 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2009 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2010 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2011 | break; |
| 2012 | } |
| 2013 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2014 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2015 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2016 | struct buffer *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2017 | |
| 2018 | /* collect reference */ |
| 2019 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2020 | if (!ref) |
| 2021 | continue; |
| 2022 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2023 | /* allocate key */ |
| 2024 | key = alloc_trash_chunk(); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 2025 | if (!key) { |
| 2026 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2027 | goto end; |
| 2028 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2029 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2030 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2031 | key->data = build_logline(s, key->area, key->size, |
| 2032 | &rule->arg.map.key); |
| 2033 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2034 | |
| 2035 | /* perform update */ |
| 2036 | /* add entry only if it does not already exist */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2037 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2038 | if (pat_ref_find_elt(ref, key->area) == NULL) |
| 2039 | pat_ref_add(ref, key->area, NULL, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2040 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2041 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2042 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2043 | break; |
| 2044 | } |
| 2045 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2046 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2047 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2048 | struct buffer *key, *value; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2049 | |
| 2050 | /* collect reference */ |
| 2051 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2052 | if (!ref) |
| 2053 | continue; |
| 2054 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2055 | /* allocate key */ |
| 2056 | key = alloc_trash_chunk(); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 2057 | if (!key) { |
| 2058 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2059 | goto end; |
| 2060 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2061 | |
| 2062 | /* allocate value */ |
| 2063 | value = alloc_trash_chunk(); |
| 2064 | if (!value) { |
| 2065 | free_trash_chunk(key); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 2066 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2067 | goto end; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2068 | } |
| 2069 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2070 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2071 | key->data = build_logline(s, key->area, key->size, |
| 2072 | &rule->arg.map.key); |
| 2073 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2074 | |
| 2075 | /* collect value */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2076 | value->data = build_logline(s, value->area, |
| 2077 | value->size, |
| 2078 | &rule->arg.map.value); |
| 2079 | value->area[value->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2080 | |
| 2081 | /* perform update */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2082 | if (pat_ref_find_elt(ref, key->area) != NULL) |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2083 | /* update entry if it exists */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2084 | pat_ref_set(ref, key->area, value->area, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2085 | else |
| 2086 | /* insert a new entry */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2087 | pat_ref_add(ref, key->area, value->area, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2088 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2089 | free_trash_chunk(key); |
| 2090 | free_trash_chunk(value); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2091 | break; |
| 2092 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2093 | |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 2094 | case ACT_HTTP_EARLY_HINT: |
| 2095 | if (!(txn->req.flags & HTTP_MSGF_VER_11)) |
| 2096 | break; |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 2097 | early_hints = http_apply_early_hint_rule(s, early_hints, |
| 2098 | rule->arg.early_hint.name, |
| 2099 | rule->arg.early_hint.name_len, |
| 2100 | &rule->arg.early_hint.fmt); |
| 2101 | if (!early_hints) { |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 2102 | rule_ret = HTTP_RULE_RES_DONE; |
| 2103 | goto end; |
| 2104 | } |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 2105 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2106 | case ACT_CUSTOM: |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 2107 | if ((s->req.flags & CF_READ_ERROR) || |
| 2108 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 2109 | !(s->si[0].flags & SI_FL_CLEAN_ABRT) && |
| 2110 | (px->options & PR_O_ABRT_CLOSE))) |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2111 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 2112 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2113 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2114 | case ACT_RET_ERR: |
| 2115 | case ACT_RET_CONT: |
| 2116 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2117 | case ACT_RET_STOP: |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 2118 | rule_ret = HTTP_RULE_RES_DONE; |
| 2119 | goto end; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2120 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2121 | s->current_rule = rule; |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 2122 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2123 | goto end; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2124 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2125 | break; |
| 2126 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2127 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2128 | /* Note: only the first valid tracking parameter of each |
| 2129 | * applies. |
| 2130 | */ |
| 2131 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2132 | if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) { |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2133 | struct stktable *t; |
| 2134 | struct stksess *ts; |
| 2135 | struct stktable_key *key; |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2136 | void *ptr1, *ptr2; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2137 | |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 2138 | t = rule->arg.trk_ctr.table.t; |
| 2139 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2140 | |
| 2141 | if (key && (ts = stktable_get_entry(t, key))) { |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2142 | stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2143 | |
| 2144 | /* let's count a new HTTP request as it's the first time we do it */ |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2145 | ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 2146 | ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 2147 | if (ptr1 || ptr2) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2148 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2149 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2150 | if (ptr1) |
| 2151 | stktable_data_cast(ptr1, http_req_cnt)++; |
| 2152 | |
| 2153 | if (ptr2) |
| 2154 | update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate), |
| 2155 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 2156 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2157 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 2158 | |
| 2159 | /* If data was modified, we need to touch to re-schedule sync */ |
| 2160 | stktable_touch_local(t, ts, 0); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2161 | } |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2162 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2163 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2164 | if (sess->fe != s->be) |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2165 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 2166 | } |
| 2167 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 2168 | break; |
| 2169 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 2170 | /* other flags exists, but normaly, they never be matched. */ |
| 2171 | default: |
| 2172 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2173 | } |
| 2174 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2175 | |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 2176 | end: |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 2177 | if (early_hints) { |
| 2178 | http_send_early_hints(s, early_hints); |
| 2179 | free_trash_chunk(early_hints); |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 2180 | } |
| 2181 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 2182 | /* we reached the end of the rules, nothing to report */ |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 2183 | return rule_ret; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2184 | } |
| 2185 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2186 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2187 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 2188 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 2189 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 2190 | * is returned, the process can continue the evaluation of next rule list. If |
| 2191 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 2192 | * is returned, it means the operation could not be processed and a server error |
| 2193 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 2194 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 2195 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2196 | */ |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 2197 | enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2198 | http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s) |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2199 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2200 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 2201 | struct http_txn *txn = s->txn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2202 | struct connection *cli_conn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 2203 | struct act_rule *rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2204 | struct hdr_ctx ctx; |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2205 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2206 | int act_flags = 0; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2207 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2208 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2209 | * resume condition. If a resume is needed it is always in the action |
| 2210 | * and never in the ACL or converters. In this case, we initialise the |
| 2211 | * current rule, and go to the action execution point. |
| 2212 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2213 | if (s->current_rule) { |
| 2214 | rule = s->current_rule; |
| 2215 | s->current_rule = NULL; |
| 2216 | if (s->current_rule_list == rules) |
| 2217 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2218 | } |
| 2219 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2220 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2221 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2222 | |
| 2223 | /* check optional condition */ |
| 2224 | if (rule->cond) { |
| 2225 | int ret; |
| 2226 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2227 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2228 | ret = acl_pass(ret); |
| 2229 | |
| 2230 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2231 | ret = !ret; |
| 2232 | |
| 2233 | if (!ret) /* condition not matched */ |
| 2234 | continue; |
| 2235 | } |
| 2236 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2237 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2238 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2239 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2240 | case ACT_ACTION_ALLOW: |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2241 | rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
| 2242 | goto end; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2243 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2244 | case ACT_ACTION_DENY: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2245 | txn->flags |= TX_SVDENY; |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2246 | rule_ret = HTTP_RULE_RES_STOP; |
| 2247 | goto end; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2248 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2249 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 2250 | s->task->nice = rule->arg.nice; |
| 2251 | break; |
| 2252 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2253 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2254 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2255 | inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 2256 | break; |
| 2257 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2258 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 2259 | #ifdef SO_MARK |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2260 | if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 2261 | setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark)); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 2262 | #endif |
| 2263 | break; |
| 2264 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2265 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 2266 | s->logs.level = rule->arg.loglevel; |
| 2267 | break; |
| 2268 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2269 | case ACT_HTTP_REPLACE_HDR: |
| 2270 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 2271 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 2272 | rule->arg.hdr_add.name_len, |
| 2273 | &rule->arg.hdr_add.fmt, |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2274 | &rule->arg.hdr_add.re, rule->action)) { |
| 2275 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2276 | goto end; |
| 2277 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 2278 | break; |
| 2279 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2280 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2281 | ctx.idx = 0; |
| 2282 | /* remove all occurrences of the header */ |
| 2283 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2284 | ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2285 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 2286 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2287 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2288 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2289 | case ACT_HTTP_SET_HDR: |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2290 | case ACT_HTTP_ADD_HDR: { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2291 | struct buffer *replace; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2292 | |
| 2293 | replace = alloc_trash_chunk(); |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2294 | if (!replace) { |
| 2295 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2296 | goto end; |
| 2297 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2298 | |
| 2299 | chunk_printf(replace, "%s: ", rule->arg.hdr_add.name); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2300 | memcpy(replace->area, rule->arg.hdr_add.name, |
| 2301 | rule->arg.hdr_add.name_len); |
| 2302 | replace->data = rule->arg.hdr_add.name_len; |
| 2303 | replace->area[replace->data++] = ':'; |
| 2304 | replace->area[replace->data++] = ' '; |
| 2305 | replace->data += build_logline(s, |
| 2306 | replace->area + replace->data, |
| 2307 | replace->size - replace->data, |
| 2308 | &rule->arg.hdr_add.fmt); |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2309 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2310 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2311 | /* remove all occurrences of the header */ |
| 2312 | ctx.idx = 0; |
| 2313 | while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2314 | ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 2315 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 2316 | } |
| 2317 | } |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 2318 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2319 | if (http_header_add_tail2(&txn->rsp, &txn->hdr_idx, replace->area, replace->data) < 0) { |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 2320 | static unsigned char rate_limit = 0; |
| 2321 | |
| 2322 | if ((rate_limit++ & 255) == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2323 | replace->area[rule->arg.hdr_add.name_len] = 0; |
| 2324 | send_log(px, LOG_WARNING, "Proxy %s failed to add or set the response header '%s' for request #%u. You might need to increase tune.maxrewrite.", px->id, |
| 2325 | replace->area, s->uniq_id); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1); |
| 2329 | if (sess->fe != s->be) |
| 2330 | HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1); |
| 2331 | if (sess->listener->counters) |
| 2332 | HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1); |
| 2333 | if (objt_server(s->target)) |
| 2334 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1); |
| 2335 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2336 | |
| 2337 | free_trash_chunk(replace); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2338 | break; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2339 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2340 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2341 | case ACT_HTTP_DEL_ACL: |
| 2342 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2343 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2344 | struct buffer *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2345 | |
| 2346 | /* collect reference */ |
| 2347 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2348 | if (!ref) |
| 2349 | continue; |
| 2350 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2351 | /* allocate key */ |
| 2352 | key = alloc_trash_chunk(); |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2353 | if (!key) { |
| 2354 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2355 | goto end; |
| 2356 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2357 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2358 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2359 | key->data = build_logline(s, key->area, key->size, |
| 2360 | &rule->arg.map.key); |
| 2361 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2362 | |
| 2363 | /* perform update */ |
| 2364 | /* returned code: 1=ok, 0=ko */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2365 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2366 | pat_ref_delete(ref, key->area); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2367 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2368 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2369 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2370 | break; |
| 2371 | } |
| 2372 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2373 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2374 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2375 | struct buffer *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2376 | |
| 2377 | /* collect reference */ |
| 2378 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2379 | if (!ref) |
| 2380 | continue; |
| 2381 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2382 | /* allocate key */ |
| 2383 | key = alloc_trash_chunk(); |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2384 | if (!key) { |
| 2385 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2386 | goto end; |
| 2387 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2388 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2389 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2390 | key->data = build_logline(s, key->area, key->size, |
| 2391 | &rule->arg.map.key); |
| 2392 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2393 | |
| 2394 | /* perform update */ |
| 2395 | /* check if the entry already exists */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2396 | if (pat_ref_find_elt(ref, key->area) == NULL) |
| 2397 | pat_ref_add(ref, key->area, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2398 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2399 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2400 | break; |
| 2401 | } |
| 2402 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2403 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2404 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2405 | struct buffer *key, *value; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2406 | |
| 2407 | /* collect reference */ |
| 2408 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2409 | if (!ref) |
| 2410 | continue; |
| 2411 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2412 | /* allocate key */ |
| 2413 | key = alloc_trash_chunk(); |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2414 | if (!key) { |
| 2415 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2416 | goto end; |
| 2417 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2418 | |
| 2419 | /* allocate value */ |
| 2420 | value = alloc_trash_chunk(); |
| 2421 | if (!value) { |
| 2422 | free_trash_chunk(key); |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2423 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2424 | goto end; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2425 | } |
| 2426 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2427 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2428 | key->data = build_logline(s, key->area, key->size, |
| 2429 | &rule->arg.map.key); |
| 2430 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2431 | |
| 2432 | /* collect value */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2433 | value->data = build_logline(s, value->area, |
| 2434 | value->size, |
| 2435 | &rule->arg.map.value); |
| 2436 | value->area[value->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2437 | |
| 2438 | /* perform update */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2439 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2440 | if (pat_ref_find_elt(ref, key->area) != NULL) |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2441 | /* update entry if it exists */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2442 | pat_ref_set(ref, key->area, value->area, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2443 | else |
| 2444 | /* insert a new entry */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2445 | pat_ref_add(ref, key->area, value->area, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2446 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2447 | free_trash_chunk(key); |
| 2448 | free_trash_chunk(value); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2449 | break; |
| 2450 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2451 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2452 | case ACT_HTTP_REDIR: |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2453 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2454 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2455 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2456 | goto end; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2457 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2458 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
| 2459 | /* Note: only the first valid tracking parameter of each |
| 2460 | * applies. |
| 2461 | */ |
| 2462 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2463 | if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) { |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2464 | struct stktable *t; |
| 2465 | struct stksess *ts; |
| 2466 | struct stktable_key *key; |
| 2467 | void *ptr; |
| 2468 | |
| 2469 | t = rule->arg.trk_ctr.table.t; |
| 2470 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
| 2471 | |
| 2472 | if (key && (ts = stktable_get_entry(t, key))) { |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2473 | stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2474 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2475 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2476 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2477 | /* let's count a new HTTP request as it's the first time we do it */ |
| 2478 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 2479 | if (ptr) |
| 2480 | stktable_data_cast(ptr, http_req_cnt)++; |
| 2481 | |
| 2482 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 2483 | if (ptr) |
| 2484 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 2485 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 2486 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2487 | /* When the client triggers a 4xx from the server, it's most often due |
| 2488 | * to a missing object or permission. These events should be tracked |
| 2489 | * because if they happen often, it may indicate a brute force or a |
| 2490 | * vulnerability scan. Normally this is done when receiving the response |
| 2491 | * but here we're tracking after this ought to have been done so we have |
| 2492 | * to do it on purpose. |
| 2493 | */ |
Willy Tarreau | 3146a4c | 2016-07-26 15:22:33 +0200 | [diff] [blame] | 2494 | if ((unsigned)(txn->status - 400) < 100) { |
| 2495 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 2496 | if (ptr) |
| 2497 | stktable_data_cast(ptr, http_err_cnt)++; |
| 2498 | |
| 2499 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 2500 | if (ptr) |
| 2501 | update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
| 2502 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
| 2503 | } |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2504 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2505 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2506 | |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 2507 | /* If data was modified, we need to touch to re-schedule sync */ |
| 2508 | stktable_touch_local(t, ts, 0); |
| 2509 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2510 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
| 2511 | if (sess->fe != s->be) |
| 2512 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 2513 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2514 | } |
| 2515 | } |
| 2516 | break; |
| 2517 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2518 | case ACT_CUSTOM: |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 2519 | if ((s->req.flags & CF_READ_ERROR) || |
| 2520 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 2521 | !(s->si[0].flags & SI_FL_CLEAN_ABRT) && |
| 2522 | (px->options & PR_O_ABRT_CLOSE))) |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2523 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 2524 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2525 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2526 | case ACT_RET_ERR: |
| 2527 | case ACT_RET_CONT: |
| 2528 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2529 | case ACT_RET_STOP: |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2530 | rule_ret = HTTP_RULE_RES_STOP; |
| 2531 | goto end; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2532 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2533 | s->current_rule = rule; |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2534 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2535 | goto end; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2536 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2537 | break; |
| 2538 | |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 2539 | /* other flags exists, but normaly, they never be matched. */ |
| 2540 | default: |
| 2541 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2542 | } |
| 2543 | } |
| 2544 | |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2545 | end: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2546 | /* we reached the end of the rules, nothing to report */ |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2547 | return rule_ret; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2548 | } |
| 2549 | |
| 2550 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2551 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 2552 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 2553 | * as too large a request to build a valid response. |
| 2554 | */ |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 2555 | int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn) |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2556 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 2557 | struct http_msg *req = &txn->req; |
| 2558 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2559 | const char *msg_fmt; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2560 | struct buffer *chunk; |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2561 | int ret = 0; |
| 2562 | |
| 2563 | chunk = alloc_trash_chunk(); |
| 2564 | if (!chunk) |
| 2565 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2566 | |
| 2567 | /* build redirect message */ |
| 2568 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 2569 | case 308: |
| 2570 | msg_fmt = HTTP_308; |
| 2571 | break; |
| 2572 | case 307: |
| 2573 | msg_fmt = HTTP_307; |
| 2574 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2575 | case 303: |
| 2576 | msg_fmt = HTTP_303; |
| 2577 | break; |
| 2578 | case 301: |
| 2579 | msg_fmt = HTTP_301; |
| 2580 | break; |
| 2581 | case 302: |
| 2582 | default: |
| 2583 | msg_fmt = HTTP_302; |
| 2584 | break; |
| 2585 | } |
| 2586 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2587 | if (unlikely(!chunk_strcpy(chunk, msg_fmt))) |
| 2588 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2589 | |
| 2590 | switch(rule->type) { |
| 2591 | case REDIRECT_TYPE_SCHEME: { |
| 2592 | const char *path; |
| 2593 | const char *host; |
| 2594 | struct hdr_ctx ctx; |
| 2595 | int pathlen; |
| 2596 | int hostlen; |
| 2597 | |
| 2598 | host = ""; |
| 2599 | hostlen = 0; |
| 2600 | ctx.idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2601 | if (http_find_header2("Host", 4, ci_head(req->chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2602 | host = ctx.line + ctx.val; |
| 2603 | hostlen = ctx.vlen; |
| 2604 | } |
| 2605 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 2606 | path = http_txn_get_path(txn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2607 | /* build message using path */ |
| 2608 | if (path) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2609 | pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2610 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2611 | int qs = 0; |
| 2612 | while (qs < pathlen) { |
| 2613 | if (path[qs] == '?') { |
| 2614 | pathlen = qs; |
| 2615 | break; |
| 2616 | } |
| 2617 | qs++; |
| 2618 | } |
| 2619 | } |
| 2620 | } else { |
| 2621 | path = "/"; |
| 2622 | pathlen = 1; |
| 2623 | } |
| 2624 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2625 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2626 | /* check if we can add scheme + "://" + host + path */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2627 | if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2628 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2629 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2630 | /* add scheme */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2631 | memcpy(chunk->area + chunk->data, rule->rdr_str, |
| 2632 | rule->rdr_len); |
| 2633 | chunk->data += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2634 | } |
| 2635 | else { |
| 2636 | /* add scheme with executing log format */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2637 | chunk->data += build_logline(s, |
| 2638 | chunk->area + chunk->data, |
| 2639 | chunk->size - chunk->data, |
| 2640 | &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2641 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2642 | /* check if we can add scheme + "://" + host + path */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2643 | if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2644 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2645 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2646 | /* add "://" */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2647 | memcpy(chunk->area + chunk->data, "://", 3); |
| 2648 | chunk->data += 3; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2649 | |
| 2650 | /* add host */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2651 | memcpy(chunk->area + chunk->data, host, hostlen); |
| 2652 | chunk->data += hostlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2653 | |
| 2654 | /* add path */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2655 | memcpy(chunk->area + chunk->data, path, pathlen); |
| 2656 | chunk->data += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2657 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2658 | /* append a slash at the end of the location if needed and missing */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2659 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2660 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2661 | if (chunk->data > chunk->size - 5) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2662 | goto leave; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2663 | chunk->area[chunk->data] = '/'; |
| 2664 | chunk->data++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2665 | } |
| 2666 | |
| 2667 | break; |
| 2668 | } |
| 2669 | case REDIRECT_TYPE_PREFIX: { |
| 2670 | const char *path; |
| 2671 | int pathlen; |
| 2672 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 2673 | path = http_txn_get_path(txn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2674 | /* build message using path */ |
| 2675 | if (path) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2676 | pathlen = req->sl.rq.u_l + (ci_head(req->chn) + req->sl.rq.u) - path; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2677 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2678 | int qs = 0; |
| 2679 | while (qs < pathlen) { |
| 2680 | if (path[qs] == '?') { |
| 2681 | pathlen = qs; |
| 2682 | break; |
| 2683 | } |
| 2684 | qs++; |
| 2685 | } |
| 2686 | } |
| 2687 | } else { |
| 2688 | path = "/"; |
| 2689 | pathlen = 1; |
| 2690 | } |
| 2691 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2692 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2693 | if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2694 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2695 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2696 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2697 | * add anything, otherwise it makes it hard for the user to |
| 2698 | * configure a self-redirection. |
| 2699 | */ |
| 2700 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2701 | memcpy(chunk->area + chunk->data, |
| 2702 | rule->rdr_str, rule->rdr_len); |
| 2703 | chunk->data += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2704 | } |
| 2705 | } |
| 2706 | else { |
| 2707 | /* add prefix with executing log format */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2708 | chunk->data += build_logline(s, |
| 2709 | chunk->area + chunk->data, |
| 2710 | chunk->size - chunk->data, |
| 2711 | &rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2712 | |
| 2713 | /* Check length */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2714 | if (chunk->data + pathlen > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2715 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | /* add path */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2719 | memcpy(chunk->area + chunk->data, path, pathlen); |
| 2720 | chunk->data += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2721 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2722 | /* append a slash at the end of the location if needed and missing */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2723 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2724 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2725 | if (chunk->data > chunk->size - 5) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2726 | goto leave; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2727 | chunk->area[chunk->data] = '/'; |
| 2728 | chunk->data++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2729 | } |
| 2730 | |
| 2731 | break; |
| 2732 | } |
| 2733 | case REDIRECT_TYPE_LOCATION: |
| 2734 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2735 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2736 | if (chunk->data + rule->rdr_len > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2737 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2738 | |
| 2739 | /* add location */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2740 | memcpy(chunk->area + chunk->data, rule->rdr_str, |
| 2741 | rule->rdr_len); |
| 2742 | chunk->data += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2743 | } |
| 2744 | else { |
| 2745 | /* add location with executing log format */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2746 | chunk->data += build_logline(s, |
| 2747 | chunk->area + chunk->data, |
| 2748 | chunk->size - chunk->data, |
| 2749 | &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2750 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2751 | /* Check left length */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2752 | if (chunk->data > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2753 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2754 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2755 | break; |
| 2756 | } |
| 2757 | |
| 2758 | if (rule->cookie_len) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2759 | memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14); |
| 2760 | chunk->data += 14; |
| 2761 | memcpy(chunk->area + chunk->data, rule->cookie_str, |
| 2762 | rule->cookie_len); |
| 2763 | chunk->data += rule->cookie_len; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2764 | } |
| 2765 | |
Willy Tarreau | 19b1412 | 2017-02-28 09:48:11 +0100 | [diff] [blame] | 2766 | /* add end of headers and the keep-alive/close status. */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2767 | txn->status = rule->code; |
| 2768 | /* let's log the request time */ |
| 2769 | s->logs.tv_request = now; |
| 2770 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 2771 | if (((!(req->flags & HTTP_MSGF_TE_CHNK) && !req->body_len) || (req->msg_state == HTTP_MSG_DONE)) && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2772 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 2773 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 2774 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 2775 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2776 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2777 | memcpy(chunk->area + chunk->data, |
| 2778 | "\r\nProxy-Connection: keep-alive", 30); |
| 2779 | chunk->data += 30; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2780 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2781 | memcpy(chunk->area + chunk->data, |
| 2782 | "\r\nConnection: keep-alive", 24); |
| 2783 | chunk->data += 24; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2784 | } |
| 2785 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2786 | memcpy(chunk->area + chunk->data, "\r\n\r\n", 4); |
| 2787 | chunk->data += 4; |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2788 | FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk)); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2789 | co_inject(res->chn, chunk->area, chunk->data); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2790 | /* "eat" the request */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2791 | b_del(&req->chn->buf, req->sov); |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 2792 | req->next -= req->sov; |
| 2793 | req->sov = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2794 | s->req.analysers = AN_REQ_HTTP_XFER_BODY | (s->req.analysers & AN_REQ_FLT_END); |
Christopher Faulet | 014e39c | 2017-03-10 13:52:30 +0100 | [diff] [blame] | 2795 | s->res.analysers = AN_RES_HTTP_XFER_BODY | (s->res.analysers & AN_RES_FLT_END); |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 2796 | req->msg_state = HTTP_MSG_CLOSED; |
| 2797 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2798 | /* Trim any possible response */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2799 | b_set_data(&res->chn->buf, co_data(res->chn)); |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2800 | res->next = res->sov = 0; |
Christopher Faulet | 5d468ca | 2017-09-11 09:27:29 +0200 | [diff] [blame] | 2801 | /* let the server side turn to SI_ST_CLO */ |
| 2802 | channel_shutw_now(req->chn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2803 | } else { |
| 2804 | /* keep-alive not possible */ |
| 2805 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2806 | memcpy(chunk->area + chunk->data, |
| 2807 | "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 2808 | chunk->data += 29; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2809 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2810 | memcpy(chunk->area + chunk->data, |
| 2811 | "\r\nConnection: close\r\n\r\n", 23); |
| 2812 | chunk->data += 23; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2813 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2814 | http_reply_and_close(s, txn->status, chunk); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2815 | req->chn->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2816 | } |
| 2817 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2818 | if (!(s->flags & SF_ERR_MASK)) |
| 2819 | s->flags |= SF_ERR_LOCAL; |
| 2820 | if (!(s->flags & SF_FINST_MASK)) |
| 2821 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2822 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2823 | ret = 1; |
| 2824 | leave: |
| 2825 | free_trash_chunk(chunk); |
| 2826 | return ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2827 | } |
| 2828 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2829 | /* This stream analyser runs all HTTP request processing which is common to |
| 2830 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2831 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2832 | * 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] | 2833 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2834 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2835 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2836 | int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px) |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2837 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2838 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2839 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2840 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2841 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 2842 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2843 | enum rule_result verdict; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2844 | int deny_status = HTTP_ERR_403; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2845 | struct connection *conn = objt_conn(sess->origin); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2846 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2847 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2848 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2849 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2850 | } |
| 2851 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 2852 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2853 | now_ms, __FUNCTION__, |
| 2854 | s, |
| 2855 | req, |
| 2856 | req->rex, req->wex, |
| 2857 | req->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 2858 | ci_data(req), |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2859 | req->analysers); |
| 2860 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 2861 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2862 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 2863 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2864 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2865 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2866 | verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 2867 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2868 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2869 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 2870 | goto return_prx_yield; |
| 2871 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2872 | case HTTP_RULE_RES_CONT: |
| 2873 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 2874 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2875 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2876 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 2877 | if (txn->flags & TX_CLTARPIT) |
| 2878 | goto tarpit; |
| 2879 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2880 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2881 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 2882 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2883 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2884 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2885 | goto done; |
| 2886 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2887 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 2888 | goto return_bad_req; |
| 2889 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2890 | } |
| 2891 | |
Olivier Houchard | 25ae45a | 2017-11-29 19:51:19 +0100 | [diff] [blame] | 2892 | if (conn && (conn->flags & CO_FL_EARLY_DATA) && |
| 2893 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2894 | struct hdr_ctx ctx; |
| 2895 | |
| 2896 | ctx.idx = 0; |
| 2897 | if (!http_find_header2("Early-Data", strlen("Early-Data"), |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2898 | ci_head(&s->req), &txn->hdr_idx, &ctx)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2899 | if (unlikely(http_header_add_tail2(&txn->req, |
| 2900 | &txn->hdr_idx, "Early-Data: 1", |
Christopher Faulet | 005e79e | 2018-07-20 09:54:26 +0200 | [diff] [blame] | 2901 | strlen("Early-Data: 1")) < 0)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2902 | goto return_bad_req; |
| 2903 | } |
| 2904 | } |
| 2905 | |
| 2906 | } |
| 2907 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2908 | /* OK at this stage, we know that the request was accepted according to |
| 2909 | * the http-request rules, we can check for the stats. Note that the |
| 2910 | * URI is detected *before* the req* rules in order not to be affected |
| 2911 | * by a possible reqrep, while they are processed *after* so that a |
| 2912 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 2913 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2914 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2915 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2916 | if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2917 | txn->status = 500; |
| 2918 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2919 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2920 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2921 | if (!(s->flags & SF_ERR_MASK)) |
| 2922 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2923 | goto return_prx_cond; |
| 2924 | } |
| 2925 | |
| 2926 | /* parse the whole stats request and extract the relevant information */ |
| 2927 | http_handle_stats(s, req); |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2928 | verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status); |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2929 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2930 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2931 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 2932 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2933 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2934 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 2935 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2936 | } |
| 2937 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2938 | /* evaluate the req* rules except reqadd */ |
| 2939 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 2940 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2941 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2942 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2943 | if (txn->flags & TX_CLDENY) |
| 2944 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2945 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 2946 | if (txn->flags & TX_CLTARPIT) { |
| 2947 | deny_status = HTTP_ERR_500; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2948 | goto tarpit; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 2949 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2950 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2951 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2952 | /* add request headers from the rule sets in the same order */ |
| 2953 | list_for_each_entry(wl, &px->req_add, list) { |
| 2954 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2955 | int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2956 | ret = acl_pass(ret); |
| 2957 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 2958 | ret = !ret; |
| 2959 | if (!ret) |
| 2960 | continue; |
| 2961 | } |
| 2962 | |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 2963 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0)) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2964 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 2965 | } |
| 2966 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2967 | |
| 2968 | /* Proceed with the stats now. */ |
William Lallemand | 71bd11a | 2017-11-20 19:13:14 +0100 | [diff] [blame] | 2969 | if (unlikely(objt_applet(s->target) == &http_stats_applet) || |
| 2970 | unlikely(objt_applet(s->target) == &http_cache_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2971 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2972 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 2973 | HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2974 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2975 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 2976 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 2977 | if (!(s->flags & SF_FINST_MASK)) |
| 2978 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2979 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 2980 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2981 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 2982 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 2983 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2984 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2985 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2986 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2987 | /* check whether we have some ACLs set to redirect this request */ |
| 2988 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 2989 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2990 | int ret; |
| 2991 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2992 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 2993 | ret = acl_pass(ret); |
| 2994 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2995 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2996 | if (!ret) |
| 2997 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 2998 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2999 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 3000 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3001 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3002 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3003 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3004 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3005 | * If this happens, then the data will not come immediately, so we must |
| 3006 | * send all what we have without waiting. Note that due to the small gain |
| 3007 | * in waiting for the body of the request, it's easier to simply put the |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3008 | * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3009 | * itself once used. |
| 3010 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3011 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3012 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3013 | done: /* done with this analyser, continue with next ones that the calling |
| 3014 | * points will have set, if any. |
| 3015 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3016 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 3017 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 3018 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3019 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3020 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3021 | tarpit: |
Willy Tarreau | 6a0bca9 | 2017-06-11 17:56:27 +0200 | [diff] [blame] | 3022 | /* Allow cookie logging |
| 3023 | */ |
| 3024 | if (s->be->cookie_name || sess->fe->capture_name) |
| 3025 | manage_client_side_cookies(s, req); |
| 3026 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3027 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3028 | * which may be the same as the connect timeout if unspecified. |
| 3029 | * If unset, then set it to zero because we really want it to |
| 3030 | * eventually expire. We build the tarpit as an analyser. |
| 3031 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3032 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3033 | |
| 3034 | /* wipe the request out so that we can drop the connection early |
| 3035 | * if the client closes first. |
| 3036 | */ |
| 3037 | channel_dont_connect(req); |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3038 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 3039 | txn->status = http_err_codes[deny_status]; |
| 3040 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3041 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3042 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3043 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3044 | if (!req->analyse_exp) |
| 3045 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3046 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3047 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3048 | if (sess->fe != s->be) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3049 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3050 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3051 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 3052 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3053 | |
| 3054 | deny: /* this request was blocked (denied) */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3055 | |
| 3056 | /* Allow cookie logging |
| 3057 | */ |
| 3058 | if (s->be->cookie_name || sess->fe->capture_name) |
| 3059 | manage_client_side_cookies(s, req); |
| 3060 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 3061 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 3062 | txn->status = http_err_codes[deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3063 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3064 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3065 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3066 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3067 | if (sess->fe != s->be) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3068 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3069 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3070 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 3071 | goto return_prx_cond; |
| 3072 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3073 | return_bad_req: |
| 3074 | /* We centralize bad requests processing here */ |
| 3075 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3076 | /* we detected a parsing error. We want to archive this request |
| 3077 | * in the dedicated proxy area for later troubleshooting. |
| 3078 | */ |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 3079 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3080 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3081 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3082 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3083 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3084 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3085 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3086 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3087 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3088 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3089 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3090 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3091 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3092 | if (!(s->flags & SF_ERR_MASK)) |
| 3093 | s->flags |= SF_ERR_PRXCOND; |
| 3094 | if (!(s->flags & SF_FINST_MASK)) |
| 3095 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3096 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3097 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3098 | req->analyse_exp = TICK_ETERNITY; |
| 3099 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 3100 | |
| 3101 | return_prx_yield: |
| 3102 | channel_dont_connect(req); |
| 3103 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3104 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3105 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3106 | /* This function performs all the processing enabled for the current request. |
| 3107 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3108 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3109 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3110 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3111 | int http_process_request(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3112 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3113 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3114 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3115 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 3116 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3117 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3118 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3119 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3120 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3121 | return 0; |
| 3122 | } |
| 3123 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 3124 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3125 | now_ms, __FUNCTION__, |
| 3126 | s, |
| 3127 | req, |
| 3128 | req->rex, req->wex, |
| 3129 | req->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 3130 | ci_data(req), |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3131 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3132 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3133 | /* |
| 3134 | * Right now, we know that we have processed the entire headers |
| 3135 | * and that unwanted requests have been filtered out. We can do |
| 3136 | * whatever we want with the remaining request. Also, now we |
| 3137 | * may have separate values for ->fe, ->be. |
| 3138 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3139 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3140 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3141 | * If HTTP PROXY is set we simply get remote server address parsing |
| 3142 | * incoming request. Note that this requires that a connection is |
| 3143 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3144 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3145 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) { |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3146 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3147 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3148 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 3149 | /* Note that for now we don't reuse existing proxy connections */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 3150 | if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3151 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3152 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3153 | txn->status = 500; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3154 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3155 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3156 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3157 | if (!(s->flags & SF_ERR_MASK)) |
| 3158 | s->flags |= SF_ERR_RESOURCE; |
| 3159 | if (!(s->flags & SF_FINST_MASK)) |
| 3160 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 3161 | |
| 3162 | return 0; |
| 3163 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3164 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 3165 | path = http_txn_get_path(txn); |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3166 | if (url2sa(ci_head(req) + msg->sl.rq.u, |
| 3167 | path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l, |
Christopher Faulet | 11ebb20 | 2018-04-13 15:53:12 +0200 | [diff] [blame] | 3168 | &conn->addr.to, NULL) == -1) |
| 3169 | goto return_bad_req; |
| 3170 | |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3171 | /* if the path was found, we have to remove everything between |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3172 | * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not |
| 3173 | * found, we need to replace from ci_head(req) + msg->sl.rq.u for |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3174 | * u_l characters by a single "/". |
| 3175 | */ |
| 3176 | if (path) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3177 | char *cur_ptr = ci_head(req); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3178 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3179 | int delta; |
| 3180 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3181 | delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3182 | http_msg_move_end(&txn->req, delta); |
| 3183 | cur_end += delta; |
| 3184 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3185 | goto return_bad_req; |
| 3186 | } |
| 3187 | else { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3188 | char *cur_ptr = ci_head(req); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3189 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 3190 | int delta; |
| 3191 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3192 | delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3193 | cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 3194 | http_msg_move_end(&txn->req, delta); |
| 3195 | cur_end += delta; |
| 3196 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 3197 | goto return_bad_req; |
| 3198 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3199 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3200 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3201 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3202 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3203 | * Note that doing so might move headers in the request, but |
| 3204 | * the fields will stay coherent and the URI will not move. |
| 3205 | * This should only be performed in the backend. |
| 3206 | */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 3207 | if (s->be->cookie_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3208 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3209 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3210 | /* add unique-id if "header-unique-id" is specified */ |
| 3211 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 3212 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3213 | if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3214 | goto return_bad_req; |
| 3215 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3216 | build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id); |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 3217 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3218 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3219 | if (sess->fe->header_unique_id && s->unique_id) { |
Willy Tarreau | 5f6333c | 2018-08-22 05:14:37 +0200 | [diff] [blame] | 3220 | if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3221 | goto return_bad_req; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3222 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0)) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3223 | goto return_bad_req; |
| 3224 | } |
| 3225 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3226 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3227 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3228 | * asks for it. |
| 3229 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3230 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3231 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3232 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 3233 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 3234 | s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3235 | ci_head(req), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3236 | /* The header is set to be added only if none is present |
| 3237 | * and we found it, so don't do anything. |
| 3238 | */ |
| 3239 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3240 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3241 | /* Add an X-Forwarded-For header unless the source IP is |
| 3242 | * in the 'except' network range. |
| 3243 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3244 | if ((!sess->fe->except_mask.s_addr || |
| 3245 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 3246 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3247 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3248 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3249 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3250 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3251 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3252 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3253 | |
| 3254 | /* Note: we rely on the backend to get the header name to be used for |
| 3255 | * x-forwarded-for, because the header is really meant for the backends. |
| 3256 | * However, if the backend did not specify any option, we have to rely |
| 3257 | * on the frontend's header name. |
| 3258 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3259 | if (s->be->fwdfor_hdr_len) { |
| 3260 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3261 | memcpy(trash.area, |
| 3262 | s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3263 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3264 | len = sess->fe->fwdfor_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3265 | memcpy(trash.area, |
| 3266 | sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3267 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3268 | len += snprintf(trash.area + len, |
| 3269 | trash.size - len, |
| 3270 | ": %d.%d.%d.%d", pn[0], pn[1], |
| 3271 | pn[2], pn[3]); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 3272 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3273 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3274 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3275 | } |
| 3276 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3277 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3278 | /* FIXME: for the sake of completeness, we should also support |
| 3279 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3280 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3281 | int len; |
| 3282 | char pn[INET6_ADDRSTRLEN]; |
| 3283 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3284 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3285 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3286 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3287 | /* Note: we rely on the backend to get the header name to be used for |
| 3288 | * x-forwarded-for, because the header is really meant for the backends. |
| 3289 | * However, if the backend did not specify any option, we have to rely |
| 3290 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3291 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3292 | if (s->be->fwdfor_hdr_len) { |
| 3293 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3294 | memcpy(trash.area, s->be->fwdfor_hdr_name, |
| 3295 | len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3296 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3297 | len = sess->fe->fwdfor_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3298 | memcpy(trash.area, sess->fe->fwdfor_hdr_name, |
| 3299 | len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3300 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3301 | len += snprintf(trash.area + len, trash.size - len, |
| 3302 | ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3303 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3304 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3305 | goto return_bad_req; |
| 3306 | } |
| 3307 | } |
| 3308 | |
| 3309 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3310 | * 10: add X-Original-To if either the frontend or the backend |
| 3311 | * asks for it. |
| 3312 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3313 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3314 | |
| 3315 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3316 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3317 | /* Add an X-Original-To header unless the destination IP is |
| 3318 | * in the 'except' network range. |
| 3319 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3320 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3321 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3322 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3323 | ((!sess->fe->except_mask_to.s_addr || |
| 3324 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 3325 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3326 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3327 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3328 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3329 | int len; |
| 3330 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 3331 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3332 | |
| 3333 | /* Note: we rely on the backend to get the header name to be used for |
| 3334 | * x-original-to, because the header is really meant for the backends. |
| 3335 | * However, if the backend did not specify any option, we have to rely |
| 3336 | * on the frontend's header name. |
| 3337 | */ |
| 3338 | if (s->be->orgto_hdr_len) { |
| 3339 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3340 | memcpy(trash.area, |
| 3341 | s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3342 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3343 | len = sess->fe->orgto_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3344 | memcpy(trash.area, |
| 3345 | sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3346 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3347 | len += snprintf(trash.area + len, |
| 3348 | trash.size - len, |
| 3349 | ": %d.%d.%d.%d", pn[0], pn[1], |
| 3350 | pn[2], pn[3]); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3351 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3352 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3353 | goto return_bad_req; |
| 3354 | } |
| 3355 | } |
| 3356 | } |
| 3357 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3358 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 3359 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 3360 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 3361 | * "Upgrade" is present in the Connection header. |
| 3362 | */ |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 3363 | if (!(txn->flags & TX_HDR_CONN_UPG) && (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3364 | unsigned int want_flags = 0; |
| 3365 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3366 | if (msg->flags & HTTP_MSGF_VER_11) { |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 3367 | if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3368 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3369 | want_flags |= TX_CON_CLO_SET; |
| 3370 | } else { |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 3371 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3372 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3373 | want_flags |= TX_CON_KAL_SET; |
| 3374 | } |
| 3375 | |
| 3376 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3377 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3378 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3379 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3380 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3381 | /* If we have no server assigned yet and we're balancing on url_param |
| 3382 | * with a POST request, we may be interested in checking the body for |
| 3383 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3384 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3385 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3386 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3387 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3388 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3389 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3390 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3391 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3392 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 3393 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3394 | #ifdef TCP_QUICKACK |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3395 | /* We expect some data from the client. Unless we know for sure |
| 3396 | * we already have a full request, we have to re-enable quick-ack |
| 3397 | * in case we previously disabled it, otherwise we might cause |
| 3398 | * the client to delay further data. |
| 3399 | */ |
| 3400 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
| 3401 | cli_conn && conn_ctrl_ready(cli_conn) && |
| 3402 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3403 | (msg->body_len > ci_data(req) - txn->req.eoh - 2))) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 3404 | setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3405 | #endif |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3406 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3407 | /************************************************************* |
| 3408 | * OK, that's finished for the headers. We have done what we * |
| 3409 | * could. Let's switch to the DATA state. * |
| 3410 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3411 | req->analyse_exp = TICK_ETERNITY; |
| 3412 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3413 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3414 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3415 | /* OK let's go on with the BODY now */ |
| 3416 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3417 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3418 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3419 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3420 | /* we detected a parsing error. We want to archive this request |
| 3421 | * in the dedicated proxy area for later troubleshooting. |
| 3422 | */ |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 3423 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3424 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3425 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3426 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3427 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3428 | txn->status = 400; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3429 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3430 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3431 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3432 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3433 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3434 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3435 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3436 | if (!(s->flags & SF_ERR_MASK)) |
| 3437 | s->flags |= SF_ERR_PRXCOND; |
| 3438 | if (!(s->flags & SF_FINST_MASK)) |
| 3439 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3440 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3441 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3442 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3443 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3444 | * returns zero, at the beginning because it prevents any other processing |
| 3445 | * from occurring, and at the end because it terminates the request. |
| 3446 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3447 | int http_process_tarpit(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3448 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3449 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3450 | |
| 3451 | /* This connection is being tarpitted. The CLIENT side has |
| 3452 | * already set the connect expiration date to the right |
| 3453 | * timeout. We just have to check that the client is still |
| 3454 | * there and that the timeout has not expired. |
| 3455 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3456 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3457 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3458 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3459 | return 0; |
| 3460 | |
| 3461 | /* We will set the queue timer to the time spent, just for |
| 3462 | * logging purposes. We fake a 500 server error, so that the |
| 3463 | * attacker will not suspect his connection has been tarpitted. |
| 3464 | * It will not cause trouble to the logs because we can exclude |
| 3465 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3466 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3467 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3468 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3469 | if (!(req->flags & CF_READ_ERROR)) |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3470 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3471 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3472 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3473 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3474 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3475 | if (!(s->flags & SF_ERR_MASK)) |
| 3476 | s->flags |= SF_ERR_PRXCOND; |
| 3477 | if (!(s->flags & SF_FINST_MASK)) |
| 3478 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3479 | return 0; |
| 3480 | } |
| 3481 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 3482 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 3483 | * for either the buffer to be full, or the full advertised contents to have |
| 3484 | * reached the buffer. It must only be called after the standard HTTP request |
| 3485 | * processing has occurred, because it expects the request to be parsed and will |
| 3486 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 3487 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 3488 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 3489 | * needs to read more data, or 1 once it has completed its analysis. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3490 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3491 | int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3492 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3493 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3494 | struct http_txn *txn = s->txn; |
| 3495 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3496 | |
| 3497 | /* We have to parse the HTTP request body to find any required data. |
| 3498 | * "balance url_param check_post" should have been the only way to get |
| 3499 | * into this. We were brought here after HTTP header analysis, so all |
| 3500 | * related structures are ready. |
| 3501 | */ |
| 3502 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3503 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 3504 | /* This is the first call */ |
| 3505 | if (msg->msg_state < HTTP_MSG_BODY) |
| 3506 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3507 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3508 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3509 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3510 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3511 | */ |
| 3512 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 3513 | struct hdr_ctx ctx; |
| 3514 | ctx.idx = 0; |
| 3515 | /* Expect is allowed in 1.1, look for it */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3516 | if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) && |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3517 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 3518 | co_inject(&s->res, HTTP_100.ptr, HTTP_100.len); |
Thierry FOURNIER / OZON.IO | 43ad11d | 2016-12-12 15:19:58 +0100 | [diff] [blame] | 3519 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3520 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3521 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3522 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3523 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3524 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 3525 | /* we have msg->sov which points to the first byte of message body. |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3526 | * ci_head(req) still points to the beginning of the message. We |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3527 | * must save the body in msg->next because it survives buffer |
| 3528 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3529 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 3530 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3531 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3532 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3533 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3534 | else |
| 3535 | msg->msg_state = HTTP_MSG_DATA; |
| 3536 | } |
| 3537 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3538 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 3539 | /* We're in content-length mode, we just have to wait for enough data. */ |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 3540 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3541 | goto missing_data; |
| 3542 | |
| 3543 | /* OK we have everything we need now */ |
| 3544 | goto http_end; |
| 3545 | } |
| 3546 | |
| 3547 | /* OK here we're parsing a chunked-encoded message */ |
| 3548 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3549 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3550 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3551 | * set ->sov and ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3552 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3553 | */ |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 3554 | unsigned int chunk; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3555 | int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3556 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3557 | if (!ret) |
| 3558 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3559 | else if (ret < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3560 | msg->err_pos = ci_data(req) + ret; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 3561 | if (msg->err_pos < 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3562 | msg->err_pos += req->buf.size; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3563 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3564 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3565 | } |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 3566 | |
| 3567 | msg->chunk_len = chunk; |
| 3568 | msg->body_len += chunk; |
| 3569 | |
| 3570 | msg->sol = ret; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 3571 | msg->next += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3572 | msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3573 | } |
| 3574 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3575 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 3576 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 3577 | * for at least a whole chunk or the whole content length bytes after |
| 3578 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3579 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3580 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 3581 | goto http_end; |
| 3582 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 3583 | if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3584 | goto http_end; |
| 3585 | |
| 3586 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 3587 | /* we get here if we need to wait for more data. If the buffer is full, |
| 3588 | * we have the maximum we can expect. |
| 3589 | */ |
Willy Tarreau | 2375233 | 2018-06-15 14:54:53 +0200 | [diff] [blame] | 3590 | if (channel_full(req, global.tune.maxrewrite)) |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 3591 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3592 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3593 | if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3594 | txn->status = 408; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3595 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3596 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3597 | if (!(s->flags & SF_ERR_MASK)) |
| 3598 | s->flags |= SF_ERR_CLITO; |
| 3599 | if (!(s->flags & SF_FINST_MASK)) |
| 3600 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3601 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3602 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3603 | |
| 3604 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 3605 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3606 | /* Not enough data. We'll re-use the http-request |
| 3607 | * timeout here. Ideally, we should set the timeout |
| 3608 | * relative to the accept() date. We just set the |
| 3609 | * request timeout once at the beginning of the |
| 3610 | * request. |
| 3611 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3612 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3613 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3614 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3615 | return 0; |
| 3616 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3617 | |
| 3618 | http_end: |
| 3619 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3620 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3621 | req->analysers &= ~an_bit; |
| 3622 | req->analyse_exp = TICK_ETERNITY; |
| 3623 | return 1; |
| 3624 | |
| 3625 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3626 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3627 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3628 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3629 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3630 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3631 | if (!(s->flags & SF_ERR_MASK)) |
| 3632 | s->flags |= SF_ERR_PRXCOND; |
| 3633 | if (!(s->flags & SF_FINST_MASK)) |
| 3634 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3635 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3636 | return_err_msg: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3637 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3638 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3639 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3640 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3641 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3642 | } |
| 3643 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3644 | /* send a server's name with an outgoing request over an established connection. |
| 3645 | * Note: this function is designed to be called once the request has been scheduled |
| 3646 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 3647 | * proceeding. |
| 3648 | */ |
Christopher Faulet | 27a3dc8 | 2018-10-23 15:34:07 +0200 | [diff] [blame] | 3649 | int http_send_name_header(struct stream *s, struct proxy* be, const char* srv_name) { |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3650 | |
| 3651 | struct hdr_ctx ctx; |
Christopher Faulet | 27a3dc8 | 2018-10-23 15:34:07 +0200 | [diff] [blame] | 3652 | struct http_txn *txn = s->txn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3653 | char *hdr_name = be->server_id_hdr_name; |
| 3654 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 3655 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3656 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3657 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3658 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 3659 | ctx.idx = 0; |
| 3660 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 3661 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3662 | if (old_o) { |
| 3663 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3664 | c_rew(chn, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3665 | txn->req.next += old_o; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3666 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3667 | } |
| 3668 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3669 | old_i = ci_data(chn); |
| 3670 | while (http_find_header2(hdr_name, hdr_name_len, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) { |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3671 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3672 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3673 | } |
| 3674 | |
| 3675 | /* Add the new header requested with the server value */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3676 | hdr_val = trash.area; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3677 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 3678 | hdr_val += hdr_name_len; |
| 3679 | *hdr_val++ = ':'; |
| 3680 | *hdr_val++ = ' '; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3681 | hdr_val += strlcpy2(hdr_val, srv_name, |
| 3682 | trash.area + trash.size - hdr_val); |
| 3683 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, |
| 3684 | hdr_val - trash.area); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3685 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3686 | if (old_o) { |
| 3687 | /* If this was a forwarded request, we must readjust the amount of |
| 3688 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3689 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 3690 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3691 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3692 | old_o += ci_data(chn) - old_i; |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3693 | c_adv(chn, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3694 | txn->req.next -= old_o; |
| 3695 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3696 | } |
| 3697 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3698 | return 0; |
| 3699 | } |
| 3700 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3701 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3702 | * right now but it works. |
| 3703 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3704 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3705 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3706 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 3707 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 3708 | struct proxy *be = s->be; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 3709 | struct conn_stream *cs; |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 3710 | struct connection *srv_conn; |
| 3711 | struct server *srv; |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 3712 | unsigned int prev_flags = s->txn->flags; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 3713 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3714 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3715 | * server's connections. We need a safer way to reinitialize buffer |
| 3716 | * flags. We also need a more accurate method for computing per-request |
| 3717 | * data. |
| 3718 | */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 3719 | cs = objt_cs(s->si[1].end); |
| 3720 | srv_conn = cs_conn(cs); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3721 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3722 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 3723 | * to the server. |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 3724 | * XXX cognet: If the connection doesn't have a owner then it may not |
| 3725 | * be referenced anywhere, just kill it now, even if it could be reused. |
| 3726 | * To be revisited later when revisited later when we handle connection |
| 3727 | * pools properly. |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3728 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3729 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 3730 | !si_conn_ready(&s->si[1]) || !srv_conn->owner) { |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3731 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 3732 | si_shutr(&s->si[1]); |
| 3733 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3734 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3735 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3736 | if (s->flags & SF_BE_ASSIGNED) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3737 | HA_ATOMIC_SUB(&be->beconn, 1); |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 3738 | if (unlikely(s->srv_conn)) |
| 3739 | sess_change_server(s, NULL); |
| 3740 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3741 | |
| 3742 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3743 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3744 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3745 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3746 | int n; |
| 3747 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3748 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3749 | if (n < 1 || n > 5) |
| 3750 | n = 0; |
| 3751 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3752 | if (fe->mode == PR_MODE_HTTP) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3753 | HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1); |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 3754 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3755 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 3756 | (be->mode == PR_MODE_HTTP)) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3757 | HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1); |
| 3758 | HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1); |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 3759 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3760 | } |
| 3761 | |
| 3762 | /* don't count other requests' data */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3763 | s->logs.bytes_in -= ci_data(&s->req); |
| 3764 | s->logs.bytes_out -= ci_data(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3765 | |
Willy Tarreau | 66425e3 | 2018-07-25 06:55:12 +0200 | [diff] [blame] | 3766 | /* we may need to know the position in the queue */ |
| 3767 | pendconn_free(s); |
| 3768 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3769 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3770 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3771 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3772 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3773 | s->do_log(s); |
| 3774 | } |
| 3775 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 3776 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3777 | stream_stop_content_counters(s); |
| 3778 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 3779 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3780 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3781 | s->logs.tv_accept = now; /* corrected date for internal use */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 3782 | s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */ |
| 3783 | s->logs.t_idle = -1; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3784 | tv_zero(&s->logs.tv_request); |
| 3785 | s->logs.t_queue = -1; |
| 3786 | s->logs.t_connect = -1; |
| 3787 | s->logs.t_data = -1; |
| 3788 | s->logs.t_close = 0; |
Patrick Hemmer | ffe5e8c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 3789 | s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */ |
| 3790 | s->logs.srv_queue_pos = 0; /* we will get this number soon */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3791 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3792 | s->logs.bytes_in = s->req.total = ci_data(&s->req); |
| 3793 | s->logs.bytes_out = s->res.total = ci_data(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3794 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3795 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3796 | if (s->flags & SF_CURR_SESS) { |
| 3797 | s->flags &= ~SF_CURR_SESS; |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 3798 | HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3799 | } |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 3800 | if (may_dequeue_tasks(objt_server(s->target), be)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3801 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3802 | } |
| 3803 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3804 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3805 | |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 3806 | |
| 3807 | /* If we're doing keepalive, first call the mux detach() method |
| 3808 | * to let it know we want to detach without freing the connection. |
| 3809 | * We then can call si_release_endpoint() to destroy the conn_stream |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3810 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3811 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 3812 | !si_conn_ready(&s->si[1]) || !srv_conn->owner) |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 3813 | srv_conn = NULL; |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 3814 | si_release_endpoint(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3815 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3816 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 3817 | s->si[1].err_type = SI_ET_NONE; |
| 3818 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 3819 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3820 | s->si[1].flags &= SI_FL_ISBACK | SI_FL_DONT_WAKE; /* we're in the context of process_stream */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3821 | s->req.flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT|CF_WAKE_CONNECT|CF_WROTE_DATA); |
Willy Tarreau | ede3d88 | 2018-10-24 17:17:56 +0200 | [diff] [blame] | 3822 | s->res.flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT|CF_WROTE_DATA); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3823 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 3824 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 3825 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 3826 | |
Patrick Hemmer | e3faf02 | 2018-08-22 10:02:00 -0400 | [diff] [blame] | 3827 | hlua_ctx_destroy(s->hlua); |
| 3828 | s->hlua = NULL; |
| 3829 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3830 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3831 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3832 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 3833 | |
| 3834 | if (prev_status == 401 || prev_status == 407) { |
| 3835 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 3836 | * a chance of being able to send the visitor again to the same |
| 3837 | * server over the same connection. This is required by some |
| 3838 | * broken protocols such as NTLM, and anyway whenever there is |
| 3839 | * an opportunity for sending the challenge to the proper place, |
Lukas Tribus | 80512b1 | 2018-10-27 20:07:40 +0200 | [diff] [blame] | 3840 | * it's better to do it (at least it helps with debugging), at |
| 3841 | * least for non-deterministic load balancing algorithms. |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 3842 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3843 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 3844 | } |
| 3845 | |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 3846 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3847 | if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3848 | srv_conn->flags |= CO_FL_PRIVATE; |
| 3849 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3850 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3851 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3852 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3853 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3854 | s->req.flags |= CF_NEVER_WAIT; |
| 3855 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 3856 | } |
| 3857 | |
Willy Tarreau | 714ea78 | 2015-11-25 20:11:11 +0100 | [diff] [blame] | 3858 | /* we're removing the analysers, we MUST re-enable events detection. |
| 3859 | * We don't enable close on the response channel since it's either |
| 3860 | * already closed, or in keep-alive with an idle connection handler. |
| 3861 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3862 | channel_auto_read(&s->req); |
| 3863 | channel_auto_close(&s->req); |
| 3864 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3865 | |
Willy Tarreau | 1c59bd5 | 2015-11-02 20:20:11 +0100 | [diff] [blame] | 3866 | /* we're in keep-alive with an idle connection, monitor it if not already done */ |
| 3867 | if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 3868 | srv = objt_server(srv_conn->target); |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 3869 | if (srv) { |
| 3870 | if (srv_conn->flags & CO_FL_PRIVATE) |
| 3871 | LIST_ADD(&srv->priv_conns[tid], &srv_conn->list); |
| 3872 | else if (prev_flags & TX_NOT_FIRST) |
| 3873 | /* note: we check the request, not the connection, but |
| 3874 | * this is valid for strategies SAFE and AGGR, and in |
| 3875 | * case of ALWS, we don't care anyway. |
| 3876 | */ |
| 3877 | LIST_ADD(&srv->safe_conns[tid], &srv_conn->list); |
| 3878 | else |
| 3879 | LIST_ADD(&srv->idle_conns[tid], &srv_conn->list); |
| 3880 | } |
Willy Tarreau | 4320eaa | 2015-08-05 11:08:30 +0200 | [diff] [blame] | 3881 | } |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 3882 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
| 3883 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3884 | } |
| 3885 | |
| 3886 | |
| 3887 | /* This function updates the request state machine according to the response |
| 3888 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3889 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3890 | * it is only used to find when a request/response couple is complete. Both |
| 3891 | * this function and its equivalent should loop until both return zero. It |
| 3892 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3893 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3894 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3895 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3896 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3897 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3898 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3899 | unsigned int old_state = txn->req.msg_state; |
| 3900 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 3901 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3902 | return 0; |
| 3903 | |
| 3904 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3905 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 3906 | * We can shut the read side unless we want to abort_on_close, |
| 3907 | * or we have a POST request. The issue with POST requests is |
| 3908 | * that some browsers still send a CRLF after the request, and |
| 3909 | * this CRLF must be read so that it does not remain in the kernel |
| 3910 | * buffers, otherwise a close could cause an RST on some systems |
| 3911 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 3912 | * Note that if we're using keep-alive on the client side, we'd |
| 3913 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3914 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 3915 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3916 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 3917 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 3918 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 3919 | (!(s->be->options & PR_O_ABRT_CLOSE) || |
| 3920 | (s->si[0].flags & SI_FL_CLEAN_ABRT)) && |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 3921 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3922 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3923 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 3924 | /* if the server closes the connection, we want to immediately react |
| 3925 | * and close the socket to save packets and syscalls. |
| 3926 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3927 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 3928 | |
Willy Tarreau | 7f876a1 | 2015-11-18 11:59:55 +0100 | [diff] [blame] | 3929 | /* In any case we've finished parsing the request so we must |
| 3930 | * disable Nagle when sending data because 1) we're not going |
| 3931 | * to shut this side, and 2) the server is waiting for us to |
| 3932 | * send pending data. |
| 3933 | */ |
| 3934 | chn->flags |= CF_NEVER_WAIT; |
| 3935 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3936 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 3937 | goto wait_other_side; |
| 3938 | |
| 3939 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 3940 | /* The server has not finished to respond, so we |
| 3941 | * don't want to move in order not to upset it. |
| 3942 | */ |
| 3943 | goto wait_other_side; |
| 3944 | } |
| 3945 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3946 | /* When we get here, it means that both the request and the |
| 3947 | * response have finished receiving. Depending on the connection |
| 3948 | * mode, we'll have to wait for the last bytes to leave in either |
| 3949 | * direction, and sometimes for a close to be effective. |
| 3950 | */ |
| 3951 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3952 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3953 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3954 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 3955 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3956 | } |
| 3957 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3958 | /* Option forceclose is set, or either side wants to close, |
| 3959 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3960 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3961 | * once both states are CLOSED. |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 3962 | * |
| 3963 | * However, there is an exception if the response |
| 3964 | * length is undefined. In this case, we need to wait |
| 3965 | * the close from the server. The response will be |
| 3966 | * switched in TUNNEL mode until the end. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3967 | */ |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 3968 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 3969 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
| 3970 | goto check_channel_flags; |
| 3971 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3972 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 3973 | channel_shutr_now(chn); |
| 3974 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3975 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3976 | } |
| 3977 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3978 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 3979 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3980 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3981 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 3982 | channel_auto_read(chn); |
| 3983 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3984 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3985 | } |
| 3986 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 3987 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3988 | } |
| 3989 | |
| 3990 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 3991 | http_msg_closing: |
| 3992 | /* nothing else to forward, just waiting for the output buffer |
| 3993 | * to be empty and for the shutw_now to take effect. |
| 3994 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3995 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3996 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3997 | goto http_msg_closed; |
| 3998 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3999 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4000 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4001 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4002 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 4003 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4004 | } |
| 4005 | |
| 4006 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4007 | http_msg_closed: |
Willy Tarreau | 8059351 | 2017-12-14 10:43:31 +0100 | [diff] [blame] | 4008 | /* if we don't know whether the server will close, we need to hard close */ |
| 4009 | if (txn->rsp.flags & HTTP_MSGF_XFER_LEN) |
| 4010 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 4011 | |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 4012 | /* see above in MSG_DONE why we only do this in these states */ |
| 4013 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 4014 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 4015 | (!(s->be->options & PR_O_ABRT_CLOSE) || |
| 4016 | (s->si[0].flags & SI_FL_CLEAN_ABRT))) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 4017 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4018 | goto wait_other_side; |
| 4019 | } |
| 4020 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4021 | check_channel_flags: |
| 4022 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4023 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4024 | /* if we've just closed an output, let's switch */ |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4025 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4026 | goto http_msg_closing; |
| 4027 | } |
| 4028 | |
| 4029 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4030 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4031 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4032 | } |
| 4033 | |
| 4034 | |
| 4035 | /* This function updates the response state machine according to the request |
| 4036 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4037 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4038 | * it is only used to find when a request/response couple is complete. Both |
| 4039 | * this function and its equivalent should loop until both return zero. It |
| 4040 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4041 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4042 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4043 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4044 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4045 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4046 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4047 | unsigned int old_state = txn->rsp.msg_state; |
| 4048 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4049 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4050 | return 0; |
| 4051 | |
| 4052 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4053 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4054 | * still monitor the server connection for a possible close |
| 4055 | * while the request is being uploaded, so we don't disable |
| 4056 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4057 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4058 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4059 | |
| 4060 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4061 | goto wait_other_side; |
| 4062 | |
| 4063 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4064 | /* The client seems to still be sending data, probably |
| 4065 | * because we got an error response during an upload. |
| 4066 | * We have the choice of either breaking the connection |
| 4067 | * or letting it pass through. Let's do the later. |
| 4068 | */ |
| 4069 | goto wait_other_side; |
| 4070 | } |
| 4071 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4072 | /* When we get here, it means that both the request and the |
| 4073 | * response have finished receiving. Depending on the connection |
| 4074 | * mode, we'll have to wait for the last bytes to leave in either |
| 4075 | * direction, and sometimes for a close to be effective. |
| 4076 | */ |
| 4077 | |
| 4078 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4079 | /* Server-close mode : shut read and wait for the request |
| 4080 | * side to close its output buffer. The caller will detect |
| 4081 | * when we're in DONE and the other is in CLOSED and will |
| 4082 | * catch that for the final cleanup. |
| 4083 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4084 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 4085 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4086 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4087 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4088 | /* Option forceclose is set, or either side wants to close, |
| 4089 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4090 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4091 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4092 | */ |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 4093 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4094 | channel_shutr_now(chn); |
| 4095 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4096 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4097 | } |
| 4098 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4099 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 4100 | * need to forward remaining data. Keep-alive will need to monitor |
| 4101 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4102 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4103 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4104 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4105 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 4106 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4107 | } |
| 4108 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4109 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4113 | http_msg_closing: |
| 4114 | /* nothing else to forward, just waiting for the output buffer |
| 4115 | * to be empty and for the shutw_now to take effect. |
| 4116 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4117 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4118 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4119 | goto http_msg_closed; |
| 4120 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4121 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | a3992e0 | 2017-07-18 10:35:55 +0200 | [diff] [blame] | 4122 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4123 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4124 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4125 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4126 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4127 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 4128 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4129 | } |
| 4130 | |
| 4131 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4132 | http_msg_closed: |
| 4133 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4134 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4135 | channel_auto_close(chn); |
| 4136 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4137 | goto wait_other_side; |
| 4138 | } |
| 4139 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 4140 | check_channel_flags: |
| 4141 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4142 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4143 | /* if we've just closed an output, let's switch */ |
| 4144 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4145 | goto http_msg_closing; |
| 4146 | } |
| 4147 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4148 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4149 | /* We force the response to leave immediately if we're waiting for the |
| 4150 | * other side, since there is no pending shutdown to push it out. |
| 4151 | */ |
| 4152 | if (!channel_is_empty(chn)) |
| 4153 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4154 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4155 | } |
| 4156 | |
| 4157 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4158 | /* Resync the request and response state machines. */ |
| 4159 | void http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4160 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4161 | struct http_txn *txn = s->txn; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4162 | #ifdef DEBUG_FULL |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4163 | int old_req_state = txn->req.msg_state; |
| 4164 | int old_res_state = txn->rsp.msg_state; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4165 | #endif |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4166 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4167 | http_sync_req_state(s); |
| 4168 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4169 | if (!http_sync_res_state(s)) |
| 4170 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4171 | if (!http_sync_req_state(s)) |
| 4172 | break; |
| 4173 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 4174 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4175 | DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s " |
| 4176 | "req->analysers=0x%08x res->analysers=0x%08x\n", |
| 4177 | now_ms, __FUNCTION__, s, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 4178 | h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state), |
| 4179 | h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state), |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4180 | s->req.analysers, s->res.analysers); |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4181 | |
| 4182 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4183 | /* OK, both state machines agree on a compatible state. |
| 4184 | * There are a few cases we're interested in : |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4185 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4186 | * directions, so let's simply disable both analysers. |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4187 | * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either |
| 4188 | * means we must abort the request. |
| 4189 | * - HTTP_MSG_TUNNEL on either means we have to disable analyser on |
| 4190 | * corresponding channel. |
| 4191 | * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE |
| 4192 | * on the response with server-close mode means we've completed one |
| 4193 | * request and we must re-initialize the server connection. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4194 | */ |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4195 | if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4196 | txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4197 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4198 | channel_auto_close(&s->req); |
| 4199 | channel_auto_read(&s->req); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4200 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4201 | channel_auto_close(&s->res); |
| 4202 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4203 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4204 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4205 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4206 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4207 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4208 | channel_auto_close(&s->res); |
| 4209 | channel_auto_read(&s->res); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4210 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4211 | channel_abort(&s->req); |
| 4212 | channel_auto_close(&s->req); |
| 4213 | channel_auto_read(&s->req); |
| 4214 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4215 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 4216 | else if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4217 | txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4218 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4219 | s->req.analysers &= AN_REQ_FLT_END; |
| 4220 | if (HAS_REQ_DATA_FILTERS(s)) |
| 4221 | s->req.analysers |= AN_REQ_FLT_XFER_DATA; |
| 4222 | } |
| 4223 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4224 | s->res.analysers &= AN_RES_FLT_END; |
| 4225 | if (HAS_RSP_DATA_FILTERS(s)) |
| 4226 | s->res.analysers |= AN_RES_FLT_XFER_DATA; |
| 4227 | } |
| 4228 | channel_auto_close(&s->req); |
| 4229 | channel_auto_read(&s->req); |
| 4230 | channel_auto_close(&s->res); |
| 4231 | channel_auto_read(&s->res); |
| 4232 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4233 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 4234 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4235 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 4236 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 4237 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 4238 | /* server-close/keep-alive: terminate this transaction, |
| 4239 | * possibly killing the server connection and reinitialize |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4240 | * a fresh-new transaction, but only once we're sure there's |
| 4241 | * enough room in the request and response buffer to process |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 4242 | * another request. They must not hold any pending output data |
| 4243 | * and the response buffer must realigned |
| 4244 | * (realign is done is http_end_txn_clean_session). |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4245 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4246 | if (co_data(&s->req)) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4247 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4248 | else if (co_data(&s->res)) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 4249 | s->res.flags |= CF_WAKE_WRITE; |
Christopher Faulet | a81ff60 | 2017-07-18 22:01:05 +0200 | [diff] [blame] | 4250 | else { |
| 4251 | s->req.analysers = AN_REQ_FLT_END; |
| 4252 | s->res.analysers = AN_RES_FLT_END; |
| 4253 | txn->flags |= TX_WAIT_CLEANUP; |
| 4254 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4255 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4256 | } |
| 4257 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4258 | /* This function is an analyser which forwards request body (including chunk |
| 4259 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4260 | * zero byte. The only situation where it must not be called is when we're in |
| 4261 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4262 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4263 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4264 | * read more data, or 1 once we can go on with next request or end the stream. |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4265 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
Willy Tarreau | c24715e | 2014-04-17 15:21:20 +0200 | [diff] [blame] | 4266 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4267 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4268 | int http_request_forward_body(struct stream *s, struct channel *req, int an_bit) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4269 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4270 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4271 | struct http_txn *txn = s->txn; |
| 4272 | struct http_msg *msg = &s->txn->req; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 4273 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4274 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 4275 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4276 | now_ms, __FUNCTION__, |
| 4277 | s, |
| 4278 | req, |
| 4279 | req->rex, req->wex, |
| 4280 | req->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 4281 | ci_data(req), |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 4282 | req->analysers); |
| 4283 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4284 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4285 | return 0; |
| 4286 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4287 | if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4288 | ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4289 | /* Output closed while we were sending data. We must abort and |
| 4290 | * wake the other side up. |
| 4291 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4292 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4293 | msg->msg_state = HTTP_MSG_ERROR; |
| 4294 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4295 | return 1; |
| 4296 | } |
| 4297 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4298 | /* Note that we don't have to send 100-continue back because we don't |
| 4299 | * need the data to complete our job, and it's up to the server to |
| 4300 | * decide whether to return 100, 417 or anything else in return of |
| 4301 | * an "Expect: 100-continue" header. |
| 4302 | */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4303 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4304 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4305 | ? HTTP_MSG_CHUNK_SIZE |
| 4306 | : HTTP_MSG_DATA); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4307 | |
| 4308 | /* TODO/filters: when http-buffer-request option is set or if a |
| 4309 | * rule on url_param exists, the first chunk size could be |
| 4310 | * already parsed. In that case, msg->next is after the chunk |
| 4311 | * size (including the CRLF after the size). So this case should |
| 4312 | * be handled to */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4313 | } |
| 4314 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4315 | /* Some post-connect processing might want us to refrain from starting to |
| 4316 | * forward data. Currently, the only reason for this is "balance url_param" |
| 4317 | * whichs need to parse/process the request after we've enabled forwarding. |
| 4318 | */ |
| 4319 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4320 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4321 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 4322 | req->flags |= CF_WAKE_CONNECT; |
Willy Tarreau | ba20dfc | 2018-05-16 11:35:05 +0200 | [diff] [blame] | 4323 | channel_dont_close(req); /* don't fail on early shutr */ |
| 4324 | goto waiting; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4325 | } |
| 4326 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 4327 | } |
| 4328 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 4329 | /* in most states, we should abort in case of early close */ |
| 4330 | channel_auto_close(req); |
| 4331 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4332 | if (req->to_forward) { |
| 4333 | /* We can't process the buffer's contents yet */ |
| 4334 | req->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4335 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4336 | } |
| 4337 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4338 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 4339 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4340 | ? http_msg_forward_chunked_body(s, msg) |
| 4341 | : http_msg_forward_body(s, msg)); |
| 4342 | if (!ret) |
| 4343 | goto missing_data_or_waiting; |
| 4344 | if (ret < 0) |
| 4345 | goto return_bad_req; |
| 4346 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4347 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4348 | /* other states, DONE...TUNNEL */ |
| 4349 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 4350 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4351 | channel_dont_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4352 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4353 | http_resync_states(s); |
| 4354 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4355 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4356 | if (req->flags & CF_SHUTW) { |
| 4357 | /* request errors are most likely due to the |
| 4358 | * server aborting the transfer. */ |
| 4359 | goto aborted_xfer; |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4360 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4361 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4362 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, s->be); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4363 | goto return_bad_req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4364 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4365 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4366 | } |
| 4367 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4368 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 4369 | * the client's connection and forward any shutdown notification to the |
| 4370 | * server, which will decide whether to close or to go on processing the |
| 4371 | * request. We only do that in tunnel mode, and not in other modes since |
| 4372 | * it can be abused to exhaust source ports. */ |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 4373 | if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4374 | channel_auto_read(req); |
| 4375 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 4376 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 4377 | s->si[1].flags |= SI_FL_NOLINGER; |
| 4378 | channel_auto_close(req); |
| 4379 | } |
| 4380 | else if (s->txn->meth == HTTP_METH_POST) { |
| 4381 | /* POST requests may require to read extra CRLF sent by broken |
| 4382 | * browsers and which could cause an RST to be sent upon close |
| 4383 | * on some systems (eg: Linux). */ |
| 4384 | channel_auto_read(req); |
| 4385 | } |
| 4386 | return 0; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 4387 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4388 | missing_data_or_waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4389 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 4390 | if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4391 | if (!(s->flags & SF_ERR_MASK)) |
| 4392 | s->flags |= SF_ERR_CLICL; |
| 4393 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4394 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4395 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4396 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4397 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4398 | } |
| 4399 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4400 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 4401 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4402 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4403 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4404 | |
| 4405 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4406 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4407 | |
Willy Tarreau | ba20dfc | 2018-05-16 11:35:05 +0200 | [diff] [blame] | 4408 | waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4409 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4410 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4411 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4412 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4413 | /* When TE: chunked is used, we need to get there again to parse remaining |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4414 | * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE. |
Willy Tarreau | ff47b3f | 2017-12-29 16:30:31 +0100 | [diff] [blame] | 4415 | * And when content-length is used, we never want to let the possible |
| 4416 | * shutdown be forwarded to the other side, as the state machine will |
| 4417 | * take care of it once the client responds. It's also important to |
| 4418 | * prevent TIME_WAITs from accumulating on the backend side, and for |
| 4419 | * HTTP/2 where the last frame comes with a shutdown. |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4420 | */ |
Willy Tarreau | ff47b3f | 2017-12-29 16:30:31 +0100 | [diff] [blame] | 4421 | if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN)) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4422 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4423 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4424 | /* We know that more data are expected, but we couldn't send more that |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4425 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 4426 | * system knows it must not set a PUSH on this first part. Interactive |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 4427 | * modes are already handled by the stream sock layer. We must not do |
| 4428 | * this in content-length mode because it could present the MSG_MORE |
| 4429 | * flag with the last block of forwarded data, which would cause an |
| 4430 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4431 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4432 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4433 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4434 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4435 | return 0; |
| 4436 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4437 | return_bad_req: /* let's centralize all bad requests */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4438 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4439 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 4440 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 4441 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4442 | return_bad_req_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4443 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4444 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4445 | if (txn->status) { |
| 4446 | /* Note: we don't send any error if some data were already sent */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4447 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4448 | } else { |
| 4449 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4450 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4451 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4452 | req->analysers &= AN_REQ_FLT_END; |
| 4453 | s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4454 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4455 | if (!(s->flags & SF_ERR_MASK)) |
| 4456 | s->flags |= SF_ERR_PRXCOND; |
| 4457 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4458 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4459 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4460 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4461 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4462 | } |
| 4463 | return 0; |
| 4464 | |
| 4465 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4466 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4467 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4468 | if (txn->status) { |
| 4469 | /* Note: we don't send any error if some data were already sent */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4470 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4471 | } else { |
| 4472 | txn->status = 502; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4473 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4474 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4475 | req->analysers &= AN_REQ_FLT_END; |
| 4476 | s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4477 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4478 | HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1); |
| 4479 | HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4480 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4481 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4482 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4483 | if (!(s->flags & SF_ERR_MASK)) |
| 4484 | s->flags |= SF_ERR_SRVCL; |
| 4485 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4486 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4487 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4488 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4489 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4490 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4491 | return 0; |
| 4492 | } |
| 4493 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4494 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4495 | * processing can continue on next analysers, or zero if it either needs more |
| 4496 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4497 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4498 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4499 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4500 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4501 | int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4502 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4503 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4504 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4505 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4506 | struct hdr_ctx ctx; |
Lukas Tribus | fd9b68c | 2018-10-27 20:06:59 +0200 | [diff] [blame] | 4507 | struct connection *srv_conn; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4508 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4509 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4510 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4511 | |
Lukas Tribus | fd9b68c | 2018-10-27 20:06:59 +0200 | [diff] [blame] | 4512 | srv_conn = cs_conn(objt_cs(s->si[1].end)); |
| 4513 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 4514 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 4515 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4516 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4517 | rep, |
| 4518 | rep->rex, rep->wex, |
| 4519 | rep->flags, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4520 | ci_data(rep), |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4521 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4522 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4523 | /* |
| 4524 | * Now parse the partial (or complete) lines. |
| 4525 | * We will check the response syntax, and also join multi-line |
| 4526 | * headers. An index of all the lines will be elaborated while |
| 4527 | * parsing. |
| 4528 | * |
| 4529 | * For the parsing, we use a 28 states FSM. |
| 4530 | * |
| 4531 | * Here is the information we currently have : |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4532 | * ci_head(rep) = beginning of response |
| 4533 | * ci_head(rep) + msg->eoh = end of processed headers / start of current one |
| 4534 | * ci_tail(rep) = end of input data |
| 4535 | * msg->eol = end of current header or line (LF or CRLF) |
| 4536 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4537 | */ |
| 4538 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 4539 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4540 | /* There's a protected area at the end of the buffer for rewriting |
| 4541 | * purposes. We don't want to start to parse the request if the |
| 4542 | * protected area is affected, because we may have to move processed |
| 4543 | * data later, which is much more complicated. |
| 4544 | */ |
Willy Tarreau | d760eec | 2018-07-10 09:50:25 +0200 | [diff] [blame] | 4545 | if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 4546 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 4547 | /* some data has still not left the buffer, wake us once that's done */ |
| 4548 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 4549 | goto abort_response; |
| 4550 | channel_dont_close(rep); |
| 4551 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 4552 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 4553 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4554 | } |
| 4555 | |
Willy Tarreau | 188e230 | 2018-06-15 11:11:53 +0200 | [diff] [blame] | 4556 | if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) || |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4557 | ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4558 | channel_slow_realign(rep, trash.area); |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 4559 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4560 | if (likely(msg->next < ci_data(rep))) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 4561 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4562 | } |
| 4563 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4564 | /* 1: we might have to print this header in debug mode */ |
| 4565 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4566 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 4567 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4568 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4569 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4570 | sol = ci_head(rep); |
| 4571 | eol = sol + (msg->sl.st.l ? msg->sl.st.l : ci_data(rep)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4572 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4573 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4574 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4575 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4576 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4577 | while (cur_idx) { |
| 4578 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4579 | debug_hdr("srvhdr", s, sol, eol); |
| 4580 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4581 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4582 | } |
| 4583 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4584 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4585 | /* |
| 4586 | * Now we quickly check if we have found a full valid response. |
| 4587 | * If not so, we check the FD and buffer states before leaving. |
| 4588 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4589 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4590 | * responses are checked first. |
| 4591 | * |
| 4592 | * Depending on whether the client is still there or not, we |
| 4593 | * may send an error response back or not. Note that normally |
| 4594 | * we should only check for HTTP status there, and check I/O |
| 4595 | * errors somewhere else. |
| 4596 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4597 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4598 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4599 | /* Invalid response */ |
| 4600 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4601 | /* we detected a parsing error. We want to archive this response |
| 4602 | * in the dedicated proxy area for later troubleshooting. |
| 4603 | */ |
| 4604 | hdr_response_bad: |
| 4605 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4606 | http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4607 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4608 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4609 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4610 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4611 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4612 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4613 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4614 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4615 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4616 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4617 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4618 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4619 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4620 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4621 | if (!(s->flags & SF_ERR_MASK)) |
| 4622 | s->flags |= SF_ERR_PRXCOND; |
| 4623 | if (!(s->flags & SF_FINST_MASK)) |
| 4624 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4625 | |
| 4626 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4627 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4628 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4629 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 2375233 | 2018-06-15 14:54:53 +0200 | [diff] [blame] | 4630 | else if (channel_full(rep, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 4631 | if (msg->err_pos < 0) |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4632 | msg->err_pos = ci_data(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4633 | goto hdr_response_bad; |
| 4634 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4635 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4636 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4637 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4638 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4639 | http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 4640 | else if (txn->flags & TX_NOT_FIRST) |
| 4641 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4642 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4643 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4644 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4645 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4646 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4647 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4648 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4649 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4650 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4651 | txn->status = 502; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 4652 | |
| 4653 | /* Check to see if the server refused the early data. |
| 4654 | * If so, just send a 425 |
| 4655 | */ |
| 4656 | if (objt_cs(s->si[1].end)) { |
| 4657 | struct connection *conn = objt_cs(s->si[1].end)->conn; |
| 4658 | |
| 4659 | if (conn->err_code == CO_ER_SSL_EARLY_FAILED) |
| 4660 | txn->status = 425; |
| 4661 | } |
| 4662 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4663 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4664 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4665 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4666 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4667 | if (!(s->flags & SF_ERR_MASK)) |
| 4668 | s->flags |= SF_ERR_SRVCL; |
| 4669 | if (!(s->flags & SF_FINST_MASK)) |
| 4670 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4671 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4672 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4673 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 4674 | /* read timeout : return a 504 to the client. */ |
| 4675 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4676 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4677 | http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4678 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4679 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4680 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4681 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4682 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4683 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4684 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4685 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4686 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4687 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4688 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4689 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4690 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4691 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4692 | if (!(s->flags & SF_ERR_MASK)) |
| 4693 | s->flags |= SF_ERR_SRVTO; |
| 4694 | if (!(s->flags & SF_FINST_MASK)) |
| 4695 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4696 | return 0; |
| 4697 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4698 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4699 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4700 | else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4701 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 4702 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4703 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4704 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4705 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4706 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4707 | channel_auto_close(rep); |
| 4708 | |
| 4709 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4710 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4711 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4712 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4713 | if (!(s->flags & SF_ERR_MASK)) |
| 4714 | s->flags |= SF_ERR_CLICL; |
| 4715 | if (!(s->flags & SF_FINST_MASK)) |
| 4716 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4717 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4718 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4719 | return 0; |
| 4720 | } |
| 4721 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4722 | /* close from server, capture the response if the server has started to respond */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4723 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4724 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4725 | http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 4726 | else if (txn->flags & TX_NOT_FIRST) |
| 4727 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4728 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4729 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4730 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4731 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4732 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4733 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4734 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4735 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4736 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4737 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4738 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4739 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4740 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4741 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4742 | if (!(s->flags & SF_ERR_MASK)) |
| 4743 | s->flags |= SF_ERR_SRVCL; |
| 4744 | if (!(s->flags & SF_FINST_MASK)) |
| 4745 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4746 | return 0; |
| 4747 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4748 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4749 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4750 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4751 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4752 | http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | 6b726ad | 2013-12-15 19:31:37 +0100 | [diff] [blame] | 4753 | else if (txn->flags & TX_NOT_FIRST) |
| 4754 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4755 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4756 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4757 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4758 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4759 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4760 | if (!(s->flags & SF_ERR_MASK)) |
| 4761 | s->flags |= SF_ERR_CLICL; |
| 4762 | if (!(s->flags & SF_FINST_MASK)) |
| 4763 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4764 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4765 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4766 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4767 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4768 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4769 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 4770 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4771 | return 0; |
| 4772 | } |
| 4773 | |
| 4774 | /* More interesting part now : we know that we have a complete |
| 4775 | * response which at least looks like HTTP. We have an indicator |
| 4776 | * of each header's length, so we can parse them quickly. |
| 4777 | */ |
| 4778 | |
| 4779 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4780 | http_capture_bad_message(s->be, s, msg, msg->err_state, sess->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4781 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4782 | /* |
| 4783 | * 1: get the status code |
| 4784 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4785 | n = ci_head(rep)[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4786 | if (n < 1 || n > 5) |
| 4787 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4788 | /* when the client triggers a 4xx from the server, it's most often due |
| 4789 | * to a missing object or permission. These events should be tracked |
| 4790 | * because if they happen often, it may indicate a brute force or a |
| 4791 | * vulnerability scan. |
| 4792 | */ |
| 4793 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4794 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4795 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4796 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4797 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4798 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 4799 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 4800 | * exactly one digit "." one digit. This check may be disabled using |
| 4801 | * option accept-invalid-http-response. |
| 4802 | */ |
| 4803 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 4804 | if (msg->sl.st.v_l != 8) { |
| 4805 | msg->err_pos = 0; |
| 4806 | goto hdr_response_bad; |
| 4807 | } |
| 4808 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4809 | if (ci_head(rep)[4] != '/' || |
| 4810 | !isdigit((unsigned char)ci_head(rep)[5]) || |
| 4811 | ci_head(rep)[6] != '.' || |
| 4812 | !isdigit((unsigned char)ci_head(rep)[7])) { |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 4813 | msg->err_pos = 4; |
| 4814 | goto hdr_response_bad; |
| 4815 | } |
| 4816 | } |
| 4817 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4818 | /* check if the response is HTTP/1.1 or above */ |
| 4819 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4820 | ((ci_head(rep)[5] > '1') || |
| 4821 | ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4822 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4823 | |
| 4824 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4825 | txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_HDR_CONN_UPG|TX_CON_CLO_SET|TX_CON_KAL_SET); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4826 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4827 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4828 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4829 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4830 | txn->status = strl2ui(ci_head(rep) + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4831 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 4832 | /* Adjust server's health based on status code. Note: status codes 501 |
| 4833 | * and 505 are triggered on demand by client request, so we must not |
| 4834 | * count them as server failures. |
| 4835 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4836 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4837 | if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4838 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4839 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4840 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4841 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4842 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4843 | /* |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 4844 | * We may be facing a 100-continue response, or any other informational |
| 4845 | * 1xx response which is non-final, in which case this is not the right |
| 4846 | * response, and we're waiting for the next one. Let's allow this response |
| 4847 | * to go to the client and wait for the next one. There's an exception for |
| 4848 | * 101 which is used later in the code to switch protocols. |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4849 | */ |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 4850 | if (txn->status < 200 && |
| 4851 | (txn->status == 100 || txn->status >= 102)) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 4852 | hdr_idx_init(&txn->hdr_idx); |
| 4853 | msg->next -= channel_forward(rep, msg->next); |
| 4854 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 4855 | txn->status = 0; |
| 4856 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 4857 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 4858 | goto next_one; |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 4859 | } |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 4860 | |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 4861 | /* |
| 4862 | * 2: check for cacheability. |
| 4863 | */ |
| 4864 | |
| 4865 | switch (txn->status) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4866 | case 200: |
| 4867 | case 203: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 4868 | case 204: |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4869 | case 206: |
| 4870 | case 300: |
| 4871 | case 301: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 4872 | case 404: |
| 4873 | case 405: |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4874 | case 410: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 4875 | case 414: |
| 4876 | case 501: |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 4877 | break; |
| 4878 | default: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 4879 | /* RFC7231#6.1: |
| 4880 | * Responses with status codes that are defined as |
| 4881 | * cacheable by default (e.g., 200, 203, 204, 206, |
| 4882 | * 300, 301, 404, 405, 410, 414, and 501 in this |
| 4883 | * specification) can be reused by a cache with |
| 4884 | * heuristic expiration unless otherwise indicated |
| 4885 | * by the method definition or explicit cache |
| 4886 | * controls [RFC7234]; all other status codes are |
| 4887 | * not cacheable by default. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4888 | */ |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 4889 | txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4890 | break; |
| 4891 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4892 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4893 | /* |
| 4894 | * 3: we may need to capture headers |
| 4895 | */ |
| 4896 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 4897 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 4898 | http_capture_headers(ci_head(rep), &txn->hdr_idx, |
| 4899 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4900 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4901 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 4902 | * by RFC7230#3.3.3 : |
| 4903 | * |
| 4904 | * The length of a message body is determined by one of the following |
| 4905 | * (in order of precedence): |
| 4906 | * |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 4907 | * 1. Any 2xx (Successful) response to a CONNECT request implies that |
| 4908 | * the connection will become a tunnel immediately after the empty |
| 4909 | * line that concludes the header fields. A client MUST ignore |
| 4910 | * any Content-Length or Transfer-Encoding header fields received |
| 4911 | * in such a message. Any 101 response (Switching Protocols) is |
| 4912 | * managed in the same manner. |
| 4913 | * |
| 4914 | * 2. Any response to a HEAD request and any response with a 1xx |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4915 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 4916 | * code is always terminated by the first empty line after the |
| 4917 | * header fields, regardless of the header fields present in the |
| 4918 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4919 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4920 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 4921 | * transfer coding (Section 4.1) is the final encoding, the message |
| 4922 | * body length is determined by reading and decoding the chunked |
| 4923 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4924 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4925 | * If a Transfer-Encoding header field is present in a response and |
| 4926 | * the chunked transfer coding is not the final encoding, the |
| 4927 | * message body length is determined by reading the connection until |
| 4928 | * it is closed by the server. If a Transfer-Encoding header field |
| 4929 | * is present in a request and the chunked transfer coding is not |
| 4930 | * the final encoding, the message body length cannot be determined |
| 4931 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 4932 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4933 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4934 | * If a message is received with both a Transfer-Encoding and a |
| 4935 | * Content-Length header field, the Transfer-Encoding overrides the |
| 4936 | * Content-Length. Such a message might indicate an attempt to |
| 4937 | * perform request smuggling (Section 9.5) or response splitting |
| 4938 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 4939 | * remove the received Content-Length field prior to forwarding such |
| 4940 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4941 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4942 | * 4. If a message is received without Transfer-Encoding and with |
| 4943 | * either multiple Content-Length header fields having differing |
| 4944 | * field-values or a single Content-Length header field having an |
| 4945 | * invalid value, then the message framing is invalid and the |
| 4946 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 4947 | * request message, the server MUST respond with a 400 (Bad Request) |
| 4948 | * status code and then close the connection. If this is a response |
| 4949 | * message received by a proxy, the proxy MUST close the connection |
| 4950 | * to the server, discard the received response, and send a 502 (Bad |
| 4951 | * Gateway) response to the client. If this is a response message |
| 4952 | * received by a user agent, the user agent MUST close the |
| 4953 | * connection to the server and discard the received response. |
| 4954 | * |
| 4955 | * 5. If a valid Content-Length header field is present without |
| 4956 | * Transfer-Encoding, its decimal value defines the expected message |
| 4957 | * body length in octets. If the sender closes the connection or |
| 4958 | * the recipient times out before the indicated number of octets are |
| 4959 | * received, the recipient MUST consider the message to be |
| 4960 | * incomplete and close the connection. |
| 4961 | * |
| 4962 | * 6. If this is a request message and none of the above are true, then |
| 4963 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4964 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4965 | * 7. Otherwise, this is a response message without a declared message |
| 4966 | * body length, so the message body length is determined by the |
| 4967 | * number of octets received prior to the server closing the |
| 4968 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4969 | */ |
| 4970 | |
| 4971 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4972 | * remain 0 as well as the chunk_len, which may or may not mirror |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4973 | * 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] | 4974 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 4975 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 4976 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 4977 | txn->status == 101)) { |
| 4978 | /* Either we've established an explicit tunnel, or we're |
| 4979 | * switching the protocol. In both cases, we're very unlikely |
| 4980 | * to understand the next protocols. We have to switch to tunnel |
| 4981 | * mode, so that we transfer the request and responses then let |
| 4982 | * this protocol pass unmodified. When we later implement specific |
| 4983 | * parsers for such protocols, we'll want to check the Upgrade |
| 4984 | * header which contains information about that protocol for |
| 4985 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 4986 | */ |
| 4987 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 4988 | msg->flags |= HTTP_MSGF_XFER_LEN; |
| 4989 | goto end; |
| 4990 | } |
| 4991 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4992 | if (txn->meth == HTTP_METH_HEAD || |
| 4993 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4994 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4995 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4996 | goto skip_content_length; |
| 4997 | } |
| 4998 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4999 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5000 | ctx.idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5001 | while (http_find_header2("Transfer-Encoding", 17, ci_head(rep), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5002 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5003 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 5004 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5005 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5006 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5007 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5008 | break; |
| 5009 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5010 | } |
| 5011 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 5012 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5013 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 5014 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5015 | while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 5016 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 5017 | } |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5018 | else while (http_find_header2("Content-Length", 14, ci_head(rep), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5019 | signed long long cl; |
| 5020 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5021 | if (!ctx.vlen) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5022 | msg->err_pos = ctx.line + ctx.val - ci_head(rep); |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5023 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5024 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5025 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5026 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5027 | msg->err_pos = ctx.line + ctx.val - ci_head(rep); |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5028 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5029 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5030 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5031 | if (cl < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5032 | msg->err_pos = ctx.line + ctx.val - ci_head(rep); |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5033 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5034 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5035 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5036 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5037 | msg->err_pos = ctx.line + ctx.val - ci_head(rep); |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5038 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5039 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5040 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5041 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5042 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5043 | } |
| 5044 | |
Lukas Tribus | fd9b68c | 2018-10-27 20:06:59 +0200 | [diff] [blame] | 5045 | /* check for NTML authentication headers in 401 (WWW-Authenticate) and |
| 5046 | * 407 (Proxy-Authenticate) responses and set the connection to private |
| 5047 | */ |
| 5048 | if (srv_conn && txn->status == 401) { |
| 5049 | /* check for Negotiate/NTLM WWW-Authenticate headers */ |
| 5050 | ctx.idx = 0; |
| 5051 | while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) { |
| 5052 | if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) || |
| 5053 | (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4))) |
| 5054 | srv_conn->flags |= CO_FL_PRIVATE; |
| 5055 | } |
| 5056 | } else if (srv_conn && txn->status == 407) { |
| 5057 | /* check for Negotiate/NTLM Proxy-Authenticate headers */ |
| 5058 | ctx.idx = 0; |
| 5059 | while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) { |
| 5060 | if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) || |
| 5061 | (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4))) |
| 5062 | srv_conn->flags |= CO_FL_PRIVATE; |
| 5063 | } |
| 5064 | } |
| 5065 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5066 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5067 | /* Now we have to check if we need to modify the Connection header. |
| 5068 | * This is more difficult on the response than it is on the request, |
| 5069 | * because we can have two different HTTP versions and we don't know |
| 5070 | * how the client will interprete a response. For instance, let's say |
| 5071 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5072 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5073 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5074 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5075 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5076 | * indicating how a request MAY be understood by the client. In case |
| 5077 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5078 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5079 | * will fall back to explicit close. Note that we won't take risks with |
| 5080 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5081 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5082 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5083 | if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 5084 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5085 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5086 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5087 | /* on unknown transfer length, we must close */ |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 5088 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5089 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5090 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5091 | /* now adjust header transformations depending on current state */ |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 5092 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5093 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5094 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5095 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5096 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5097 | else { /* SCL / KAL */ |
| 5098 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5099 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5100 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5101 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5102 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5103 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5104 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5105 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5106 | /* Some keep-alive responses are converted to Server-close if |
| 5107 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5108 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5109 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5110 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5111 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5112 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5113 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5114 | } |
| 5115 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 5116 | end: |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5117 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 5118 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 5119 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5120 | /* end of job, return OK */ |
| 5121 | rep->analysers &= ~an_bit; |
| 5122 | rep->analyse_exp = TICK_ETERNITY; |
| 5123 | channel_auto_close(rep); |
| 5124 | return 1; |
| 5125 | |
| 5126 | abort_keep_alive: |
| 5127 | /* A keep-alive request to the server failed on a network error. |
| 5128 | * The client is required to retry. We need to close without returning |
| 5129 | * any other information so that the client retries. |
| 5130 | */ |
| 5131 | txn->status = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5132 | rep->analysers &= AN_RES_FLT_END; |
| 5133 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5134 | channel_auto_close(rep); |
| 5135 | s->logs.logwait = 0; |
| 5136 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5137 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5138 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5139 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5140 | return 0; |
| 5141 | } |
| 5142 | |
| 5143 | /* This function performs all the processing enabled for the current response. |
| 5144 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5145 | * and updates s->res.analysers. It might make sense to explode it into several |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5146 | * other functions. It works like process_request (see indications above). |
| 5147 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5148 | int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px) |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5149 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5150 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5151 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5152 | struct http_msg *msg = &txn->rsp; |
| 5153 | struct proxy *cur_proxy; |
| 5154 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 5155 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5156 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 5157 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5158 | now_ms, __FUNCTION__, |
| 5159 | s, |
| 5160 | rep, |
| 5161 | rep->rex, rep->wex, |
| 5162 | rep->flags, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5163 | ci_data(rep), |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 5164 | rep->analysers); |
| 5165 | |
| 5166 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 5167 | return 0; |
| 5168 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5169 | /* The stats applet needs to adjust the Connection header but we don't |
| 5170 | * apply any filter there. |
| 5171 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 5172 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 5173 | rep->analysers &= ~an_bit; |
| 5174 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5175 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 5176 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5177 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5178 | /* |
| 5179 | * We will have to evaluate the filters. |
| 5180 | * As opposed to version 1.2, now they will be evaluated in the |
| 5181 | * filters order and not in the header order. This means that |
| 5182 | * each filter has to be validated among all headers. |
| 5183 | * |
| 5184 | * Filters are tried with ->be first, then with ->fe if it is |
| 5185 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5186 | * |
| 5187 | * Maybe we are in resume condiion. In this case I choose the |
| 5188 | * "struct proxy" which contains the rule list matching the resume |
| 5189 | * pointer. If none of theses "struct proxy" match, I initialise |
| 5190 | * the process with the first one. |
| 5191 | * |
| 5192 | * In fact, I check only correspondance betwwen the current list |
| 5193 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 5194 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5195 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5196 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 5197 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5198 | else |
| 5199 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5200 | while (1) { |
| 5201 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5202 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5203 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 5204 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5205 | ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 5206 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 5207 | if (ret == HTTP_RULE_RES_BADREQ) |
| 5208 | goto return_srv_prx_502; |
| 5209 | |
| 5210 | if (ret == HTTP_RULE_RES_DONE) { |
| 5211 | rep->analysers &= ~an_bit; |
| 5212 | rep->analyse_exp = TICK_ETERNITY; |
| 5213 | return 1; |
| 5214 | } |
| 5215 | } |
| 5216 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5217 | /* we need to be called again. */ |
| 5218 | if (ret == HTTP_RULE_RES_YIELD) { |
| 5219 | channel_dont_close(rep); |
| 5220 | return 0; |
| 5221 | } |
| 5222 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5223 | /* try headers filters */ |
| 5224 | if (rule_set->rsp_exp != NULL) { |
| 5225 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 5226 | return_bad_resp: |
| 5227 | if (objt_server(s->target)) { |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5228 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5229 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5230 | } |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5231 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5232 | return_srv_prx_502: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5233 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5234 | txn->status = 502; |
| 5235 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 5236 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 5237 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 5238 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5239 | if (!(s->flags & SF_ERR_MASK)) |
| 5240 | s->flags |= SF_ERR_PRXCOND; |
| 5241 | if (!(s->flags & SF_FINST_MASK)) |
| 5242 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5243 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5244 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5245 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5246 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5247 | /* has the response been denied ? */ |
| 5248 | if (txn->flags & TX_SVDENY) { |
| 5249 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5250 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5251 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5252 | HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 5253 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5254 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5255 | HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5256 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5257 | goto return_srv_prx_502; |
| 5258 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5259 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5260 | /* add response headers from the rule sets in the same order */ |
| 5261 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5262 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5263 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5264 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 5265 | int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5266 | ret = acl_pass(ret); |
| 5267 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5268 | ret = !ret; |
| 5269 | if (!ret) |
| 5270 | continue; |
| 5271 | } |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 5272 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5273 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5274 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5275 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5276 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5277 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5278 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5279 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5280 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5281 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 5282 | /* After this point, this anayzer can't return yield, so we can |
| 5283 | * remove the bit corresponding to this analyzer from the list. |
| 5284 | * |
| 5285 | * Note that the intermediate returns and goto found previously |
| 5286 | * reset the analyzers. |
| 5287 | */ |
| 5288 | rep->analysers &= ~an_bit; |
| 5289 | rep->analyse_exp = TICK_ETERNITY; |
| 5290 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5291 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5292 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5293 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5294 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5295 | /* |
| 5296 | * Now check for a server cookie. |
| 5297 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 5298 | if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5299 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5300 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5301 | /* |
| 5302 | * Check for cache-control or pragma headers if required. |
| 5303 | */ |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 5304 | if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5305 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5306 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5307 | /* |
| 5308 | * Add server cookie in the response if needed |
| 5309 | */ |
| 5310 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 5311 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5312 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5313 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 5314 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5315 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5316 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 5317 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5318 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5319 | /* the server is known, it's not the one the client requested, or the |
| 5320 | * cookie's last seen date needs to be refreshed. We have to |
| 5321 | * insert a set-cookie here, except if we want to insert only on POST |
| 5322 | * requests and this one isn't. Note that servers which don't have cookies |
| 5323 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5324 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5325 | if (!objt_server(s->target)->cookie) { |
| 5326 | chunk_printf(&trash, |
| 5327 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5328 | s->be->cookie_name); |
| 5329 | } |
| 5330 | else { |
| 5331 | chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5332 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5333 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 5334 | /* emit last_date, which is mandatory */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5335 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 5336 | s30tob64((date.tv_sec+3) >> 2, |
| 5337 | trash.area + trash.data); |
| 5338 | trash.data += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5339 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5340 | if (s->be->cookie_maxlife) { |
| 5341 | /* emit first_date, which is either the original one or |
| 5342 | * the current date. |
| 5343 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5344 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5345 | s30tob64(txn->cookie_first_date ? |
| 5346 | txn->cookie_first_date >> 2 : |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5347 | (date.tv_sec+3) >> 2, |
| 5348 | trash.area + trash.data); |
| 5349 | trash.data += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5350 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5351 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5352 | chunk_appendf(&trash, "; path=/"); |
| 5353 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5354 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5355 | if (s->be->cookie_domain) |
| 5356 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5357 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5358 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 5359 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5360 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5361 | if (s->be->ck_opts & PR_CK_SECURE) |
| 5362 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5363 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5364 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5365 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5366 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5367 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | b05e48a | 2018-09-20 11:12:58 +0200 | [diff] [blame] | 5368 | if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5369 | /* the server did not change, only the date was updated */ |
| 5370 | txn->flags |= TX_SCK_UPDATED; |
| 5371 | else |
| 5372 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5373 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5374 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5375 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5376 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5377 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5378 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5379 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5380 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5381 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5382 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5383 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 5384 | "Cache-control: private", 22) < 0)) |
| 5385 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5386 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5387 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5388 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5389 | /* |
| 5390 | * Check if result will be cacheable with a cookie. |
| 5391 | * We'll block the response if security checks have caught |
| 5392 | * nasty things such as a cacheable cookie. |
| 5393 | */ |
| 5394 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5395 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 5396 | (s->be->options & PR_O_CHK_CACHE)) { |
| 5397 | /* we're in presence of a cacheable response containing |
| 5398 | * a set-cookie header. We'll block it as requested by |
| 5399 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5400 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5401 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5402 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5403 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5404 | HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 5405 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5406 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5407 | HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5408 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5409 | ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5410 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5411 | send_log(s->be, LOG_ALERT, |
| 5412 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5413 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 5414 | goto return_srv_prx_502; |
| 5415 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5416 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5417 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5418 | /* |
| 5419 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 5420 | * If an "Upgrade" token is found, the header is left untouched in order |
| 5421 | * not to have to deal with some client bugs : some of them fail an upgrade |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5422 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 5423 | * want to touch any 101 response either since it's switching to another |
| 5424 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5425 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5426 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 5427 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5428 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5429 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5430 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5431 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5432 | /* we want a keep-alive response here. Keep-alive header |
| 5433 | * required if either side is not 1.1. |
| 5434 | */ |
| 5435 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 5436 | want_flags |= TX_CON_KAL_SET; |
| 5437 | } |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 5438 | else { /* CLO */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5439 | /* we want a close response here. Close header required if |
| 5440 | * the server is 1.1, regardless of the client. |
| 5441 | */ |
| 5442 | if (msg->flags & HTTP_MSGF_VER_11) |
| 5443 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5444 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5445 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5446 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 5447 | http_change_connection_header(txn, msg, want_flags); |
| 5448 | } |
| 5449 | |
| 5450 | skip_header_mangling: |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 5451 | /* Always enter in the body analyzer */ |
| 5452 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 5453 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5454 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5455 | /* if the user wants to log as soon as possible, without counting |
| 5456 | * bytes from the server, then this is the right moment. We have |
| 5457 | * to temporarily assign bytes_out to log what we currently have. |
| 5458 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5459 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5460 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 5461 | s->logs.bytes_out = txn->rsp.eoh; |
| 5462 | s->do_log(s); |
| 5463 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5464 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5465 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5466 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5467 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5468 | /* This function is an analyser which forwards response body (including chunk |
| 5469 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5470 | * zero byte. The only situation where it must not be called is when we're in |
| 5471 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5472 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5473 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5474 | * read more data, or 1 once we can go on with next request or end the stream. |
Willy Tarreau | d351021 | 2014-04-21 11:24:13 +0200 | [diff] [blame] | 5475 | * |
| 5476 | * It is capable of compressing response data both in content-length mode and |
| 5477 | * in chunked mode. The state machines follows different flows depending on |
| 5478 | * whether content-length and chunked modes are used, since there are no |
| 5479 | * trailers in content-length : |
| 5480 | * |
| 5481 | * chk-mode cl-mode |
| 5482 | * ,----- BODY -----. |
| 5483 | * / \ |
| 5484 | * V size > 0 V chk-mode |
| 5485 | * .--> SIZE -------------> DATA -------------> CRLF |
| 5486 | * | | size == 0 | last byte | |
| 5487 | * | v final crlf v inspected | |
| 5488 | * | TRAILERS -----------> DONE | |
| 5489 | * | | |
| 5490 | * `----------------------------------------------' |
| 5491 | * |
| 5492 | * Compression only happens in the DATA state, and must be flushed in final |
| 5493 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 5494 | * is performed at once on final states for all bytes parsed, or when leaving |
| 5495 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5496 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5497 | int http_response_forward_body(struct stream *s, struct channel *res, int an_bit) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5498 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5499 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5500 | struct http_txn *txn = s->txn; |
| 5501 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5502 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5503 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 5504 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 5505 | now_ms, __FUNCTION__, |
| 5506 | s, |
| 5507 | res, |
| 5508 | res->rex, res->wex, |
| 5509 | res->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 5510 | ci_data(res), |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 5511 | res->analysers); |
| 5512 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5513 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5514 | return 0; |
| 5515 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5516 | if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5517 | ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) || |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5518 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5519 | /* Output closed while we were sending data. We must abort and |
| 5520 | * wake the other side up. |
| 5521 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5522 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5523 | msg->msg_state = HTTP_MSG_ERROR; |
| 5524 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5525 | return 1; |
| 5526 | } |
| 5527 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5528 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5529 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5530 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5531 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5532 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5533 | ? HTTP_MSG_CHUNK_SIZE |
| 5534 | : HTTP_MSG_DATA); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5535 | } |
| 5536 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5537 | if (res->to_forward) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5538 | /* We can't process the buffer's contents yet */ |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5539 | res->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5540 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5541 | } |
| 5542 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5543 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 5544 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5545 | ? http_msg_forward_chunked_body(s, msg) |
| 5546 | : http_msg_forward_body(s, msg)); |
| 5547 | if (!ret) |
| 5548 | goto missing_data_or_waiting; |
| 5549 | if (ret < 0) |
| 5550 | goto return_bad_res; |
| 5551 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5552 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5553 | /* other states, DONE...TUNNEL */ |
| 5554 | /* for keep-alive we don't want to forward closes on DONE */ |
| 5555 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5556 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5557 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5558 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 5559 | http_resync_states(s); |
| 5560 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5561 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5562 | if (res->flags & CF_SHUTW) { |
| 5563 | /* response errors are most likely due to the |
| 5564 | * client aborting the transfer. */ |
| 5565 | goto aborted_xfer; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5566 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5567 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 5568 | http_capture_bad_message(s->be, s, msg, msg->err_state, strm_fe(s)); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5569 | goto return_bad_res; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5570 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5571 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5572 | } |
Willy Tarreau | f51d03c | 2016-05-02 15:25:15 +0200 | [diff] [blame] | 5573 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5574 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5575 | missing_data_or_waiting: |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5576 | if (res->flags & CF_SHUTW) |
| 5577 | goto aborted_xfer; |
| 5578 | |
| 5579 | /* stop waiting for data if the input is closed before the end. If the |
| 5580 | * client side was already closed, it means that the client has aborted, |
| 5581 | * so we don't want to count this as a server abort. Otherwise it's a |
| 5582 | * server abort. |
| 5583 | */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 5584 | if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5585 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5586 | goto aborted_xfer; |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 5587 | /* If we have some pending data, we continue the processing */ |
Willy Tarreau | 5ba6552 | 2018-06-15 15:14:53 +0200 | [diff] [blame] | 5588 | if (!ci_data(res)) { |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 5589 | if (!(s->flags & SF_ERR_MASK)) |
| 5590 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5591 | HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 5592 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5593 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 5594 | goto return_bad_res_stats_ok; |
| 5595 | } |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5596 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5597 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5598 | /* we need to obey the req analyser, so if it leaves, we must too */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5599 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5600 | goto return_bad_res; |
| 5601 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5602 | /* When TE: chunked is used, we need to get there again to parse |
| 5603 | * remaining chunks even if the server has closed, so we don't want to |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 5604 | * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side |
| 5605 | * or if there are filters registered on the stream, we don't want to |
| 5606 | * forward a close |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5607 | */ |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 5608 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 5609 | HAS_DATA_FILTERS(s, res) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5610 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5611 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5612 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5613 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5614 | /* We know that more data are expected, but we couldn't send more that |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5615 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
Willy Tarreau | 0729303 | 2011-05-30 18:29:28 +0200 | [diff] [blame] | 5616 | * system knows it must not set a PUSH on this first part. Interactive |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 5617 | * modes are already handled by the stream sock layer. We must not do |
| 5618 | * this in content-length mode because it could present the MSG_MORE |
| 5619 | * flag with the last block of forwarded data, which would cause an |
| 5620 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5621 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 5622 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING)) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5623 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5624 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5625 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5626 | return 0; |
| 5627 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5628 | return_bad_res: /* let's centralize all bad responses */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5629 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5630 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5631 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5632 | |
| 5633 | return_bad_res_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5634 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5635 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 5636 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5637 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5638 | res->analysers &= AN_RES_FLT_END; |
| 5639 | s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5640 | if (objt_server(s->target)) |
| 5641 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5642 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5643 | if (!(s->flags & SF_ERR_MASK)) |
| 5644 | s->flags |= SF_ERR_PRXCOND; |
| 5645 | if (!(s->flags & SF_FINST_MASK)) |
| 5646 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5647 | return 0; |
| 5648 | |
| 5649 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5650 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5651 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 5652 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5653 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5654 | res->analysers &= AN_RES_FLT_END; |
| 5655 | s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5656 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5657 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 5658 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5659 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5660 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5661 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5662 | if (!(s->flags & SF_ERR_MASK)) |
| 5663 | s->flags |= SF_ERR_CLICL; |
| 5664 | if (!(s->flags & SF_FINST_MASK)) |
| 5665 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5666 | return 0; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5667 | } |
| 5668 | |
| 5669 | |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 5670 | int http_msg_forward_body(struct stream *s, struct http_msg *msg) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5671 | { |
| 5672 | struct channel *chn = msg->chn; |
| 5673 | int ret; |
| 5674 | |
| 5675 | /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */ |
| 5676 | |
| 5677 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 5678 | goto ending; |
| 5679 | |
| 5680 | /* Neither content-length, nor transfer-encoding was found, so we must |
| 5681 | * read the body until the server connection is closed. In that case, we |
| 5682 | * eat data as they come. Of course, this happens for response only. */ |
| 5683 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5684 | unsigned long long len = ci_data(chn) - msg->next; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5685 | msg->chunk_len += len; |
| 5686 | msg->body_len += len; |
| 5687 | } |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5688 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5689 | /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next), |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5690 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5691 | msg->next += ret; |
| 5692 | msg->chunk_len -= ret; |
| 5693 | if (msg->chunk_len) { |
| 5694 | /* input empty or output full */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5695 | if (ci_data(chn) > msg->next) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5696 | chn->flags |= CF_WAKE_WRITE; |
| 5697 | goto missing_data_or_waiting; |
| 5698 | } |
| 5699 | |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 5700 | /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is |
| 5701 | * always set for a request. */ |
| 5702 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 5703 | /* The server still sending data that should be filtered */ |
| 5704 | if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn)) |
| 5705 | goto missing_data_or_waiting; |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 5706 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 5707 | goto ending; |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 5708 | } |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 5709 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5710 | msg->msg_state = HTTP_MSG_ENDING; |
| 5711 | |
| 5712 | ending: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5713 | /* we may have some pending data starting at res->buf.p such as a last |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5714 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5715 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 5716 | /* default_ret */ msg->next, |
| 5717 | /* on_error */ goto error); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 5718 | c_adv(chn, ret); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5719 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 5720 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 5721 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 5722 | if (msg->next) |
| 5723 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 5724 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5725 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
| 5726 | /* default_ret */ 1, |
| 5727 | /* on_error */ goto error, |
| 5728 | /* on_wait */ goto waiting); |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 5729 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 5730 | msg->msg_state = HTTP_MSG_DONE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5731 | return 1; |
| 5732 | |
| 5733 | missing_data_or_waiting: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5734 | /* we may have some pending data starting at chn->buf.p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5735 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 5736 | /* default_ret */ msg->next, |
| 5737 | /* on_error */ goto error); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 5738 | c_adv(chn, ret); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5739 | msg->next -= ret; |
| 5740 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 5741 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 5742 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5743 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 5744 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5745 | return 0; |
| 5746 | error: |
| 5747 | return -1; |
| 5748 | } |
| 5749 | |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 5750 | int http_msg_forward_chunked_body(struct stream *s, struct http_msg *msg) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5751 | { |
| 5752 | struct channel *chn = msg->chn; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5753 | unsigned int chunk; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5754 | int ret; |
| 5755 | |
| 5756 | /* Here we have the guarantee to be in one of the following state: |
| 5757 | * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF, |
| 5758 | * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */ |
| 5759 | |
Christopher Faulet | ca874b8 | 2018-09-20 11:31:01 +0200 | [diff] [blame] | 5760 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 5761 | goto ending; |
| 5762 | |
| 5763 | /* Don't parse chunks if there is no input data */ |
| 5764 | if (!ci_data(chn)) |
| 5765 | goto waiting; |
| 5766 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5767 | switch_states: |
| 5768 | switch (msg->msg_state) { |
| 5769 | case HTTP_MSG_DATA: |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5770 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5771 | /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next), |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5772 | /* on_error */ goto error); |
| 5773 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5774 | msg->chunk_len -= ret; |
| 5775 | if (msg->chunk_len) { |
| 5776 | /* input empty or output full */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5777 | if (ci_data(chn) > msg->next) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5778 | chn->flags |= CF_WAKE_WRITE; |
| 5779 | goto missing_data_or_waiting; |
| 5780 | } |
| 5781 | |
| 5782 | /* nothing left to forward for this chunk*/ |
| 5783 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
| 5784 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 5785 | |
| 5786 | case HTTP_MSG_CHUNK_CRLF: |
| 5787 | /* we want the CRLF after the data */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5788 | ret = h1_skip_chunk_crlf(&chn->buf, co_data(chn) + msg->next, c_data(chn)); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5789 | if (ret == 0) |
| 5790 | goto missing_data_or_waiting; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 5791 | if (ret < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5792 | msg->err_pos = ci_data(chn) + ret; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 5793 | if (msg->err_pos < 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5794 | msg->err_pos += chn->buf.size; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5795 | goto chunk_parsing_error; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 5796 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 5797 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5798 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5799 | /* fall through for HTTP_MSG_CHUNK_SIZE */ |
| 5800 | |
| 5801 | case HTTP_MSG_CHUNK_SIZE: |
| 5802 | /* read the chunk size and assign it to ->chunk_len, |
| 5803 | * then set ->next to point to the body and switch to |
| 5804 | * DATA or TRAILERS state. |
| 5805 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5806 | ret = h1_parse_chunk_size(&chn->buf, co_data(chn) + msg->next, c_data(chn), &chunk); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5807 | if (ret == 0) |
| 5808 | goto missing_data_or_waiting; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5809 | if (ret < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5810 | msg->err_pos = ci_data(chn) + ret; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5811 | if (msg->err_pos < 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5812 | msg->err_pos += chn->buf.size; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5813 | goto chunk_parsing_error; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5814 | } |
| 5815 | |
| 5816 | msg->sol = ret; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 5817 | msg->next += ret; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5818 | msg->chunk_len = chunk; |
| 5819 | msg->body_len += chunk; |
| 5820 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5821 | if (msg->chunk_len) { |
| 5822 | msg->msg_state = HTTP_MSG_DATA; |
| 5823 | goto switch_states; |
| 5824 | } |
| 5825 | msg->msg_state = HTTP_MSG_TRAILERS; |
| 5826 | /* fall through for HTTP_MSG_TRAILERS */ |
| 5827 | |
| 5828 | case HTTP_MSG_TRAILERS: |
| 5829 | ret = http_forward_trailers(msg); |
| 5830 | if (ret < 0) |
| 5831 | goto chunk_parsing_error; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5832 | FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg), |
| 5833 | /* default_ret */ 1, |
| 5834 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5835 | msg->next += msg->sol; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5836 | if (!ret) |
| 5837 | goto missing_data_or_waiting; |
| 5838 | break; |
| 5839 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5840 | default: |
| 5841 | /* This should no happen in this function */ |
| 5842 | goto error; |
| 5843 | } |
| 5844 | |
| 5845 | msg->msg_state = HTTP_MSG_ENDING; |
| 5846 | ending: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5847 | /* we may have some pending data starting at res->buf.p such as a last |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5848 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5849 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5850 | /* default_ret */ msg->next, |
| 5851 | /* on_error */ goto error); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 5852 | c_adv(chn, ret); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5853 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 5854 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 5855 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 5856 | if (msg->next) |
| 5857 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 5858 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5859 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5860 | /* default_ret */ 1, |
| 5861 | /* on_error */ goto error, |
| 5862 | /* on_wait */ goto waiting); |
| 5863 | msg->msg_state = HTTP_MSG_DONE; |
| 5864 | return 1; |
| 5865 | |
| 5866 | missing_data_or_waiting: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5867 | /* we may have some pending data starting at chn->buf.p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5868 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5869 | /* default_ret */ msg->next, |
| 5870 | /* on_error */ goto error); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 5871 | c_adv(chn, ret); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5872 | msg->next -= ret; |
| 5873 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 5874 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 5875 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5876 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 5877 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5878 | return 0; |
| 5879 | |
| 5880 | chunk_parsing_error: |
| 5881 | if (msg->err_pos >= 0) { |
| 5882 | if (chn->flags & CF_ISRESP) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 5883 | http_capture_bad_message(s->be, s, msg, |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5884 | msg->msg_state, strm_fe(s)); |
| 5885 | else |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 5886 | http_capture_bad_message(strm_fe(s), s, |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5887 | msg, msg->msg_state, s->be); |
| 5888 | } |
| 5889 | error: |
| 5890 | return -1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5891 | } |
| 5892 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5893 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5894 | /* Iterate the same filter through all request headers. |
| 5895 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5896 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5897 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5898 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5899 | int apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5900 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5901 | char *cur_ptr, *cur_end, *cur_next; |
| 5902 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5903 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5904 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 5905 | int delta, len; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 5906 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5907 | last_hdr = 0; |
| 5908 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5909 | cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5910 | old_idx = 0; |
| 5911 | |
| 5912 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5913 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5914 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5915 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5916 | (exp->action == ACT_ALLOW || |
| 5917 | exp->action == ACT_DENY || |
| 5918 | exp->action == ACT_TARPIT)) |
| 5919 | return 0; |
| 5920 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5921 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5922 | if (!cur_idx) |
| 5923 | break; |
| 5924 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5925 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5926 | cur_ptr = cur_next; |
| 5927 | cur_end = cur_ptr + cur_hdr->len; |
| 5928 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5929 | |
| 5930 | /* Now we have one header between cur_ptr and cur_end, |
| 5931 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5932 | */ |
| 5933 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 5934 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5935 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5936 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5937 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5938 | last_hdr = 1; |
| 5939 | break; |
| 5940 | |
| 5941 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5942 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5943 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5944 | break; |
| 5945 | |
| 5946 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5947 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5948 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5949 | break; |
| 5950 | |
| 5951 | case ACT_REPLACE: |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 5952 | len = exp_replace(trash.area, |
| 5953 | trash.size, cur_ptr, |
| 5954 | exp->replace, pmatch); |
| 5955 | if (len < 0) |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 5956 | return -1; |
| 5957 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 5958 | delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len); |
| 5959 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5960 | /* FIXME: if the user adds a newline in the replacement, the |
| 5961 | * index will not be recalculated for now, and the new line |
| 5962 | * will not be counted as a new header. |
| 5963 | */ |
| 5964 | |
| 5965 | cur_end += delta; |
| 5966 | cur_next += delta; |
| 5967 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5968 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5969 | break; |
| 5970 | |
| 5971 | case ACT_REMOVE: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5972 | delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5973 | cur_next += delta; |
| 5974 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5975 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5976 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5977 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5978 | cur_hdr->len = 0; |
| 5979 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 5980 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5981 | break; |
| 5982 | |
| 5983 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5984 | } |
| 5985 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5986 | /* keep the link from this header to next one in case of later |
| 5987 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5988 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5989 | old_idx = cur_idx; |
| 5990 | } |
| 5991 | return 0; |
| 5992 | } |
| 5993 | |
| 5994 | |
| 5995 | /* Apply the filter to the request line. |
| 5996 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5997 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5998 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5999 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6000 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6001 | int apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6002 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6003 | char *cur_ptr, *cur_end; |
| 6004 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6005 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6006 | int delta, len; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6007 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6008 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6009 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6010 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6011 | (exp->action == ACT_ALLOW || |
| 6012 | exp->action == ACT_DENY || |
| 6013 | exp->action == ACT_TARPIT)) |
| 6014 | return 0; |
| 6015 | else if (exp->action == ACT_REMOVE) |
| 6016 | return 0; |
| 6017 | |
| 6018 | done = 0; |
| 6019 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6020 | cur_ptr = ci_head(req); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6021 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6022 | |
| 6023 | /* Now we have the request line between cur_ptr and cur_end */ |
| 6024 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6025 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6026 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6027 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6028 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6029 | done = 1; |
| 6030 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6031 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6032 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6033 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6034 | done = 1; |
| 6035 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6036 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6037 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6038 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6039 | done = 1; |
| 6040 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6041 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6042 | case ACT_REPLACE: |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6043 | len = exp_replace(trash.area, trash.size, |
| 6044 | cur_ptr, exp->replace, pmatch); |
| 6045 | if (len < 0) |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6046 | return -1; |
| 6047 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6048 | delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len); |
| 6049 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6050 | /* FIXME: if the user adds a newline in the replacement, the |
| 6051 | * index will not be recalculated for now, and the new line |
| 6052 | * will not be counted as a new header. |
| 6053 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6054 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6055 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6056 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6057 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6058 | HTTP_MSG_RQMETH, |
| 6059 | cur_ptr, cur_end + 1, |
| 6060 | NULL, NULL); |
| 6061 | if (unlikely(!cur_end)) |
| 6062 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6063 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6064 | /* we have a full request and we know that we have either a CR |
| 6065 | * or an LF at <ptr>. |
| 6066 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6067 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 6068 | 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] | 6069 | /* there is no point trying this regex on headers */ |
| 6070 | return 1; |
| 6071 | } |
| 6072 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6073 | return done; |
| 6074 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 6075 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6076 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6077 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6078 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6079 | * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6080 | * 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] | 6081 | * unparsable request. Since it can manage the switch to another backend, it |
| 6082 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6083 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6084 | int apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6085 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6086 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6087 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6088 | struct hdr_exp *exp; |
| 6089 | |
| 6090 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6091 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6092 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6093 | /* |
| 6094 | * The interleaving of transformations and verdicts |
| 6095 | * makes it difficult to decide to continue or stop |
| 6096 | * the evaluation. |
| 6097 | */ |
| 6098 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6099 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 6100 | break; |
| 6101 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6102 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6103 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6104 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6105 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6106 | |
| 6107 | /* if this filter had a condition, evaluate it now and skip to |
| 6108 | * next filter if the condition does not match. |
| 6109 | */ |
| 6110 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6111 | ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6112 | ret = acl_pass(ret); |
| 6113 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6114 | ret = !ret; |
| 6115 | |
| 6116 | if (!ret) |
| 6117 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6118 | } |
| 6119 | |
| 6120 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6121 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6122 | if (unlikely(ret < 0)) |
| 6123 | return -1; |
| 6124 | |
| 6125 | if (likely(ret == 0)) { |
| 6126 | /* The filter did not match the request, it can be |
| 6127 | * iterated through all headers. |
| 6128 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 6129 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 6130 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6131 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6132 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6133 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6134 | } |
| 6135 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6136 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6137 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6138 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6139 | * the first delimiter is updated if required. The function tries as much as |
| 6140 | * possible to respect the following principles : |
| 6141 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6142 | * colon, in which case <next> is simply removed |
| 6143 | * - set exactly one space character after the new first delimiter, unless |
| 6144 | * there are not enough characters in the block being moved to do so. |
| 6145 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6146 | * one. |
| 6147 | * |
| 6148 | * It is the caller's responsibility to ensure that : |
| 6149 | * - <from> points to a valid delimiter or the colon ; |
| 6150 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6151 | * - there are non-space chars before <from> ; |
| 6152 | * - there is a CR/LF at or after <next>. |
| 6153 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6154 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6155 | { |
| 6156 | char *prev = *from; |
| 6157 | |
| 6158 | if (*prev == ':') { |
| 6159 | /* We're removing the first value, preserve the colon and add a |
| 6160 | * space if possible. |
| 6161 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6162 | if (!HTTP_IS_CRLF(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6163 | next++; |
| 6164 | prev++; |
| 6165 | if (prev < next) |
| 6166 | *prev++ = ' '; |
| 6167 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6168 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6169 | next++; |
| 6170 | } else { |
| 6171 | /* Remove useless spaces before the old delimiter. */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6172 | while (HTTP_IS_SPHT(*(prev-1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6173 | prev--; |
| 6174 | *from = prev; |
| 6175 | |
| 6176 | /* copy the delimiter and if possible a space if we're |
| 6177 | * not at the end of the line. |
| 6178 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6179 | if (!HTTP_IS_CRLF(*next)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6180 | *prev++ = *next++; |
| 6181 | if (prev + 1 < next) |
| 6182 | *prev++ = ' '; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6183 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6184 | next++; |
| 6185 | } |
| 6186 | } |
Willy Tarreau | e312802 | 2018-07-12 15:55:34 +0200 | [diff] [blame] | 6187 | return b_rep_blk(buf, prev, next, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6188 | } |
| 6189 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6190 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6191 | * Manage client-side cookie. It can impact performance by about 2% so it is |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6192 | * desirable to call it only when needed. This code is quite complex because |
| 6193 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6194 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6195 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6196 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6197 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6198 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6199 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6200 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6201 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6202 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6203 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6204 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6205 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6206 | old_idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6207 | hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6208 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6209 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6210 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6211 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6212 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6213 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6214 | hdr_beg = hdr_next; |
| 6215 | hdr_end = hdr_beg + cur_hdr->len; |
| 6216 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6217 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6218 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6219 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6220 | * "Cookie:" headers. |
| 6221 | */ |
| 6222 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6223 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6224 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6225 | old_idx = cur_idx; |
| 6226 | continue; |
| 6227 | } |
| 6228 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6229 | del_from = NULL; /* nothing to be deleted */ |
| 6230 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6231 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6232 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6233 | * attributes whose name begin with a '$', and associate them with |
| 6234 | * the right cookie, if we want to delete this cookie. |
| 6235 | * So there are 3 cases for each cookie read : |
| 6236 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6237 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6238 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6239 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6240 | * "special" cookie. |
| 6241 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6242 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6243 | * *MUST* delete it. |
| 6244 | * |
| 6245 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6246 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6247 | * the RFC explicitly allows spaces before it, and not within the |
| 6248 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6249 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6250 | * after the equal sign too, resulting in some (rare) buggy |
| 6251 | * implementations trying to do that. So let's do what servers do. |
| 6252 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6253 | * allowed quoted strings in values, with any possible character |
| 6254 | * after a backslash, including control chars and delimitors, which |
| 6255 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6256 | * within values even without quotes. |
| 6257 | * |
| 6258 | * We have to keep multiple pointers in order to support cookie |
| 6259 | * removal at the beginning, middle or end of header without |
| 6260 | * corrupting the header. All of these headers are valid : |
| 6261 | * |
| 6262 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6263 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6264 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6265 | * | | | | | | | | | |
| 6266 | * | | | | | | | | hdr_end <--+ |
| 6267 | * | | | | | | | +--> next |
| 6268 | * | | | | | | +----> val_end |
| 6269 | * | | | | | +-----------> val_beg |
| 6270 | * | | | | +--------------> equal |
| 6271 | * | | | +----------------> att_end |
| 6272 | * | | +---------------------> att_beg |
| 6273 | * | +--------------------------> prev |
| 6274 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6275 | */ |
| 6276 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6277 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6278 | /* Iterate through all cookies on this line */ |
| 6279 | |
| 6280 | /* find att_beg */ |
| 6281 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6282 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6283 | att_beg++; |
| 6284 | |
| 6285 | /* find att_end : this is the first character after the last non |
| 6286 | * space before the equal. It may be equal to hdr_end. |
| 6287 | */ |
| 6288 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6289 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6290 | while (equal < hdr_end) { |
| 6291 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6292 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6293 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6294 | continue; |
| 6295 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6296 | } |
| 6297 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6298 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6299 | * is between <att_beg> and <equal>, both may be identical. |
| 6300 | */ |
| 6301 | |
| 6302 | /* look for end of cookie if there is an equal sign */ |
| 6303 | if (equal < hdr_end && *equal == '=') { |
| 6304 | /* look for the beginning of the value */ |
| 6305 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6306 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6307 | val_beg++; |
| 6308 | |
| 6309 | /* find the end of the value, respecting quotes */ |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 6310 | next = http_find_cookie_value_end(val_beg, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6311 | |
| 6312 | /* make val_end point to the first white space or delimitor after the value */ |
| 6313 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6314 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6315 | val_end--; |
| 6316 | } else { |
| 6317 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6318 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6319 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6320 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6321 | * they will automatically be removed if a header before them is removed, |
| 6322 | * since they're supposed to be linked together. |
| 6323 | */ |
| 6324 | if (*att_beg == '$') |
| 6325 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6326 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6327 | /* Ignore cookies with no equal sign */ |
| 6328 | if (equal == next) { |
| 6329 | /* This is not our cookie, so we must preserve it. But if we already |
| 6330 | * scheduled another cookie for removal, we cannot remove the |
| 6331 | * complete header, but we can remove the previous block itself. |
| 6332 | */ |
| 6333 | preserve_hdr = 1; |
| 6334 | if (del_from != NULL) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6335 | int delta = del_hdr_value(&req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6336 | val_end += delta; |
| 6337 | next += delta; |
| 6338 | hdr_end += delta; |
| 6339 | hdr_next += delta; |
| 6340 | cur_hdr->len += delta; |
| 6341 | http_msg_move_end(&txn->req, delta); |
| 6342 | prev = del_from; |
| 6343 | del_from = NULL; |
| 6344 | } |
| 6345 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6346 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6347 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6348 | /* if there are spaces around the equal sign, we need to |
| 6349 | * strip them otherwise we'll get trouble for cookie captures, |
| 6350 | * or even for rewrites. Since this happens extremely rarely, |
| 6351 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6352 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6353 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6354 | int stripped_before = 0; |
| 6355 | int stripped_after = 0; |
| 6356 | |
| 6357 | if (att_end != equal) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6358 | stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6359 | equal += stripped_before; |
| 6360 | val_beg += stripped_before; |
| 6361 | } |
| 6362 | |
| 6363 | if (val_beg > equal + 1) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6364 | stripped_after = b_rep_blk(&req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6365 | val_beg += stripped_after; |
| 6366 | stripped_before += stripped_after; |
| 6367 | } |
| 6368 | |
| 6369 | val_end += stripped_before; |
| 6370 | next += stripped_before; |
| 6371 | hdr_end += stripped_before; |
| 6372 | hdr_next += stripped_before; |
| 6373 | cur_hdr->len += stripped_before; |
| 6374 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6375 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6376 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6377 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6378 | /* First, let's see if we want to capture this cookie. We check |
| 6379 | * that we don't already have a client side cookie, because we |
| 6380 | * can only capture one. Also as an optimisation, we ignore |
| 6381 | * cookies shorter than the declared name. |
| 6382 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6383 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6384 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 6385 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6386 | int log_len = val_end - att_beg; |
| 6387 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6388 | if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6389 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6390 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6391 | if (log_len > sess->fe->capture_len) |
| 6392 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6393 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6394 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6395 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6396 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6397 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6398 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6399 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6400 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6401 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6402 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6403 | * For cookies in prefix mode, the form is : |
| 6404 | * |
| 6405 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6406 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6407 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 6408 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 6409 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6410 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6411 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6412 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6413 | * have the server ID between val_beg and delim, and the original cookie between |
| 6414 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6415 | * |
| 6416 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6417 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6418 | * | || || | |+-> next |
| 6419 | * | || || | +--> val_end |
| 6420 | * | || || +---------> delim |
| 6421 | * | || |+------------> val_beg |
| 6422 | * | || +-------------> att_end = equal |
| 6423 | * | |+-----------------> att_beg |
| 6424 | * | +------------------> prev |
| 6425 | * +-------------------------> hdr_beg |
| 6426 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6427 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6428 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6429 | for (delim = val_beg; delim < val_end; delim++) |
| 6430 | if (*delim == COOKIE_DELIM) |
| 6431 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6432 | } else { |
| 6433 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6434 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6435 | /* Now check if the cookie contains a date field, which would |
| 6436 | * appear after a vertical bar ('|') just after the server name |
| 6437 | * and before the delimiter. |
| 6438 | */ |
| 6439 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6440 | if (vbar1) { |
| 6441 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6442 | * right is the last seen date. It is a base64 encoded |
| 6443 | * 30-bit value representing the UNIX date since the |
| 6444 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6445 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6446 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6447 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6448 | if (val_end - vbar1 >= 5) { |
| 6449 | val = b64tos30(vbar1); |
| 6450 | if (val > 0) |
| 6451 | txn->cookie_last_date = val << 2; |
| 6452 | } |
| 6453 | /* look for a second vertical bar */ |
| 6454 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6455 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6456 | val = b64tos30(vbar1 + 1); |
| 6457 | if (val > 0) |
| 6458 | txn->cookie_first_date = val << 2; |
| 6459 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6460 | } |
| 6461 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6462 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6463 | /* if the cookie has an expiration date and the proxy wants to check |
| 6464 | * it, then we do that now. We first check if the cookie is too old, |
| 6465 | * then only if it has expired. We detect strict overflow because the |
| 6466 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6467 | * in the future are ignored if their offset is beyond one day. This |
| 6468 | * allows an admin to fix timezone issues without expiring everyone |
| 6469 | * and at the same time avoids keeping unwanted side effects for too |
| 6470 | * long. |
| 6471 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6472 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 6473 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) || |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6474 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6475 | txn->flags &= ~TX_CK_MASK; |
| 6476 | txn->flags |= TX_CK_OLD; |
| 6477 | delim = val_beg; // let's pretend we have not found the cookie |
| 6478 | txn->cookie_first_date = 0; |
| 6479 | txn->cookie_last_date = 0; |
| 6480 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6481 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 6482 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) || |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6483 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6484 | txn->flags &= ~TX_CK_MASK; |
| 6485 | txn->flags |= TX_CK_EXPIRED; |
| 6486 | delim = val_beg; // let's pretend we have not found the cookie |
| 6487 | txn->cookie_first_date = 0; |
| 6488 | txn->cookie_last_date = 0; |
| 6489 | } |
| 6490 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6491 | /* Here, we'll look for the first running server which supports the cookie. |
| 6492 | * This allows to share a same cookie between several servers, for example |
| 6493 | * to dedicate backup servers to specific servers only. |
| 6494 | * However, to prevent clients from sticking to cookie-less backup server |
| 6495 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6496 | * empty cookies and mark them as invalid. |
| 6497 | * The same behaviour is applied when persistence must be ignored. |
| 6498 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6499 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6500 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6501 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6502 | while (srv) { |
| 6503 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6504 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 6505 | if ((srv->cur_state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6506 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6507 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6508 | /* we found the server and we can use it */ |
| 6509 | txn->flags &= ~TX_CK_MASK; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 6510 | txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6511 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6512 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6513 | break; |
| 6514 | } else { |
| 6515 | /* we found a server, but it's down, |
| 6516 | * mark it as such and go on in case |
| 6517 | * another one is available. |
| 6518 | */ |
| 6519 | txn->flags &= ~TX_CK_MASK; |
| 6520 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6521 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6522 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6523 | srv = srv->next; |
| 6524 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6525 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6526 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 6527 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6528 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6529 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 6530 | txn->flags |= TX_CK_UNUSED; |
| 6531 | else |
| 6532 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6533 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6534 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6535 | /* depending on the cookie mode, we may have to either : |
| 6536 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6537 | * the server never sees it ; |
| 6538 | * - remove the server id from the cookie value, and tag the cookie as an |
| 6539 | * application cookie so that it does not get accidentely removed later, |
| 6540 | * if we're in cookie prefix mode |
| 6541 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6542 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6543 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6544 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6545 | delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6546 | val_end += delta; |
| 6547 | next += delta; |
| 6548 | hdr_end += delta; |
| 6549 | hdr_next += delta; |
| 6550 | cur_hdr->len += delta; |
| 6551 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6552 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6553 | del_from = NULL; |
| 6554 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6555 | } |
| 6556 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6557 | (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6558 | del_from = prev; |
| 6559 | } |
| 6560 | } else { |
| 6561 | /* This is not our cookie, so we must preserve it. But if we already |
| 6562 | * scheduled another cookie for removal, we cannot remove the |
| 6563 | * complete header, but we can remove the previous block itself. |
| 6564 | */ |
| 6565 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6566 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6567 | if (del_from != NULL) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6568 | int delta = del_hdr_value(&req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 6569 | if (att_beg >= del_from) |
| 6570 | att_beg += delta; |
| 6571 | if (att_end >= del_from) |
| 6572 | att_end += delta; |
| 6573 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6574 | val_end += delta; |
| 6575 | next += delta; |
| 6576 | hdr_end += delta; |
| 6577 | hdr_next += delta; |
| 6578 | cur_hdr->len += delta; |
| 6579 | http_msg_move_end(&txn->req, delta); |
| 6580 | prev = del_from; |
| 6581 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6582 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6583 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6584 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6585 | /* continue with next cookie on this header line */ |
| 6586 | att_beg = next; |
| 6587 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6588 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6589 | /* There are no more cookies on this line. |
| 6590 | * We may still have one (or several) marked for deletion at the |
| 6591 | * end of the line. We must do this now in two ways : |
| 6592 | * - if some cookies must be preserved, we only delete from the |
| 6593 | * mark to the end of line ; |
| 6594 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6595 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6596 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6597 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6598 | if (preserve_hdr) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6599 | delta = del_hdr_value(&req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6600 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6601 | cur_hdr->len += delta; |
| 6602 | } else { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6603 | delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6604 | |
| 6605 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6606 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6607 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6608 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6609 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6610 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6611 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6612 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6613 | } |
| 6614 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6615 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6616 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6617 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6618 | } |
| 6619 | |
| 6620 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6621 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6622 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6623 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6624 | int apply_filter_to_resp_headers(struct stream *s, struct channel *rtr, struct hdr_exp *exp) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6625 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6626 | char *cur_ptr, *cur_end, *cur_next; |
| 6627 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6628 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6629 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6630 | int delta, len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6631 | |
| 6632 | last_hdr = 0; |
| 6633 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6634 | cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6635 | old_idx = 0; |
| 6636 | |
| 6637 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6638 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6639 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6640 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6641 | (exp->action == ACT_ALLOW || |
| 6642 | exp->action == ACT_DENY)) |
| 6643 | return 0; |
| 6644 | |
| 6645 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 6646 | if (!cur_idx) |
| 6647 | break; |
| 6648 | |
| 6649 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6650 | cur_ptr = cur_next; |
| 6651 | cur_end = cur_ptr + cur_hdr->len; |
| 6652 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6653 | |
| 6654 | /* Now we have one header between cur_ptr and cur_end, |
| 6655 | * and the next header starts at cur_next. |
| 6656 | */ |
| 6657 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6658 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6659 | switch (exp->action) { |
| 6660 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6661 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6662 | last_hdr = 1; |
| 6663 | break; |
| 6664 | |
| 6665 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6666 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6667 | last_hdr = 1; |
| 6668 | break; |
| 6669 | |
| 6670 | case ACT_REPLACE: |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6671 | len = exp_replace(trash.area, |
| 6672 | trash.size, cur_ptr, |
| 6673 | exp->replace, pmatch); |
| 6674 | if (len < 0) |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6675 | return -1; |
| 6676 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6677 | delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len); |
| 6678 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6679 | /* FIXME: if the user adds a newline in the replacement, the |
| 6680 | * index will not be recalculated for now, and the new line |
| 6681 | * will not be counted as a new header. |
| 6682 | */ |
| 6683 | |
| 6684 | cur_end += delta; |
| 6685 | cur_next += delta; |
| 6686 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6687 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6688 | break; |
| 6689 | |
| 6690 | case ACT_REMOVE: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6691 | delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6692 | cur_next += delta; |
| 6693 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6694 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6695 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6696 | txn->hdr_idx.used--; |
| 6697 | cur_hdr->len = 0; |
| 6698 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6699 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6700 | break; |
| 6701 | |
| 6702 | } |
| 6703 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6704 | |
| 6705 | /* keep the link from this header to next one in case of later |
| 6706 | * removal of next header. |
| 6707 | */ |
| 6708 | old_idx = cur_idx; |
| 6709 | } |
| 6710 | return 0; |
| 6711 | } |
| 6712 | |
| 6713 | |
| 6714 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 6715 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6716 | * or -1 if a replacement resulted in an invalid status line. |
| 6717 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6718 | int apply_filter_to_sts_line(struct stream *s, struct channel *rtr, struct hdr_exp *exp) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6719 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6720 | char *cur_ptr, *cur_end; |
| 6721 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6722 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6723 | int delta, len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6724 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6725 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6726 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6727 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6728 | (exp->action == ACT_ALLOW || |
| 6729 | exp->action == ACT_DENY)) |
| 6730 | return 0; |
| 6731 | else if (exp->action == ACT_REMOVE) |
| 6732 | return 0; |
| 6733 | |
| 6734 | done = 0; |
| 6735 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6736 | cur_ptr = ci_head(rtr); |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6737 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6738 | |
| 6739 | /* Now we have the status line between cur_ptr and cur_end */ |
| 6740 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6741 | if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch, 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6742 | switch (exp->action) { |
| 6743 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6744 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6745 | done = 1; |
| 6746 | break; |
| 6747 | |
| 6748 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6749 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6750 | done = 1; |
| 6751 | break; |
| 6752 | |
| 6753 | case ACT_REPLACE: |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6754 | len = exp_replace(trash.area, trash.size, |
| 6755 | cur_ptr, exp->replace, pmatch); |
| 6756 | if (len < 0) |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6757 | return -1; |
| 6758 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6759 | delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len); |
| 6760 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6761 | /* FIXME: if the user adds a newline in the replacement, the |
| 6762 | * index will not be recalculated for now, and the new line |
| 6763 | * will not be counted as a new header. |
| 6764 | */ |
| 6765 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6766 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6767 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6768 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 6769 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6770 | cur_ptr, cur_end + 1, |
| 6771 | NULL, NULL); |
| 6772 | if (unlikely(!cur_end)) |
| 6773 | return -1; |
| 6774 | |
| 6775 | /* we have a full respnse and we know that we have either a CR |
| 6776 | * or an LF at <ptr>. |
| 6777 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6778 | txn->status = strl2ui(ci_head(rtr) + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l); |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6779 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r'); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6780 | /* there is no point trying this regex on headers */ |
| 6781 | return 1; |
| 6782 | } |
| 6783 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6784 | return done; |
| 6785 | } |
| 6786 | |
| 6787 | |
| 6788 | |
| 6789 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6790 | * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of stream <s>. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6791 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 6792 | * unparsable response. |
| 6793 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6794 | int apply_filters_to_response(struct stream *s, struct channel *rtr, struct proxy *px) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6795 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6796 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6797 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6798 | struct hdr_exp *exp; |
| 6799 | |
| 6800 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6801 | int ret; |
| 6802 | |
| 6803 | /* |
| 6804 | * The interleaving of transformations and verdicts |
| 6805 | * makes it difficult to decide to continue or stop |
| 6806 | * the evaluation. |
| 6807 | */ |
| 6808 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6809 | if (txn->flags & TX_SVDENY) |
| 6810 | break; |
| 6811 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6812 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6813 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 6814 | exp->action == ACT_PASS)) { |
| 6815 | exp = exp->next; |
| 6816 | continue; |
| 6817 | } |
| 6818 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6819 | /* if this filter had a condition, evaluate it now and skip to |
| 6820 | * next filter if the condition does not match. |
| 6821 | */ |
| 6822 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6823 | ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6824 | ret = acl_pass(ret); |
| 6825 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6826 | ret = !ret; |
| 6827 | if (!ret) |
| 6828 | continue; |
| 6829 | } |
| 6830 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6831 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6832 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6833 | if (unlikely(ret < 0)) |
| 6834 | return -1; |
| 6835 | |
| 6836 | if (likely(ret == 0)) { |
| 6837 | /* The filter did not match the response, it can be |
| 6838 | * iterated through all headers. |
| 6839 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6840 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 6841 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6842 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6843 | } |
| 6844 | return 0; |
| 6845 | } |
| 6846 | |
| 6847 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6848 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6849 | * Manage server-side cookies. It can impact performance by about 2% so it is |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6850 | * desirable to call it only when needed. This function is also used when we |
| 6851 | * just need to know if there is a cookie (eg: for check-cache). |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6852 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6853 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6854 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6855 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6856 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6857 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6858 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6859 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6860 | char *hdr_beg, *hdr_end, *hdr_next; |
| 6861 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6862 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6863 | /* Iterate through the headers. |
| 6864 | * we start with the start line. |
| 6865 | */ |
| 6866 | old_idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6867 | hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6868 | |
| 6869 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 6870 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6871 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6872 | |
| 6873 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6874 | hdr_beg = hdr_next; |
| 6875 | hdr_end = hdr_beg + cur_hdr->len; |
| 6876 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6877 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6878 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6879 | * next header starts at hdr_next. We're only interested in |
| 6880 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6881 | */ |
| 6882 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6883 | is_cookie2 = 0; |
| 6884 | prev = hdr_beg + 10; |
| 6885 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6886 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6887 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 6888 | if (!val) { |
| 6889 | old_idx = cur_idx; |
| 6890 | continue; |
| 6891 | } |
| 6892 | is_cookie2 = 1; |
| 6893 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6894 | } |
| 6895 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6896 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 6897 | * <prev> points to the colon. |
| 6898 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6899 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6900 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6901 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 6902 | * check-cache is enabled) and we are not interested in checking |
| 6903 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6904 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 6905 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6906 | return; |
| 6907 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6908 | /* OK so now we know we have to process this response cookie. |
| 6909 | * The format of the Set-Cookie header is slightly different |
| 6910 | * from the format of the Cookie header in that it does not |
| 6911 | * support the comma as a cookie delimiter (thus the header |
| 6912 | * cannot be folded) because the Expires attribute described in |
| 6913 | * the original Netscape's spec may contain an unquoted date |
| 6914 | * with a comma inside. We have to live with this because |
| 6915 | * many browsers don't support Max-Age and some browsers don't |
| 6916 | * support quoted strings. However the Set-Cookie2 header is |
| 6917 | * clean. |
| 6918 | * |
| 6919 | * We have to keep multiple pointers in order to support cookie |
| 6920 | * removal at the beginning, middle or end of header without |
| 6921 | * corrupting the header (in case of set-cookie2). A special |
| 6922 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 6923 | * fields after the first semi-colon. The <next> pointer points |
| 6924 | * either to the end of line (set-cookie) or next unquoted comma |
| 6925 | * (set-cookie2). All of these headers are valid : |
| 6926 | * |
| 6927 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 6928 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6929 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6930 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 6931 | * | | | | | | | | | | |
| 6932 | * | | | | | | | | +-> next hdr_end <--+ |
| 6933 | * | | | | | | | +------------> scav |
| 6934 | * | | | | | | +--------------> val_end |
| 6935 | * | | | | | +--------------------> val_beg |
| 6936 | * | | | | +----------------------> equal |
| 6937 | * | | | +------------------------> att_end |
| 6938 | * | | +----------------------------> att_beg |
| 6939 | * | +------------------------------> prev |
| 6940 | * +-----------------------------------------> hdr_beg |
| 6941 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6942 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6943 | for (; prev < hdr_end; prev = next) { |
| 6944 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6945 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6946 | /* find att_beg */ |
| 6947 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6948 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6949 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6950 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6951 | /* find att_end : this is the first character after the last non |
| 6952 | * space before the equal. It may be equal to hdr_end. |
| 6953 | */ |
| 6954 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6955 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6956 | while (equal < hdr_end) { |
| 6957 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 6958 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6959 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6960 | continue; |
| 6961 | att_end = equal; |
| 6962 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6963 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6964 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6965 | * is between <att_beg> and <equal>, both may be identical. |
| 6966 | */ |
| 6967 | |
| 6968 | /* look for end of cookie if there is an equal sign */ |
| 6969 | if (equal < hdr_end && *equal == '=') { |
| 6970 | /* look for the beginning of the value */ |
| 6971 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6972 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6973 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6974 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6975 | /* find the end of the value, respecting quotes */ |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 6976 | next = http_find_cookie_value_end(val_beg, hdr_end); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6977 | |
| 6978 | /* make val_end point to the first white space or delimitor after the value */ |
| 6979 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6980 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6981 | val_end--; |
| 6982 | } else { |
| 6983 | /* <equal> points to next comma, semi-colon or EOL */ |
| 6984 | val_beg = val_end = next = equal; |
| 6985 | } |
| 6986 | |
| 6987 | if (next < hdr_end) { |
| 6988 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 6989 | * a colon or semi-colon before the end. So skip all attr-value |
| 6990 | * pairs and look for the next comma. For Set-Cookie, since |
| 6991 | * commas are permitted in values, skip to the end. |
| 6992 | */ |
| 6993 | if (is_cookie2) |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 6994 | next = http_find_hdr_value_end(next, hdr_end); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6995 | else |
| 6996 | next = hdr_end; |
| 6997 | } |
| 6998 | |
| 6999 | /* Now everything is as on the diagram above */ |
| 7000 | |
| 7001 | /* Ignore cookies with no equal sign */ |
| 7002 | if (equal == val_end) |
| 7003 | continue; |
| 7004 | |
| 7005 | /* If there are spaces around the equal sign, we need to |
| 7006 | * strip them otherwise we'll get trouble for cookie captures, |
| 7007 | * or even for rewrites. Since this happens extremely rarely, |
| 7008 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7009 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7010 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7011 | int stripped_before = 0; |
| 7012 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7013 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7014 | if (att_end != equal) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7015 | stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7016 | equal += stripped_before; |
| 7017 | val_beg += stripped_before; |
| 7018 | } |
| 7019 | |
| 7020 | if (val_beg > equal + 1) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7021 | stripped_after = b_rep_blk(&res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7022 | val_beg += stripped_after; |
| 7023 | stripped_before += stripped_after; |
| 7024 | } |
| 7025 | |
| 7026 | val_end += stripped_before; |
| 7027 | next += stripped_before; |
| 7028 | hdr_end += stripped_before; |
| 7029 | hdr_next += stripped_before; |
| 7030 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 7031 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7032 | } |
| 7033 | |
| 7034 | /* First, let's see if we want to capture this cookie. We check |
| 7035 | * that we don't already have a server side cookie, because we |
| 7036 | * can only capture one. Also as an optimisation, we ignore |
| 7037 | * cookies shorter than the declared name. |
| 7038 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7039 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 7040 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7041 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 7042 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7043 | int log_len = val_end - att_beg; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7044 | if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7045 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7046 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7047 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7048 | if (log_len > sess->fe->capture_len) |
| 7049 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7050 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7051 | txn->srv_cookie[log_len] = 0; |
| 7052 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7053 | } |
| 7054 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7055 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7056 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7057 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7058 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 7059 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7060 | /* assume passive cookie by default */ |
| 7061 | txn->flags &= ~TX_SCK_MASK; |
| 7062 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7063 | |
| 7064 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7065 | * this occurrence because we'll insert another one later. |
| 7066 | * We'll delete it too if the "indirect" option is set and we're in |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7067 | * a direct access. |
| 7068 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7069 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7070 | /* The "preserve" flag was set, we don't want to touch the |
| 7071 | * server's cookie. |
| 7072 | */ |
| 7073 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7074 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 7075 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7076 | /* this cookie must be deleted */ |
| 7077 | if (*prev == ':' && next == hdr_end) { |
| 7078 | /* whole header */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7079 | delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7080 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7081 | txn->hdr_idx.used--; |
| 7082 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7083 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7084 | hdr_next += delta; |
| 7085 | http_msg_move_end(&txn->rsp, delta); |
| 7086 | /* note: while both invalid now, <next> and <hdr_end> |
| 7087 | * are still equal, so the for() will stop as expected. |
| 7088 | */ |
| 7089 | } else { |
| 7090 | /* just remove the value */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7091 | int delta = del_hdr_value(&res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7092 | next = prev; |
| 7093 | hdr_end += delta; |
| 7094 | hdr_next += delta; |
| 7095 | cur_hdr->len += delta; |
| 7096 | http_msg_move_end(&txn->rsp, delta); |
| 7097 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7098 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7099 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7100 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7101 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7102 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7103 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7104 | * with this server since we know it. |
| 7105 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7106 | delta = b_rep_blk(&res->buf, val_beg, val_end, srv->cookie, srv->cklen); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7107 | next += delta; |
| 7108 | hdr_end += delta; |
| 7109 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7110 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7111 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7112 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7113 | txn->flags &= ~TX_SCK_MASK; |
| 7114 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7115 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7116 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7117 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7118 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7119 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7120 | delta = b_rep_blk(&res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7121 | next += delta; |
| 7122 | hdr_end += delta; |
| 7123 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7124 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7125 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7126 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7127 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7128 | txn->flags &= ~TX_SCK_MASK; |
| 7129 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7130 | } |
| 7131 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7132 | /* that's done for this cookie, check the next one on the same |
| 7133 | * line when next != hdr_end (only if is_cookie2). |
| 7134 | */ |
| 7135 | } |
| 7136 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7137 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7138 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7139 | } |
| 7140 | |
| 7141 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7142 | /* |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 7143 | * Parses the Cache-Control and Pragma request header fields to determine if |
| 7144 | * the request may be served from the cache and/or if it is cacheable. Updates |
| 7145 | * s->txn->flags. |
| 7146 | */ |
| 7147 | void check_request_for_cacheability(struct stream *s, struct channel *chn) |
| 7148 | { |
| 7149 | struct http_txn *txn = s->txn; |
| 7150 | char *p1, *p2; |
| 7151 | char *cur_ptr, *cur_end, *cur_next; |
| 7152 | int pragma_found; |
| 7153 | int cc_found; |
| 7154 | int cur_idx; |
| 7155 | |
| 7156 | if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE) |
| 7157 | return; /* nothing more to do here */ |
| 7158 | |
| 7159 | cur_idx = 0; |
| 7160 | pragma_found = cc_found = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7161 | cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 7162 | |
| 7163 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7164 | struct hdr_idx_elem *cur_hdr; |
| 7165 | int val; |
| 7166 | |
| 7167 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7168 | cur_ptr = cur_next; |
| 7169 | cur_end = cur_ptr + cur_hdr->len; |
| 7170 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7171 | |
| 7172 | /* We have one full header between cur_ptr and cur_end, and the |
| 7173 | * next header starts at cur_next. |
| 7174 | */ |
| 7175 | |
| 7176 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7177 | if (val) { |
| 7178 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7179 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7180 | pragma_found = 1; |
| 7181 | continue; |
| 7182 | } |
| 7183 | } |
| 7184 | |
William Lallemand | 8a16fe0 | 2018-05-22 11:04:33 +0200 | [diff] [blame] | 7185 | /* Don't use the cache and don't try to store if we found the |
| 7186 | * Authorization header */ |
| 7187 | val = http_header_match2(cur_ptr, cur_end, "Authorization", 13); |
| 7188 | if (val) { |
| 7189 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7190 | txn->flags |= TX_CACHE_IGNORE; |
| 7191 | continue; |
| 7192 | } |
| 7193 | |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 7194 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7195 | if (!val) |
| 7196 | continue; |
| 7197 | |
| 7198 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7199 | cc_found = 1; |
| 7200 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
| 7201 | |
| 7202 | if (p1 >= cur_end) /* no more info */ |
| 7203 | continue; |
| 7204 | |
| 7205 | /* p1 is at the beginning of the value */ |
| 7206 | p2 = p1; |
| 7207 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
| 7208 | p2++; |
| 7209 | |
| 7210 | /* we have a complete value between p1 and p2. We don't check the |
| 7211 | * values after max-age, max-stale nor min-fresh, we simply don't |
| 7212 | * use the cache when they're specified. |
| 7213 | */ |
| 7214 | if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) || |
| 7215 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
| 7216 | ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) || |
| 7217 | ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) { |
| 7218 | txn->flags |= TX_CACHE_IGNORE; |
| 7219 | continue; |
| 7220 | } |
| 7221 | |
| 7222 | if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) { |
| 7223 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7224 | continue; |
| 7225 | } |
| 7226 | } |
| 7227 | |
| 7228 | /* RFC7234#5.4: |
| 7229 | * When the Cache-Control header field is also present and |
| 7230 | * understood in a request, Pragma is ignored. |
| 7231 | * When the Cache-Control header field is not present in a |
| 7232 | * request, caches MUST consider the no-cache request |
| 7233 | * pragma-directive as having the same effect as if |
| 7234 | * "Cache-Control: no-cache" were present. |
| 7235 | */ |
| 7236 | if (!cc_found && pragma_found) |
| 7237 | txn->flags |= TX_CACHE_IGNORE; |
| 7238 | } |
| 7239 | |
| 7240 | /* |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7241 | * Check if response is cacheable or not. Updates s->txn->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7242 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7243 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7244 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7245 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7246 | char *p1, *p2; |
| 7247 | |
| 7248 | char *cur_ptr, *cur_end, *cur_next; |
| 7249 | int cur_idx; |
| 7250 | |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 7251 | if (txn->status < 200) { |
| 7252 | /* do not try to cache interim responses! */ |
| 7253 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7254 | return; |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 7255 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7256 | |
| 7257 | /* Iterate through the headers. |
| 7258 | * we start with the start line. |
| 7259 | */ |
| 7260 | cur_idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7261 | cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7262 | |
| 7263 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7264 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7265 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7266 | |
| 7267 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7268 | cur_ptr = cur_next; |
| 7269 | cur_end = cur_ptr + cur_hdr->len; |
| 7270 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7271 | |
| 7272 | /* We have one full header between cur_ptr and cur_end, and the |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7273 | * next header starts at cur_next. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7274 | */ |
| 7275 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7276 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7277 | if (val) { |
| 7278 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7279 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7280 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7281 | return; |
| 7282 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7283 | } |
| 7284 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7285 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7286 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7287 | continue; |
| 7288 | |
| 7289 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7290 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7291 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7292 | |
| 7293 | if (p1 >= cur_end) /* no more info */ |
| 7294 | continue; |
| 7295 | |
| 7296 | /* p1 is at the beginning of the value */ |
| 7297 | p2 = p1; |
| 7298 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7299 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7300 | p2++; |
| 7301 | |
| 7302 | /* we have a complete value between p1 and p2 */ |
| 7303 | if (p2 < cur_end && *p2 == '=') { |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7304 | if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7305 | ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
| 7306 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7307 | continue; |
| 7308 | } |
| 7309 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7310 | /* we have something of the form no-cache="set-cookie" */ |
| 7311 | if ((cur_end - p1 >= 21) && |
| 7312 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7313 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7314 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7315 | continue; |
| 7316 | } |
| 7317 | |
| 7318 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7319 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 7320 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7321 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7322 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7323 | return; |
| 7324 | } |
| 7325 | |
| 7326 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7327 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7328 | continue; |
| 7329 | } |
| 7330 | } |
| 7331 | } |
| 7332 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7333 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7334 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7335 | * In a GET, HEAD or POST request, check if the requested URI matches the stats uri |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7336 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7337 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7338 | * It is assumed that the request is either a HEAD, GET, or POST and that the |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7339 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7340 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7341 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7342 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7343 | int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7344 | { |
| 7345 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7346 | struct http_msg *msg = &txn->req; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7347 | const char *uri = ci_head(msg->chn)+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7348 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7349 | if (!uri_auth) |
| 7350 | return 0; |
| 7351 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7352 | if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST) |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7353 | return 0; |
| 7354 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7355 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7356 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7357 | return 0; |
| 7358 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 7359 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7360 | return 0; |
| 7361 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7362 | return 1; |
| 7363 | } |
| 7364 | |
Willy Tarreau | 7ccdd8d | 2018-09-07 14:01:39 +0200 | [diff] [blame] | 7365 | /* Append the description of what is present in error snapshot <es> into <out>. |
| 7366 | * The description must be small enough to always fit in a trash. The output |
| 7367 | * buffer may be the trash so the trash must not be used inside this function. |
| 7368 | */ |
| 7369 | void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es) |
| 7370 | { |
| 7371 | chunk_appendf(&trash, |
| 7372 | " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n" |
| 7373 | " HTTP msg state %s(%d), msg flags 0x%08x\n" |
| 7374 | " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n", |
| 7375 | es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags, |
| 7376 | h1_msg_state_str(es->ctx.http.state), es->ctx.http.state, |
| 7377 | es->ctx.http.m_flags, es->ctx.http.m_clen, |
| 7378 | es->ctx.http.m_blen, es->ctx.http.b_flags); |
| 7379 | } |
| 7380 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7381 | /* |
| 7382 | * Capture a bad request or response and archive it in the proxy's structure. |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7383 | * By default it tries to report the error position as msg->err_pos. However if |
| 7384 | * this one is not set, it will then report msg->next, which is the last known |
| 7385 | * parsing point. The function is able to deal with wrapping buffers. It always |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 7386 | * displays buffers as a contiguous area starting at buf->p. The direction is |
| 7387 | * determined thanks to the channel's flags. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7388 | */ |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 7389 | void http_capture_bad_message(struct proxy *proxy, struct stream *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7390 | struct http_msg *msg, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 7391 | enum h1_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7392 | { |
Willy Tarreau | ef3ca73 | 2018-09-07 15:47:35 +0200 | [diff] [blame] | 7393 | union error_snapshot_ctx ctx; |
| 7394 | long ofs; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7395 | |
Willy Tarreau | ef3ca73 | 2018-09-07 15:47:35 +0200 | [diff] [blame] | 7396 | /* http-specific part now */ |
| 7397 | ctx.http.sid = s->uniq_id; |
| 7398 | ctx.http.state = state; |
| 7399 | ctx.http.b_flags = msg->chn->flags; |
| 7400 | ctx.http.s_flags = s->flags; |
| 7401 | ctx.http.t_flags = s->txn->flags; |
| 7402 | ctx.http.m_flags = msg->flags; |
| 7403 | ctx.http.m_clen = msg->chunk_len; |
| 7404 | ctx.http.m_blen = msg->body_len; |
Willy Tarreau | 7480f32 | 2018-09-06 19:41:22 +0200 | [diff] [blame] | 7405 | |
Willy Tarreau | ef3ca73 | 2018-09-07 15:47:35 +0200 | [diff] [blame] | 7406 | ofs = msg->chn->total - ci_data(msg->chn); |
| 7407 | if (ofs < 0) |
| 7408 | ofs = 0; |
Willy Tarreau | 0b5b480 | 2018-09-07 13:49:44 +0200 | [diff] [blame] | 7409 | |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 7410 | proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP), |
Willy Tarreau | ef3ca73 | 2018-09-07 15:47:35 +0200 | [diff] [blame] | 7411 | other_end, s->target, |
| 7412 | strm_sess(s), &msg->chn->buf, |
| 7413 | ofs, co_data(msg->chn), |
| 7414 | (msg->err_pos >= 0) ? msg->err_pos : msg->next, |
| 7415 | &ctx, http_show_error_snapshot); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7416 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7417 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7418 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7419 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7420 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7421 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7422 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7423 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7424 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7425 | * -1. The value fetch stops at commas, so this function is suited for use with |
| 7426 | * list headers. |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7427 | * The return value is 0 if nothing was found, or non-zero otherwise. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7428 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7429 | unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen, |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7430 | struct hdr_idx *idx, int occ, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7431 | struct hdr_ctx *ctx, char **vptr, size_t *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7432 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7433 | struct hdr_ctx local_ctx; |
| 7434 | char *ptr_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7435 | unsigned int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7436 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7437 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7438 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7439 | if (!ctx) { |
| 7440 | local_ctx.idx = 0; |
| 7441 | ctx = &local_ctx; |
| 7442 | } |
| 7443 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7444 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7445 | /* search from the beginning */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7446 | while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7447 | occ--; |
| 7448 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7449 | *vptr = ctx->line + ctx->val; |
| 7450 | *vlen = ctx->vlen; |
| 7451 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7452 | } |
| 7453 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7454 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7455 | } |
| 7456 | |
| 7457 | /* negative occurrence, we scan all the list then walk back */ |
| 7458 | if (-occ > MAX_HDR_HISTORY) |
| 7459 | return 0; |
| 7460 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7461 | found = hist_ptr = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7462 | while (http_find_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7463 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7464 | len_hist[hist_ptr] = ctx->vlen; |
| 7465 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7466 | hist_ptr = 0; |
| 7467 | found++; |
| 7468 | } |
| 7469 | if (-occ > found) |
| 7470 | return 0; |
| 7471 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 7472 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 7473 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 7474 | * to remain in the 0..9 range. |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7475 | */ |
Willy Tarreau | 67dad27 | 2013-06-12 22:27:44 +0200 | [diff] [blame] | 7476 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7477 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7478 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7479 | *vptr = ptr_hist[hist_ptr]; |
| 7480 | *vlen = len_hist[hist_ptr]; |
| 7481 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7482 | } |
| 7483 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7484 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7485 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7486 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7487 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7488 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7489 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7490 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 7491 | * -1. This function differs from http_get_hdr() in that it only returns full |
| 7492 | * line header values and does not stop at commas. |
| 7493 | * The return value is 0 if nothing was found, or non-zero otherwise. |
| 7494 | */ |
| 7495 | unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen, |
| 7496 | struct hdr_idx *idx, int occ, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7497 | struct hdr_ctx *ctx, char **vptr, size_t *vlen) |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7498 | { |
| 7499 | struct hdr_ctx local_ctx; |
| 7500 | char *ptr_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7501 | unsigned int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7502 | unsigned int hist_ptr; |
| 7503 | int found; |
| 7504 | |
| 7505 | if (!ctx) { |
| 7506 | local_ctx.idx = 0; |
| 7507 | ctx = &local_ctx; |
| 7508 | } |
| 7509 | |
| 7510 | if (occ >= 0) { |
| 7511 | /* search from the beginning */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7512 | while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) { |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7513 | occ--; |
| 7514 | if (occ <= 0) { |
| 7515 | *vptr = ctx->line + ctx->val; |
| 7516 | *vlen = ctx->vlen; |
| 7517 | return 1; |
| 7518 | } |
| 7519 | } |
| 7520 | return 0; |
| 7521 | } |
| 7522 | |
| 7523 | /* negative occurrence, we scan all the list then walk back */ |
| 7524 | if (-occ > MAX_HDR_HISTORY) |
| 7525 | return 0; |
| 7526 | |
| 7527 | found = hist_ptr = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7528 | while (http_find_full_header2(hname, hlen, ci_head(msg->chn), idx, ctx)) { |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7529 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7530 | len_hist[hist_ptr] = ctx->vlen; |
| 7531 | if (++hist_ptr >= MAX_HDR_HISTORY) |
| 7532 | hist_ptr = 0; |
| 7533 | found++; |
| 7534 | } |
| 7535 | if (-occ > found) |
| 7536 | return 0; |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 7537 | |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7538 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 7539 | * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have |
| 7540 | * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ] |
| 7541 | * to remain in the 0..9 range. |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7542 | */ |
Nenad Merdanovic | 69ad4b9 | 2016-03-29 13:14:30 +0200 | [diff] [blame] | 7543 | hist_ptr += occ + MAX_HDR_HISTORY; |
Willy Tarreau | 04ff9f1 | 2013-06-10 18:39:42 +0200 | [diff] [blame] | 7544 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7545 | hist_ptr -= MAX_HDR_HISTORY; |
| 7546 | *vptr = ptr_hist[hist_ptr]; |
| 7547 | *vlen = len_hist[hist_ptr]; |
| 7548 | return 1; |
| 7549 | } |
| 7550 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7551 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 7552 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 7553 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 7554 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7555 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7556 | void debug_hdr(const char *dir, struct stream *s, const char *start, const char *end) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7557 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7558 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7559 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7560 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7561 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7562 | dir, |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 7563 | objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1, |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 7564 | objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 7565 | |
| 7566 | for (max = 0; start + max < end; max++) |
| 7567 | if (start[max] == '\r' || start[max] == '\n') |
| 7568 | break; |
| 7569 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7570 | UBOUND(max, trash.size - trash.data - 3); |
| 7571 | trash.data += strlcpy2(trash.area + trash.data, start, max + 1); |
| 7572 | trash.area[trash.data++] = '\n'; |
| 7573 | shut_your_big_mouth_gcc(write(1, trash.area, trash.data)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7574 | } |
| 7575 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7576 | |
| 7577 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 7578 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 7579 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 7580 | * assigned to the stream and returned. |
| 7581 | */ |
| 7582 | struct http_txn *http_alloc_txn(struct stream *s) |
| 7583 | { |
| 7584 | struct http_txn *txn = s->txn; |
| 7585 | |
| 7586 | if (txn) |
| 7587 | return txn; |
| 7588 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7589 | txn = pool_alloc(pool_head_http_txn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7590 | if (!txn) |
| 7591 | return txn; |
| 7592 | |
| 7593 | txn->hdr_idx.size = global.tune.max_http_hdr; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7594 | txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7595 | if (!txn->hdr_idx.v) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7596 | pool_free(pool_head_http_txn, txn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7597 | return NULL; |
| 7598 | } |
| 7599 | |
| 7600 | s->txn = txn; |
| 7601 | return txn; |
| 7602 | } |
| 7603 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 7604 | void http_txn_reset_req(struct http_txn *txn) |
| 7605 | { |
| 7606 | txn->req.flags = 0; |
| 7607 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
| 7608 | txn->req.next = 0; |
| 7609 | txn->req.chunk_len = 0LL; |
| 7610 | txn->req.body_len = 0LL; |
| 7611 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7612 | } |
| 7613 | |
| 7614 | void http_txn_reset_res(struct http_txn *txn) |
| 7615 | { |
| 7616 | txn->rsp.flags = 0; |
| 7617 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 7618 | txn->rsp.next = 0; |
| 7619 | txn->rsp.chunk_len = 0LL; |
| 7620 | txn->rsp.body_len = 0LL; |
| 7621 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 7622 | } |
| 7623 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7624 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7625 | * Initialize a new HTTP transaction for stream <s>. It is assumed that all |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7626 | * the required fields are properly allocated and that we only need to (re)init |
| 7627 | * them. This should be used before processing any new request. |
| 7628 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7629 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7630 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7631 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7632 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7633 | |
| 7634 | txn->flags = 0; |
| 7635 | txn->status = -1; |
| 7636 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7637 | txn->cookie_first_date = 0; |
| 7638 | txn->cookie_last_date = 0; |
| 7639 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7640 | txn->srv_cookie = NULL; |
| 7641 | txn->cli_cookie = NULL; |
| 7642 | txn->uri = NULL; |
| 7643 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 7644 | http_txn_reset_req(txn); |
| 7645 | http_txn_reset_res(txn); |
| 7646 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7647 | txn->req.chn = &s->req; |
| 7648 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7649 | |
| 7650 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7651 | |
| 7652 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7653 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7654 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7655 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7656 | if (txn->hdr_idx.v) |
| 7657 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 7658 | |
| 7659 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 7660 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7661 | } |
| 7662 | |
| 7663 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7664 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7665 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7666 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7667 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7668 | |
| 7669 | /* these ones will have been dynamically allocated */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7670 | pool_free(pool_head_requri, txn->uri); |
| 7671 | pool_free(pool_head_capture, txn->cli_cookie); |
| 7672 | pool_free(pool_head_capture, txn->srv_cookie); |
| 7673 | pool_free(pool_head_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7674 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7675 | s->unique_id = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7676 | txn->uri = NULL; |
| 7677 | txn->srv_cookie = NULL; |
| 7678 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7679 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 7680 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7681 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7682 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7683 | pool_free(h->pool, s->req_cap[h->index]); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 7684 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7685 | } |
| 7686 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 7687 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7688 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7689 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7690 | pool_free(h->pool, s->res_cap[h->index]); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 7691 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7692 | } |
| 7693 | |
Willy Tarreau | cda7f3f | 2018-10-28 13:44:36 +0100 | [diff] [blame] | 7694 | if (!LIST_ISEMPTY(&s->vars_txn.head)) |
| 7695 | vars_prune(&s->vars_txn, s->sess, s); |
| 7696 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 7697 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7698 | } |
| 7699 | |
| 7700 | /* to be used at the end of a transaction to prepare a new one */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7701 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7702 | { |
| 7703 | http_end_txn(s); |
| 7704 | http_init_txn(s); |
| 7705 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 7706 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 7707 | * any rulelist match the NULL pointer. |
| 7708 | */ |
| 7709 | s->current_rule_list = NULL; |
| 7710 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7711 | s->be = strm_fe(s); |
| 7712 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 7713 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7714 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7715 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7716 | /* re-init store persistence */ |
| 7717 | s->store_count = 0; |
Willy Tarreau | 90a7c03 | 2018-09-05 16:21:29 +0200 | [diff] [blame] | 7718 | s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1); |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7719 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7720 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7721 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7722 | /* We must trim any excess data from the response buffer, because we |
| 7723 | * may have blocked an invalid response from a server that we don't |
| 7724 | * want to accidentely forward once we disable the analysers, nor do |
| 7725 | * we want those data to come along with next response. A typical |
| 7726 | * example of such data would be from a buggy server responding to |
| 7727 | * a HEAD with some data, or sending more than the advertised |
| 7728 | * content-length. |
| 7729 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7730 | if (unlikely(ci_data(&s->res))) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7731 | b_set_data(&s->res.buf, co_data(&s->res)); |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7732 | |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 7733 | /* Now we can realign the response buffer */ |
Willy Tarreau | d5b343b | 2018-06-06 06:42:46 +0200 | [diff] [blame] | 7734 | c_realign_if_empty(&s->res); |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 7735 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7736 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7737 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7738 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7739 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7740 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7741 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7742 | s->req.rex = TICK_ETERNITY; |
| 7743 | s->req.wex = TICK_ETERNITY; |
| 7744 | s->req.analyse_exp = TICK_ETERNITY; |
| 7745 | s->res.rex = TICK_ETERNITY; |
| 7746 | s->res.wex = TICK_ETERNITY; |
| 7747 | s->res.analyse_exp = TICK_ETERNITY; |
Hongbo Long | e39683c | 2017-03-10 18:41:51 +0100 | [diff] [blame] | 7748 | s->si[1].hcto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7749 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7750 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7751 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 7752 | * takes the string from the variable 'replace' with length 'len', then modifies |
| 7753 | * the relevant part of the request line accordingly. Then it updates various |
| 7754 | * pointers to the next elements which were moved, and the total buffer length. |
| 7755 | * It finds the action to be performed in p[2], previously filled by function |
| 7756 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 7757 | * error, though this can be revisited when this code is finally exploited. |
| 7758 | * |
| 7759 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 7760 | * query string and 3 to replace uri. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7761 | * |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7762 | * In query string case, the mark question '?' must be set at the start of the |
| 7763 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7764 | */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7765 | int http_replace_req_line(int action, const char *replace, int len, |
| 7766 | struct proxy *px, struct stream *s) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7767 | { |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7768 | struct http_txn *txn = s->txn; |
| 7769 | char *cur_ptr, *cur_end; |
| 7770 | int offset = 0; |
| 7771 | int delta; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7772 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7773 | switch (action) { |
| 7774 | case 0: // method |
| 7775 | cur_ptr = ci_head(&s->req); |
| 7776 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7777 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7778 | /* adjust req line offsets and lengths */ |
| 7779 | delta = len - offset - (cur_end - cur_ptr); |
| 7780 | txn->req.sl.rq.m_l += delta; |
| 7781 | txn->req.sl.rq.u += delta; |
| 7782 | txn->req.sl.rq.v += delta; |
| 7783 | break; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 7784 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7785 | case 1: // path |
| 7786 | cur_ptr = http_txn_get_path(txn); |
| 7787 | if (!cur_ptr) |
| 7788 | cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7789 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7790 | cur_end = cur_ptr; |
| 7791 | while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?') |
| 7792 | cur_end++; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7793 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7794 | /* adjust req line offsets and lengths */ |
| 7795 | delta = len - offset - (cur_end - cur_ptr); |
| 7796 | txn->req.sl.rq.u_l += delta; |
| 7797 | txn->req.sl.rq.v += delta; |
| 7798 | break; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7799 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7800 | case 2: // query |
| 7801 | offset = 1; |
| 7802 | cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u; |
| 7803 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 7804 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 7805 | cur_ptr++; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 7806 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7807 | /* skip the question mark or indicate that we must insert it |
| 7808 | * (but only if the format string is not empty then). |
| 7809 | */ |
| 7810 | if (cur_ptr < cur_end) |
| 7811 | cur_ptr++; |
| 7812 | else if (len > 1) |
| 7813 | offset = 0; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 7814 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7815 | /* adjust req line offsets and lengths */ |
| 7816 | delta = len - offset - (cur_end - cur_ptr); |
| 7817 | txn->req.sl.rq.u_l += delta; |
| 7818 | txn->req.sl.rq.v += delta; |
| 7819 | break; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7820 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7821 | case 3: // uri |
| 7822 | cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u; |
| 7823 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7824 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7825 | /* adjust req line offsets and lengths */ |
| 7826 | delta = len - offset - (cur_end - cur_ptr); |
| 7827 | txn->req.sl.rq.u_l += delta; |
| 7828 | txn->req.sl.rq.v += delta; |
| 7829 | break; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7830 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7831 | default: |
| 7832 | return -1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7833 | } |
| 7834 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7835 | /* commit changes and adjust end of message */ |
| 7836 | delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset); |
| 7837 | txn->req.sl.rq.l += delta; |
| 7838 | txn->hdr_idx.v[0].len += delta; |
| 7839 | http_msg_move_end(&txn->req, delta); |
| 7840 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7841 | } |
| 7842 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7843 | /* This function replace the HTTP status code and the associated message. The |
| 7844 | * variable <status> contains the new status code. This function never fails. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7845 | */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7846 | void http_set_status(unsigned int status, const char *reason, struct stream *s) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7847 | { |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7848 | struct http_txn *txn = s->txn; |
| 7849 | char *cur_ptr, *cur_end; |
| 7850 | int delta; |
| 7851 | char *res; |
| 7852 | int c_l; |
| 7853 | const char *msg = reason; |
| 7854 | int msg_len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7855 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7856 | chunk_reset(&trash); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7857 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7858 | res = ultoa_o(status, trash.area, trash.size); |
| 7859 | c_l = res - trash.area; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7860 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7861 | trash.area[c_l] = ' '; |
| 7862 | trash.data = c_l + 1; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7863 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7864 | /* Do we have a custom reason format string? */ |
| 7865 | if (msg == NULL) |
| 7866 | msg = http_get_reason(status); |
| 7867 | msg_len = strlen(msg); |
| 7868 | strncpy(&trash.area[trash.data], msg, trash.size - trash.data); |
| 7869 | trash.data += msg_len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7870 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7871 | cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c; |
| 7872 | cur_end = ci_head(&s->res) + txn->rsp.sl.st.r + txn->rsp.sl.st.r_l; |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7873 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7874 | /* commit changes and adjust message */ |
| 7875 | delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area, |
| 7876 | trash.data); |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 7877 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7878 | /* adjust res line offsets and lengths */ |
| 7879 | txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l; |
| 7880 | txn->rsp.sl.st.c_l = c_l; |
| 7881 | txn->rsp.sl.st.r_l = msg_len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7882 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7883 | delta = trash.data - (cur_end - cur_ptr); |
| 7884 | txn->rsp.sl.st.l += delta; |
| 7885 | txn->hdr_idx.v[0].len += delta; |
| 7886 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7887 | } |
| 7888 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7889 | __attribute__((constructor)) |
| 7890 | static void __http_protocol_init(void) |
| 7891 | { |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7892 | } |
| 7893 | |
| 7894 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7895 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7896 | * Local variables: |
| 7897 | * c-indent-level: 8 |
| 7898 | * c-basic-offset: 8 |
| 7899 | * End: |
| 7900 | */ |