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 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 26 | #include <common/base64.h> |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 27 | #include <common/cfgparse.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 28 | #include <common/chunk.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 29 | #include <common/compat.h> |
| 30 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 31 | #include <common/debug.h> |
Willy Tarreau | afba57a | 2018-12-11 13:44:24 +0100 | [diff] [blame] | 32 | #include <common/h1.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 33 | #include <common/memory.h> |
| 34 | #include <common/mini-clist.h> |
| 35 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 36 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 37 | #include <common/time.h> |
| 38 | #include <common/uri_auth.h> |
| 39 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | |
| 41 | #include <types/capture.h> |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 42 | #include <types/cli.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 43 | #include <types/filters.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | #include <types/global.h> |
William Lallemand | 71bd11a | 2017-11-20 19:13:14 +0100 | [diff] [blame] | 45 | #include <types/cache.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 46 | #include <types/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 48 | #include <proto/acl.h> |
Thierry FOURNIER | 322a124 | 2015-08-19 09:07:47 +0200 | [diff] [blame] | 49 | #include <proto/action.h> |
Willy Tarreau | 61612d4 | 2012-04-19 18:42:05 +0200 | [diff] [blame] | 50 | #include <proto/arg.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 51 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | #include <proto/backend.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 53 | #include <proto/channel.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 54 | #include <proto/checks.h> |
Willy Tarreau | 12207b3 | 2016-11-22 19:48:51 +0100 | [diff] [blame] | 55 | #include <proto/cli.h> |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 56 | #include <proto/compression.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 57 | #include <proto/stats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/fd.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 59 | #include <proto/filters.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 60 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 62 | #include <proto/hdr_idx.h> |
Patrick Hemmer | e3faf02 | 2018-08-22 10:02:00 -0400 | [diff] [blame] | 63 | #include <proto/hlua.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 64 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 65 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 67 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | #include <proto/queue.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 69 | #include <proto/sample.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 70 | #include <proto/server.h> |
Olivier Houchard | 985f139 | 2018-11-30 17:31:52 +0100 | [diff] [blame] | 71 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 72 | #include <proto/stream.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 73 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 74 | #include <proto/task.h> |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 75 | #include <proto/pattern.h> |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 76 | #include <proto/vars.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 77 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 78 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 79 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 80 | [STAT_STATUS_DENY] = "DENY", |
| 81 | [STAT_STATUS_DONE] = "DONE", |
| 82 | [STAT_STATUS_ERRP] = "ERRP", |
| 83 | [STAT_STATUS_EXCD] = "EXCD", |
| 84 | [STAT_STATUS_NONE] = "NONE", |
| 85 | [STAT_STATUS_PART] = "PART", |
| 86 | [STAT_STATUS_UNKN] = "UNKN", |
| 87 | }; |
| 88 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 89 | /* This function handles a server error at the stream interface level. The |
| 90 | * 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] | 91 | * message is copied into the input buffer. |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 92 | * 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] | 93 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 94 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 95 | static void http_server_error(struct stream *s, struct stream_interface *si, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 96 | int err, int finst, const struct buffer *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 97 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 98 | if (IS_HTX_STRM(s)) |
| 99 | return htx_server_error(s, si, err, finst, msg); |
| 100 | |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 101 | FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg)); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 102 | channel_auto_read(si_oc(si)); |
| 103 | channel_abort(si_oc(si)); |
| 104 | channel_auto_close(si_oc(si)); |
| 105 | channel_erase(si_oc(si)); |
| 106 | channel_auto_close(si_ic(si)); |
| 107 | channel_auto_read(si_ic(si)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 108 | if (msg) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 109 | co_inject(si_ic(si), msg->area, msg->data); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 110 | if (!(s->flags & SF_ERR_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 111 | s->flags |= err; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 112 | if (!(s->flags & SF_FINST_MASK)) |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 113 | s->flags |= finst; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 116 | /* This function returns the appropriate error location for the given stream |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 117 | * and message. |
| 118 | */ |
| 119 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 120 | struct buffer *http_error_message(struct stream *s) |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 121 | { |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 122 | const int msgnum = http_get_status_idx(s->txn->status); |
| 123 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 124 | if (IS_HTX_STRM(s)) |
| 125 | return htx_error_message(s); |
| 126 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 127 | if (s->be->errmsg[msgnum].area) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 128 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 129 | else if (strm_fe(s)->errmsg[msgnum].area) |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 130 | return &strm_fe(s)->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 131 | else |
| 132 | return &http_err_chunks[msgnum]; |
| 133 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 134 | |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 135 | void |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 136 | http_reply_and_close(struct stream *s, short status, struct buffer *msg) |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 137 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 138 | if (IS_HTX_STRM(s)) |
| 139 | return htx_reply_and_close(s, status, msg); |
| 140 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 141 | s->txn->flags &= ~TX_WAIT_NEXT_RQ; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 142 | FLT_STRM_CB(s, flt_http_reply(s, status, msg)); |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 143 | si_retnclose(&s->si[0], msg); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 146 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 147 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 148 | * It is returned otherwise. |
| 149 | */ |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 150 | char *http_txn_get_path(const struct http_txn *txn) |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 151 | { |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 152 | struct ist ret; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 153 | |
Willy Tarreau | 9d9ccdb | 2018-10-28 20:13:12 +0100 | [diff] [blame] | 154 | if (!txn->req.chn->buf.size) |
| 155 | return NULL; |
| 156 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 157 | 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] | 158 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 159 | return ret.ptr; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 162 | /* Returns a 302 for a redirectable request that reaches a server working in |
| 163 | * in redirect mode. This may only be called just after the stream interface |
| 164 | * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will |
| 165 | * follow normal proxy processing. NOTE: this function is designed to support |
| 166 | * being called once data are scheduled for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 167 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 168 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 169 | { |
| 170 | struct http_txn *txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 171 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 172 | char *path; |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 173 | int len, rewind; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 174 | |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 175 | if (IS_HTX_STRM(s)) |
| 176 | return htx_perform_server_redirect(s, si); |
| 177 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 178 | /* 1: create the response header */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 179 | trash.data = strlen(HTTP_302); |
| 180 | memcpy(trash.area, HTTP_302, trash.data); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 181 | |
Willy Tarreau | b05e48a | 2018-09-20 11:12:58 +0200 | [diff] [blame] | 182 | srv = __objt_server(s->target); |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 183 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 184 | /* 2: add the server's prefix */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 185 | if (trash.data + srv->rdr_len > trash.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 186 | return; |
| 187 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 188 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 189 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 190 | memcpy(trash.area + trash.data, srv->rdr_pfx, srv->rdr_len); |
| 191 | trash.data += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 192 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 193 | |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 194 | /* 3: add the request URI. Since it was already forwarded, we need |
| 195 | * to temporarily rewind the buffer. |
| 196 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 197 | txn = s->txn; |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 198 | c_rew(&s->req, rewind = http_hdr_rewind(&txn->req)); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 199 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 200 | path = http_txn_get_path(txn); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 201 | 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] | 202 | |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 203 | c_adv(&s->req, rewind); |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 204 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 205 | if (!path) |
| 206 | return; |
| 207 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 208 | if (trash.data + len > trash.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 209 | return; |
| 210 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 211 | memcpy(trash.area + trash.data, path, len); |
| 212 | trash.data += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 213 | |
| 214 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 215 | memcpy(trash.area + trash.data, |
| 216 | "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 217 | trash.data += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 218 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 219 | memcpy(trash.area + trash.data, |
| 220 | "\r\nConnection: close\r\n\r\n", 23); |
| 221 | trash.data += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 222 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 223 | |
| 224 | /* prepare to return without error. */ |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 225 | si_shutr(si); |
| 226 | si_shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 227 | si->err_type = SI_ET_NONE; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 228 | si->state = SI_ST_CLO; |
| 229 | |
| 230 | /* send the message */ |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 231 | txn->status = 302; |
| 232 | http_server_error(s, si, SF_ERR_LOCAL, SF_FINST_C, &trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 233 | |
| 234 | /* 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] | 235 | srv_inc_sess_ctr(srv); |
Bhaskar Maddala | a20cb85 | 2014-02-03 16:26:46 -0500 | [diff] [blame] | 236 | srv_set_sess_last(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 239 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 240 | * that the server side is closed. Note that err_type is actually a |
| 241 | * bitmask, where almost only aborts may be cumulated with other |
| 242 | * values. We consider that aborted operations are more important |
| 243 | * than timeouts or errors due to the fact that nobody else in the |
| 244 | * logs might explain incomplete retries. All others should avoid |
| 245 | * being cumulated. It should normally not be possible to have multiple |
| 246 | * 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] | 247 | * Note that connection errors appearing on the second request of a keep-alive |
| 248 | * connection are not reported since this allows the client to retry. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 249 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 250 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 251 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 252 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 253 | |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 254 | /* set s->txn->status for http_error_message(s) */ |
| 255 | s->txn->status = 503; |
| 256 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 257 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 258 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 259 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 260 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 261 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 262 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 263 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 264 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 265 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 266 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 267 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 268 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 269 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 270 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 271 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 272 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 273 | http_error_message(s)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 274 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 275 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 276 | (s->flags & SF_SRV_REUSED) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 277 | http_error_message(s)); |
Willy Tarreau | 2d400bb | 2012-05-14 12:11:47 +0200 | [diff] [blame] | 278 | else if (err_type & SI_ET_CONN_RES) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 279 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 280 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 281 | http_error_message(s)); |
| 282 | else { /* SI_ET_CONN_OTHER and others */ |
| 283 | s->txn->status = 500; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 284 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
Willy Tarreau | 2019f95 | 2017-03-14 11:07:31 +0100 | [diff] [blame] | 285 | http_error_message(s)); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 286 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 289 | extern const char sess_term_cond[8]; |
| 290 | extern const char sess_fin_state[8]; |
| 291 | extern const char *monthname[12]; |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 292 | |
| 293 | DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn)); |
| 294 | DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN); |
| 295 | |
| 296 | struct pool_head *pool_head_requri = NULL; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 297 | struct pool_head *pool_head_capture = NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 298 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 299 | /* |
| 300 | * Capture headers from message starting at <som> according to header list |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 301 | * <cap_hdr>, and fill the <cap> pointers appropriately. |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 302 | */ |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 303 | void http_capture_headers(char *som, struct hdr_idx *idx, |
| 304 | char **cap, struct cap_hdr *cap_hdr) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 305 | { |
| 306 | char *eol, *sol, *col, *sov; |
| 307 | int cur_idx; |
| 308 | struct cap_hdr *h; |
| 309 | int len; |
| 310 | |
| 311 | sol = som + hdr_idx_first_pos(idx); |
| 312 | cur_idx = hdr_idx_first_idx(idx); |
| 313 | |
| 314 | while (cur_idx) { |
| 315 | eol = sol + idx->v[cur_idx].len; |
| 316 | |
| 317 | col = sol; |
| 318 | while (col < eol && *col != ':') |
| 319 | col++; |
| 320 | |
| 321 | sov = col + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 322 | while (sov < eol && HTTP_IS_LWS(*sov)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 323 | sov++; |
| 324 | |
| 325 | for (h = cap_hdr; h; h = h->next) { |
Willy Tarreau | 54da8db | 2014-06-13 16:11:48 +0200 | [diff] [blame] | 326 | if (h->namelen && (h->namelen == col - sol) && |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 327 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 328 | if (cap[h->index] == NULL) |
| 329 | cap[h->index] = |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 330 | pool_alloc(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 331 | |
| 332 | if (cap[h->index] == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 333 | ha_alert("HTTP capture : out of memory.\n"); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 334 | continue; |
| 335 | } |
| 336 | |
| 337 | len = eol - sov; |
| 338 | if (len > h->len) |
| 339 | len = h->len; |
| 340 | |
| 341 | memcpy(cap[h->index], sov, len); |
| 342 | cap[h->index][len]=0; |
| 343 | } |
| 344 | } |
| 345 | sol = eol + idx->v[cur_idx].cr + 1; |
| 346 | cur_idx = idx->v[cur_idx].next; |
| 347 | } |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 348 | } |
| 349 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 350 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 351 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 352 | * nothing is done and 1 is returned. |
| 353 | */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 354 | int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 355 | { |
| 356 | int delta; |
| 357 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 358 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 359 | |
| 360 | if (msg->sl.rq.v_l != 0) |
| 361 | return 1; |
| 362 | |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 363 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 364 | if (txn->meth != HTTP_METH_GET) |
| 365 | return 0; |
| 366 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 367 | cur_end = ci_head(msg->chn) + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 368 | |
| 369 | if (msg->sl.rq.u_l == 0) { |
Apollon Oikonomopoulos | 25a1522 | 2014-04-06 02:46:00 +0300 | [diff] [blame] | 370 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 371 | return 0; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 372 | } |
| 373 | /* add HTTP version */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 374 | 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] | 375 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 376 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 377 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 378 | HTTP_MSG_RQMETH, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 379 | ci_head(msg->chn), cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 380 | NULL, NULL); |
| 381 | if (unlikely(!cur_end)) |
| 382 | return 0; |
| 383 | |
| 384 | /* we have a full HTTP/1.0 request now and we know that |
| 385 | * we have either a CR or an LF at <ptr>. |
| 386 | */ |
| 387 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 388 | return 1; |
| 389 | } |
| 390 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 391 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 392 | * and "keep-alive" values. If we already know that some headers may safely |
| 393 | * 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] | 394 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 395 | * - 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] | 396 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 397 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 398 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 399 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 400 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 401 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 402 | 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] | 403 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 404 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 405 | const char *hdr_val = "Connection"; |
| 406 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 407 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 408 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 409 | return; |
| 410 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 411 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 412 | hdr_val = "Proxy-Connection"; |
| 413 | hdr_len = 16; |
| 414 | } |
| 415 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 416 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 417 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 418 | 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] | 419 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 420 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 421 | if (to_del & 2) |
| 422 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 423 | else |
| 424 | txn->flags |= TX_CON_KAL_SET; |
| 425 | } |
| 426 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 427 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 428 | if (to_del & 1) |
| 429 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 430 | else |
| 431 | txn->flags |= TX_CON_CLO_SET; |
| 432 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 433 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 434 | txn->flags |= TX_HDR_CONN_UPG; |
| 435 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 436 | } |
| 437 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 438 | txn->flags |= TX_HDR_CONN_PRS; |
| 439 | return; |
| 440 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 441 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 442 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 443 | * added depending on the <wanted> flags, which are exclusively composed of |
| 444 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 445 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 446 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 447 | 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] | 448 | { |
| 449 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 450 | const char *hdr_val = "Connection"; |
| 451 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 452 | |
| 453 | ctx.idx = 0; |
| 454 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 455 | |
| 456 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 457 | hdr_val = "Proxy-Connection"; |
| 458 | hdr_len = 16; |
| 459 | } |
| 460 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 461 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 462 | 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] | 463 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 464 | if (wanted & TX_CON_KAL_SET) |
| 465 | txn->flags |= TX_CON_KAL_SET; |
| 466 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 467 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 468 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 469 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 470 | if (wanted & TX_CON_CLO_SET) |
| 471 | txn->flags |= TX_CON_CLO_SET; |
| 472 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 473 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 474 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 475 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 476 | |
| 477 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 478 | return; |
| 479 | |
| 480 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 481 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 482 | hdr_val = "Connection: close"; |
| 483 | hdr_len = 17; |
| 484 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 485 | hdr_val = "Proxy-Connection: close"; |
| 486 | hdr_len = 23; |
| 487 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 488 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 492 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 493 | hdr_val = "Connection: keep-alive"; |
| 494 | hdr_len = 22; |
| 495 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 496 | hdr_val = "Proxy-Connection: keep-alive"; |
| 497 | hdr_len = 28; |
| 498 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 499 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 500 | } |
| 501 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 502 | } |
| 503 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 504 | 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] | 505 | { |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 506 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 507 | int tmp = TX_CON_WANT_KAL; |
| 508 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 509 | if (IS_HTX_STRM(s)) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 510 | return htx_adjust_conn_mode(s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 511 | |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 512 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN || |
| 513 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 514 | tmp = TX_CON_WANT_TUN; |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 515 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 516 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 517 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 518 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 519 | |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 520 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO || |
| 521 | (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 522 | tmp = TX_CON_WANT_CLO; |
| 523 | |
| 524 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 525 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
| 526 | |
| 527 | if (!(txn->flags & TX_HDR_CONN_PRS) && |
| 528 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
| 529 | /* parse the Connection header and possibly clean it */ |
| 530 | int to_del = 0; |
| 531 | if ((msg->flags & HTTP_MSGF_VER_11) || |
| 532 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 533 | !((fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 534 | to_del |= 2; /* remove "keep-alive" */ |
| 535 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 536 | to_del |= 1; /* remove "close" */ |
| 537 | http_parse_connection_header(txn, msg, to_del); |
| 538 | } |
| 539 | |
| 540 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 541 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 542 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 543 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 544 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
| 545 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 546 | fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | 4e21ff9 | 2014-09-30 18:44:22 +0200 | [diff] [blame] | 547 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 548 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 549 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 550 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 551 | * processing can continue on next analysers, or zero if it either needs more |
| 552 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 553 | * 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] | 554 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 555 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 556 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 557 | 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] | 558 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 559 | /* |
| 560 | * We will parse the partial (or complete) lines. |
| 561 | * We will check the request syntax, and also join multi-line |
| 562 | * headers. An index of all the lines will be elaborated while |
| 563 | * parsing. |
| 564 | * |
| 565 | * For the parsing, we use a 28 states FSM. |
| 566 | * |
| 567 | * Here is the information we currently have : |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 568 | * ci_head(req) = beginning of request |
| 569 | * ci_head(req) + msg->eoh = end of processed headers / start of current one |
| 570 | * ci_tail(req) = end of input data |
| 571 | * msg->eol = end of current header or line (LF or CRLF) |
| 572 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 573 | * |
| 574 | * 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] | 575 | * we will set a few fields (txn->meth, sn->flags/SF_REDIRECTABLE). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 576 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 577 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 578 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 579 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 580 | int cur_idx; |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 581 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 582 | struct http_txn *txn = s->txn; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 583 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 584 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 585 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 586 | if (IS_HTX_STRM(s)) |
| 587 | return htx_wait_for_request(s, req, an_bit); |
| 588 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 589 | 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] | 590 | now_ms, __FUNCTION__, |
| 591 | s, |
| 592 | req, |
| 593 | req->rex, req->wex, |
| 594 | req->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 595 | ci_data(req), |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 596 | req->analysers); |
| 597 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 598 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 599 | s->srv_error = http_return_srv_error; |
| 600 | |
Rian McGuire | 89fcb7d | 2018-04-24 11:19:21 -0300 | [diff] [blame] | 601 | /* 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] | 602 | if (c_data(req) && s->logs.t_idle == -1) |
Rian McGuire | 89fcb7d | 2018-04-24 11:19:21 -0300 | [diff] [blame] | 603 | s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake; |
| 604 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 605 | /* There's a protected area at the end of the buffer for rewriting |
| 606 | * purposes. We don't want to start to parse the request if the |
| 607 | * protected area is affected, because we may have to move processed |
| 608 | * data later, which is much more complicated. |
| 609 | */ |
Willy Tarreau | d760eec | 2018-07-10 09:50:25 +0200 | [diff] [blame] | 610 | if (c_data(req) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 611 | if (txn->flags & TX_NOT_FIRST) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 612 | if (unlikely(!channel_is_rewritable(req))) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 613 | 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] | 614 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 615 | /* 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] | 616 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 617 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 618 | req->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 619 | return 0; |
| 620 | } |
Willy Tarreau | 188e230 | 2018-06-15 11:11:53 +0200 | [diff] [blame] | 621 | if (unlikely(ci_tail(req) < c_ptr(req, msg->next) || |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 622 | ci_tail(req) > b_wrap(&req->buf) - global.tune.maxrewrite)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 623 | channel_slow_realign(req, trash.area); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 624 | } |
| 625 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 626 | if (likely(msg->next < ci_data(req))) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 627 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 628 | } |
| 629 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 630 | /* 1: we might have to print this header in debug mode */ |
| 631 | if (unlikely((global.mode & MODE_DEBUG) && |
| 632 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 633 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 634 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 635 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 636 | sol = ci_head(req); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 637 | /* this is a bit complex : in case of error on the request line, |
| 638 | * we know that rq.l is still zero, so we display only the part |
| 639 | * up to the end of the line (truncated by debug_hdr). |
| 640 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 641 | 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] | 642 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 643 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 644 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 645 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 646 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 647 | while (cur_idx) { |
| 648 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 649 | debug_hdr("clihdr", s, sol, eol); |
| 650 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 651 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 652 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 653 | } |
| 654 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 655 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 656 | /* |
| 657 | * Now we quickly check if we have found a full valid request. |
| 658 | * If not so, we check the FD and buffer states before leaving. |
| 659 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 660 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 661 | * requests are checked first. When waiting for a second request |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 662 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 663 | * 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] | 664 | * 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] | 665 | * process_stream() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 666 | * Last, we may increase some tracked counters' http request errors on |
| 667 | * the cases that are deliberately the client's fault. For instance, |
| 668 | * a timeout or connection reset is not counted as an error. However |
| 669 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 670 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 671 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 672 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 673 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 674 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 675 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 676 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 677 | stream_inc_http_req_ctr(s); |
| 678 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 679 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 680 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 681 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 682 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 683 | /* 1: Since we are in header mode, if there's no space |
| 684 | * left for headers, we won't be able to free more |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 685 | * later, so the stream will never terminate. We |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 686 | * must terminate it now. |
| 687 | */ |
Willy Tarreau | 2375233 | 2018-06-15 14:54:53 +0200 | [diff] [blame] | 688 | if (unlikely(channel_full(req, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 689 | /* FIXME: check if URI is set and return Status |
| 690 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 691 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 692 | stream_inc_http_req_ctr(s); |
| 693 | stream_inc_http_err_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 694 | proxy_inc_fe_req_ctr(sess->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 695 | if (msg->err_pos < 0) |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 696 | msg->err_pos = ci_data(req); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 697 | goto return_bad_req; |
| 698 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 699 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 700 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 701 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 702 | if (!(s->flags & SF_ERR_MASK)) |
| 703 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 704 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 705 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 706 | goto failed_keep_alive; |
| 707 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 708 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 709 | goto failed_keep_alive; |
| 710 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 711 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 712 | if (msg->err_pos >= 0) { |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 713 | 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] | 714 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 715 | } |
| 716 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 717 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 718 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 719 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 720 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 721 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 722 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 723 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 724 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 725 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 726 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 727 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 728 | if (!(s->flags & SF_FINST_MASK)) |
| 729 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 730 | return 0; |
| 731 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 732 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 733 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 734 | 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] | 735 | if (!(s->flags & SF_ERR_MASK)) |
| 736 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 737 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 738 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 739 | goto failed_keep_alive; |
| 740 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 741 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 742 | goto failed_keep_alive; |
| 743 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 744 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 745 | if (msg->err_pos >= 0) { |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 746 | 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] | 747 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 748 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 749 | txn->status = 408; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 750 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 751 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 752 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 753 | req->analysers &= AN_REQ_FLT_END; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 754 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 755 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 756 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 757 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 758 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 759 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 760 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 761 | if (!(s->flags & SF_FINST_MASK)) |
| 762 | s->flags |= SF_FINST_R; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 763 | return 0; |
| 764 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 765 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 766 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 767 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 768 | if (!(s->flags & SF_ERR_MASK)) |
| 769 | s->flags |= SF_ERR_CLICL; |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 770 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 771 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 772 | goto failed_keep_alive; |
| 773 | |
Willy Tarreau | 0f228a0 | 2015-05-01 15:37:53 +0200 | [diff] [blame] | 774 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 775 | goto failed_keep_alive; |
| 776 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 777 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 778 | 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] | 779 | txn->status = 400; |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 780 | msg->err_state = msg->msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 781 | msg->msg_state = HTTP_MSG_ERROR; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 782 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 783 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 784 | stream_inc_http_err_ctr(s); |
| 785 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 786 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 787 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 788 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 789 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 790 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 791 | if (!(s->flags & SF_FINST_MASK)) |
| 792 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 793 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 794 | } |
| 795 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 796 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 797 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 798 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 799 | |
| 800 | if (sess->listener->options & LI_O_NOQUICKACK && ci_data(req)) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 801 | /* We need more data, we have to re-enable quick-ack in case we |
| 802 | * previously disabled it, otherwise we might cause the client |
| 803 | * to delay next data. |
| 804 | */ |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 805 | conn_set_quickack(objt_conn(sess->origin), 1); |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 806 | } |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 807 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 808 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 809 | /* If the client starts to talk, let's fall back to |
| 810 | * request timeout processing. |
| 811 | */ |
| 812 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 813 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 814 | } |
| 815 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 816 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 817 | if (!tick_isset(req->analyse_exp)) { |
| 818 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 819 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 820 | tick_isset(s->be->timeout.httpka)) |
| 821 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 822 | else |
| 823 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 824 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 825 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 826 | /* we're not ready yet */ |
| 827 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 828 | |
| 829 | failed_keep_alive: |
| 830 | /* Here we process low-level errors for keep-alive requests. In |
| 831 | * short, if the request is not the first one and it experiences |
| 832 | * a timeout, read error or shutdown, we just silently close so |
| 833 | * that the client can try again. |
| 834 | */ |
| 835 | txn->status = 0; |
| 836 | msg->msg_state = HTTP_MSG_RQBEFORE; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 837 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 838 | s->logs.logwait = 0; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 839 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 840 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 841 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 842 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 843 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 844 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 845 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 846 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 847 | * 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] | 848 | * 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] | 849 | * 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] | 850 | * byte after the last LF. msg->sov points to the first byte of data. |
| 851 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 852 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 853 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 854 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 855 | stream_inc_http_req_ctr(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 856 | 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] | 857 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 858 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 859 | /* kill the pending keep-alive timeout */ |
| 860 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 861 | req->analyse_exp = TICK_ETERNITY; |
| 862 | } |
| 863 | |
| 864 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 865 | /* Maybe we found in invalid header name while we were configured not |
| 866 | * to block on that, so we have to capture it now. |
| 867 | */ |
| 868 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 869 | 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] | 870 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 871 | /* |
| 872 | * 1: identify the method |
| 873 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 874 | 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] | 875 | |
| 876 | /* we can make use of server redirect on GET and HEAD */ |
| 877 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 878 | s->flags |= SF_REDIRECTABLE; |
Willy Tarreau | 9165979 | 2017-11-10 19:38:10 +0100 | [diff] [blame] | 879 | else if (txn->meth == HTTP_METH_OTHER && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 880 | 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] | 881 | /* PRI is reserved for the HTTP/2 preface */ |
| 882 | msg->err_pos = 0; |
| 883 | goto return_bad_req; |
| 884 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 885 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 886 | /* |
| 887 | * 2: check if the URI matches the monitor_uri. |
| 888 | * We have to do this for every request which gets in, because |
| 889 | * the monitor-uri is defined by the frontend. |
| 890 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 891 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
| 892 | (sess->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 893 | !memcmp(ci_head(req) + msg->sl.rq.u, |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 894 | sess->fe->monitor_uri, |
| 895 | sess->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 896 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 897 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 898 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 899 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 900 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 901 | s->flags |= SF_MONITOR; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 902 | HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 903 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 904 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 905 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 906 | 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] | 907 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 908 | ret = acl_pass(ret); |
| 909 | if (cond->pol == ACL_COND_UNLESS) |
| 910 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 911 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 912 | if (ret) { |
| 913 | /* we fail this request, let's return 503 service unavail */ |
| 914 | txn->status = 503; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 915 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 916 | if (!(s->flags & SF_ERR_MASK)) |
| 917 | 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] | 918 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 919 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 920 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 921 | |
Joseph Herlant | 5ba8025 | 2018-11-15 09:25:36 -0800 | [diff] [blame] | 922 | /* nothing to fail, let's reply normally */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 923 | txn->status = 200; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 924 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 925 | if (!(s->flags & SF_ERR_MASK)) |
| 926 | 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] | 927 | goto return_prx_cond; |
| 928 | } |
| 929 | |
| 930 | /* |
| 931 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 932 | * Note: we cannot log anymore if the request has been |
| 933 | * classified as invalid. |
| 934 | */ |
| 935 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 936 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 937 | if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 938 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 939 | |
Stéphane Cottin | 23e9e93 | 2017-05-18 08:58:41 +0200 | [diff] [blame] | 940 | if (urilen >= global.tune.requri_len ) |
| 941 | urilen = global.tune.requri_len - 1; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 942 | memcpy(txn->uri, ci_head(req), urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 943 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 944 | |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 945 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 946 | s->do_log(s); |
| 947 | } else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 948 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 949 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 950 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 951 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 952 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 953 | * exactly one digit "." one digit. This check may be disabled using |
| 954 | * option accept-invalid-http-request. |
| 955 | */ |
| 956 | if (!(sess->fe->options2 & PR_O2_REQBUG_OK)) { |
| 957 | if (msg->sl.rq.v_l != 8) { |
| 958 | msg->err_pos = msg->sl.rq.v; |
| 959 | goto return_bad_req; |
| 960 | } |
| 961 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 962 | if (ci_head(req)[msg->sl.rq.v + 4] != '/' || |
| 963 | !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 5]) || |
| 964 | ci_head(req)[msg->sl.rq.v + 6] != '.' || |
| 965 | !isdigit((unsigned char)ci_head(req)[msg->sl.rq.v + 7])) { |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 966 | msg->err_pos = msg->sl.rq.v + 4; |
| 967 | goto return_bad_req; |
| 968 | } |
| 969 | } |
Willy Tarreau | 1331766 | 2015-05-01 13:47:08 +0200 | [diff] [blame] | 970 | else { |
| 971 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 972 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
| 973 | goto return_bad_req; |
| 974 | } |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 975 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 976 | /* ... and check if the request is HTTP/1.1 or above */ |
| 977 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 978 | ((ci_head(req)[msg->sl.rq.v + 5] > '1') || |
| 979 | ((ci_head(req)[msg->sl.rq.v + 5] == '1') && |
| 980 | (ci_head(req)[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 981 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 982 | |
| 983 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 984 | 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] | 985 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 986 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 987 | * we have what looks like a proxied connection instead of a connection, |
| 988 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 989 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 990 | * happen to do that despite being non-standard :-( |
| 991 | * We consider that a request not beginning with either '/' or '*' is |
| 992 | * a proxied connection, which covers both "scheme://location" and |
| 993 | * CONNECT ip:port. |
| 994 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 995 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 996 | 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] | 997 | txn->flags |= TX_USE_PX_CONN; |
| 998 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 999 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 1000 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1001 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1002 | /* 5: we may need to capture headers */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1003 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 1004 | http_capture_headers(ci_head(req), &txn->hdr_idx, |
| 1005 | s->req_cap, sess->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1006 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1007 | /* 6: determine the transfer-length according to RFC2616 #4.4, updated |
| 1008 | * by RFC7230#3.3.3 : |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1009 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1010 | * The length of a message body is determined by one of the following |
| 1011 | * (in order of precedence): |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1012 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1013 | * 1. Any response to a HEAD request and any response with a 1xx |
| 1014 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 1015 | * code is always terminated by the first empty line after the |
| 1016 | * header fields, regardless of the header fields present in the |
| 1017 | * message, and thus cannot contain a message body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1018 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1019 | * 2. Any 2xx (Successful) response to a CONNECT request implies that |
| 1020 | * the connection will become a tunnel immediately after the empty |
| 1021 | * line that concludes the header fields. A client MUST ignore any |
| 1022 | * Content-Length or Transfer-Encoding header fields received in |
| 1023 | * such a message. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1024 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 1025 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 1026 | * transfer coding (Section 4.1) is the final encoding, the message |
| 1027 | * body length is determined by reading and decoding the chunked |
| 1028 | * data until the transfer coding indicates the data is complete. |
| 1029 | * |
| 1030 | * If a Transfer-Encoding header field is present in a response and |
| 1031 | * the chunked transfer coding is not the final encoding, the |
| 1032 | * message body length is determined by reading the connection until |
| 1033 | * it is closed by the server. If a Transfer-Encoding header field |
| 1034 | * is present in a request and the chunked transfer coding is not |
| 1035 | * the final encoding, the message body length cannot be determined |
| 1036 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 1037 | * status code and then close the connection. |
| 1038 | * |
| 1039 | * If a message is received with both a Transfer-Encoding and a |
| 1040 | * Content-Length header field, the Transfer-Encoding overrides the |
| 1041 | * Content-Length. Such a message might indicate an attempt to |
| 1042 | * perform request smuggling (Section 9.5) or response splitting |
| 1043 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 1044 | * remove the received Content-Length field prior to forwarding such |
| 1045 | * a message downstream. |
| 1046 | * |
| 1047 | * 4. If a message is received without Transfer-Encoding and with |
| 1048 | * either multiple Content-Length header fields having differing |
| 1049 | * field-values or a single Content-Length header field having an |
| 1050 | * invalid value, then the message framing is invalid and the |
| 1051 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 1052 | * request message, the server MUST respond with a 400 (Bad Request) |
| 1053 | * status code and then close the connection. If this is a response |
| 1054 | * message received by a proxy, the proxy MUST close the connection |
| 1055 | * to the server, discard the received response, and send a 502 (Bad |
| 1056 | * Gateway) response to the client. If this is a response message |
| 1057 | * received by a user agent, the user agent MUST close the |
| 1058 | * connection to the server and discard the received response. |
| 1059 | * |
| 1060 | * 5. If a valid Content-Length header field is present without |
| 1061 | * Transfer-Encoding, its decimal value defines the expected message |
| 1062 | * body length in octets. If the sender closes the connection or |
| 1063 | * the recipient times out before the indicated number of octets are |
| 1064 | * received, the recipient MUST consider the message to be |
| 1065 | * incomplete and close the connection. |
| 1066 | * |
| 1067 | * 6. If this is a request message and none of the above are true, then |
| 1068 | * the message body length is zero (no message body is present). |
| 1069 | * |
| 1070 | * 7. Otherwise, this is a response message without a declared message |
| 1071 | * body length, so the message body length is determined by the |
| 1072 | * number of octets received prior to the server closing the |
| 1073 | * connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1074 | */ |
| 1075 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1076 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1077 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1078 | 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] | 1079 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 1080 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 1081 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 1082 | /* chunked not last, return badreq */ |
| 1083 | goto return_bad_req; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1084 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1085 | } |
| 1086 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 1087 | /* Chunked requests must have their content-length removed */ |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1088 | ctx.idx = 0; |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 1089 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1090 | 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] | 1091 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 1092 | } |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1093 | 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] | 1094 | signed long long cl; |
| 1095 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1096 | if (!ctx.vlen) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1097 | msg->err_pos = ctx.line + ctx.val - ci_head(req); |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1098 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1099 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1100 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1101 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1102 | msg->err_pos = ctx.line + ctx.val - ci_head(req); |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1103 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1104 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1105 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1106 | if (cl < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1107 | msg->err_pos = ctx.line + ctx.val - ci_head(req); |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1108 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1109 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1110 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 1111 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1112 | msg->err_pos = ctx.line + ctx.val - ci_head(req); |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1113 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 1114 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1115 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 1116 | msg->flags |= HTTP_MSGF_CNT_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 1117 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1118 | } |
| 1119 | |
Willy Tarreau | 34dfc60 | 2015-05-01 10:09:49 +0200 | [diff] [blame] | 1120 | /* even bodyless requests have a known length */ |
| 1121 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 1122 | |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 1123 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 1124 | * only change if both the request and the config reference something else. |
| 1125 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 1126 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 1127 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 1128 | * if FE and BE have the same settings (common). The method consists in |
| 1129 | * checking if options changed between the two calls (implying that either |
| 1130 | * one is non-null, or one of them is non-null and we are there for the first |
| 1131 | * time. |
| 1132 | */ |
| 1133 | if (!(txn->flags & TX_HDR_CONN_PRS) || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1134 | ((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] | 1135 | http_adjust_conn_mode(s, txn, msg); |
Willy Tarreau | 179085c | 2014-04-28 16:48:56 +0200 | [diff] [blame] | 1136 | |
Willy Tarreau | 9fbe18e | 2015-05-01 22:42:08 +0200 | [diff] [blame] | 1137 | /* we may have to wait for the request's body */ |
| 1138 | if ((s->be->options & PR_O_WREQ_BODY) && |
| 1139 | (msg->body_len || (msg->flags & HTTP_MSGF_TE_CHNK))) |
| 1140 | req->analysers |= AN_REQ_HTTP_BODY; |
| 1141 | |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 1142 | /* |
| 1143 | * RFC7234#4: |
| 1144 | * A cache MUST write through requests with methods |
| 1145 | * that are unsafe (Section 4.2.1 of [RFC7231]) to |
| 1146 | * the origin server; i.e., a cache is not allowed |
| 1147 | * to generate a reply to such a request before |
| 1148 | * having forwarded the request and having received |
| 1149 | * a corresponding response. |
| 1150 | * |
| 1151 | * RFC7231#4.2.1: |
| 1152 | * Of the request methods defined by this |
| 1153 | * specification, the GET, HEAD, OPTIONS, and TRACE |
| 1154 | * methods are defined to be safe. |
| 1155 | */ |
| 1156 | if (likely(txn->meth == HTTP_METH_GET || |
| 1157 | txn->meth == HTTP_METH_HEAD || |
| 1158 | txn->meth == HTTP_METH_OPTIONS || |
| 1159 | txn->meth == HTTP_METH_TRACE)) |
| 1160 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 1161 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1162 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 1163 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1164 | req->analyse_exp = TICK_ETERNITY; |
| 1165 | return 1; |
| 1166 | |
| 1167 | return_bad_req: |
| 1168 | /* We centralize bad requests processing here */ |
| 1169 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 1170 | /* we detected a parsing error. We want to archive this request |
| 1171 | * in the dedicated proxy area for later troubleshooting. |
| 1172 | */ |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 1173 | 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] | 1174 | } |
| 1175 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 1176 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1177 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 1178 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 1179 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1180 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 1181 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1182 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 1183 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1184 | |
| 1185 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1186 | if (!(s->flags & SF_ERR_MASK)) |
| 1187 | s->flags |= SF_ERR_PRXCOND; |
| 1188 | if (!(s->flags & SF_FINST_MASK)) |
| 1189 | s->flags |= SF_FINST_R; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1190 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1191 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1192 | req->analyse_exp = TICK_ETERNITY; |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 1196 | |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 1197 | /* This function prepares an applet to handle the stats. It can deal with the |
| 1198 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 1199 | * and program a response message if something was unexpected. It cannot fail |
| 1200 | * 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] | 1201 | * 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] | 1202 | * 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] | 1203 | * is expected to have already assigned an appctx to the stream. |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1204 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1205 | int http_handle_stats(struct stream *s, struct channel *req) |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1206 | { |
| 1207 | struct stats_admin_rule *stats_admin_rule; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1208 | struct stream_interface *si = &s->si[1]; |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1209 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1210 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1211 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1212 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 1213 | const char *uri, *h, *lookup; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1214 | struct appctx *appctx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1215 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1216 | appctx = si_appctx(si); |
| 1217 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 1218 | appctx->st1 = appctx->st2 = 0; |
| 1219 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
| 1220 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1221 | 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] | 1222 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1223 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1224 | uri = ci_head(msg->chn) + msg->sl.rq.u; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1225 | lookup = uri + uri_auth->uri_len; |
| 1226 | |
| 1227 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) { |
| 1228 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1229 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1230 | break; |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | if (uri_auth->refresh) { |
| 1235 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) { |
| 1236 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1237 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1238 | break; |
| 1239 | } |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) { |
| 1244 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1245 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1246 | break; |
| 1247 | } |
| 1248 | } |
| 1249 | |
Willy Tarreau | 1e62df9 | 2016-01-11 18:57:53 +0100 | [diff] [blame] | 1250 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 6; h++) { |
| 1251 | if (memcmp(h, ";typed", 6) == 0) { |
| 1252 | appctx->ctx.stats.flags &= ~STAT_FMT_HTML; |
| 1253 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
| 1254 | break; |
| 1255 | } |
| 1256 | } |
| 1257 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1258 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 1259 | if (memcmp(h, ";st=", 4) == 0) { |
| 1260 | int i; |
| 1261 | h += 4; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1262 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1263 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 1264 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1265 | appctx->ctx.stats.st_code = i; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1266 | break; |
| 1267 | } |
| 1268 | } |
| 1269 | break; |
| 1270 | } |
| 1271 | } |
| 1272 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1273 | appctx->ctx.stats.scope_str = 0; |
| 1274 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1275 | for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) { |
| 1276 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 1277 | int itx = 0; |
| 1278 | const char *h2; |
| 1279 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 1280 | const char *err; |
| 1281 | |
| 1282 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 1283 | h2 = h; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 1284 | appctx->ctx.stats.scope_str = h2 - ci_head(msg->chn); |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1285 | while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') { |
| 1286 | itx++; |
| 1287 | h++; |
| 1288 | } |
| 1289 | |
| 1290 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 1291 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1292 | appctx->ctx.stats.scope_len = itx; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1293 | |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1294 | /* 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] | 1295 | memcpy(scope_txt, h2, itx); |
| 1296 | scope_txt[itx] = '\0'; |
| 1297 | err = invalid_char(scope_txt); |
| 1298 | if (err) { |
| 1299 | /* bad char in search text => clear scope */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1300 | appctx->ctx.stats.scope_str = 0; |
| 1301 | appctx->ctx.stats.scope_len = 0; |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1302 | } |
| 1303 | break; |
| 1304 | } |
| 1305 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1306 | |
| 1307 | /* now check whether we have some admin rules for this request */ |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 1308 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1309 | int ret = 1; |
| 1310 | |
| 1311 | if (stats_admin_rule->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1312 | 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] | 1313 | ret = acl_pass(ret); |
| 1314 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 1315 | ret = !ret; |
| 1316 | } |
| 1317 | |
| 1318 | if (ret) { |
| 1319 | /* no rule, or the rule matches */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1320 | appctx->ctx.stats.flags |= STAT_ADMIN; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1321 | break; |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | /* Was the status page requested with a POST ? */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 1326 | if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1327 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Willy Tarreau | cfe7fdd | 2014-04-26 22:08:32 +0200 | [diff] [blame] | 1328 | /* we'll need the request body, possibly after sending 100-continue */ |
Willy Tarreau | b8cdf52 | 2015-05-29 01:09:15 +0200 | [diff] [blame] | 1329 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) |
| 1330 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1331 | appctx->st0 = STAT_HTTP_POST; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1332 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 1333 | else { |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1334 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 1335 | appctx->st0 = STAT_HTTP_LAST; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 1336 | } |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1337 | } |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 1338 | else { |
| 1339 | /* So it was another method (GET/HEAD) */ |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 1340 | appctx->st0 = STAT_HTTP_HEAD; |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1341 | } |
| 1342 | |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1343 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 1344 | return 1; |
| 1345 | } |
| 1346 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1347 | int http_transform_header_str(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1348 | const char* name, unsigned int name_len, |
| 1349 | const char *str, struct my_regex *re, |
| 1350 | int action) |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1351 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1352 | struct hdr_idx *idx = &s->txn->hdr_idx; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1353 | struct buffer *output = get_trash_chunk(); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1354 | |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1355 | /* Choose the header browsing function. */ |
| 1356 | switch (action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1357 | case ACT_HTTP_REPLACE_VAL: |
Willy Tarreau | c5a4fd5 | 2018-12-11 11:42:27 +0100 | [diff] [blame] | 1358 | return http_legacy_replace_header(idx, msg, name, name_len, str, re, output); |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1359 | case ACT_HTTP_REPLACE_HDR: |
Willy Tarreau | c5a4fd5 | 2018-12-11 11:42:27 +0100 | [diff] [blame] | 1360 | return http_legacy_replace_full_header(idx, msg, name, name_len, str, re, output); |
Thierry FOURNIER | 191f9ef | 2015-03-16 23:23:53 +0100 | [diff] [blame] | 1361 | default: /* impossible */ |
| 1362 | return -1; |
| 1363 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1364 | } |
| 1365 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1366 | static int http_transform_header(struct stream* s, struct http_msg *msg, |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1367 | const char* name, unsigned int name_len, |
| 1368 | struct list *fmt, struct my_regex *re, |
| 1369 | int action) |
| 1370 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1371 | struct buffer *replace; |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 1372 | int ret = -1; |
| 1373 | |
| 1374 | replace = alloc_trash_chunk(); |
| 1375 | if (!replace) |
| 1376 | goto leave; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1377 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1378 | replace->data = build_logline(s, replace->area, replace->size, fmt); |
| 1379 | if (replace->data >= replace->size - 1) |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 1380 | goto leave; |
| 1381 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1382 | ret = http_transform_header_str(s, msg, name, name_len, replace->area, |
| 1383 | re, action); |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1384 | |
Christopher Faulet | 07a0fec | 2017-02-08 12:17:07 +0100 | [diff] [blame] | 1385 | leave: |
| 1386 | free_trash_chunk(replace); |
| 1387 | return ret; |
Thierry FOURNIER | 5531f87 | 2015-03-16 11:15:50 +0100 | [diff] [blame] | 1388 | } |
| 1389 | |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1390 | /* |
| 1391 | * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value |
| 1392 | * built according to <fmt> log line format. |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1393 | * If <early_hints> is NULL, it is allocated and the HTTP 103 response first |
| 1394 | * line is inserted before the header. If an error occurred <early_hints> is |
| 1395 | * 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] | 1396 | */ |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1397 | static struct buffer *http_apply_early_hint_rule(struct stream* s, struct buffer *early_hints, |
| 1398 | const char* name, unsigned int name_len, |
| 1399 | struct list *fmt) |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1400 | { |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1401 | if (!early_hints) { |
| 1402 | early_hints = alloc_trash_chunk(); |
| 1403 | if (!early_hints) |
| 1404 | goto fail; |
| 1405 | if (!chunk_memcat(early_hints, HTTP_103.ptr, HTTP_103.len)) |
| 1406 | goto fail; |
| 1407 | } |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1408 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1409 | if (!chunk_memcat(early_hints, name, name_len) || !chunk_memcat(early_hints, ": ", 2)) |
| 1410 | goto fail; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1411 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1412 | early_hints->data += build_logline(s, b_tail(early_hints), b_room(early_hints), fmt); |
| 1413 | if (!chunk_memcat(early_hints, "\r\n", 2)) |
| 1414 | goto fail; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1415 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1416 | return early_hints; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1417 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1418 | fail: |
| 1419 | free_trash_chunk(early_hints); |
| 1420 | return NULL; |
| 1421 | } |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1422 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1423 | /* Sends an HTTP 103 response. Before sending it, the last CRLF finishing the |
| 1424 | * response is added. If an error occurred or if another response was already |
| 1425 | * sent, this function does nothing. |
| 1426 | */ |
| 1427 | static void http_send_early_hints(struct stream *s, struct buffer *early_hints) |
| 1428 | { |
| 1429 | struct channel *chn = s->txn->rsp.chn; |
| 1430 | char *cur_ptr = ci_head(chn); |
| 1431 | int ret; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1432 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1433 | /* If a response was already sent, skip early hints */ |
| 1434 | if (s->txn->status > 0) |
| 1435 | return; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1436 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1437 | if (!chunk_memcat(early_hints, "\r\n", 2)) |
| 1438 | return; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1439 | |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1440 | ret = b_rep_blk(&chn->buf, cur_ptr, cur_ptr, b_head(early_hints), b_data(early_hints)); |
| 1441 | c_adv(chn, ret); |
| 1442 | chn->total += ret; |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1443 | } |
| 1444 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1445 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 1446 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 1447 | * further processing of the request (auth, deny, ...), and defaults to |
| 1448 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 1449 | * 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] | 1450 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 1451 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 1452 | * status. |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1453 | */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 1454 | enum rule_result |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 1455 | 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] | 1456 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1457 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 1458 | struct http_txn *txn = s->txn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 1459 | struct act_rule *rule; |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 1460 | struct hdr_ctx ctx; |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 1461 | const char *auth_realm; |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1462 | struct buffer *early_hints = NULL; |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1463 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 1464 | int act_flags = 0; |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 1465 | int len; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1466 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 1467 | /* If "the current_rule_list" match the executed rule list, we are in |
| 1468 | * resume condition. If a resume is needed it is always in the action |
| 1469 | * and never in the ACL or converters. In this case, we initialise the |
| 1470 | * current rule, and go to the action execution point. |
| 1471 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 1472 | if (s->current_rule) { |
| 1473 | rule = s->current_rule; |
| 1474 | s->current_rule = NULL; |
| 1475 | if (s->current_rule_list == rules) |
| 1476 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 1477 | } |
| 1478 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 1479 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 1480 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1481 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1482 | /* check optional condition */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 1483 | if (rule->cond) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1484 | int ret; |
| 1485 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1486 | 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] | 1487 | ret = acl_pass(ret); |
| 1488 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 1489 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1490 | ret = !ret; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1491 | |
| 1492 | if (!ret) /* condition not matched */ |
| 1493 | continue; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1494 | } |
| 1495 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 1496 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 1497 | resume_execution: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1498 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1499 | case ACT_ACTION_ALLOW: |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1500 | rule_ret = HTTP_RULE_RES_STOP; |
| 1501 | goto end; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1502 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1503 | case ACT_ACTION_DENY: |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 1504 | if (deny_status) |
| 1505 | *deny_status = rule->deny_status; |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1506 | rule_ret = HTTP_RULE_RES_DENY; |
| 1507 | goto end; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1508 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1509 | case ACT_HTTP_REQ_TARPIT: |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 1510 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 1511 | if (deny_status) |
| 1512 | *deny_status = rule->deny_status; |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1513 | rule_ret = HTTP_RULE_RES_DENY; |
| 1514 | goto end; |
Willy Tarreau | ccbcc37 | 2012-12-27 12:37:57 +0100 | [diff] [blame] | 1515 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1516 | case ACT_HTTP_REQ_AUTH: |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1517 | /* Be sure to send any pending HTTP 103 response first */ |
| 1518 | if (early_hints) { |
| 1519 | http_send_early_hints(s, early_hints); |
| 1520 | free_trash_chunk(early_hints); |
| 1521 | early_hints = NULL; |
| 1522 | } |
Willy Tarreau | ae3c010 | 2014-04-28 23:22:08 +0200 | [diff] [blame] | 1523 | /* Auth might be performed on regular http-req rules as well as on stats */ |
| 1524 | auth_realm = rule->arg.auth.realm; |
| 1525 | if (!auth_realm) { |
| 1526 | if (px->uri_auth && rules == &px->uri_auth->http_req_rules) |
| 1527 | auth_realm = STATS_DEFAULT_REALM; |
| 1528 | else |
| 1529 | auth_realm = px->id; |
| 1530 | } |
| 1531 | /* send 401/407 depending on whether we use a proxy or not. We still |
| 1532 | * count one error, because normal browsing won't significantly |
| 1533 | * increase the counter but brute force attempts will. |
| 1534 | */ |
| 1535 | chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm); |
| 1536 | txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401; |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 1537 | http_reply_and_close(s, txn->status, &trash); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1538 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1539 | rule_ret = HTTP_RULE_RES_ABRT; |
| 1540 | goto end; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1541 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1542 | case ACT_HTTP_REDIR: |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1543 | /* Be sure to send any pending HTTP 103 response first */ |
| 1544 | if (early_hints) { |
| 1545 | http_send_early_hints(s, early_hints); |
| 1546 | free_trash_chunk(early_hints); |
| 1547 | early_hints = NULL; |
| 1548 | } |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1549 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 1550 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1551 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1552 | goto end; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 1553 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1554 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 1555 | s->task->nice = rule->arg.nice; |
| 1556 | break; |
| 1557 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1558 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 1559 | conn_set_tos(objt_conn(sess->origin), rule->arg.tos); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 1560 | break; |
| 1561 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1562 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 1563 | conn_set_mark(objt_conn(sess->origin), rule->arg.mark); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 1564 | break; |
| 1565 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1566 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 1567 | s->logs.level = rule->arg.loglevel; |
| 1568 | break; |
| 1569 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1570 | case ACT_HTTP_REPLACE_HDR: |
| 1571 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1572 | if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, |
| 1573 | rule->arg.hdr_add.name_len, |
| 1574 | &rule->arg.hdr_add.fmt, |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1575 | &rule->arg.hdr_add.re, rule->action)) { |
| 1576 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1577 | goto end; |
| 1578 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1579 | break; |
| 1580 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1581 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1582 | ctx.idx = 0; |
| 1583 | /* remove all occurrences of the header */ |
| 1584 | 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] | 1585 | ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1586 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 20b0de5 | 2012-12-24 15:45:22 +0100 | [diff] [blame] | 1587 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1588 | break; |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1589 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1590 | case ACT_HTTP_SET_HDR: |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1591 | case ACT_HTTP_ADD_HDR: { |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 1592 | /* The scope of the trash buffer must be limited to this function. The |
| 1593 | * build_logline() function can execute a lot of other function which |
| 1594 | * can use the trash buffer. So for limiting the scope of this global |
| 1595 | * buffer, we build first the header value using build_logline, and |
| 1596 | * after we store the header name. |
| 1597 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1598 | struct buffer *replace; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1599 | |
| 1600 | replace = alloc_trash_chunk(); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1601 | if (!replace) { |
| 1602 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1603 | goto end; |
| 1604 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1605 | |
Thierry Fournier | 4b788f7 | 2016-06-01 13:35:36 +0200 | [diff] [blame] | 1606 | len = rule->arg.hdr_add.name_len + 2, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1607 | len += build_logline(s, replace->area + len, |
| 1608 | replace->size - len, |
| 1609 | &rule->arg.hdr_add.fmt); |
| 1610 | memcpy(replace->area, rule->arg.hdr_add.name, |
| 1611 | rule->arg.hdr_add.name_len); |
| 1612 | replace->area[rule->arg.hdr_add.name_len] = ':'; |
| 1613 | replace->area[rule->arg.hdr_add.name_len + 1] = ' '; |
| 1614 | replace->data = len; |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1615 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1616 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1617 | /* remove all occurrences of the header */ |
| 1618 | ctx.idx = 0; |
| 1619 | 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] | 1620 | ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1621 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 1622 | } |
| 1623 | } |
| 1624 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1625 | 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] | 1626 | static unsigned char rate_limit = 0; |
| 1627 | |
| 1628 | if ((rate_limit++ & 255) == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1629 | replace->area[rule->arg.hdr_add.name_len] = 0; |
| 1630 | 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, |
| 1631 | replace->area, s->uniq_id); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1); |
| 1635 | if (sess->fe != s->be) |
| 1636 | HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1); |
| 1637 | if (sess->listener->counters) |
| 1638 | HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1); |
| 1639 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1640 | |
| 1641 | free_trash_chunk(replace); |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1642 | break; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1643 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1644 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1645 | case ACT_HTTP_DEL_ACL: |
| 1646 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1647 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1648 | struct buffer *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1649 | |
| 1650 | /* collect reference */ |
| 1651 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 1652 | if (!ref) |
| 1653 | continue; |
| 1654 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1655 | /* allocate key */ |
| 1656 | key = alloc_trash_chunk(); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1657 | if (!key) { |
| 1658 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1659 | goto end; |
| 1660 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1661 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1662 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1663 | key->data = build_logline(s, key->area, key->size, |
| 1664 | &rule->arg.map.key); |
| 1665 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1666 | |
| 1667 | /* perform update */ |
| 1668 | /* returned code: 1=ok, 0=ko */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1669 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1670 | pat_ref_delete(ref, key->area); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1671 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1672 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1673 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1674 | break; |
| 1675 | } |
| 1676 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1677 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1678 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1679 | struct buffer *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1680 | |
| 1681 | /* collect reference */ |
| 1682 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 1683 | if (!ref) |
| 1684 | continue; |
| 1685 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1686 | /* allocate key */ |
| 1687 | key = alloc_trash_chunk(); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1688 | if (!key) { |
| 1689 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1690 | goto end; |
| 1691 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1692 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1693 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1694 | key->data = build_logline(s, key->area, key->size, |
| 1695 | &rule->arg.map.key); |
| 1696 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1697 | |
| 1698 | /* perform update */ |
| 1699 | /* add entry only if it does not already exist */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1700 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1701 | if (pat_ref_find_elt(ref, key->area) == NULL) |
| 1702 | pat_ref_add(ref, key->area, NULL, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1703 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1704 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1705 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1706 | break; |
| 1707 | } |
| 1708 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1709 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1710 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1711 | struct buffer *key, *value; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1712 | |
| 1713 | /* collect reference */ |
| 1714 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 1715 | if (!ref) |
| 1716 | continue; |
| 1717 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1718 | /* allocate key */ |
| 1719 | key = alloc_trash_chunk(); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1720 | if (!key) { |
| 1721 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1722 | goto end; |
| 1723 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1724 | |
| 1725 | /* allocate value */ |
| 1726 | value = alloc_trash_chunk(); |
| 1727 | if (!value) { |
| 1728 | free_trash_chunk(key); |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1729 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1730 | goto end; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1731 | } |
| 1732 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1733 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1734 | key->data = build_logline(s, key->area, key->size, |
| 1735 | &rule->arg.map.key); |
| 1736 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1737 | |
| 1738 | /* collect value */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1739 | value->data = build_logline(s, value->area, |
| 1740 | value->size, |
| 1741 | &rule->arg.map.value); |
| 1742 | value->area[value->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1743 | |
| 1744 | /* perform update */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1745 | if (pat_ref_find_elt(ref, key->area) != NULL) |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1746 | /* update entry if it exists */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1747 | pat_ref_set(ref, key->area, value->area, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1748 | else |
| 1749 | /* insert a new entry */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1750 | pat_ref_add(ref, key->area, value->area, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1751 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1752 | free_trash_chunk(key); |
| 1753 | free_trash_chunk(value); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1754 | break; |
| 1755 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 1756 | |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1757 | case ACT_HTTP_EARLY_HINT: |
| 1758 | if (!(txn->req.flags & HTTP_MSGF_VER_11)) |
| 1759 | break; |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1760 | early_hints = http_apply_early_hint_rule(s, early_hints, |
| 1761 | rule->arg.early_hint.name, |
| 1762 | rule->arg.early_hint.name_len, |
| 1763 | &rule->arg.early_hint.fmt); |
| 1764 | if (!early_hints) { |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1765 | rule_ret = HTTP_RULE_RES_DONE; |
| 1766 | goto end; |
| 1767 | } |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1768 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 1769 | case ACT_CUSTOM: |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 1770 | if ((s->req.flags & CF_READ_ERROR) || |
| 1771 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 1772 | !(s->si[0].flags & SI_FL_CLEAN_ABRT) && |
| 1773 | (px->options & PR_O_ABRT_CLOSE))) |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 1774 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 1775 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 1776 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 1777 | case ACT_RET_ERR: |
| 1778 | case ACT_RET_CONT: |
| 1779 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 1780 | case ACT_RET_STOP: |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1781 | rule_ret = HTTP_RULE_RES_DONE; |
| 1782 | goto end; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 1783 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 1784 | s->current_rule = rule; |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1785 | rule_ret = HTTP_RULE_RES_YIELD; |
| 1786 | goto end; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 1787 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 1788 | break; |
| 1789 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1790 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 1791 | /* Note: only the first valid tracking parameter of each |
| 1792 | * applies. |
| 1793 | */ |
| 1794 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 1795 | if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) { |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 1796 | struct stktable *t; |
| 1797 | struct stksess *ts; |
| 1798 | struct stktable_key *key; |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 1799 | void *ptr1, *ptr2; |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 1800 | |
Thierry FOURNIER | 5ec63e0 | 2015-08-04 09:09:48 +0200 | [diff] [blame] | 1801 | t = rule->arg.trk_ctr.table.t; |
| 1802 | 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] | 1803 | |
| 1804 | if (key && (ts = stktable_get_entry(t, key))) { |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 1805 | stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 1806 | |
| 1807 | /* 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] | 1808 | ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 1809 | ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 1810 | if (ptr1 || ptr2) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1811 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 1812 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 1813 | if (ptr1) |
| 1814 | stktable_data_cast(ptr1, http_req_cnt)++; |
| 1815 | |
| 1816 | if (ptr2) |
| 1817 | update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate), |
| 1818 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 1819 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1820 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 1821 | |
| 1822 | /* If data was modified, we need to touch to re-schedule sync */ |
| 1823 | stktable_touch_local(t, ts, 0); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 1824 | } |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 1825 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 1826 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1827 | if (sess->fe != s->be) |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 1828 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
Willy Tarreau | 09448f7 | 2014-06-25 18:12:15 +0200 | [diff] [blame] | 1829 | } |
| 1830 | } |
Adis Nezirovic | 2fbcafc | 2015-07-06 15:44:30 +0200 | [diff] [blame] | 1831 | break; |
| 1832 | |
Joseph Herlant | 5ba8025 | 2018-11-15 09:25:36 -0800 | [diff] [blame] | 1833 | /* other flags exists, but normally, they never be matched. */ |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 1834 | default: |
| 1835 | break; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1836 | } |
| 1837 | } |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1838 | |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1839 | end: |
Christopher Faulet | 3c0544e | 2018-11-15 15:41:55 +0100 | [diff] [blame] | 1840 | if (early_hints) { |
| 1841 | http_send_early_hints(s, early_hints); |
| 1842 | free_trash_chunk(early_hints); |
Frédéric Lécaille | 9ca51aa | 2018-11-12 10:06:54 +0100 | [diff] [blame] | 1843 | } |
| 1844 | |
Willy Tarreau | 96257ec | 2012-12-27 10:46:37 +0100 | [diff] [blame] | 1845 | /* we reached the end of the rules, nothing to report */ |
Christopher Faulet | ea827bd | 2018-11-15 15:34:11 +0100 | [diff] [blame] | 1846 | return rule_ret; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 1847 | } |
| 1848 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 1849 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 1850 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 1851 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 1852 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 1853 | * is returned, the process can continue the evaluation of next rule list. If |
| 1854 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 1855 | * is returned, it means the operation could not be processed and a server error |
| 1856 | * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a |
| 1857 | * deny rule. If *YIELD is returned, the caller must call again the function |
| 1858 | * with the same context. |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1859 | */ |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 1860 | enum rule_result |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 1861 | 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] | 1862 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1863 | struct session *sess = strm_sess(s); |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 1864 | struct http_txn *txn = s->txn; |
Thierry FOURNIER | a28a942 | 2015-08-04 19:35:46 +0200 | [diff] [blame] | 1865 | struct act_rule *rule; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1866 | struct hdr_ctx ctx; |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 1867 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 1868 | int act_flags = 0; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1869 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 1870 | /* If "the current_rule_list" match the executed rule list, we are in |
| 1871 | * resume condition. If a resume is needed it is always in the action |
| 1872 | * and never in the ACL or converters. In this case, we initialise the |
| 1873 | * current rule, and go to the action execution point. |
| 1874 | */ |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 1875 | if (s->current_rule) { |
| 1876 | rule = s->current_rule; |
| 1877 | s->current_rule = NULL; |
| 1878 | if (s->current_rule_list == rules) |
| 1879 | goto resume_execution; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 1880 | } |
| 1881 | s->current_rule_list = rules; |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 1882 | |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1883 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1884 | |
| 1885 | /* check optional condition */ |
| 1886 | if (rule->cond) { |
| 1887 | int ret; |
| 1888 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1889 | 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] | 1890 | ret = acl_pass(ret); |
| 1891 | |
| 1892 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1893 | ret = !ret; |
| 1894 | |
| 1895 | if (!ret) /* condition not matched */ |
| 1896 | continue; |
| 1897 | } |
| 1898 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 1899 | act_flags |= ACT_FLAG_FIRST; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 1900 | resume_execution: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1901 | switch (rule->action) { |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1902 | case ACT_ACTION_ALLOW: |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 1903 | rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
| 1904 | goto end; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1905 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1906 | case ACT_ACTION_DENY: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1907 | txn->flags |= TX_SVDENY; |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 1908 | rule_ret = HTTP_RULE_RES_STOP; |
| 1909 | goto end; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1910 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1911 | case ACT_HTTP_SET_NICE: |
Willy Tarreau | f4c43c1 | 2013-06-11 17:01:13 +0200 | [diff] [blame] | 1912 | s->task->nice = rule->arg.nice; |
| 1913 | break; |
| 1914 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1915 | case ACT_HTTP_SET_TOS: |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 1916 | conn_set_tos(objt_conn(sess->origin), rule->arg.tos); |
Willy Tarreau | 42cf39e | 2013-06-11 18:51:32 +0200 | [diff] [blame] | 1917 | break; |
| 1918 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1919 | case ACT_HTTP_SET_MARK: |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 1920 | conn_set_mark(objt_conn(sess->origin), rule->arg.mark); |
Willy Tarreau | 51347ed | 2013-06-11 19:34:13 +0200 | [diff] [blame] | 1921 | break; |
| 1922 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1923 | case ACT_HTTP_SET_LOGL: |
Willy Tarreau | 9a355ec | 2013-06-11 17:45:46 +0200 | [diff] [blame] | 1924 | s->logs.level = rule->arg.loglevel; |
| 1925 | break; |
| 1926 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1927 | case ACT_HTTP_REPLACE_HDR: |
| 1928 | case ACT_HTTP_REPLACE_VAL: |
Thierry FOURNIER | 5a33ac7 | 2015-03-16 23:54:35 +0100 | [diff] [blame] | 1929 | if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, |
| 1930 | rule->arg.hdr_add.name_len, |
| 1931 | &rule->arg.hdr_add.fmt, |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 1932 | &rule->arg.hdr_add.re, rule->action)) { |
| 1933 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1934 | goto end; |
| 1935 | } |
Sasha Pachev | 218f064 | 2014-06-16 12:05:59 -0600 | [diff] [blame] | 1936 | break; |
| 1937 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1938 | case ACT_HTTP_DEL_HDR: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1939 | ctx.idx = 0; |
| 1940 | /* remove all occurrences of the header */ |
| 1941 | 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] | 1942 | ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1943 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 1944 | } |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1945 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1946 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1947 | case ACT_HTTP_SET_HDR: |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1948 | case ACT_HTTP_ADD_HDR: { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1949 | struct buffer *replace; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1950 | |
| 1951 | replace = alloc_trash_chunk(); |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 1952 | if (!replace) { |
| 1953 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 1954 | goto end; |
| 1955 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1956 | |
| 1957 | chunk_printf(replace, "%s: ", rule->arg.hdr_add.name); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1958 | memcpy(replace->area, rule->arg.hdr_add.name, |
| 1959 | rule->arg.hdr_add.name_len); |
| 1960 | replace->data = rule->arg.hdr_add.name_len; |
| 1961 | replace->area[replace->data++] = ':'; |
| 1962 | replace->area[replace->data++] = ' '; |
| 1963 | replace->data += build_logline(s, |
| 1964 | replace->area + replace->data, |
| 1965 | replace->size - replace->data, |
| 1966 | &rule->arg.hdr_add.fmt); |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1967 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1968 | if (rule->action == ACT_HTTP_SET_HDR) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1969 | /* remove all occurrences of the header */ |
| 1970 | ctx.idx = 0; |
| 1971 | 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] | 1972 | ci_head(txn->rsp.chn), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 8560328 | 2015-01-21 20:39:27 +0100 | [diff] [blame] | 1973 | http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx); |
| 1974 | } |
| 1975 | } |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 1976 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1977 | 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] | 1978 | static unsigned char rate_limit = 0; |
| 1979 | |
| 1980 | if ((rate_limit++ & 255) == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1981 | replace->area[rule->arg.hdr_add.name_len] = 0; |
| 1982 | 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, |
| 1983 | replace->area, s->uniq_id); |
Tim Duesterhus | 3fd1973 | 2018-05-27 20:35:08 +0200 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1); |
| 1987 | if (sess->fe != s->be) |
| 1988 | HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1); |
| 1989 | if (sess->listener->counters) |
| 1990 | HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1); |
| 1991 | if (objt_server(s->target)) |
| 1992 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1); |
| 1993 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1994 | |
| 1995 | free_trash_chunk(replace); |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 1996 | break; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 1997 | } |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 1998 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 1999 | case ACT_HTTP_DEL_ACL: |
| 2000 | case ACT_HTTP_DEL_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2001 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2002 | struct buffer *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2003 | |
| 2004 | /* collect reference */ |
| 2005 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2006 | if (!ref) |
| 2007 | continue; |
| 2008 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2009 | /* allocate key */ |
| 2010 | key = alloc_trash_chunk(); |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2011 | if (!key) { |
| 2012 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2013 | goto end; |
| 2014 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2015 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2016 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2017 | key->data = build_logline(s, key->area, key->size, |
| 2018 | &rule->arg.map.key); |
| 2019 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2020 | |
| 2021 | /* perform update */ |
| 2022 | /* returned code: 1=ok, 0=ko */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2023 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2024 | pat_ref_delete(ref, key->area); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2025 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2026 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2027 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2028 | break; |
| 2029 | } |
| 2030 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2031 | case ACT_HTTP_ADD_ACL: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2032 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2033 | struct buffer *key; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2034 | |
| 2035 | /* collect reference */ |
| 2036 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2037 | if (!ref) |
| 2038 | continue; |
| 2039 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2040 | /* allocate key */ |
| 2041 | key = alloc_trash_chunk(); |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2042 | if (!key) { |
| 2043 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2044 | goto end; |
| 2045 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2046 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2047 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2048 | key->data = build_logline(s, key->area, key->size, |
| 2049 | &rule->arg.map.key); |
| 2050 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2051 | |
| 2052 | /* perform update */ |
| 2053 | /* check if the entry already exists */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2054 | if (pat_ref_find_elt(ref, key->area) == NULL) |
| 2055 | pat_ref_add(ref, key->area, NULL, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2056 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2057 | free_trash_chunk(key); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2058 | break; |
| 2059 | } |
| 2060 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2061 | case ACT_HTTP_SET_MAP: { |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2062 | struct pat_ref *ref; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2063 | struct buffer *key, *value; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2064 | |
| 2065 | /* collect reference */ |
| 2066 | ref = pat_ref_lookup(rule->arg.map.ref); |
| 2067 | if (!ref) |
| 2068 | continue; |
| 2069 | |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2070 | /* allocate key */ |
| 2071 | key = alloc_trash_chunk(); |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2072 | if (!key) { |
| 2073 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2074 | goto end; |
| 2075 | } |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2076 | |
| 2077 | /* allocate value */ |
| 2078 | value = alloc_trash_chunk(); |
| 2079 | if (!value) { |
| 2080 | free_trash_chunk(key); |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2081 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2082 | goto end; |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2083 | } |
| 2084 | |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2085 | /* collect key */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2086 | key->data = build_logline(s, key->area, key->size, |
| 2087 | &rule->arg.map.key); |
| 2088 | key->area[key->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2089 | |
| 2090 | /* collect value */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2091 | value->data = build_logline(s, value->area, |
| 2092 | value->size, |
| 2093 | &rule->arg.map.value); |
| 2094 | value->area[value->data] = '\0'; |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2095 | |
| 2096 | /* perform update */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2097 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2098 | if (pat_ref_find_elt(ref, key->area) != NULL) |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2099 | /* update entry if it exists */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2100 | pat_ref_set(ref, key->area, value->area, NULL); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2101 | else |
| 2102 | /* insert a new entry */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2103 | pat_ref_add(ref, key->area, value->area, NULL); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2104 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Dragan Dosen | 2ae327e | 2017-10-26 11:25:10 +0200 | [diff] [blame] | 2105 | free_trash_chunk(key); |
| 2106 | free_trash_chunk(value); |
Baptiste Assmann | fabcbe0 | 2014-04-24 22:16:59 +0200 | [diff] [blame] | 2107 | break; |
| 2108 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2109 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 2110 | case ACT_HTTP_REDIR: |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2111 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2112 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2113 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2114 | goto end; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2115 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2116 | case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX: |
| 2117 | /* Note: only the first valid tracking parameter of each |
| 2118 | * applies. |
| 2119 | */ |
| 2120 | |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2121 | if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) { |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2122 | struct stktable *t; |
| 2123 | struct stksess *ts; |
| 2124 | struct stktable_key *key; |
| 2125 | void *ptr; |
| 2126 | |
| 2127 | t = rule->arg.trk_ctr.table.t; |
| 2128 | key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL); |
| 2129 | |
| 2130 | if (key && (ts = stktable_get_entry(t, key))) { |
Christopher Faulet | 4fce0d8 | 2017-09-18 11:57:31 +0200 | [diff] [blame] | 2131 | stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts); |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2132 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2133 | HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2134 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2135 | /* let's count a new HTTP request as it's the first time we do it */ |
| 2136 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 2137 | if (ptr) |
| 2138 | stktable_data_cast(ptr, http_req_cnt)++; |
| 2139 | |
| 2140 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 2141 | if (ptr) |
| 2142 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 2143 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 2144 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2145 | /* When the client triggers a 4xx from the server, it's most often due |
| 2146 | * to a missing object or permission. These events should be tracked |
| 2147 | * because if they happen often, it may indicate a brute force or a |
| 2148 | * vulnerability scan. Normally this is done when receiving the response |
| 2149 | * but here we're tracking after this ought to have been done so we have |
| 2150 | * to do it on purpose. |
| 2151 | */ |
Willy Tarreau | 3146a4c | 2016-07-26 15:22:33 +0200 | [diff] [blame] | 2152 | if ((unsigned)(txn->status - 400) < 100) { |
| 2153 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 2154 | if (ptr) |
| 2155 | stktable_data_cast(ptr, http_err_cnt)++; |
| 2156 | |
| 2157 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 2158 | if (ptr) |
| 2159 | update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
| 2160 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
| 2161 | } |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2162 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2163 | HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2164 | |
Emeric Brun | 0fed0b0 | 2017-11-29 16:15:07 +0100 | [diff] [blame] | 2165 | /* If data was modified, we need to touch to re-schedule sync */ |
| 2166 | stktable_touch_local(t, ts, 0); |
| 2167 | |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 2168 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT); |
| 2169 | if (sess->fe != s->be) |
| 2170 | stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND); |
| 2171 | |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 2172 | } |
| 2173 | } |
| 2174 | break; |
| 2175 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2176 | case ACT_CUSTOM: |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 2177 | if ((s->req.flags & CF_READ_ERROR) || |
| 2178 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 2179 | !(s->si[0].flags & SI_FL_CLEAN_ABRT) && |
| 2180 | (px->options & PR_O_ABRT_CLOSE))) |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2181 | act_flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3945868 | 2015-09-27 10:33:15 +0200 | [diff] [blame] | 2182 | |
Willy Tarreau | acc9800 | 2015-09-27 23:34:39 +0200 | [diff] [blame] | 2183 | switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) { |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2184 | case ACT_RET_ERR: |
| 2185 | case ACT_RET_CONT: |
| 2186 | break; |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 2187 | case ACT_RET_STOP: |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2188 | rule_ret = HTTP_RULE_RES_STOP; |
| 2189 | goto end; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 2190 | case ACT_RET_YIELD: |
Willy Tarreau | 152b81e | 2015-04-20 13:26:17 +0200 | [diff] [blame] | 2191 | s->current_rule = rule; |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2192 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2193 | goto end; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2194 | } |
William Lallemand | 73025dd | 2014-04-24 14:38:37 +0200 | [diff] [blame] | 2195 | break; |
| 2196 | |
Joseph Herlant | 5ba8025 | 2018-11-15 09:25:36 -0800 | [diff] [blame] | 2197 | /* other flags exists, but normally, they never be matched. */ |
Thierry FOURNIER | 22e4901 | 2015-08-05 19:13:48 +0200 | [diff] [blame] | 2198 | default: |
| 2199 | break; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2200 | } |
| 2201 | } |
| 2202 | |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2203 | end: |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2204 | /* we reached the end of the rules, nothing to report */ |
Christopher Faulet | 6c243eb | 2018-11-15 15:40:29 +0100 | [diff] [blame] | 2205 | return rule_ret; |
Willy Tarreau | e365c0b | 2013-06-11 16:06:12 +0200 | [diff] [blame] | 2206 | } |
| 2207 | |
| 2208 | |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2209 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 2210 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 2211 | * as too large a request to build a valid response. |
| 2212 | */ |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 2213 | 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] | 2214 | { |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 2215 | struct http_msg *req = &txn->req; |
| 2216 | struct http_msg *res = &txn->rsp; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2217 | const char *msg_fmt; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2218 | struct buffer *chunk; |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2219 | int ret = 0; |
| 2220 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2221 | if (IS_HTX_STRM(s)) |
| 2222 | return htx_apply_redirect_rule(rule, s, txn); |
| 2223 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2224 | chunk = alloc_trash_chunk(); |
| 2225 | if (!chunk) |
| 2226 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2227 | |
| 2228 | /* build redirect message */ |
| 2229 | switch(rule->code) { |
Yves Lafon | 3e8d1ae | 2013-03-11 11:06:05 -0400 | [diff] [blame] | 2230 | case 308: |
| 2231 | msg_fmt = HTTP_308; |
| 2232 | break; |
| 2233 | case 307: |
| 2234 | msg_fmt = HTTP_307; |
| 2235 | break; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2236 | case 303: |
| 2237 | msg_fmt = HTTP_303; |
| 2238 | break; |
| 2239 | case 301: |
| 2240 | msg_fmt = HTTP_301; |
| 2241 | break; |
| 2242 | case 302: |
| 2243 | default: |
| 2244 | msg_fmt = HTTP_302; |
| 2245 | break; |
| 2246 | } |
| 2247 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2248 | if (unlikely(!chunk_strcpy(chunk, msg_fmt))) |
| 2249 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2250 | |
| 2251 | switch(rule->type) { |
| 2252 | case REDIRECT_TYPE_SCHEME: { |
| 2253 | const char *path; |
| 2254 | const char *host; |
| 2255 | struct hdr_ctx ctx; |
| 2256 | int pathlen; |
| 2257 | int hostlen; |
| 2258 | |
| 2259 | host = ""; |
| 2260 | hostlen = 0; |
| 2261 | ctx.idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2262 | 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] | 2263 | host = ctx.line + ctx.val; |
| 2264 | hostlen = ctx.vlen; |
| 2265 | } |
| 2266 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 2267 | path = http_txn_get_path(txn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2268 | /* build message using path */ |
| 2269 | if (path) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2270 | 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] | 2271 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2272 | int qs = 0; |
| 2273 | while (qs < pathlen) { |
| 2274 | if (path[qs] == '?') { |
| 2275 | pathlen = qs; |
| 2276 | break; |
| 2277 | } |
| 2278 | qs++; |
| 2279 | } |
| 2280 | } |
| 2281 | } else { |
| 2282 | path = "/"; |
| 2283 | pathlen = 1; |
| 2284 | } |
| 2285 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2286 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2287 | /* check if we can add scheme + "://" + host + path */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2288 | if (chunk->data + rule->rdr_len + 3 + hostlen + pathlen > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2289 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2290 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2291 | /* add scheme */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2292 | memcpy(chunk->area + chunk->data, rule->rdr_str, |
| 2293 | rule->rdr_len); |
| 2294 | chunk->data += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2295 | } |
| 2296 | else { |
| 2297 | /* add scheme with executing log format */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2298 | chunk->data += build_logline(s, |
| 2299 | chunk->area + chunk->data, |
| 2300 | chunk->size - chunk->data, |
| 2301 | &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2302 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2303 | /* check if we can add scheme + "://" + host + path */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2304 | if (chunk->data + 3 + hostlen + pathlen > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2305 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2306 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2307 | /* add "://" */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2308 | memcpy(chunk->area + chunk->data, "://", 3); |
| 2309 | chunk->data += 3; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2310 | |
| 2311 | /* add host */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2312 | memcpy(chunk->area + chunk->data, host, hostlen); |
| 2313 | chunk->data += hostlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2314 | |
| 2315 | /* add path */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2316 | memcpy(chunk->area + chunk->data, path, pathlen); |
| 2317 | chunk->data += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2318 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2319 | /* 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] | 2320 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2321 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2322 | if (chunk->data > chunk->size - 5) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2323 | goto leave; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2324 | chunk->area[chunk->data] = '/'; |
| 2325 | chunk->data++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2326 | } |
| 2327 | |
| 2328 | break; |
| 2329 | } |
| 2330 | case REDIRECT_TYPE_PREFIX: { |
| 2331 | const char *path; |
| 2332 | int pathlen; |
| 2333 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 2334 | path = http_txn_get_path(txn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2335 | /* build message using path */ |
| 2336 | if (path) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2337 | 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] | 2338 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2339 | int qs = 0; |
| 2340 | while (qs < pathlen) { |
| 2341 | if (path[qs] == '?') { |
| 2342 | pathlen = qs; |
| 2343 | break; |
| 2344 | } |
| 2345 | qs++; |
| 2346 | } |
| 2347 | } |
| 2348 | } else { |
| 2349 | path = "/"; |
| 2350 | pathlen = 1; |
| 2351 | } |
| 2352 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2353 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2354 | if (chunk->data + rule->rdr_len + pathlen > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2355 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2356 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2357 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2358 | * add anything, otherwise it makes it hard for the user to |
| 2359 | * configure a self-redirection. |
| 2360 | */ |
| 2361 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2362 | memcpy(chunk->area + chunk->data, |
| 2363 | rule->rdr_str, rule->rdr_len); |
| 2364 | chunk->data += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2365 | } |
| 2366 | } |
| 2367 | else { |
| 2368 | /* add prefix with executing log format */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2369 | chunk->data += build_logline(s, |
| 2370 | chunk->area + chunk->data, |
| 2371 | chunk->size - chunk->data, |
| 2372 | &rule->rdr_fmt); |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2373 | |
| 2374 | /* Check length */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2375 | if (chunk->data + pathlen > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2376 | goto leave; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | /* add path */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2380 | memcpy(chunk->area + chunk->data, path, pathlen); |
| 2381 | chunk->data += pathlen; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2382 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2383 | /* 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] | 2384 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2385 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2386 | if (chunk->data > chunk->size - 5) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2387 | goto leave; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2388 | chunk->area[chunk->data] = '/'; |
| 2389 | chunk->data++; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2390 | } |
| 2391 | |
| 2392 | break; |
| 2393 | } |
| 2394 | case REDIRECT_TYPE_LOCATION: |
| 2395 | default: |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2396 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2397 | if (chunk->data + rule->rdr_len > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2398 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2399 | |
| 2400 | /* add location */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2401 | memcpy(chunk->area + chunk->data, rule->rdr_str, |
| 2402 | rule->rdr_len); |
| 2403 | chunk->data += rule->rdr_len; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2404 | } |
| 2405 | else { |
| 2406 | /* add location with executing log format */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2407 | chunk->data += build_logline(s, |
| 2408 | chunk->area + chunk->data, |
| 2409 | chunk->size - chunk->data, |
| 2410 | &rule->rdr_fmt); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2411 | |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2412 | /* Check left length */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2413 | if (chunk->data > chunk->size - 4) |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2414 | goto leave; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 2415 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2416 | break; |
| 2417 | } |
| 2418 | |
| 2419 | if (rule->cookie_len) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2420 | memcpy(chunk->area + chunk->data, "\r\nSet-Cookie: ", 14); |
| 2421 | chunk->data += 14; |
| 2422 | memcpy(chunk->area + chunk->data, rule->cookie_str, |
| 2423 | rule->cookie_len); |
| 2424 | chunk->data += rule->cookie_len; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2425 | } |
| 2426 | |
Willy Tarreau | 19b1412 | 2017-02-28 09:48:11 +0100 | [diff] [blame] | 2427 | /* add end of headers and the keep-alive/close status. */ |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2428 | txn->status = rule->code; |
| 2429 | /* let's log the request time */ |
| 2430 | s->logs.tv_request = now; |
| 2431 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 2432 | 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] | 2433 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 2434 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 2435 | /* keep-alive possible */ |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 2436 | if (!(req->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2437 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2438 | memcpy(chunk->area + chunk->data, |
| 2439 | "\r\nProxy-Connection: keep-alive", 30); |
| 2440 | chunk->data += 30; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2441 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2442 | memcpy(chunk->area + chunk->data, |
| 2443 | "\r\nConnection: keep-alive", 24); |
| 2444 | chunk->data += 24; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2445 | } |
| 2446 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2447 | memcpy(chunk->area + chunk->data, "\r\n\r\n", 4); |
| 2448 | chunk->data += 4; |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2449 | FLT_STRM_CB(s, flt_http_reply(s, txn->status, chunk)); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2450 | co_inject(res->chn, chunk->area, chunk->data); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2451 | /* "eat" the request */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2452 | b_del(&req->chn->buf, req->sov); |
Willy Tarreau | b329a31 | 2015-05-22 16:27:37 +0200 | [diff] [blame] | 2453 | req->next -= req->sov; |
| 2454 | req->sov = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2455 | 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] | 2456 | 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] | 2457 | req->msg_state = HTTP_MSG_CLOSED; |
| 2458 | res->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2459 | /* Trim any possible response */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2460 | b_set_data(&res->chn->buf, co_data(res->chn)); |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 2461 | res->next = res->sov = 0; |
Christopher Faulet | 5d468ca | 2017-09-11 09:27:29 +0200 | [diff] [blame] | 2462 | /* let the server side turn to SI_ST_CLO */ |
| 2463 | channel_shutw_now(req->chn); |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2464 | } else { |
| 2465 | /* keep-alive not possible */ |
| 2466 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2467 | memcpy(chunk->area + chunk->data, |
| 2468 | "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 2469 | chunk->data += 29; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2470 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2471 | memcpy(chunk->area + chunk->data, |
| 2472 | "\r\nConnection: close\r\n\r\n", 23); |
| 2473 | chunk->data += 23; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2474 | } |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2475 | http_reply_and_close(s, txn->status, chunk); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2476 | req->chn->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2477 | } |
| 2478 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2479 | if (!(s->flags & SF_ERR_MASK)) |
| 2480 | s->flags |= SF_ERR_LOCAL; |
| 2481 | if (!(s->flags & SF_FINST_MASK)) |
| 2482 | s->flags |= SF_FINST_R; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2483 | |
Thierry FOURNIER | 0d94576 | 2017-01-28 07:39:53 +0100 | [diff] [blame] | 2484 | ret = 1; |
| 2485 | leave: |
| 2486 | free_trash_chunk(chunk); |
| 2487 | return ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2488 | } |
| 2489 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2490 | /* This stream analyser runs all HTTP request processing which is common to |
| 2491 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2492 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2493 | * 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] | 2494 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2495 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2496 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2497 | 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] | 2498 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2499 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2500 | struct http_txn *txn = s->txn; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2501 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2502 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 2503 | struct cond_wordlist *wl; |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2504 | enum rule_result verdict; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2505 | int deny_status = HTTP_ERR_403; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2506 | struct connection *conn = objt_conn(sess->origin); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2507 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2508 | if (IS_HTX_STRM(s)) |
| 2509 | return htx_process_req_common(s, req, an_bit, px); |
| 2510 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2511 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2512 | /* we need more data */ |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2513 | goto return_prx_yield; |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2514 | } |
| 2515 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 2516 | 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] | 2517 | now_ms, __FUNCTION__, |
| 2518 | s, |
| 2519 | req, |
| 2520 | req->rex, req->wex, |
| 2521 | req->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 2522 | ci_data(req), |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2523 | req->analysers); |
| 2524 | |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 2525 | /* just in case we have some per-backend tracking */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2526 | stream_inc_be_http_req_ctr(s); |
Willy Tarreau | 6541083 | 2014-04-28 21:25:43 +0200 | [diff] [blame] | 2527 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2528 | /* evaluate http-request rules */ |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2529 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2530 | 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] | 2531 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2532 | switch (verdict) { |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2533 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 2534 | goto return_prx_yield; |
| 2535 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2536 | case HTTP_RULE_RES_CONT: |
| 2537 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 2538 | break; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2539 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2540 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 2541 | if (txn->flags & TX_CLTARPIT) |
| 2542 | goto tarpit; |
| 2543 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2544 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2545 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 2546 | goto return_prx_cond; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2547 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2548 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2549 | goto done; |
| 2550 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2551 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 2552 | goto return_bad_req; |
| 2553 | } |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2554 | } |
| 2555 | |
Olivier Houchard | 25ae45a | 2017-11-29 19:51:19 +0100 | [diff] [blame] | 2556 | if (conn && (conn->flags & CO_FL_EARLY_DATA) && |
| 2557 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2558 | struct hdr_ctx ctx; |
| 2559 | |
| 2560 | ctx.idx = 0; |
| 2561 | if (!http_find_header2("Early-Data", strlen("Early-Data"), |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2562 | ci_head(&s->req), &txn->hdr_idx, &ctx)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2563 | if (unlikely(http_header_add_tail2(&txn->req, |
| 2564 | &txn->hdr_idx, "Early-Data: 1", |
Christopher Faulet | 005e79e | 2018-07-20 09:54:26 +0200 | [diff] [blame] | 2565 | strlen("Early-Data: 1")) < 0)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2566 | goto return_bad_req; |
| 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | } |
| 2571 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2572 | /* OK at this stage, we know that the request was accepted according to |
| 2573 | * the http-request rules, we can check for the stats. Note that the |
| 2574 | * URI is detected *before* the req* rules in order not to be affected |
| 2575 | * by a possible reqrep, while they are processed *after* so that a |
| 2576 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 2577 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2578 | if (stats_check_uri(&s->si[1], txn, px)) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2579 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 2580 | if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) { |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2581 | txn->status = 500; |
| 2582 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2583 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 7b4b499 | 2013-12-01 09:15:12 +0100 | [diff] [blame] | 2584 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2585 | if (!(s->flags & SF_ERR_MASK)) |
| 2586 | s->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2587 | goto return_prx_cond; |
| 2588 | } |
| 2589 | |
| 2590 | /* parse the whole stats request and extract the relevant information */ |
| 2591 | http_handle_stats(s, req); |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2592 | 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] | 2593 | /* not all actions implemented: deny, allow, auth */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2594 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2595 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 2596 | goto deny; |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2597 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2598 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 2599 | goto return_prx_cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2600 | } |
| 2601 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2602 | /* evaluate the req* rules except reqadd */ |
| 2603 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 2604 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2605 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2606 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2607 | if (txn->flags & TX_CLDENY) |
| 2608 | goto deny; |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2609 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 2610 | if (txn->flags & TX_CLTARPIT) { |
| 2611 | deny_status = HTTP_ERR_500; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2612 | goto tarpit; |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 2613 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2614 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2615 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2616 | /* add request headers from the rule sets in the same order */ |
| 2617 | list_for_each_entry(wl, &px->req_add, list) { |
| 2618 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2619 | 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] | 2620 | ret = acl_pass(ret); |
| 2621 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 2622 | ret = !ret; |
| 2623 | if (!ret) |
| 2624 | continue; |
| 2625 | } |
| 2626 | |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 2627 | 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] | 2628 | goto return_bad_req; |
Willy Tarreau | 81499eb | 2012-12-27 12:19:02 +0100 | [diff] [blame] | 2629 | } |
| 2630 | |
Willy Tarreau | 5254259 | 2014-04-28 18:33:26 +0200 | [diff] [blame] | 2631 | |
| 2632 | /* Proceed with the stats now. */ |
William Lallemand | 71bd11a | 2017-11-20 19:13:14 +0100 | [diff] [blame] | 2633 | if (unlikely(objt_applet(s->target) == &http_stats_applet) || |
| 2634 | unlikely(objt_applet(s->target) == &http_cache_applet)) { |
Willy Tarreau | 1facd6d | 2012-12-22 22:03:39 +0100 | [diff] [blame] | 2635 | /* process the stats request now */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2636 | 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] | 2637 | HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2638 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2639 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 2640 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 2641 | if (!(s->flags & SF_FINST_MASK)) |
| 2642 | s->flags |= SF_FINST_R; |
Willy Tarreau | 347a35d | 2013-11-22 17:51:09 +0100 | [diff] [blame] | 2643 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 2644 | /* 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] | 2645 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 2646 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 2647 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2648 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2649 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2650 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2651 | /* check whether we have some ACLs set to redirect this request */ |
| 2652 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 2653 | if (rule->cond) { |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2654 | int ret; |
| 2655 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 2656 | 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] | 2657 | ret = acl_pass(ret); |
| 2658 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2659 | ret = !ret; |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2660 | if (!ret) |
| 2661 | continue; |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 2662 | } |
Willy Tarreau | 71241ab | 2012-12-27 11:30:54 +0100 | [diff] [blame] | 2663 | if (!http_apply_redirect_rule(rule, s, txn)) |
| 2664 | goto return_bad_req; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2665 | goto done; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2666 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2667 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 2668 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 2669 | * If this happens, then the data will not come immediately, so we must |
| 2670 | * send all what we have without waiting. Note that due to the small gain |
| 2671 | * 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] | 2672 | * 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] | 2673 | * itself once used. |
| 2674 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2675 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 2676 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2677 | done: /* done with this analyser, continue with next ones that the calling |
| 2678 | * points will have set, if any. |
| 2679 | */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2680 | req->analyse_exp = TICK_ETERNITY; |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 2681 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 2682 | req->analysers &= ~an_bit; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2683 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2684 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2685 | tarpit: |
Willy Tarreau | 6a0bca9 | 2017-06-11 17:56:27 +0200 | [diff] [blame] | 2686 | /* Allow cookie logging |
| 2687 | */ |
| 2688 | if (s->be->cookie_name || sess->fe->capture_name) |
| 2689 | manage_client_side_cookies(s, req); |
| 2690 | |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2691 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2692 | * which may be the same as the connect timeout if unspecified. |
| 2693 | * If unset, then set it to zero because we really want it to |
| 2694 | * eventually expire. We build the tarpit as an analyser. |
| 2695 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2696 | channel_erase(&s->req); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2697 | |
| 2698 | /* wipe the request out so that we can drop the connection early |
| 2699 | * if the client closes first. |
| 2700 | */ |
| 2701 | channel_dont_connect(req); |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 2702 | |
Jarno Huuskonen | 800d176 | 2017-03-06 14:56:36 +0200 | [diff] [blame] | 2703 | txn->status = http_err_codes[deny_status]; |
| 2704 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2705 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2706 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2707 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2708 | if (!req->analyse_exp) |
| 2709 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2710 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 2711 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2712 | if (sess->fe != s->be) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 2713 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2714 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 2715 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Thierry FOURNIER | 7566e30 | 2014-08-22 06:55:26 +0200 | [diff] [blame] | 2716 | goto done_without_exp; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2717 | |
| 2718 | deny: /* this request was blocked (denied) */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 2719 | |
| 2720 | /* Allow cookie logging |
| 2721 | */ |
| 2722 | if (s->be->cookie_name || sess->fe->capture_name) |
| 2723 | manage_client_side_cookies(s, req); |
| 2724 | |
Willy Tarreau | 0b74833 | 2014-04-29 00:13:29 +0200 | [diff] [blame] | 2725 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 58727ec | 2016-05-25 16:23:59 +0200 | [diff] [blame] | 2726 | txn->status = http_err_codes[deny_status]; |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2727 | s->logs.tv_request = now; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2728 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2729 | stream_inc_http_err_ctr(s); |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 2730 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2731 | if (sess->fe != s->be) |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 2732 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2733 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 2734 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Willy Tarreau | bbba2a8 | 2014-04-28 13:57:26 +0200 | [diff] [blame] | 2735 | goto return_prx_cond; |
| 2736 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2737 | return_bad_req: |
| 2738 | /* We centralize bad requests processing here */ |
| 2739 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2740 | /* we detected a parsing error. We want to archive this request |
| 2741 | * in the dedicated proxy area for later troubleshooting. |
| 2742 | */ |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 2743 | 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] | 2744 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2745 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 2746 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2747 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2748 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2749 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2750 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 2751 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2752 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 2753 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2754 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2755 | return_prx_cond: |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2756 | if (!(s->flags & SF_ERR_MASK)) |
| 2757 | s->flags |= SF_ERR_PRXCOND; |
| 2758 | if (!(s->flags & SF_FINST_MASK)) |
| 2759 | s->flags |= SF_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2760 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2761 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2762 | req->analyse_exp = TICK_ETERNITY; |
| 2763 | return 0; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 2764 | |
| 2765 | return_prx_yield: |
| 2766 | channel_dont_connect(req); |
| 2767 | return 0; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2768 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2769 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2770 | /* This function performs all the processing enabled for the current request. |
| 2771 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 2772 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2773 | * request. It relies on buffers flags, and updates s->req.analysers. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2774 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2775 | 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] | 2776 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 2777 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2778 | struct http_txn *txn = s->txn; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2779 | struct http_msg *msg = &txn->req; |
Willy Tarreau | ee335e6 | 2015-04-21 18:15:13 +0200 | [diff] [blame] | 2780 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2781 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2782 | if (IS_HTX_STRM(s)) |
| 2783 | return htx_process_request(s, req, an_bit); |
| 2784 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2785 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2786 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2787 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2788 | return 0; |
| 2789 | } |
| 2790 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 2791 | 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] | 2792 | now_ms, __FUNCTION__, |
| 2793 | s, |
| 2794 | req, |
| 2795 | req->rex, req->wex, |
| 2796 | req->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 2797 | ci_data(req), |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2798 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2799 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2800 | /* |
| 2801 | * Right now, we know that we have processed the entire headers |
| 2802 | * and that unwanted requests have been filtered out. We can do |
| 2803 | * whatever we want with the remaining request. Also, now we |
| 2804 | * may have separate values for ->fe, ->be. |
| 2805 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2806 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2807 | /* |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2808 | * If HTTP PROXY is set we simply get remote server address parsing |
| 2809 | * incoming request. Note that this requires that a connection is |
| 2810 | * allocated on the server side. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2811 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2812 | 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] | 2813 | struct connection *conn; |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 2814 | char *path; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 2815 | |
Willy Tarreau | 9471b8c | 2013-12-15 13:31:35 +0100 | [diff] [blame] | 2816 | /* Note that for now we don't reuse existing proxy connections */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2817 | 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] | 2818 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 2819 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2820 | txn->status = 500; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 2821 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 2822 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 2823 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2824 | if (!(s->flags & SF_ERR_MASK)) |
| 2825 | s->flags |= SF_ERR_RESOURCE; |
| 2826 | if (!(s->flags & SF_FINST_MASK)) |
| 2827 | s->flags |= SF_FINST_R; |
Willy Tarreau | 32e3c6a | 2013-10-11 19:34:20 +0200 | [diff] [blame] | 2828 | |
| 2829 | return 0; |
| 2830 | } |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 2831 | |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 2832 | path = http_txn_get_path(txn); |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2833 | if (url2sa(ci_head(req) + msg->sl.rq.u, |
| 2834 | 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] | 2835 | &conn->addr.to, NULL) == -1) |
| 2836 | goto return_bad_req; |
| 2837 | |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 2838 | /* if the path was found, we have to remove everything between |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2839 | * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not |
| 2840 | * 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] | 2841 | * u_l characters by a single "/". |
| 2842 | */ |
| 2843 | if (path) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2844 | char *cur_ptr = ci_head(req); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 2845 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 2846 | int delta; |
| 2847 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2848 | 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] | 2849 | http_msg_move_end(&txn->req, delta); |
| 2850 | cur_end += delta; |
| 2851 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 2852 | goto return_bad_req; |
| 2853 | } |
| 2854 | else { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2855 | char *cur_ptr = ci_head(req); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 2856 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 2857 | int delta; |
| 2858 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2859 | delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 2860 | cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
Willy Tarreau | e8df1e1 | 2013-12-16 14:30:55 +0100 | [diff] [blame] | 2861 | http_msg_move_end(&txn->req, delta); |
| 2862 | cur_end += delta; |
| 2863 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 2864 | goto return_bad_req; |
| 2865 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2866 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2867 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2868 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 2869 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2870 | * Note that doing so might move headers in the request, but |
| 2871 | * the fields will stay coherent and the URI will not move. |
| 2872 | * This should only be performed in the backend. |
| 2873 | */ |
Bertrand Paquet | 83a2c3d | 2016-04-06 11:58:31 +0200 | [diff] [blame] | 2874 | if (s->be->cookie_name || sess->fe->capture_name) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2875 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2876 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2877 | /* add unique-id if "header-unique-id" is specified */ |
| 2878 | |
Thierry Fournier | f4011dd | 2016-03-29 17:23:51 +0200 | [diff] [blame] | 2879 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2880 | if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 2881 | goto return_bad_req; |
| 2882 | s->unique_id[0] = '\0'; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2883 | 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] | 2884 | } |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2885 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2886 | if (sess->fe->header_unique_id && s->unique_id) { |
Willy Tarreau | 5f6333c | 2018-08-22 05:14:37 +0200 | [diff] [blame] | 2887 | 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] | 2888 | goto return_bad_req; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2889 | 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] | 2890 | goto return_bad_req; |
| 2891 | } |
| 2892 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 2893 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2894 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2895 | * asks for it. |
| 2896 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2897 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 2898 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2899 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 2900 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 2901 | 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] | 2902 | ci_head(req), &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 2903 | /* The header is set to be added only if none is present |
| 2904 | * and we found it, so don't do anything. |
| 2905 | */ |
| 2906 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2907 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2908 | /* Add an X-Forwarded-For header unless the source IP is |
| 2909 | * in the 'except' network range. |
| 2910 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2911 | if ((!sess->fe->except_mask.s_addr || |
| 2912 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 2913 | != sess->fe->except_net.s_addr) && |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2914 | (!s->be->except_mask.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2915 | (((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] | 2916 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2917 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2918 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2919 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2920 | |
| 2921 | /* Note: we rely on the backend to get the header name to be used for |
| 2922 | * x-forwarded-for, because the header is really meant for the backends. |
| 2923 | * However, if the backend did not specify any option, we have to rely |
| 2924 | * on the frontend's header name. |
| 2925 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2926 | if (s->be->fwdfor_hdr_len) { |
| 2927 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2928 | memcpy(trash.area, |
| 2929 | s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2930 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2931 | len = sess->fe->fwdfor_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2932 | memcpy(trash.area, |
| 2933 | sess->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 2934 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2935 | len += snprintf(trash.area + len, |
| 2936 | trash.size - len, |
| 2937 | ": %d.%d.%d.%d", pn[0], pn[1], |
| 2938 | pn[2], pn[3]); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 2939 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2940 | 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] | 2941 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2942 | } |
| 2943 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2944 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2945 | /* FIXME: for the sake of completeness, we should also support |
| 2946 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2947 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2948 | int len; |
| 2949 | char pn[INET6_ADDRSTRLEN]; |
| 2950 | inet_ntop(AF_INET6, |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2951 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2952 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2953 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2954 | /* Note: we rely on the backend to get the header name to be used for |
| 2955 | * x-forwarded-for, because the header is really meant for the backends. |
| 2956 | * However, if the backend did not specify any option, we have to rely |
| 2957 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2958 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2959 | if (s->be->fwdfor_hdr_len) { |
| 2960 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2961 | memcpy(trash.area, s->be->fwdfor_hdr_name, |
| 2962 | len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2963 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2964 | len = sess->fe->fwdfor_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2965 | memcpy(trash.area, sess->fe->fwdfor_hdr_name, |
| 2966 | len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2967 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2968 | len += snprintf(trash.area + len, trash.size - len, |
| 2969 | ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2970 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2971 | 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] | 2972 | goto return_bad_req; |
| 2973 | } |
| 2974 | } |
| 2975 | |
| 2976 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 2977 | * 10: add X-Original-To if either the frontend or the backend |
| 2978 | * asks for it. |
| 2979 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2980 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 2981 | |
| 2982 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2983 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 2984 | /* Add an X-Original-To header unless the destination IP is |
| 2985 | * in the 'except' network range. |
| 2986 | */ |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2987 | conn_get_to_addr(cli_conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 2988 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2989 | if (cli_conn->addr.to.ss_family == AF_INET && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2990 | ((!sess->fe->except_mask_to.s_addr || |
| 2991 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 2992 | != sess->fe->except_to.s_addr) && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 2993 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2994 | (((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] | 2995 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 2996 | int len; |
| 2997 | unsigned char *pn; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2998 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 2999 | |
| 3000 | /* Note: we rely on the backend to get the header name to be used for |
| 3001 | * x-original-to, because the header is really meant for the backends. |
| 3002 | * However, if the backend did not specify any option, we have to rely |
| 3003 | * on the frontend's header name. |
| 3004 | */ |
| 3005 | if (s->be->orgto_hdr_len) { |
| 3006 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3007 | memcpy(trash.area, |
| 3008 | s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3009 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3010 | len = sess->fe->orgto_hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3011 | memcpy(trash.area, |
| 3012 | sess->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3013 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3014 | len += snprintf(trash.area + len, |
| 3015 | trash.size - len, |
| 3016 | ": %d.%d.%d.%d", pn[0], pn[1], |
| 3017 | pn[2], pn[3]); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3018 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3019 | 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] | 3020 | goto return_bad_req; |
| 3021 | } |
| 3022 | } |
| 3023 | } |
| 3024 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3025 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 3026 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 3027 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 3028 | * "Upgrade" is present in the Connection header. |
| 3029 | */ |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 3030 | 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] | 3031 | unsigned int want_flags = 0; |
| 3032 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3033 | if (msg->flags & HTTP_MSGF_VER_11) { |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 3034 | if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3035 | !((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3036 | want_flags |= TX_CON_CLO_SET; |
| 3037 | } else { |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 3038 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3039 | ((sess->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3040 | want_flags |= TX_CON_KAL_SET; |
| 3041 | } |
| 3042 | |
| 3043 | 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] | 3044 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3045 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3046 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3047 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3048 | /* If we have no server assigned yet and we're balancing on url_param |
| 3049 | * with a POST request, we may be interested in checking the body for |
| 3050 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3051 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3052 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3053 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3054 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3055 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3056 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3057 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3058 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3059 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 3060 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 3061 | |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3062 | /* We expect some data from the client. Unless we know for sure |
| 3063 | * we already have a full request, we have to re-enable quick-ack |
| 3064 | * in case we previously disabled it, otherwise we might cause |
| 3065 | * the client to delay further data. |
| 3066 | */ |
| 3067 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
Christopher Faulet | be821b9 | 2017-03-30 11:21:53 +0200 | [diff] [blame] | 3068 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3069 | (msg->body_len > ci_data(req) - txn->req.eoh - 2))) |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 3070 | conn_set_quickack(cli_conn, 1); |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3071 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3072 | /************************************************************* |
| 3073 | * OK, that's finished for the headers. We have done what we * |
| 3074 | * could. Let's switch to the DATA state. * |
| 3075 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3076 | req->analyse_exp = TICK_ETERNITY; |
| 3077 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3078 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3079 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3080 | /* OK let's go on with the BODY now */ |
| 3081 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3082 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3083 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3084 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3085 | /* we detected a parsing error. We want to archive this request |
| 3086 | * in the dedicated proxy area for later troubleshooting. |
| 3087 | */ |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 3088 | 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] | 3089 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3090 | |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3091 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3092 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3093 | txn->status = 400; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3094 | req->analysers &= AN_REQ_FLT_END; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3095 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3096 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3097 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3098 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3099 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3100 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3101 | if (!(s->flags & SF_ERR_MASK)) |
| 3102 | s->flags |= SF_ERR_PRXCOND; |
| 3103 | if (!(s->flags & SF_FINST_MASK)) |
| 3104 | s->flags |= SF_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3105 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3106 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3107 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3108 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3109 | * returns zero, at the beginning because it prevents any other processing |
| 3110 | * from occurring, and at the end because it terminates the request. |
| 3111 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3112 | 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] | 3113 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3114 | struct http_txn *txn = s->txn; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3115 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 3116 | if (IS_HTX_STRM(s)) |
| 3117 | return htx_process_tarpit(s, req, an_bit); |
| 3118 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3119 | /* This connection is being tarpitted. The CLIENT side has |
| 3120 | * already set the connect expiration date to the right |
| 3121 | * timeout. We just have to check that the client is still |
| 3122 | * there and that the timeout has not expired. |
| 3123 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3124 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3125 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3126 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3127 | return 0; |
| 3128 | |
| 3129 | /* We will set the queue timer to the time spent, just for |
| 3130 | * logging purposes. We fake a 500 server error, so that the |
| 3131 | * attacker will not suspect his connection has been tarpitted. |
| 3132 | * It will not cause trouble to the logs because we can exclude |
| 3133 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3134 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3135 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3136 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3137 | if (!(req->flags & CF_READ_ERROR)) |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3138 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3139 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3140 | req->analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3141 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3142 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3143 | if (!(s->flags & SF_ERR_MASK)) |
| 3144 | s->flags |= SF_ERR_PRXCOND; |
| 3145 | if (!(s->flags & SF_FINST_MASK)) |
| 3146 | s->flags |= SF_FINST_T; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3147 | return 0; |
| 3148 | } |
| 3149 | |
Willy Tarreau | 5a8f947 | 2014-04-10 11:16:06 +0200 | [diff] [blame] | 3150 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 3151 | * for either the buffer to be full, or the full advertised contents to have |
| 3152 | * reached the buffer. It must only be called after the standard HTTP request |
| 3153 | * processing has occurred, because it expects the request to be parsed and will |
| 3154 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 3155 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 3156 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 3157 | * 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] | 3158 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3159 | 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] | 3160 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3161 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3162 | struct http_txn *txn = s->txn; |
| 3163 | struct http_msg *msg = &s->txn->req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3164 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 3165 | if (IS_HTX_STRM(s)) |
| 3166 | return htx_wait_for_request_body(s, req, an_bit); |
| 3167 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3168 | /* We have to parse the HTTP request body to find any required data. |
| 3169 | * "balance url_param check_post" should have been the only way to get |
| 3170 | * into this. We were brought here after HTTP header analysis, so all |
| 3171 | * related structures are ready. |
| 3172 | */ |
| 3173 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3174 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 3175 | /* This is the first call */ |
| 3176 | if (msg->msg_state < HTTP_MSG_BODY) |
| 3177 | goto missing_data; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3178 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3179 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3180 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3181 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3182 | */ |
| 3183 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 3184 | struct hdr_ctx ctx; |
| 3185 | ctx.idx = 0; |
| 3186 | /* Expect is allowed in 1.1, look for it */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3187 | 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] | 3188 | 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] | 3189 | co_inject(&s->res, HTTP_100.ptr, HTTP_100.len); |
Thierry FOURNIER / OZON.IO | 43ad11d | 2016-12-12 15:19:58 +0100 | [diff] [blame] | 3190 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3191 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3192 | } |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3193 | msg->msg_state = HTTP_MSG_100_SENT; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3194 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3195 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 3196 | /* 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] | 3197 | * ci_head(req) still points to the beginning of the message. We |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3198 | * must save the body in msg->next because it survives buffer |
| 3199 | * re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3200 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 3201 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3202 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3203 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3204 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3205 | else |
| 3206 | msg->msg_state = HTTP_MSG_DATA; |
| 3207 | } |
| 3208 | |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3209 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 3210 | /* 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] | 3211 | if (http_body_bytes(msg) < msg->body_len) |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3212 | goto missing_data; |
| 3213 | |
| 3214 | /* OK we have everything we need now */ |
| 3215 | goto http_end; |
| 3216 | } |
| 3217 | |
| 3218 | /* OK here we're parsing a chunked-encoded message */ |
| 3219 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3220 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3221 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3222 | * 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] | 3223 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3224 | */ |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 3225 | unsigned int chunk; |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3226 | 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] | 3227 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3228 | if (!ret) |
| 3229 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3230 | else if (ret < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3231 | msg->err_pos = ci_data(req) + ret; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 3232 | if (msg->err_pos < 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3233 | msg->err_pos += req->buf.size; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3234 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3235 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3236 | } |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 3237 | |
| 3238 | msg->chunk_len = chunk; |
| 3239 | msg->body_len += chunk; |
| 3240 | |
| 3241 | msg->sol = ret; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 3242 | msg->next += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3243 | msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3244 | } |
| 3245 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3246 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 3247 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 3248 | * for at least a whole chunk or the whole content length bytes after |
| 3249 | * msg->sov + msg->sol. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3250 | */ |
Willy Tarreau | 890988f | 2014-04-10 11:59:33 +0200 | [diff] [blame] | 3251 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 3252 | goto http_end; |
| 3253 | |
Willy Tarreau | e115b49 | 2015-05-01 23:05:14 +0200 | [diff] [blame] | 3254 | 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] | 3255 | goto http_end; |
| 3256 | |
| 3257 | missing_data: |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 3258 | /* we get here if we need to wait for more data. If the buffer is full, |
| 3259 | * we have the maximum we can expect. |
| 3260 | */ |
Willy Tarreau | 2375233 | 2018-06-15 14:54:53 +0200 | [diff] [blame] | 3261 | if (channel_full(req, global.tune.maxrewrite)) |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 3262 | goto http_end; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3263 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3264 | 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] | 3265 | txn->status = 408; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3266 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3267 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3268 | if (!(s->flags & SF_ERR_MASK)) |
| 3269 | s->flags |= SF_ERR_CLITO; |
| 3270 | if (!(s->flags & SF_FINST_MASK)) |
| 3271 | s->flags |= SF_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3272 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3273 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3274 | |
| 3275 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 31a1995 | 2014-04-10 11:50:37 +0200 | [diff] [blame] | 3276 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3277 | /* Not enough data. We'll re-use the http-request |
| 3278 | * timeout here. Ideally, we should set the timeout |
| 3279 | * relative to the accept() date. We just set the |
| 3280 | * request timeout once at the beginning of the |
| 3281 | * request. |
| 3282 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3283 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3284 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3285 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3286 | return 0; |
| 3287 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3288 | |
| 3289 | http_end: |
| 3290 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3291 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3292 | req->analysers &= ~an_bit; |
| 3293 | req->analyse_exp = TICK_ETERNITY; |
| 3294 | return 1; |
| 3295 | |
| 3296 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3297 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3298 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3299 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 3300 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3301 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3302 | if (!(s->flags & SF_ERR_MASK)) |
| 3303 | s->flags |= SF_ERR_PRXCOND; |
| 3304 | if (!(s->flags & SF_FINST_MASK)) |
| 3305 | s->flags |= SF_FINST_R; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3306 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3307 | return_err_msg: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3308 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3309 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3310 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 3311 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3312 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3313 | } |
| 3314 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3315 | /* send a server's name with an outgoing request over an established connection. |
| 3316 | * Note: this function is designed to be called once the request has been scheduled |
| 3317 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 3318 | * proceeding. |
| 3319 | */ |
Christopher Faulet | 27a3dc8 | 2018-10-23 15:34:07 +0200 | [diff] [blame] | 3320 | 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] | 3321 | |
| 3322 | struct hdr_ctx ctx; |
Christopher Faulet | 27a3dc8 | 2018-10-23 15:34:07 +0200 | [diff] [blame] | 3323 | struct http_txn *txn = s->txn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3324 | char *hdr_name = be->server_id_hdr_name; |
| 3325 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 3326 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3327 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3328 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3329 | |
Christopher Faulet | 64159df | 2018-10-24 21:15:35 +0200 | [diff] [blame] | 3330 | if (IS_HTX_STRM(s)) |
| 3331 | return htx_send_name_header(s, be, srv_name); |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 3332 | ctx.idx = 0; |
| 3333 | |
Willy Tarreau | 211cdec | 2014-04-17 20:18:08 +0200 | [diff] [blame] | 3334 | old_o = http_hdr_rewind(&txn->req); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3335 | if (old_o) { |
| 3336 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3337 | c_rew(chn, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3338 | txn->req.next += old_o; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 3339 | txn->req.sov += old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3340 | } |
| 3341 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3342 | old_i = ci_data(chn); |
| 3343 | 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] | 3344 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3345 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3346 | } |
| 3347 | |
| 3348 | /* Add the new header requested with the server value */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3349 | hdr_val = trash.area; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3350 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 3351 | hdr_val += hdr_name_len; |
| 3352 | *hdr_val++ = ':'; |
| 3353 | *hdr_val++ = ' '; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3354 | hdr_val += strlcpy2(hdr_val, srv_name, |
| 3355 | trash.area + trash.size - hdr_val); |
| 3356 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, |
| 3357 | hdr_val - trash.area); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3358 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3359 | if (old_o) { |
| 3360 | /* If this was a forwarded request, we must readjust the amount of |
| 3361 | * data to be forwarded in order to take into account the size |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3362 | * variations. Note that the current state is >= HTTP_MSG_BODY, |
| 3363 | * so we don't have to adjust ->sol. |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3364 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3365 | old_o += ci_data(chn) - old_i; |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3366 | c_adv(chn, old_o); |
Willy Tarreau | 877e78d | 2013-04-07 18:48:08 +0200 | [diff] [blame] | 3367 | txn->req.next -= old_o; |
| 3368 | txn->req.sov -= old_o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3369 | } |
| 3370 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3371 | return 0; |
| 3372 | } |
| 3373 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3374 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3375 | * right now but it works. |
| 3376 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3377 | void http_end_txn_clean_session(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3378 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3379 | int prev_status = s->txn->status; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 3380 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 3381 | struct proxy *be = s->be; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 3382 | struct conn_stream *cs; |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 3383 | struct connection *srv_conn; |
| 3384 | struct server *srv; |
Willy Tarreau | 449d74a | 2015-08-05 17:16:33 +0200 | [diff] [blame] | 3385 | unsigned int prev_flags = s->txn->flags; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 3386 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3387 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3388 | * server's connections. We need a safer way to reinitialize buffer |
| 3389 | * flags. We also need a more accurate method for computing per-request |
| 3390 | * data. |
| 3391 | */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 3392 | cs = objt_cs(s->si[1].end); |
| 3393 | srv_conn = cs_conn(cs); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3394 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3395 | /* unless we're doing keep-alive, we want to quickly close the connection |
| 3396 | * to the server. |
| 3397 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3398 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 3399 | !si_conn_ready(&s->si[1]) || !srv_conn->owner) { |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3400 | s->si[1].flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
| 3401 | si_shutr(&s->si[1]); |
| 3402 | si_shutw(&s->si[1]); |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3403 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3404 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3405 | if (s->flags & SF_BE_ASSIGNED) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3406 | HA_ATOMIC_SUB(&be->beconn, 1); |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 3407 | if (unlikely(s->srv_conn)) |
| 3408 | sess_change_server(s, NULL); |
| 3409 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3410 | |
| 3411 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3412 | stream_process_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3413 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3414 | if (s->txn->status) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3415 | int n; |
| 3416 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3417 | n = s->txn->status / 100; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3418 | if (n < 1 || n > 5) |
| 3419 | n = 0; |
| 3420 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3421 | if (fe->mode == PR_MODE_HTTP) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3422 | HA_ATOMIC_ADD(&fe->fe_counters.p.http.rsp[n], 1); |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 3423 | } |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3424 | if ((s->flags & SF_BE_ASSIGNED) && |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 3425 | (be->mode == PR_MODE_HTTP)) { |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3426 | HA_ATOMIC_ADD(&be->be_counters.p.http.rsp[n], 1); |
| 3427 | HA_ATOMIC_ADD(&be->be_counters.p.http.cum_req, 1); |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 3428 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3429 | } |
| 3430 | |
| 3431 | /* don't count other requests' data */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3432 | s->logs.bytes_in -= ci_data(&s->req); |
| 3433 | s->logs.bytes_out -= ci_data(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3434 | |
Willy Tarreau | 66425e3 | 2018-07-25 06:55:12 +0200 | [diff] [blame] | 3435 | /* we may need to know the position in the queue */ |
| 3436 | pendconn_free(s); |
| 3437 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3438 | /* let's do a final log if we need it */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3439 | if (!LIST_ISEMPTY(&fe->logformat) && s->logs.logwait && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3440 | !(s->flags & SF_MONITOR) && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3441 | (!(fe->options & PR_O_NULLNOLOG) || s->req.total)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3442 | s->do_log(s); |
| 3443 | } |
| 3444 | |
Willy Tarreau | d713bcc | 2014-06-25 15:36:04 +0200 | [diff] [blame] | 3445 | /* stop tracking content-based counters */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3446 | stream_stop_content_counters(s); |
| 3447 | stream_update_time_stats(s); |
Willy Tarreau | 4bfc580 | 2014-06-17 12:19:18 +0200 | [diff] [blame] | 3448 | |
Willy Tarreau | 9efd745 | 2018-05-31 14:48:54 +0200 | [diff] [blame] | 3449 | /* reset the profiling counter */ |
| 3450 | s->task->calls = 0; |
| 3451 | s->task->cpu_time = 0; |
| 3452 | s->task->lat_time = 0; |
| 3453 | s->task->call_date = (profiling & HA_PROF_TASKS) ? now_mono_time() : 0; |
| 3454 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3455 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3456 | s->logs.tv_accept = now; /* corrected date for internal use */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 3457 | s->logs.t_handshake = 0; /* There are no handshake in keep alive connection. */ |
| 3458 | s->logs.t_idle = -1; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3459 | tv_zero(&s->logs.tv_request); |
| 3460 | s->logs.t_queue = -1; |
| 3461 | s->logs.t_connect = -1; |
| 3462 | s->logs.t_data = -1; |
| 3463 | s->logs.t_close = 0; |
Patrick Hemmer | ffe5e8c | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 3464 | s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */ |
| 3465 | s->logs.srv_queue_pos = 0; /* we will get this number soon */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3466 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3467 | s->logs.bytes_in = s->req.total = ci_data(&s->req); |
| 3468 | s->logs.bytes_out = s->res.total = ci_data(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3469 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3470 | if (objt_server(s->target)) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 3471 | if (s->flags & SF_CURR_SESS) { |
| 3472 | s->flags &= ~SF_CURR_SESS; |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 3473 | HA_ATOMIC_SUB(&__objt_server(s->target)->cur_sess, 1); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3474 | } |
Willy Tarreau | 858b103 | 2015-12-07 17:04:59 +0100 | [diff] [blame] | 3475 | if (may_dequeue_tasks(objt_server(s->target), be)) |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3476 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3477 | } |
| 3478 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3479 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3480 | |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 3481 | |
| 3482 | /* If we're doing keepalive, first call the mux detach() method |
| 3483 | * to let it know we want to detach without freing the connection. |
| 3484 | * We then can call si_release_endpoint() to destroy the conn_stream |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3485 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3486 | if (((s->txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) || |
Olivier Houchard | a70e176 | 2018-12-13 18:01:00 +0100 | [diff] [blame] | 3487 | !si_conn_ready(&s->si[1]) || |
| 3488 | (srv_conn && srv_conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 3489 | srv_conn = NULL; |
Olivier Houchard | 985f139 | 2018-11-30 17:31:52 +0100 | [diff] [blame] | 3490 | else if (!srv_conn->owner) { |
| 3491 | srv_conn->owner = s->sess; |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3492 | /* Add it unconditionally to the session list, it'll be removed |
| 3493 | * later if needed by session_check_idle_conn(), once we'll |
| 3494 | * have released the endpoint and know if it no longer has |
| 3495 | * attached streams, and so an idling connection |
| 3496 | */ |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3497 | if (!session_add_conn(s->sess, srv_conn, s->target)) { |
| 3498 | srv_conn->owner = NULL; |
| 3499 | /* Try to add the connection to the server idle list. |
| 3500 | * If it fails, as the connection no longer has an |
| 3501 | * owner, it will be destroy later by |
| 3502 | * si_release_endpoint(), anyway |
| 3503 | */ |
| 3504 | srv_add_to_idle_list(objt_server(srv_conn->target), srv_conn); |
| 3505 | srv_conn = NULL; |
| 3506 | |
| 3507 | } |
Olivier Houchard | 985f139 | 2018-11-30 17:31:52 +0100 | [diff] [blame] | 3508 | } |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 3509 | si_release_endpoint(&s->si[1]); |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3510 | if (srv_conn && srv_conn->owner == s->sess) { |
| 3511 | if (session_check_idle_conn(s->sess, srv_conn) != 0) |
| 3512 | srv_conn = NULL; |
| 3513 | } |
| 3514 | |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3515 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3516 | s->si[1].state = s->si[1].prev_state = SI_ST_INI; |
| 3517 | s->si[1].err_type = SI_ET_NONE; |
| 3518 | s->si[1].conn_retries = 0; /* used for logging too */ |
| 3519 | s->si[1].exp = TICK_ETERNITY; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3520 | 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] | 3521 | 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] | 3522 | 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] | 3523 | s->flags &= ~(SF_DIRECT|SF_ASSIGNED|SF_ADDR_SET|SF_BE_ASSIGNED|SF_FORCE_PRST|SF_IGNORE_PRST); |
| 3524 | s->flags &= ~(SF_CURR_SESS|SF_REDIRECTABLE|SF_SRV_REUSED); |
| 3525 | s->flags &= ~(SF_ERR_MASK|SF_FINST_MASK|SF_REDISP); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 3526 | |
Patrick Hemmer | e3faf02 | 2018-08-22 10:02:00 -0400 | [diff] [blame] | 3527 | hlua_ctx_destroy(s->hlua); |
| 3528 | s->hlua = NULL; |
| 3529 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3530 | s->txn->meth = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3531 | http_reset_txn(s); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3532 | s->txn->flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 3533 | |
| 3534 | if (prev_status == 401 || prev_status == 407) { |
| 3535 | /* In HTTP keep-alive mode, if we receive a 401, we still have |
| 3536 | * a chance of being able to send the visitor again to the same |
| 3537 | * server over the same connection. This is required by some |
| 3538 | * broken protocols such as NTLM, and anyway whenever there is |
| 3539 | * an opportunity for sending the challenge to the proper place, |
Lukas Tribus | 80512b1 | 2018-10-27 20:07:40 +0200 | [diff] [blame] | 3540 | * it's better to do it (at least it helps with debugging), at |
| 3541 | * least for non-deterministic load balancing algorithms. |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 3542 | */ |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3543 | s->txn->flags |= TX_PREFER_LAST; |
Willy Tarreau | 068621e | 2013-12-23 15:11:25 +0100 | [diff] [blame] | 3544 | } |
| 3545 | |
Willy Tarreau | 53f9685 | 2016-02-02 18:50:47 +0100 | [diff] [blame] | 3546 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
| 3547 | if (srv_conn && (be->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
| 3548 | srv_conn->flags |= CO_FL_PRIVATE; |
| 3549 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3550 | if (fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3551 | s->si[1].flags |= SI_FL_INDEP_STR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3552 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 3553 | if (fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3554 | s->req.flags |= CF_NEVER_WAIT; |
| 3555 | s->res.flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 3556 | } |
| 3557 | |
Willy Tarreau | 714ea78 | 2015-11-25 20:11:11 +0100 | [diff] [blame] | 3558 | /* we're removing the analysers, we MUST re-enable events detection. |
| 3559 | * We don't enable close on the response channel since it's either |
| 3560 | * already closed, or in keep-alive with an idle connection handler. |
| 3561 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3562 | channel_auto_read(&s->req); |
| 3563 | channel_auto_close(&s->req); |
| 3564 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3565 | |
Willy Tarreau | 1c59bd5 | 2015-11-02 20:20:11 +0100 | [diff] [blame] | 3566 | /* we're in keep-alive with an idle connection, monitor it if not already done */ |
| 3567 | if (srv_conn && LIST_ISEMPTY(&srv_conn->list)) { |
Willy Tarreau | 323a2d9 | 2015-08-04 19:00:17 +0200 | [diff] [blame] | 3568 | srv = objt_server(srv_conn->target); |
Olivier Houchard | 7c6f8b1 | 2018-11-13 16:48:36 +0100 | [diff] [blame] | 3569 | if (srv) { |
| 3570 | if (srv_conn->flags & CO_FL_PRIVATE) |
| 3571 | LIST_ADD(&srv->priv_conns[tid], &srv_conn->list); |
| 3572 | else if (prev_flags & TX_NOT_FIRST) |
| 3573 | /* note: we check the request, not the connection, but |
| 3574 | * this is valid for strategies SAFE and AGGR, and in |
| 3575 | * case of ALWS, we don't care anyway. |
| 3576 | */ |
| 3577 | LIST_ADD(&srv->safe_conns[tid], &srv_conn->list); |
| 3578 | else |
| 3579 | LIST_ADD(&srv->idle_conns[tid], &srv_conn->list); |
| 3580 | } |
Willy Tarreau | 4320eaa | 2015-08-05 11:08:30 +0200 | [diff] [blame] | 3581 | } |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 3582 | s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0; |
| 3583 | s->res.analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | |
| 3587 | /* This function updates the request state machine according to the response |
| 3588 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3589 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3590 | * it is only used to find when a request/response couple is complete. Both |
| 3591 | * this function and its equivalent should loop until both return zero. It |
| 3592 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3593 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3594 | int http_sync_req_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3595 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3596 | struct channel *chn = &s->req; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3597 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3598 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3599 | unsigned int old_state = txn->req.msg_state; |
| 3600 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 3601 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3602 | return 0; |
| 3603 | |
| 3604 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3605 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 3606 | * We can shut the read side unless we want to abort_on_close, |
| 3607 | * or we have a POST request. The issue with POST requests is |
| 3608 | * that some browsers still send a CRLF after the request, and |
| 3609 | * this CRLF must be read so that it does not remain in the kernel |
| 3610 | * buffers, otherwise a close could cause an RST on some systems |
| 3611 | * (eg: Linux). |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 3612 | * Note that if we're using keep-alive on the client side, we'd |
| 3613 | * rather poll now and keep the polling enabled for the whole |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3614 | * stream's life than enabling/disabling it between each |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 3615 | * response and next request. |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3616 | */ |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 3617 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 3618 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 3619 | (!(s->be->options & PR_O_ABRT_CLOSE) || |
| 3620 | (s->si[0].flags & SI_FL_CLEAN_ABRT)) && |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 3621 | txn->meth != HTTP_METH_POST) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3622 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3623 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 3624 | /* if the server closes the connection, we want to immediately react |
| 3625 | * and close the socket to save packets and syscalls. |
| 3626 | */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 3627 | s->si[1].flags |= SI_FL_NOHALF; |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 3628 | |
Willy Tarreau | 7f876a1 | 2015-11-18 11:59:55 +0100 | [diff] [blame] | 3629 | /* In any case we've finished parsing the request so we must |
| 3630 | * disable Nagle when sending data because 1) we're not going |
| 3631 | * to shut this side, and 2) the server is waiting for us to |
| 3632 | * send pending data. |
| 3633 | */ |
| 3634 | chn->flags |= CF_NEVER_WAIT; |
| 3635 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3636 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 3637 | goto wait_other_side; |
| 3638 | |
| 3639 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 3640 | /* The server has not finished to respond, so we |
| 3641 | * don't want to move in order not to upset it. |
| 3642 | */ |
| 3643 | goto wait_other_side; |
| 3644 | } |
| 3645 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3646 | /* When we get here, it means that both the request and the |
| 3647 | * response have finished receiving. Depending on the connection |
| 3648 | * mode, we'll have to wait for the last bytes to leave in either |
| 3649 | * direction, and sometimes for a close to be effective. |
| 3650 | */ |
| 3651 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3652 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3653 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3654 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 3655 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3656 | } |
| 3657 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3658 | /* Option forceclose is set, or either side wants to close, |
| 3659 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3660 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3661 | * once both states are CLOSED. |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 3662 | * |
| 3663 | * However, there is an exception if the response |
| 3664 | * length is undefined. In this case, we need to wait |
| 3665 | * the close from the server. The response will be |
| 3666 | * switched in TUNNEL mode until the end. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3667 | */ |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 3668 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 3669 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
| 3670 | goto check_channel_flags; |
| 3671 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3672 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 3673 | channel_shutr_now(chn); |
| 3674 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3675 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3676 | } |
| 3677 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3678 | /* The last possible modes are keep-alive and tunnel. Tunnel mode |
| 3679 | * will not have any analyser so it needs to poll for reads. |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3680 | */ |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3681 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 3682 | channel_auto_read(chn); |
| 3683 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3684 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3685 | } |
| 3686 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 3687 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3688 | } |
| 3689 | |
| 3690 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 3691 | http_msg_closing: |
| 3692 | /* nothing else to forward, just waiting for the output buffer |
| 3693 | * to be empty and for the shutw_now to take effect. |
| 3694 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3695 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3696 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3697 | goto http_msg_closed; |
| 3698 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3699 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3700 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3701 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3702 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 3703 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3704 | } |
| 3705 | |
| 3706 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 3707 | http_msg_closed: |
Willy Tarreau | 8059351 | 2017-12-14 10:43:31 +0100 | [diff] [blame] | 3708 | /* if we don't know whether the server will close, we need to hard close */ |
| 3709 | if (txn->rsp.flags & HTTP_MSGF_XFER_LEN) |
| 3710 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 3711 | |
Willy Tarreau | 3988d93 | 2013-12-27 23:03:08 +0100 | [diff] [blame] | 3712 | /* see above in MSG_DONE why we only do this in these states */ |
| 3713 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) && |
| 3714 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) && |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 3715 | (!(s->be->options & PR_O_ABRT_CLOSE) || |
| 3716 | (s->si[0].flags & SI_FL_CLEAN_ABRT))) |
Willy Tarreau | 2e7a165 | 2013-12-15 15:32:10 +0100 | [diff] [blame] | 3717 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3718 | goto wait_other_side; |
| 3719 | } |
| 3720 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 3721 | check_channel_flags: |
| 3722 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 3723 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 3724 | /* if we've just closed an output, let's switch */ |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 3725 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 3726 | goto http_msg_closing; |
| 3727 | } |
| 3728 | |
| 3729 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3730 | wait_other_side: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3731 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3732 | } |
| 3733 | |
| 3734 | |
| 3735 | /* This function updates the response state machine according to the request |
| 3736 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3737 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3738 | * it is only used to find when a request/response couple is complete. Both |
| 3739 | * this function and its equivalent should loop until both return zero. It |
| 3740 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3741 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3742 | int http_sync_res_state(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3743 | { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3744 | struct channel *chn = &s->res; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3745 | struct http_txn *txn = s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3746 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3747 | unsigned int old_state = txn->rsp.msg_state; |
| 3748 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 3749 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3750 | return 0; |
| 3751 | |
| 3752 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 3753 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3754 | * still monitor the server connection for a possible close |
| 3755 | * while the request is being uploaded, so we don't disable |
| 3756 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3757 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3758 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3759 | |
| 3760 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 3761 | goto wait_other_side; |
| 3762 | |
| 3763 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 3764 | /* The client seems to still be sending data, probably |
| 3765 | * because we got an error response during an upload. |
| 3766 | * We have the choice of either breaking the connection |
| 3767 | * or letting it pass through. Let's do the later. |
| 3768 | */ |
| 3769 | goto wait_other_side; |
| 3770 | } |
| 3771 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3772 | /* When we get here, it means that both the request and the |
| 3773 | * response have finished receiving. Depending on the connection |
| 3774 | * mode, we'll have to wait for the last bytes to leave in either |
| 3775 | * direction, and sometimes for a close to be effective. |
| 3776 | */ |
| 3777 | |
| 3778 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3779 | /* Server-close mode : shut read and wait for the request |
| 3780 | * side to close its output buffer. The caller will detect |
| 3781 | * when we're in DONE and the other is in CLOSED and will |
| 3782 | * catch that for the final cleanup. |
| 3783 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3784 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 3785 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3786 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3787 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3788 | /* Option forceclose is set, or either side wants to close, |
| 3789 | * let's enforce it now that we're not expecting any new |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3790 | * data to come. The caller knows the stream is complete |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3791 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3792 | */ |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 3793 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3794 | channel_shutr_now(chn); |
| 3795 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3796 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3797 | } |
| 3798 | else { |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3799 | /* The last possible modes are keep-alive and tunnel. Tunnel will |
| 3800 | * need to forward remaining data. Keep-alive will need to monitor |
| 3801 | * for connection closing. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3802 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3803 | channel_auto_read(chn); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 3804 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3805 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
| 3806 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3807 | } |
| 3808 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 3809 | goto check_channel_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 3813 | http_msg_closing: |
| 3814 | /* nothing else to forward, just waiting for the output buffer |
| 3815 | * to be empty and for the shutw_now to take effect. |
| 3816 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3817 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3818 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 3819 | goto http_msg_closed; |
| 3820 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3821 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | a3992e0 | 2017-07-18 10:35:55 +0200 | [diff] [blame] | 3822 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3823 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 3824 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3825 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 3826 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3827 | } |
Christopher Faulet | 56d2609 | 2017-07-20 11:05:10 +0200 | [diff] [blame] | 3828 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3829 | } |
| 3830 | |
| 3831 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 3832 | http_msg_closed: |
| 3833 | /* drop any pending data */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 3834 | channel_truncate(chn); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3835 | channel_auto_close(chn); |
| 3836 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3837 | goto wait_other_side; |
| 3838 | } |
| 3839 | |
Christopher Faulet | 4be9803 | 2017-07-18 10:48:24 +0200 | [diff] [blame] | 3840 | check_channel_flags: |
| 3841 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 3842 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 3843 | /* if we've just closed an output, let's switch */ |
| 3844 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 3845 | goto http_msg_closing; |
| 3846 | } |
| 3847 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3848 | wait_other_side: |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 3849 | /* We force the response to leave immediately if we're waiting for the |
| 3850 | * other side, since there is no pending shutdown to push it out. |
| 3851 | */ |
| 3852 | if (!channel_is_empty(chn)) |
| 3853 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 3854 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3855 | } |
| 3856 | |
| 3857 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 3858 | /* Resync the request and response state machines. */ |
| 3859 | void http_resync_states(struct stream *s) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3860 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3861 | struct http_txn *txn = s->txn; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 3862 | #ifdef DEBUG_FULL |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3863 | int old_req_state = txn->req.msg_state; |
| 3864 | int old_res_state = txn->rsp.msg_state; |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 3865 | #endif |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3866 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3867 | http_sync_req_state(s); |
| 3868 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3869 | if (!http_sync_res_state(s)) |
| 3870 | break; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3871 | if (!http_sync_req_state(s)) |
| 3872 | break; |
| 3873 | } |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 3874 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 3875 | DPRINTF(stderr,"[%u] %s: stream=%p old=%s,%s cur=%s,%s " |
| 3876 | "req->analysers=0x%08x res->analysers=0x%08x\n", |
| 3877 | now_ms, __FUNCTION__, s, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 3878 | h1_msg_state_str(old_req_state), h1_msg_state_str(old_res_state), |
| 3879 | 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] | 3880 | s->req.analysers, s->res.analysers); |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 3881 | |
| 3882 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3883 | /* OK, both state machines agree on a compatible state. |
| 3884 | * There are a few cases we're interested in : |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3885 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 3886 | * directions, so let's simply disable both analysers. |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 3887 | * - HTTP_MSG_CLOSED on the response only or HTTP_MSG_ERROR on either |
| 3888 | * means we must abort the request. |
| 3889 | * - HTTP_MSG_TUNNEL on either means we have to disable analyser on |
| 3890 | * corresponding channel. |
| 3891 | * - HTTP_MSG_DONE or HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE |
| 3892 | * on the response with server-close mode means we've completed one |
| 3893 | * request and we must re-initialize the server connection. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3894 | */ |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 3895 | if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 3896 | txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3897 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3898 | channel_auto_close(&s->req); |
| 3899 | channel_auto_read(&s->req); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3900 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3901 | channel_auto_close(&s->res); |
| 3902 | channel_auto_read(&s->res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3903 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 3904 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 3905 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 3906 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3907 | s->res.analysers &= AN_RES_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3908 | channel_auto_close(&s->res); |
| 3909 | channel_auto_read(&s->res); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 3910 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 3911 | channel_abort(&s->req); |
| 3912 | channel_auto_close(&s->req); |
| 3913 | channel_auto_read(&s->req); |
| 3914 | channel_truncate(&s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3915 | } |
Christopher Faulet | f77bb53 | 2017-07-18 11:18:46 +0200 | [diff] [blame] | 3916 | else if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 3917 | txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 3918 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 3919 | s->req.analysers &= AN_REQ_FLT_END; |
| 3920 | if (HAS_REQ_DATA_FILTERS(s)) |
| 3921 | s->req.analysers |= AN_REQ_FLT_XFER_DATA; |
| 3922 | } |
| 3923 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 3924 | s->res.analysers &= AN_RES_FLT_END; |
| 3925 | if (HAS_RSP_DATA_FILTERS(s)) |
| 3926 | s->res.analysers |= AN_RES_FLT_XFER_DATA; |
| 3927 | } |
| 3928 | channel_auto_close(&s->req); |
| 3929 | channel_auto_read(&s->req); |
| 3930 | channel_auto_close(&s->res); |
| 3931 | channel_auto_read(&s->res); |
| 3932 | } |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3933 | else if ((txn->req.msg_state == HTTP_MSG_DONE || |
| 3934 | txn->req.msg_state == HTTP_MSG_CLOSED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3935 | txn->rsp.msg_state == HTTP_MSG_DONE && |
Willy Tarreau | 4213a11 | 2013-12-15 10:25:42 +0100 | [diff] [blame] | 3936 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3937 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3938 | /* server-close/keep-alive: terminate this transaction, |
| 3939 | * possibly killing the server connection and reinitialize |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 3940 | * a fresh-new transaction, but only once we're sure there's |
| 3941 | * enough room in the request and response buffer to process |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 3942 | * another request. They must not hold any pending output data |
| 3943 | * and the response buffer must realigned |
| 3944 | * (realign is done is http_end_txn_clean_session). |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3945 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3946 | if (co_data(&s->req)) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 3947 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 3948 | else if (co_data(&s->res)) |
Willy Tarreau | 3de5bd6 | 2016-05-02 16:07:07 +0200 | [diff] [blame] | 3949 | s->res.flags |= CF_WAKE_WRITE; |
Christopher Faulet | a81ff60 | 2017-07-18 22:01:05 +0200 | [diff] [blame] | 3950 | else { |
| 3951 | s->req.analysers = AN_REQ_FLT_END; |
| 3952 | s->res.analysers = AN_RES_FLT_END; |
| 3953 | txn->flags |= TX_WAIT_CLEANUP; |
| 3954 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3955 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3956 | } |
| 3957 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3958 | /* This function is an analyser which forwards request body (including chunk |
| 3959 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 3960 | * zero byte. The only situation where it must not be called is when we're in |
| 3961 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 3962 | * remaining data and to resync after end of body. It expects the msg_state to |
| 3963 | * 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] | 3964 | * 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] | 3965 | * 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] | 3966 | * bytes of pending data + the headers if not already done. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3967 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3968 | 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] | 3969 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 3970 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3971 | struct http_txn *txn = s->txn; |
| 3972 | struct http_msg *msg = &s->txn->req; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 3973 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3974 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 3975 | if (IS_HTX_STRM(s)) |
| 3976 | return htx_request_forward_body(s, req, an_bit); |
| 3977 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 3978 | 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] | 3979 | now_ms, __FUNCTION__, |
| 3980 | s, |
| 3981 | req, |
| 3982 | req->rex, req->wex, |
| 3983 | req->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 3984 | ci_data(req), |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 3985 | req->analysers); |
| 3986 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3987 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3988 | return 0; |
| 3989 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3990 | 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] | 3991 | ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 3992 | /* Output closed while we were sending data. We must abort and |
| 3993 | * wake the other side up. |
| 3994 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 3995 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 3996 | msg->msg_state = HTTP_MSG_ERROR; |
| 3997 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 3998 | return 1; |
| 3999 | } |
| 4000 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4001 | /* Note that we don't have to send 100-continue back because we don't |
| 4002 | * need the data to complete our job, and it's up to the server to |
| 4003 | * decide whether to return 100, 417 or anything else in return of |
| 4004 | * an "Expect: 100-continue" header. |
| 4005 | */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4006 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4007 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4008 | ? HTTP_MSG_CHUNK_SIZE |
| 4009 | : HTTP_MSG_DATA); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4010 | |
| 4011 | /* TODO/filters: when http-buffer-request option is set or if a |
| 4012 | * rule on url_param exists, the first chunk size could be |
| 4013 | * already parsed. In that case, msg->next is after the chunk |
| 4014 | * size (including the CRLF after the size). So this case should |
| 4015 | * be handled to */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4016 | } |
| 4017 | |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4018 | /* Some post-connect processing might want us to refrain from starting to |
| 4019 | * forward data. Currently, the only reason for this is "balance url_param" |
| 4020 | * whichs need to parse/process the request after we've enabled forwarding. |
| 4021 | */ |
| 4022 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4023 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4024 | channel_auto_connect(req); |
Willy Tarreau | 644c101 | 2014-04-30 18:11:11 +0200 | [diff] [blame] | 4025 | req->flags |= CF_WAKE_CONNECT; |
Willy Tarreau | ba20dfc | 2018-05-16 11:35:05 +0200 | [diff] [blame] | 4026 | channel_dont_close(req); /* don't fail on early shutr */ |
| 4027 | goto waiting; |
Willy Tarreau | 7ba2354 | 2014-04-17 21:50:00 +0200 | [diff] [blame] | 4028 | } |
| 4029 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 4030 | } |
| 4031 | |
Willy Tarreau | 80a92c0 | 2014-03-12 10:41:13 +0100 | [diff] [blame] | 4032 | /* in most states, we should abort in case of early close */ |
| 4033 | channel_auto_close(req); |
| 4034 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4035 | if (req->to_forward) { |
| 4036 | /* We can't process the buffer's contents yet */ |
| 4037 | req->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4038 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 4039 | } |
| 4040 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4041 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 4042 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 4043 | ? http_msg_forward_chunked_body(s, msg) |
| 4044 | : http_msg_forward_body(s, msg)); |
| 4045 | if (!ret) |
| 4046 | goto missing_data_or_waiting; |
| 4047 | if (ret < 0) |
| 4048 | goto return_bad_req; |
| 4049 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 4050 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4051 | /* other states, DONE...TUNNEL */ |
| 4052 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 4053 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4054 | channel_dont_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4055 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 4056 | http_resync_states(s); |
| 4057 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4058 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4059 | if (req->flags & CF_SHUTW) { |
| 4060 | /* request errors are most likely due to the |
| 4061 | * server aborting the transfer. */ |
| 4062 | goto aborted_xfer; |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4063 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4064 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4065 | 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] | 4066 | goto return_bad_req; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4067 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4068 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4069 | } |
| 4070 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4071 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 4072 | * the client's connection and forward any shutdown notification to the |
| 4073 | * server, which will decide whether to close or to go on processing the |
| 4074 | * request. We only do that in tunnel mode, and not in other modes since |
| 4075 | * it can be abused to exhaust source ports. */ |
Willy Tarreau | 7aa15b0 | 2017-12-20 16:56:50 +0100 | [diff] [blame] | 4076 | 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] | 4077 | channel_auto_read(req); |
| 4078 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 4079 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 4080 | s->si[1].flags |= SI_FL_NOLINGER; |
| 4081 | channel_auto_close(req); |
| 4082 | } |
| 4083 | else if (s->txn->meth == HTTP_METH_POST) { |
| 4084 | /* POST requests may require to read extra CRLF sent by broken |
| 4085 | * browsers and which could cause an RST to be sent upon close |
| 4086 | * on some systems (eg: Linux). */ |
| 4087 | channel_auto_read(req); |
| 4088 | } |
| 4089 | return 0; |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 4090 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 4091 | missing_data_or_waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4092 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 4093 | if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) { |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4094 | if (!(s->flags & SF_ERR_MASK)) |
| 4095 | s->flags |= SF_ERR_CLICL; |
| 4096 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4097 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4098 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4099 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4100 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4101 | } |
| 4102 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4103 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 4104 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4105 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4106 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4107 | |
| 4108 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4109 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4110 | |
Willy Tarreau | ba20dfc | 2018-05-16 11:35:05 +0200 | [diff] [blame] | 4111 | waiting: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4112 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4113 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4114 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4115 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4116 | /* 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] | 4117 | * 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] | 4118 | * And when content-length is used, we never want to let the possible |
| 4119 | * shutdown be forwarded to the other side, as the state machine will |
| 4120 | * take care of it once the client responds. It's also important to |
| 4121 | * prevent TIME_WAITs from accumulating on the backend side, and for |
| 4122 | * HTTP/2 where the last frame comes with a shutdown. |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4123 | */ |
Willy Tarreau | ff47b3f | 2017-12-29 16:30:31 +0100 | [diff] [blame] | 4124 | if (msg->flags & (HTTP_MSGF_TE_CHNK|HTTP_MSGF_CNT_LEN)) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4125 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4126 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4127 | /* 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] | 4128 | * 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] | 4129 | * 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] | 4130 | * modes are already handled by the stream sock layer. We must not do |
| 4131 | * this in content-length mode because it could present the MSG_MORE |
| 4132 | * flag with the last block of forwarded data, which would cause an |
| 4133 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4134 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4135 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4136 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4137 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4138 | return 0; |
| 4139 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4140 | return_bad_req: /* let's centralize all bad requests */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4141 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4142 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 4143 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Willy Tarreau | bed410e | 2014-04-22 08:19:34 +0200 | [diff] [blame] | 4144 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4145 | return_bad_req_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4146 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4147 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4148 | if (txn->status) { |
| 4149 | /* 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] | 4150 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4151 | } else { |
| 4152 | txn->status = 400; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4153 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4154 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4155 | req->analysers &= AN_REQ_FLT_END; |
| 4156 | 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] | 4157 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4158 | if (!(s->flags & SF_ERR_MASK)) |
| 4159 | s->flags |= SF_ERR_PRXCOND; |
| 4160 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4161 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4162 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4163 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4164 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4165 | } |
| 4166 | return 0; |
| 4167 | |
| 4168 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 4169 | txn->req.err_state = txn->req.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4170 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4171 | if (txn->status) { |
| 4172 | /* 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] | 4173 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4174 | } else { |
| 4175 | txn->status = 502; |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4176 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4177 | } |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4178 | req->analysers &= AN_REQ_FLT_END; |
| 4179 | 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] | 4180 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4181 | HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1); |
| 4182 | HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4183 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4184 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4185 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4186 | if (!(s->flags & SF_ERR_MASK)) |
| 4187 | s->flags |= SF_ERR_SRVCL; |
| 4188 | if (!(s->flags & SF_FINST_MASK)) { |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4189 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4190 | s->flags |= SF_FINST_H; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4191 | else |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4192 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4193 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4194 | return 0; |
| 4195 | } |
| 4196 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4197 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4198 | * processing can continue on next analysers, or zero if it either needs more |
| 4199 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4200 | * 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] | 4201 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4202 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4203 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4204 | 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] | 4205 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4206 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4207 | struct http_txn *txn = s->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4208 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4209 | struct hdr_ctx ctx; |
Lukas Tribus | fd9b68c | 2018-10-27 20:06:59 +0200 | [diff] [blame] | 4210 | struct connection *srv_conn; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4211 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4212 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4213 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4214 | |
Lukas Tribus | fd9b68c | 2018-10-27 20:06:59 +0200 | [diff] [blame] | 4215 | srv_conn = cs_conn(objt_cs(s->si[1].end)); |
| 4216 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 4217 | if (IS_HTX_STRM(s)) |
| 4218 | return htx_wait_for_response(s, rep, an_bit); |
| 4219 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 4220 | 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] | 4221 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4222 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4223 | rep, |
| 4224 | rep->rex, rep->wex, |
| 4225 | rep->flags, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4226 | ci_data(rep), |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4227 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4228 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4229 | /* |
| 4230 | * Now parse the partial (or complete) lines. |
| 4231 | * We will check the response syntax, and also join multi-line |
| 4232 | * headers. An index of all the lines will be elaborated while |
| 4233 | * parsing. |
| 4234 | * |
| 4235 | * For the parsing, we use a 28 states FSM. |
| 4236 | * |
| 4237 | * Here is the information we currently have : |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4238 | * ci_head(rep) = beginning of response |
| 4239 | * ci_head(rep) + msg->eoh = end of processed headers / start of current one |
| 4240 | * ci_tail(rep) = end of input data |
| 4241 | * msg->eol = end of current header or line (LF or CRLF) |
| 4242 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4243 | */ |
| 4244 | |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 4245 | next_one: |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4246 | /* There's a protected area at the end of the buffer for rewriting |
| 4247 | * purposes. We don't want to start to parse the request if the |
| 4248 | * protected area is affected, because we may have to move processed |
| 4249 | * data later, which is much more complicated. |
| 4250 | */ |
Willy Tarreau | d760eec | 2018-07-10 09:50:25 +0200 | [diff] [blame] | 4251 | if (c_data(rep) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | ba0902e | 2015-01-13 14:39:16 +0100 | [diff] [blame] | 4252 | if (unlikely(!channel_is_rewritable(rep))) { |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 4253 | /* some data has still not left the buffer, wake us once that's done */ |
| 4254 | if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) |
| 4255 | goto abort_response; |
| 4256 | channel_dont_close(rep); |
| 4257 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | d7ad9f5 | 2013-12-31 17:26:25 +0100 | [diff] [blame] | 4258 | rep->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 4259 | return 0; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4260 | } |
| 4261 | |
Willy Tarreau | 188e230 | 2018-06-15 11:11:53 +0200 | [diff] [blame] | 4262 | if (unlikely(ci_tail(rep) < c_ptr(rep, msg->next) || |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 4263 | ci_tail(rep) > b_wrap(&rep->buf) - global.tune.maxrewrite)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4264 | channel_slow_realign(rep, trash.area); |
Willy Tarreau | 379357a | 2013-06-08 12:55:46 +0200 | [diff] [blame] | 4265 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4266 | if (likely(msg->next < ci_data(rep))) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 4267 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4268 | } |
| 4269 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4270 | /* 1: we might have to print this header in debug mode */ |
| 4271 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4272 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 7d59e90 | 2014-10-21 19:36:09 +0200 | [diff] [blame] | 4273 | msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4274 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4275 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4276 | sol = ci_head(rep); |
| 4277 | 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] | 4278 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4279 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4280 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4281 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4282 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4283 | while (cur_idx) { |
| 4284 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4285 | debug_hdr("srvhdr", s, sol, eol); |
| 4286 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4287 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4288 | } |
| 4289 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4290 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4291 | /* |
| 4292 | * Now we quickly check if we have found a full valid response. |
| 4293 | * If not so, we check the FD and buffer states before leaving. |
| 4294 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4295 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4296 | * responses are checked first. |
| 4297 | * |
| 4298 | * Depending on whether the client is still there or not, we |
| 4299 | * may send an error response back or not. Note that normally |
| 4300 | * we should only check for HTTP status there, and check I/O |
| 4301 | * errors somewhere else. |
| 4302 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4303 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4304 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4305 | /* Invalid response */ |
| 4306 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4307 | /* we detected a parsing error. We want to archive this response |
| 4308 | * in the dedicated proxy area for later troubleshooting. |
| 4309 | */ |
| 4310 | hdr_response_bad: |
| 4311 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4312 | 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] | 4313 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4314 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4315 | if (objt_server(s->target)) { |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 4316 | HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
| 4317 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4318 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4319 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4320 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4321 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4322 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4323 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4324 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4325 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4326 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4327 | if (!(s->flags & SF_ERR_MASK)) |
| 4328 | s->flags |= SF_ERR_PRXCOND; |
| 4329 | if (!(s->flags & SF_FINST_MASK)) |
| 4330 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4331 | |
| 4332 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4333 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4334 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4335 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 2375233 | 2018-06-15 14:54:53 +0200 | [diff] [blame] | 4336 | else if (channel_full(rep, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 4337 | if (msg->err_pos < 0) |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4338 | msg->err_pos = ci_data(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4339 | goto hdr_response_bad; |
| 4340 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4341 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4342 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4343 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4344 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4345 | 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] | 4346 | else if (txn->flags & TX_NOT_FIRST) |
| 4347 | goto abort_keep_alive; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4348 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4349 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4350 | if (objt_server(s->target)) { |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 4351 | HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
| 4352 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4353 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4354 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4355 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4356 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4357 | txn->status = 502; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 4358 | |
| 4359 | /* Check to see if the server refused the early data. |
| 4360 | * If so, just send a 425 |
| 4361 | */ |
| 4362 | if (objt_cs(s->si[1].end)) { |
| 4363 | struct connection *conn = objt_cs(s->si[1].end)->conn; |
| 4364 | |
| 4365 | if (conn->err_code == CO_ER_SSL_EARLY_FAILED) |
| 4366 | txn->status = 425; |
| 4367 | } |
| 4368 | |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4369 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4370 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4371 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4372 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4373 | if (!(s->flags & SF_ERR_MASK)) |
| 4374 | s->flags |= SF_ERR_SRVCL; |
| 4375 | if (!(s->flags & SF_FINST_MASK)) |
| 4376 | s->flags |= SF_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4377 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4378 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4379 | |
Willy Tarreau | 6f0a7ba | 2014-06-23 15:22:31 +0200 | [diff] [blame] | 4380 | /* read timeout : return a 504 to the client. */ |
| 4381 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4382 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4383 | 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] | 4384 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4385 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4386 | if (objt_server(s->target)) { |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 4387 | HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
| 4388 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4389 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4390 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4391 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4392 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4393 | txn->status = 504; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4394 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4395 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4396 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4397 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4398 | if (!(s->flags & SF_ERR_MASK)) |
| 4399 | s->flags |= SF_ERR_SRVTO; |
| 4400 | if (!(s->flags & SF_FINST_MASK)) |
| 4401 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4402 | return 0; |
| 4403 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4404 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4405 | /* client abort with an abortonclose */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4406 | 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] | 4407 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 4408 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4409 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4410 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4411 | |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4412 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4413 | channel_auto_close(rep); |
| 4414 | |
| 4415 | txn->status = 400; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4416 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4417 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4418 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4419 | if (!(s->flags & SF_ERR_MASK)) |
| 4420 | s->flags |= SF_ERR_CLICL; |
| 4421 | if (!(s->flags & SF_FINST_MASK)) |
| 4422 | s->flags |= SF_FINST_H; |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4423 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4424 | /* process_stream() will take care of the error */ |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4425 | return 0; |
| 4426 | } |
| 4427 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4428 | /* 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] | 4429 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4430 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4431 | 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] | 4432 | else if (txn->flags & TX_NOT_FIRST) |
| 4433 | goto abort_keep_alive; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4434 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4435 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4436 | if (objt_server(s->target)) { |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 4437 | HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
| 4438 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4439 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4440 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4441 | channel_auto_close(rep); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4442 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4443 | txn->status = 502; |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4444 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4445 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4446 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4447 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4448 | if (!(s->flags & SF_ERR_MASK)) |
| 4449 | s->flags |= SF_ERR_SRVCL; |
| 4450 | if (!(s->flags & SF_FINST_MASK)) |
| 4451 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4452 | return 0; |
| 4453 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4454 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4455 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4456 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4457 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4458 | 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] | 4459 | else if (txn->flags & TX_NOT_FIRST) |
| 4460 | goto abort_keep_alive; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4461 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4462 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4463 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4464 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4465 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4466 | if (!(s->flags & SF_ERR_MASK)) |
| 4467 | s->flags |= SF_ERR_CLICL; |
| 4468 | if (!(s->flags & SF_FINST_MASK)) |
| 4469 | s->flags |= SF_FINST_H; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4470 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4471 | /* process_stream() will take care of the error */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4472 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4473 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4474 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4475 | channel_dont_close(rep); |
Willy Tarreau | 3f3997e | 2013-12-15 15:21:32 +0100 | [diff] [blame] | 4476 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4477 | return 0; |
| 4478 | } |
| 4479 | |
| 4480 | /* More interesting part now : we know that we have a complete |
| 4481 | * response which at least looks like HTTP. We have an indicator |
| 4482 | * of each header's length, so we can parse them quickly. |
| 4483 | */ |
| 4484 | |
| 4485 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 4486 | 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] | 4487 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4488 | /* |
| 4489 | * 1: get the status code |
| 4490 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4491 | n = ci_head(rep)[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4492 | if (n < 1 || n > 5) |
| 4493 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4494 | /* when the client triggers a 4xx from the server, it's most often due |
| 4495 | * to a missing object or permission. These events should be tracked |
| 4496 | * because if they happen often, it may indicate a brute force or a |
| 4497 | * vulnerability scan. |
| 4498 | */ |
| 4499 | if (n == 4) |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4500 | stream_inc_http_err_ctr(s); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4501 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4502 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4503 | 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] | 4504 | |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 4505 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 4506 | * exactly one digit "." one digit. This check may be disabled using |
| 4507 | * option accept-invalid-http-response. |
| 4508 | */ |
| 4509 | if (!(s->be->options2 & PR_O2_RSPBUG_OK)) { |
| 4510 | if (msg->sl.st.v_l != 8) { |
| 4511 | msg->err_pos = 0; |
| 4512 | goto hdr_response_bad; |
| 4513 | } |
| 4514 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4515 | if (ci_head(rep)[4] != '/' || |
| 4516 | !isdigit((unsigned char)ci_head(rep)[5]) || |
| 4517 | ci_head(rep)[6] != '.' || |
| 4518 | !isdigit((unsigned char)ci_head(rep)[7])) { |
Willy Tarreau | 91852eb | 2015-05-01 13:26:00 +0200 | [diff] [blame] | 4519 | msg->err_pos = 4; |
| 4520 | goto hdr_response_bad; |
| 4521 | } |
| 4522 | } |
| 4523 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4524 | /* check if the response is HTTP/1.1 or above */ |
| 4525 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4526 | ((ci_head(rep)[5] > '1') || |
| 4527 | ((ci_head(rep)[5] == '1') && (ci_head(rep)[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4528 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4529 | |
| 4530 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 4531 | 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] | 4532 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4533 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4534 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4535 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4536 | 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] | 4537 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 4538 | /* Adjust server's health based on status code. Note: status codes 501 |
| 4539 | * and 505 are triggered on demand by client request, so we must not |
| 4540 | * count them as server failures. |
| 4541 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4542 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4543 | if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505)) |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 4544 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4545 | else |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 4546 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4547 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4548 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4549 | /* |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 4550 | * We may be facing a 100-continue response, or any other informational |
| 4551 | * 1xx response which is non-final, in which case this is not the right |
| 4552 | * response, and we're waiting for the next one. Let's allow this response |
| 4553 | * to go to the client and wait for the next one. There's an exception for |
| 4554 | * 101 which is used later in the code to switch protocols. |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4555 | */ |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 4556 | if (txn->status < 200 && |
| 4557 | (txn->status == 100 || txn->status >= 102)) { |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 4558 | hdr_idx_init(&txn->hdr_idx); |
| 4559 | msg->next -= channel_forward(rep, msg->next); |
| 4560 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 4561 | txn->status = 0; |
| 4562 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 4563 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 4564 | goto next_one; |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 4565 | } |
Willy Tarreau | 628c40c | 2014-04-24 19:11:26 +0200 | [diff] [blame] | 4566 | |
Willy Tarreau | a14ad72 | 2017-07-07 11:36:32 +0200 | [diff] [blame] | 4567 | /* |
| 4568 | * 2: check for cacheability. |
| 4569 | */ |
| 4570 | |
| 4571 | switch (txn->status) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4572 | case 200: |
| 4573 | case 203: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 4574 | case 204: |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4575 | case 206: |
| 4576 | case 300: |
| 4577 | case 301: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 4578 | case 404: |
| 4579 | case 405: |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4580 | case 410: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 4581 | case 414: |
| 4582 | case 501: |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 4583 | break; |
| 4584 | default: |
Willy Tarreau | c55ddce | 2017-12-21 11:41:38 +0100 | [diff] [blame] | 4585 | /* RFC7231#6.1: |
| 4586 | * Responses with status codes that are defined as |
| 4587 | * cacheable by default (e.g., 200, 203, 204, 206, |
| 4588 | * 300, 301, 404, 405, 410, 414, and 501 in this |
| 4589 | * specification) can be reused by a cache with |
| 4590 | * heuristic expiration unless otherwise indicated |
| 4591 | * by the method definition or explicit cache |
| 4592 | * controls [RFC7234]; all other status codes are |
| 4593 | * not cacheable by default. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4594 | */ |
Willy Tarreau | 83ece46 | 2017-12-21 15:13:09 +0100 | [diff] [blame] | 4595 | txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4596 | break; |
| 4597 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4598 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4599 | /* |
| 4600 | * 3: we may need to capture headers |
| 4601 | */ |
| 4602 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 4603 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 4604 | http_capture_headers(ci_head(rep), &txn->hdr_idx, |
| 4605 | s->res_cap, sess->fe->rsp_cap); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4606 | |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4607 | /* 4: determine the transfer-length according to RFC2616 #4.4, updated |
| 4608 | * by RFC7230#3.3.3 : |
| 4609 | * |
| 4610 | * The length of a message body is determined by one of the following |
| 4611 | * (in order of precedence): |
| 4612 | * |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 4613 | * 1. Any 2xx (Successful) response to a CONNECT request implies that |
| 4614 | * the connection will become a tunnel immediately after the empty |
| 4615 | * line that concludes the header fields. A client MUST ignore |
| 4616 | * any Content-Length or Transfer-Encoding header fields received |
| 4617 | * in such a message. Any 101 response (Switching Protocols) is |
| 4618 | * managed in the same manner. |
| 4619 | * |
| 4620 | * 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] | 4621 | * (Informational), 204 (No Content), or 304 (Not Modified) status |
| 4622 | * code is always terminated by the first empty line after the |
| 4623 | * header fields, regardless of the header fields present in the |
| 4624 | * message, and thus cannot contain a message body. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4625 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4626 | * 3. If a Transfer-Encoding header field is present and the chunked |
| 4627 | * transfer coding (Section 4.1) is the final encoding, the message |
| 4628 | * body length is determined by reading and decoding the chunked |
| 4629 | * data until the transfer coding indicates the data is complete. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4630 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4631 | * If a Transfer-Encoding header field is present in a response and |
| 4632 | * the chunked transfer coding is not the final encoding, the |
| 4633 | * message body length is determined by reading the connection until |
| 4634 | * it is closed by the server. If a Transfer-Encoding header field |
| 4635 | * is present in a request and the chunked transfer coding is not |
| 4636 | * the final encoding, the message body length cannot be determined |
| 4637 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 4638 | * status code and then close the connection. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4639 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4640 | * If a message is received with both a Transfer-Encoding and a |
| 4641 | * Content-Length header field, the Transfer-Encoding overrides the |
| 4642 | * Content-Length. Such a message might indicate an attempt to |
| 4643 | * perform request smuggling (Section 9.5) or response splitting |
| 4644 | * (Section 9.4) and ought to be handled as an error. A sender MUST |
| 4645 | * remove the received Content-Length field prior to forwarding such |
| 4646 | * a message downstream. |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4647 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4648 | * 4. If a message is received without Transfer-Encoding and with |
| 4649 | * either multiple Content-Length header fields having differing |
| 4650 | * field-values or a single Content-Length header field having an |
| 4651 | * invalid value, then the message framing is invalid and the |
| 4652 | * recipient MUST treat it as an unrecoverable error. If this is a |
| 4653 | * request message, the server MUST respond with a 400 (Bad Request) |
| 4654 | * status code and then close the connection. If this is a response |
| 4655 | * message received by a proxy, the proxy MUST close the connection |
| 4656 | * to the server, discard the received response, and send a 502 (Bad |
| 4657 | * Gateway) response to the client. If this is a response message |
| 4658 | * received by a user agent, the user agent MUST close the |
| 4659 | * connection to the server and discard the received response. |
| 4660 | * |
| 4661 | * 5. If a valid Content-Length header field is present without |
| 4662 | * Transfer-Encoding, its decimal value defines the expected message |
| 4663 | * body length in octets. If the sender closes the connection or |
| 4664 | * the recipient times out before the indicated number of octets are |
| 4665 | * received, the recipient MUST consider the message to be |
| 4666 | * incomplete and close the connection. |
| 4667 | * |
| 4668 | * 6. If this is a request message and none of the above are true, then |
| 4669 | * the message body length is zero (no message body is present). |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4670 | * |
Willy Tarreau | 557f199 | 2015-05-01 10:05:17 +0200 | [diff] [blame] | 4671 | * 7. Otherwise, this is a response message without a declared message |
| 4672 | * body length, so the message body length is determined by the |
| 4673 | * number of octets received prior to the server closing the |
| 4674 | * connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4675 | */ |
| 4676 | |
| 4677 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4678 | * 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] | 4679 | * 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] | 4680 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 4681 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 4682 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 4683 | txn->status == 101)) { |
| 4684 | /* Either we've established an explicit tunnel, or we're |
| 4685 | * switching the protocol. In both cases, we're very unlikely |
| 4686 | * to understand the next protocols. We have to switch to tunnel |
| 4687 | * mode, so that we transfer the request and responses then let |
| 4688 | * this protocol pass unmodified. When we later implement specific |
| 4689 | * parsers for such protocols, we'll want to check the Upgrade |
| 4690 | * header which contains information about that protocol for |
| 4691 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 4692 | */ |
| 4693 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 4694 | msg->flags |= HTTP_MSGF_XFER_LEN; |
| 4695 | goto end; |
| 4696 | } |
| 4697 | |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4698 | if (txn->meth == HTTP_METH_HEAD || |
| 4699 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4700 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4701 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4702 | goto skip_content_length; |
| 4703 | } |
| 4704 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4705 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4706 | ctx.idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4707 | 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] | 4708 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4709 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 4710 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4711 | /* bad transfer-encoding (chunked followed by something else) */ |
| 4712 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4713 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4714 | break; |
| 4715 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4716 | } |
| 4717 | |
Willy Tarreau | 1c91391 | 2015-04-30 10:57:51 +0200 | [diff] [blame] | 4718 | /* Chunked responses must have their content-length removed */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4719 | ctx.idx = 0; |
Willy Tarreau | b4d0c03 | 2015-05-01 10:25:45 +0200 | [diff] [blame] | 4720 | if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4721 | 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] | 4722 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 4723 | } |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4724 | 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] | 4725 | signed long long cl; |
| 4726 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4727 | if (!ctx.vlen) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4728 | msg->err_pos = ctx.line + ctx.val - ci_head(rep); |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4729 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4730 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4731 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4732 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4733 | msg->err_pos = ctx.line + ctx.val - ci_head(rep); |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4734 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4735 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4736 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4737 | if (cl < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4738 | msg->err_pos = ctx.line + ctx.val - ci_head(rep); |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4739 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4740 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4741 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4742 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4743 | msg->err_pos = ctx.line + ctx.val - ci_head(rep); |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4744 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4745 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4746 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4747 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4748 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4749 | } |
| 4750 | |
Lukas Tribus | fd9b68c | 2018-10-27 20:06:59 +0200 | [diff] [blame] | 4751 | /* check for NTML authentication headers in 401 (WWW-Authenticate) and |
| 4752 | * 407 (Proxy-Authenticate) responses and set the connection to private |
| 4753 | */ |
| 4754 | if (srv_conn && txn->status == 401) { |
| 4755 | /* check for Negotiate/NTLM WWW-Authenticate headers */ |
| 4756 | ctx.idx = 0; |
| 4757 | while (http_find_header2("WWW-Authenticate", 16, ci_head(rep), &txn->hdr_idx, &ctx)) { |
| 4758 | if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) || |
| 4759 | (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4))) |
| 4760 | srv_conn->flags |= CO_FL_PRIVATE; |
| 4761 | } |
| 4762 | } else if (srv_conn && txn->status == 407) { |
| 4763 | /* check for Negotiate/NTLM Proxy-Authenticate headers */ |
| 4764 | ctx.idx = 0; |
| 4765 | while (http_find_header2("Proxy-Authenticate", 18, ci_head(rep), &txn->hdr_idx, &ctx)) { |
| 4766 | if ((ctx.vlen >= 9 && word_match(ctx.line + ctx.val, ctx.vlen, "Negotiate", 9)) || |
| 4767 | (ctx.vlen >= 4 && word_match(ctx.line + ctx.val, ctx.vlen, "NTLM", 4))) |
| 4768 | srv_conn->flags |= CO_FL_PRIVATE; |
| 4769 | } |
| 4770 | } |
| 4771 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 4772 | skip_content_length: |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4773 | /* Now we have to check if we need to modify the Connection header. |
| 4774 | * This is more difficult on the response than it is on the request, |
| 4775 | * because we can have two different HTTP versions and we don't know |
| 4776 | * how the client will interprete a response. For instance, let's say |
| 4777 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 4778 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 4779 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 4780 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 4781 | * the lack of header as a keep-alive. Thus we will use two flags |
| 4782 | * indicating how a request MAY be understood by the client. In case |
| 4783 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 4784 | * ambiguous cases such as both close and keepalive are seen, then we |
| 4785 | * will fall back to explicit close. Note that we won't take risks with |
| 4786 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4787 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4788 | */ |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 4789 | if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 4790 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4791 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4792 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4793 | /* on unknown transfer length, we must close */ |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 4794 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4795 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4796 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4797 | /* now adjust header transformations depending on current state */ |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 4798 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4799 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4800 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4801 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4802 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4803 | else { /* SCL / KAL */ |
| 4804 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4805 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4806 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4807 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4808 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4809 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4810 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4811 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4812 | /* Some keep-alive responses are converted to Server-close if |
| 4813 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4814 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4815 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 4816 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4817 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4818 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4819 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4820 | } |
| 4821 | |
Christopher Faulet | d1cd209 | 2016-11-28 10:14:03 +0100 | [diff] [blame] | 4822 | end: |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 4823 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 4824 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 7959a55 | 2013-09-23 16:44:27 +0200 | [diff] [blame] | 4825 | |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 4826 | /* end of job, return OK */ |
| 4827 | rep->analysers &= ~an_bit; |
| 4828 | rep->analyse_exp = TICK_ETERNITY; |
| 4829 | channel_auto_close(rep); |
| 4830 | return 1; |
| 4831 | |
| 4832 | abort_keep_alive: |
| 4833 | /* A keep-alive request to the server failed on a network error. |
| 4834 | * The client is required to retry. We need to close without returning |
| 4835 | * any other information so that the client retries. |
| 4836 | */ |
| 4837 | txn->status = 0; |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4838 | rep->analysers &= AN_RES_FLT_END; |
| 4839 | s->req.analysers &= AN_REQ_FLT_END; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 4840 | channel_auto_close(rep); |
| 4841 | s->logs.logwait = 0; |
| 4842 | s->logs.level = 0; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 4843 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4844 | channel_truncate(rep); |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 4845 | http_reply_and_close(s, txn->status, NULL); |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 4846 | return 0; |
| 4847 | } |
| 4848 | |
| 4849 | /* This function performs all the processing enabled for the current response. |
| 4850 | * 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] | 4851 | * 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] | 4852 | * other functions. It works like process_request (see indications above). |
| 4853 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4854 | 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] | 4855 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4856 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4857 | struct http_txn *txn = s->txn; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 4858 | struct http_msg *msg = &txn->rsp; |
| 4859 | struct proxy *cur_proxy; |
| 4860 | struct cond_wordlist *wl; |
Thierry FOURNIER | 9e2ef99 | 2015-02-25 13:51:19 +0100 | [diff] [blame] | 4861 | enum rule_result ret = HTTP_RULE_RES_CONT; |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 4862 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 4863 | if (IS_HTX_STRM(s)) |
| 4864 | return htx_process_res_common(s, rep, an_bit, px); |
| 4865 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 4866 | 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] | 4867 | now_ms, __FUNCTION__, |
| 4868 | s, |
| 4869 | rep, |
| 4870 | rep->rex, rep->wex, |
| 4871 | rep->flags, |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 4872 | ci_data(rep), |
Willy Tarreau | f118d9f | 2014-04-24 18:26:08 +0200 | [diff] [blame] | 4873 | rep->analysers); |
| 4874 | |
| 4875 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 4876 | return 0; |
| 4877 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4878 | /* The stats applet needs to adjust the Connection header but we don't |
| 4879 | * apply any filter there. |
| 4880 | */ |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 4881 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 4882 | rep->analysers &= ~an_bit; |
| 4883 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4884 | goto skip_filters; |
Willy Tarreau | 612adb8 | 2015-03-10 15:25:54 +0100 | [diff] [blame] | 4885 | } |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 4886 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4887 | /* |
| 4888 | * We will have to evaluate the filters. |
| 4889 | * As opposed to version 1.2, now they will be evaluated in the |
| 4890 | * filters order and not in the header order. This means that |
| 4891 | * each filter has to be validated among all headers. |
| 4892 | * |
| 4893 | * Filters are tried with ->be first, then with ->fe if it is |
| 4894 | * different from ->be. |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4895 | * |
| 4896 | * Maybe we are in resume condiion. In this case I choose the |
| 4897 | * "struct proxy" which contains the rule list matching the resume |
| 4898 | * pointer. If none of theses "struct proxy" match, I initialise |
| 4899 | * the process with the first one. |
| 4900 | * |
| 4901 | * In fact, I check only correspondance betwwen the current list |
| 4902 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 4903 | * the loop with the ->be. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4904 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4905 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 4906 | cur_proxy = sess->fe; |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4907 | else |
| 4908 | cur_proxy = s->be; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4909 | while (1) { |
| 4910 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4911 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4912 | /* evaluate http-response rules */ |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 4913 | if (ret == HTTP_RULE_RES_CONT) { |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4914 | 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] | 4915 | |
Willy Tarreau | 51d861a | 2015-05-22 17:30:48 +0200 | [diff] [blame] | 4916 | if (ret == HTTP_RULE_RES_BADREQ) |
| 4917 | goto return_srv_prx_502; |
| 4918 | |
| 4919 | if (ret == HTTP_RULE_RES_DONE) { |
| 4920 | rep->analysers &= ~an_bit; |
| 4921 | rep->analyse_exp = TICK_ETERNITY; |
| 4922 | return 1; |
| 4923 | } |
| 4924 | } |
| 4925 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4926 | /* we need to be called again. */ |
| 4927 | if (ret == HTTP_RULE_RES_YIELD) { |
| 4928 | channel_dont_close(rep); |
| 4929 | return 0; |
| 4930 | } |
| 4931 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4932 | /* try headers filters */ |
| 4933 | if (rule_set->rsp_exp != NULL) { |
| 4934 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 4935 | return_bad_resp: |
| 4936 | if (objt_server(s->target)) { |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 4937 | HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
| 4938 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4939 | } |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4940 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4941 | return_srv_prx_502: |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 4942 | rep->analysers &= AN_RES_FLT_END; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4943 | txn->status = 502; |
| 4944 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 4945 | s->si[1].flags |= SI_FL_NOLINGER; |
Willy Tarreau | 319f745 | 2015-01-14 20:32:59 +0100 | [diff] [blame] | 4946 | channel_truncate(rep); |
Jarno Huuskonen | 9e6906b | 2017-03-06 14:21:49 +0200 | [diff] [blame] | 4947 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 4948 | if (!(s->flags & SF_ERR_MASK)) |
| 4949 | s->flags |= SF_ERR_PRXCOND; |
| 4950 | if (!(s->flags & SF_FINST_MASK)) |
| 4951 | s->flags |= SF_FINST_H; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4952 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4953 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4954 | } |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4955 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4956 | /* has the response been denied ? */ |
| 4957 | if (txn->flags & TX_SVDENY) { |
| 4958 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 4959 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4960 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 4961 | HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 4962 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 4963 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 4964 | HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4965 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4966 | goto return_srv_prx_502; |
| 4967 | } |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 4968 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4969 | /* add response headers from the rule sets in the same order */ |
| 4970 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 4971 | if (txn->status < 200 && txn->status != 101) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4972 | break; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4973 | if (wl->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 4974 | 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] | 4975 | ret = acl_pass(ret); |
| 4976 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4977 | ret = !ret; |
| 4978 | if (!ret) |
| 4979 | continue; |
| 4980 | } |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 4981 | 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] | 4982 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4983 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4984 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4985 | /* check whether we're already working on the frontend */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4986 | if (cur_proxy == sess->fe) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4987 | break; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 4988 | cur_proxy = sess->fe; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 4989 | } |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4990 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 4991 | /* After this point, this anayzer can't return yield, so we can |
| 4992 | * remove the bit corresponding to this analyzer from the list. |
| 4993 | * |
| 4994 | * Note that the intermediate returns and goto found previously |
| 4995 | * reset the analyzers. |
| 4996 | */ |
| 4997 | rep->analysers &= ~an_bit; |
| 4998 | rep->analyse_exp = TICK_ETERNITY; |
| 4999 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5000 | /* OK that's all we can do for 1xx responses */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5001 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5002 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5003 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5004 | /* |
| 5005 | * Now check for a server cookie. |
| 5006 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 5007 | 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] | 5008 | manage_server_side_cookies(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5009 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5010 | /* |
| 5011 | * Check for cache-control or pragma headers if required. |
| 5012 | */ |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 5013 | 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] | 5014 | check_response_for_cacheability(s, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5015 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5016 | /* |
| 5017 | * Add server cookie in the response if needed |
| 5018 | */ |
| 5019 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 5020 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5021 | (!(s->flags & SF_DIRECT) || |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5022 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 5023 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5024 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5025 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 5026 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5027 | !(s->flags & SF_IGNORE_PRST)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5028 | /* the server is known, it's not the one the client requested, or the |
| 5029 | * cookie's last seen date needs to be refreshed. We have to |
| 5030 | * insert a set-cookie here, except if we want to insert only on POST |
| 5031 | * requests and this one isn't. Note that servers which don't have cookies |
| 5032 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5033 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5034 | if (!objt_server(s->target)->cookie) { |
| 5035 | chunk_printf(&trash, |
| 5036 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5037 | s->be->cookie_name); |
| 5038 | } |
| 5039 | else { |
| 5040 | 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] | 5041 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5042 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 5043 | /* emit last_date, which is mandatory */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5044 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 5045 | s30tob64((date.tv_sec+3) >> 2, |
| 5046 | trash.area + trash.data); |
| 5047 | trash.data += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5048 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5049 | if (s->be->cookie_maxlife) { |
| 5050 | /* emit first_date, which is either the original one or |
| 5051 | * the current date. |
| 5052 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5053 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5054 | s30tob64(txn->cookie_first_date ? |
| 5055 | txn->cookie_first_date >> 2 : |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5056 | (date.tv_sec+3) >> 2, |
| 5057 | trash.area + trash.data); |
| 5058 | trash.data += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5059 | } |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5060 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5061 | chunk_appendf(&trash, "; path=/"); |
| 5062 | } |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5063 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5064 | if (s->be->cookie_domain) |
| 5065 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5066 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5067 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 5068 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5069 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5070 | if (s->be->ck_opts & PR_CK_SECURE) |
| 5071 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5072 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5073 | 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] | 5074 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5075 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5076 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | b05e48a | 2018-09-20 11:12:58 +0200 | [diff] [blame] | 5077 | if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5078 | /* the server did not change, only the date was updated */ |
| 5079 | txn->flags |= TX_SCK_UPDATED; |
| 5080 | else |
| 5081 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5082 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5083 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5084 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5085 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5086 | * 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] | 5087 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5088 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5089 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5090 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5091 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5092 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 5093 | "Cache-control: private", 22) < 0)) |
| 5094 | goto return_bad_resp; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5095 | } |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5096 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5097 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5098 | /* |
| 5099 | * Check if result will be cacheable with a cookie. |
| 5100 | * We'll block the response if security checks have caught |
| 5101 | * nasty things such as a cacheable cookie. |
| 5102 | */ |
| 5103 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5104 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 5105 | (s->be->options & PR_O_CHK_CACHE)) { |
| 5106 | /* we're in presence of a cacheable response containing |
| 5107 | * a set-cookie header. We'll block it as requested by |
| 5108 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5109 | */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5110 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5111 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5112 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5113 | HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 5114 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 5115 | if (sess->listener->counters) |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 5116 | HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5117 | |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5118 | ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5119 | 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] | 5120 | send_log(s->be, LOG_ALERT, |
| 5121 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 5122 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 5123 | goto return_srv_prx_502; |
| 5124 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5125 | |
Willy Tarreau | 70730dd | 2014-04-24 18:06:27 +0200 | [diff] [blame] | 5126 | skip_filters: |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5127 | /* |
| 5128 | * Adjust "Connection: close" or "Connection: keep-alive" if needed. |
| 5129 | * If an "Upgrade" token is found, the header is left untouched in order |
| 5130 | * 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] | 5131 | * if anything but "Upgrade" is present in the Connection header. We don't |
| 5132 | * want to touch any 101 response either since it's switching to another |
| 5133 | * protocol. |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5134 | */ |
Willy Tarreau | ce730de | 2014-09-16 10:40:38 +0200 | [diff] [blame] | 5135 | if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) && |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 5136 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5137 | unsigned int want_flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5138 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5139 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5140 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5141 | /* we want a keep-alive response here. Keep-alive header |
| 5142 | * required if either side is not 1.1. |
| 5143 | */ |
| 5144 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
| 5145 | want_flags |= TX_CON_KAL_SET; |
| 5146 | } |
Christopher Faulet | 315b39c | 2018-09-21 16:26:19 +0200 | [diff] [blame] | 5147 | else { /* CLO */ |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5148 | /* we want a close response here. Close header required if |
| 5149 | * the server is 1.1, regardless of the client. |
| 5150 | */ |
| 5151 | if (msg->flags & HTTP_MSGF_VER_11) |
| 5152 | want_flags |= TX_CON_CLO_SET; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5153 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5154 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5155 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 5156 | http_change_connection_header(txn, msg, want_flags); |
| 5157 | } |
| 5158 | |
| 5159 | skip_header_mangling: |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 5160 | /* Always enter in the body analyzer */ |
| 5161 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 5162 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5163 | |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5164 | /* if the user wants to log as soon as possible, without counting |
| 5165 | * bytes from the server, then this is the right moment. We have |
| 5166 | * to temporarily assign bytes_out to log what we currently have. |
| 5167 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5168 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
Willy Tarreau | 5897567 | 2014-04-24 21:13:57 +0200 | [diff] [blame] | 5169 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 5170 | s->logs.bytes_out = txn->rsp.eoh; |
| 5171 | s->do_log(s); |
| 5172 | s->logs.bytes_out = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5173 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5174 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5175 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5176 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5177 | /* This function is an analyser which forwards response body (including chunk |
| 5178 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5179 | * zero byte. The only situation where it must not be called is when we're in |
| 5180 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5181 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5182 | * 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] | 5183 | * 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] | 5184 | * |
| 5185 | * It is capable of compressing response data both in content-length mode and |
| 5186 | * in chunked mode. The state machines follows different flows depending on |
| 5187 | * whether content-length and chunked modes are used, since there are no |
| 5188 | * trailers in content-length : |
| 5189 | * |
| 5190 | * chk-mode cl-mode |
| 5191 | * ,----- BODY -----. |
| 5192 | * / \ |
| 5193 | * V size > 0 V chk-mode |
| 5194 | * .--> SIZE -------------> DATA -------------> CRLF |
| 5195 | * | | size == 0 | last byte | |
| 5196 | * | v final crlf v inspected | |
| 5197 | * | TRAILERS -----------> DONE | |
| 5198 | * | | |
| 5199 | * `----------------------------------------------' |
| 5200 | * |
| 5201 | * Compression only happens in the DATA state, and must be flushed in final |
| 5202 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 5203 | * is performed at once on final states for all bytes parsed, or when leaving |
| 5204 | * on missing data. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5205 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5206 | 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] | 5207 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5208 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5209 | struct http_txn *txn = s->txn; |
| 5210 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 5211 | int ret; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5212 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 5213 | if (IS_HTX_STRM(s)) |
| 5214 | return htx_response_forward_body(s, res, an_bit); |
| 5215 | |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 5216 | 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] | 5217 | now_ms, __FUNCTION__, |
| 5218 | s, |
| 5219 | res, |
| 5220 | res->rex, res->wex, |
| 5221 | res->flags, |
Christopher Faulet | 4507351 | 2018-07-20 10:16:29 +0200 | [diff] [blame] | 5222 | ci_data(res), |
Christopher Faulet | 814d270 | 2017-03-30 11:33:44 +0200 | [diff] [blame] | 5223 | res->analysers); |
| 5224 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5225 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5226 | return 0; |
| 5227 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5228 | 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] | 5229 | ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res))) || |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5230 | !s->req.analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5231 | /* Output closed while we were sending data. We must abort and |
| 5232 | * wake the other side up. |
| 5233 | */ |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5234 | msg->err_state = msg->msg_state; |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5235 | msg->msg_state = HTTP_MSG_ERROR; |
| 5236 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5237 | return 1; |
| 5238 | } |
| 5239 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5240 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5241 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5242 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5243 | if (msg->msg_state == HTTP_MSG_BODY) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5244 | msg->msg_state = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5245 | ? HTTP_MSG_CHUNK_SIZE |
| 5246 | : HTTP_MSG_DATA); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5247 | } |
| 5248 | |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5249 | if (res->to_forward) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5250 | /* We can't process the buffer's contents yet */ |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5251 | res->flags |= CF_WAKE_WRITE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5252 | goto missing_data_or_waiting; |
Willy Tarreau | efdf094 | 2014-04-24 20:08:57 +0200 | [diff] [blame] | 5253 | } |
| 5254 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5255 | if (msg->msg_state < HTTP_MSG_DONE) { |
| 5256 | ret = ((msg->flags & HTTP_MSGF_TE_CHNK) |
| 5257 | ? http_msg_forward_chunked_body(s, msg) |
| 5258 | : http_msg_forward_body(s, msg)); |
| 5259 | if (!ret) |
| 5260 | goto missing_data_or_waiting; |
| 5261 | if (ret < 0) |
| 5262 | goto return_bad_res; |
| 5263 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 5264 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5265 | /* other states, DONE...TUNNEL */ |
| 5266 | /* for keep-alive we don't want to forward closes on DONE */ |
| 5267 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5268 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5269 | channel_dont_close(res); |
Willy Tarreau | 3ce10ff | 2014-04-22 08:24:38 +0200 | [diff] [blame] | 5270 | |
Christopher Faulet | 894da4c | 2017-07-18 11:29:07 +0200 | [diff] [blame] | 5271 | http_resync_states(s); |
| 5272 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5273 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5274 | if (res->flags & CF_SHUTW) { |
| 5275 | /* response errors are most likely due to the |
| 5276 | * client aborting the transfer. */ |
| 5277 | goto aborted_xfer; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5278 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5279 | if (msg->err_pos >= 0) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 5280 | 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] | 5281 | goto return_bad_res; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5282 | } |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5283 | return 1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5284 | } |
Willy Tarreau | f51d03c | 2016-05-02 15:25:15 +0200 | [diff] [blame] | 5285 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5286 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5287 | missing_data_or_waiting: |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5288 | if (res->flags & CF_SHUTW) |
| 5289 | goto aborted_xfer; |
| 5290 | |
| 5291 | /* stop waiting for data if the input is closed before the end. If the |
| 5292 | * client side was already closed, it means that the client has aborted, |
| 5293 | * so we don't want to count this as a server abort. Otherwise it's a |
| 5294 | * server abort. |
| 5295 | */ |
Christopher Faulet | a33510b | 2017-03-31 15:37:29 +0200 | [diff] [blame] | 5296 | if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5297 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5298 | goto aborted_xfer; |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 5299 | /* If we have some pending data, we continue the processing */ |
Willy Tarreau | 5ba6552 | 2018-06-15 15:14:53 +0200 | [diff] [blame] | 5300 | if (!ci_data(res)) { |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 5301 | if (!(s->flags & SF_ERR_MASK)) |
| 5302 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5303 | HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 5304 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5305 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
Christopher Faulet | a46bbd8 | 2015-06-19 09:00:58 +0200 | [diff] [blame] | 5306 | goto return_bad_res_stats_ok; |
| 5307 | } |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5308 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5309 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5310 | /* 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] | 5311 | if (!s->req.analysers) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5312 | goto return_bad_res; |
| 5313 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5314 | /* When TE: chunked is used, we need to get there again to parse |
| 5315 | * 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] | 5316 | * set CF_DONTCLOSE. Similarly, if keep-alive is set on the client side |
| 5317 | * or if there are filters registered on the stream, we don't want to |
| 5318 | * forward a close |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5319 | */ |
Christopher Faulet | 69744d9 | 2017-03-30 10:54:35 +0200 | [diff] [blame] | 5320 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 5321 | HAS_DATA_FILTERS(s, res) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5322 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5323 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5324 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5325 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5326 | /* 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] | 5327 | * 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] | 5328 | * 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] | 5329 | * modes are already handled by the stream sock layer. We must not do |
| 5330 | * this in content-length mode because it could present the MSG_MORE |
| 5331 | * flag with the last block of forwarded data, which would cause an |
| 5332 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5333 | */ |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 5334 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING)) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5335 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5336 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5337 | /* the stream handler will take care of timeouts and errors */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5338 | return 0; |
| 5339 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5340 | return_bad_res: /* let's centralize all bad responses */ |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5341 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5342 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5343 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5344 | |
| 5345 | return_bad_res_stats_ok: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5346 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5347 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 5348 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5349 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5350 | res->analysers &= AN_RES_FLT_END; |
| 5351 | 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] | 5352 | if (objt_server(s->target)) |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 5353 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5354 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5355 | if (!(s->flags & SF_ERR_MASK)) |
| 5356 | s->flags |= SF_ERR_PRXCOND; |
| 5357 | if (!(s->flags & SF_FINST_MASK)) |
| 5358 | s->flags |= SF_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5359 | return 0; |
| 5360 | |
| 5361 | aborted_xfer: |
Willy Tarreau | 10e61cb | 2017-01-04 14:51:22 +0100 | [diff] [blame] | 5362 | txn->rsp.err_state = txn->rsp.msg_state; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5363 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 5364 | /* don't send any error message as we're in the body */ |
Christopher Faulet | a94e5a5 | 2015-12-09 15:55:06 +0100 | [diff] [blame] | 5365 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 5366 | res->analysers &= AN_RES_FLT_END; |
| 5367 | 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] | 5368 | |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 5369 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 5370 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5371 | if (objt_server(s->target)) |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 5372 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5373 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 5374 | if (!(s->flags & SF_ERR_MASK)) |
| 5375 | s->flags |= SF_ERR_CLICL; |
| 5376 | if (!(s->flags & SF_FINST_MASK)) |
| 5377 | s->flags |= SF_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5378 | return 0; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5379 | } |
| 5380 | |
| 5381 | |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 5382 | int http_msg_forward_body(struct stream *s, struct http_msg *msg) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5383 | { |
| 5384 | struct channel *chn = msg->chn; |
| 5385 | int ret; |
| 5386 | |
| 5387 | /* Here we have the guarantee to be in HTTP_MSG_DATA or HTTP_MSG_ENDING state */ |
| 5388 | |
| 5389 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 5390 | goto ending; |
| 5391 | |
| 5392 | /* Neither content-length, nor transfer-encoding was found, so we must |
| 5393 | * read the body until the server connection is closed. In that case, we |
| 5394 | * eat data as they come. Of course, this happens for response only. */ |
| 5395 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5396 | unsigned long long len = ci_data(chn) - msg->next; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5397 | msg->chunk_len += len; |
| 5398 | msg->body_len += len; |
| 5399 | } |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5400 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5401 | /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next), |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5402 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5403 | msg->next += ret; |
| 5404 | msg->chunk_len -= ret; |
| 5405 | if (msg->chunk_len) { |
| 5406 | /* input empty or output full */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5407 | if (ci_data(chn) > msg->next) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5408 | chn->flags |= CF_WAKE_WRITE; |
| 5409 | goto missing_data_or_waiting; |
| 5410 | } |
| 5411 | |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 5412 | /* This check can only be true for a response. HTTP_MSGF_XFER_LEN is |
| 5413 | * always set for a request. */ |
| 5414 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 5415 | /* The server still sending data that should be filtered */ |
| 5416 | if (!(chn->flags & CF_SHUTR) && HAS_DATA_FILTERS(s, chn)) |
| 5417 | goto missing_data_or_waiting; |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 5418 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 5419 | goto ending; |
Christopher Faulet | 1486b0a | 2017-07-18 11:42:08 +0200 | [diff] [blame] | 5420 | } |
Christopher Faulet | f1cc5d0 | 2017-02-08 09:45:13 +0100 | [diff] [blame] | 5421 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5422 | msg->msg_state = HTTP_MSG_ENDING; |
| 5423 | |
| 5424 | ending: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5425 | /* 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] | 5426 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5427 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 5428 | /* default_ret */ msg->next, |
| 5429 | /* on_error */ goto error); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 5430 | c_adv(chn, ret); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5431 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 5432 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 5433 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 5434 | if (msg->next) |
| 5435 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 5436 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5437 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
| 5438 | /* default_ret */ 1, |
| 5439 | /* on_error */ goto error, |
| 5440 | /* on_wait */ goto waiting); |
Christopher Faulet | fd04fcf | 2018-02-02 15:54:15 +0100 | [diff] [blame] | 5441 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 5442 | msg->msg_state = HTTP_MSG_DONE; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5443 | return 1; |
| 5444 | |
| 5445 | missing_data_or_waiting: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5446 | /* we may have some pending data starting at chn->buf.p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5447 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_forward_data(s, msg, msg->next), |
| 5448 | /* default_ret */ msg->next, |
| 5449 | /* on_error */ goto error); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 5450 | c_adv(chn, ret); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5451 | msg->next -= ret; |
| 5452 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 5453 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 5454 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5455 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 5456 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5457 | return 0; |
| 5458 | error: |
| 5459 | return -1; |
| 5460 | } |
| 5461 | |
Christopher Faulet | 10079f5 | 2018-10-03 15:17:28 +0200 | [diff] [blame] | 5462 | 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] | 5463 | { |
| 5464 | struct channel *chn = msg->chn; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5465 | unsigned int chunk; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5466 | int ret; |
| 5467 | |
| 5468 | /* Here we have the guarantee to be in one of the following state: |
| 5469 | * HTTP_MSG_DATA, HTTP_MSG_CHUNK_SIZE, HTTP_MSG_CHUNK_CRLF, |
| 5470 | * HTTP_MSG_TRAILERS or HTTP_MSG_ENDING. */ |
| 5471 | |
Christopher Faulet | ca874b8 | 2018-09-20 11:31:01 +0200 | [diff] [blame] | 5472 | if (msg->msg_state == HTTP_MSG_ENDING) |
| 5473 | goto ending; |
| 5474 | |
| 5475 | /* Don't parse chunks if there is no input data */ |
| 5476 | if (!ci_data(chn)) |
| 5477 | goto waiting; |
| 5478 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5479 | switch_states: |
| 5480 | switch (msg->msg_state) { |
| 5481 | case HTTP_MSG_DATA: |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5482 | ret = FLT_STRM_DATA_CB(s, chn, flt_http_data(s, msg), |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5483 | /* default_ret */ MIN(msg->chunk_len, ci_data(chn) - msg->next), |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5484 | /* on_error */ goto error); |
| 5485 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5486 | msg->chunk_len -= ret; |
| 5487 | if (msg->chunk_len) { |
| 5488 | /* input empty or output full */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5489 | if (ci_data(chn) > msg->next) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5490 | chn->flags |= CF_WAKE_WRITE; |
| 5491 | goto missing_data_or_waiting; |
| 5492 | } |
| 5493 | |
| 5494 | /* nothing left to forward for this chunk*/ |
| 5495 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
| 5496 | /* fall through for HTTP_MSG_CHUNK_CRLF */ |
| 5497 | |
| 5498 | case HTTP_MSG_CHUNK_CRLF: |
| 5499 | /* we want the CRLF after the data */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5500 | 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] | 5501 | if (ret == 0) |
| 5502 | goto missing_data_or_waiting; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 5503 | if (ret < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5504 | msg->err_pos = ci_data(chn) + ret; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 5505 | if (msg->err_pos < 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5506 | msg->err_pos += chn->buf.size; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5507 | goto chunk_parsing_error; |
Willy Tarreau | b289256 | 2017-09-21 11:33:54 +0200 | [diff] [blame] | 5508 | } |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 5509 | msg->next += ret; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5510 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5511 | /* fall through for HTTP_MSG_CHUNK_SIZE */ |
| 5512 | |
| 5513 | case HTTP_MSG_CHUNK_SIZE: |
| 5514 | /* read the chunk size and assign it to ->chunk_len, |
| 5515 | * then set ->next to point to the body and switch to |
| 5516 | * DATA or TRAILERS state. |
| 5517 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5518 | 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] | 5519 | if (ret == 0) |
| 5520 | goto missing_data_or_waiting; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5521 | if (ret < 0) { |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5522 | msg->err_pos = ci_data(chn) + ret; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5523 | if (msg->err_pos < 0) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5524 | msg->err_pos += chn->buf.size; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5525 | goto chunk_parsing_error; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5526 | } |
| 5527 | |
| 5528 | msg->sol = ret; |
Christopher Faulet | 113f7de | 2015-12-14 14:52:13 +0100 | [diff] [blame] | 5529 | msg->next += ret; |
Willy Tarreau | e56cdd3 | 2017-09-21 08:36:33 +0200 | [diff] [blame] | 5530 | msg->chunk_len = chunk; |
| 5531 | msg->body_len += chunk; |
| 5532 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5533 | if (msg->chunk_len) { |
| 5534 | msg->msg_state = HTTP_MSG_DATA; |
| 5535 | goto switch_states; |
| 5536 | } |
| 5537 | msg->msg_state = HTTP_MSG_TRAILERS; |
| 5538 | /* fall through for HTTP_MSG_TRAILERS */ |
| 5539 | |
| 5540 | case HTTP_MSG_TRAILERS: |
| 5541 | ret = http_forward_trailers(msg); |
| 5542 | if (ret < 0) |
| 5543 | goto chunk_parsing_error; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5544 | FLT_STRM_DATA_CB(s, chn, flt_http_chunk_trailers(s, msg), |
| 5545 | /* default_ret */ 1, |
| 5546 | /* on_error */ goto error); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5547 | msg->next += msg->sol; |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5548 | if (!ret) |
| 5549 | goto missing_data_or_waiting; |
| 5550 | break; |
| 5551 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5552 | default: |
| 5553 | /* This should no happen in this function */ |
| 5554 | goto error; |
| 5555 | } |
| 5556 | |
| 5557 | msg->msg_state = HTTP_MSG_ENDING; |
| 5558 | ending: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5559 | /* 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] | 5560 | * chunk of data or trailers. */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5561 | 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] | 5562 | /* default_ret */ msg->next, |
| 5563 | /* on_error */ goto error); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 5564 | c_adv(chn, ret); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5565 | msg->next -= ret; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 5566 | if (unlikely(!(chn->flags & CF_WROTE_DATA) || msg->sov > 0)) |
| 5567 | msg->sov -= ret; |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 5568 | if (msg->next) |
| 5569 | goto waiting; |
Willy Tarreau | 9962f8f | 2016-06-28 11:52:08 +0200 | [diff] [blame] | 5570 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5571 | FLT_STRM_DATA_CB(s, chn, flt_http_end(s, msg), |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5572 | /* default_ret */ 1, |
| 5573 | /* on_error */ goto error, |
| 5574 | /* on_wait */ goto waiting); |
| 5575 | msg->msg_state = HTTP_MSG_DONE; |
| 5576 | return 1; |
| 5577 | |
| 5578 | missing_data_or_waiting: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5579 | /* we may have some pending data starting at chn->buf.p */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 5580 | 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] | 5581 | /* default_ret */ msg->next, |
| 5582 | /* on_error */ goto error); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 5583 | c_adv(chn, ret); |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5584 | msg->next -= ret; |
| 5585 | if (!(chn->flags & CF_WROTE_DATA) || msg->sov > 0) |
| 5586 | msg->sov -= ret; |
Christopher Faulet | 75e2eb6 | 2015-12-15 10:41:47 +0100 | [diff] [blame] | 5587 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5588 | msg->chunk_len -= channel_forward(chn, msg->chunk_len); |
Christopher Faulet | a9300a3 | 2016-06-28 15:54:44 +0200 | [diff] [blame] | 5589 | waiting: |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5590 | return 0; |
| 5591 | |
| 5592 | chunk_parsing_error: |
| 5593 | if (msg->err_pos >= 0) { |
| 5594 | if (chn->flags & CF_ISRESP) |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 5595 | http_capture_bad_message(s->be, s, msg, |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5596 | msg->msg_state, strm_fe(s)); |
| 5597 | else |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 5598 | http_capture_bad_message(strm_fe(s), s, |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5599 | msg, msg->msg_state, s->be); |
| 5600 | } |
| 5601 | error: |
| 5602 | return -1; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5603 | } |
| 5604 | |
Christopher Faulet | dbe34eb | 2015-12-02 10:01:17 +0100 | [diff] [blame] | 5605 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5606 | /* Iterate the same filter through all request headers. |
| 5607 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5608 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5609 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5610 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5611 | 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] | 5612 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5613 | char *cur_ptr, *cur_end, *cur_next; |
| 5614 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5615 | struct http_txn *txn = s->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5616 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 5617 | int delta, len; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 5618 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5619 | last_hdr = 0; |
| 5620 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5621 | cur_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5622 | old_idx = 0; |
| 5623 | |
| 5624 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5625 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5626 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5627 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5628 | (exp->action == ACT_ALLOW || |
| 5629 | exp->action == ACT_DENY || |
| 5630 | exp->action == ACT_TARPIT)) |
| 5631 | return 0; |
| 5632 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5633 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5634 | if (!cur_idx) |
| 5635 | break; |
| 5636 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5637 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5638 | cur_ptr = cur_next; |
| 5639 | cur_end = cur_ptr + cur_hdr->len; |
| 5640 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5641 | |
| 5642 | /* Now we have one header between cur_ptr and cur_end, |
| 5643 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5644 | */ |
| 5645 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 5646 | 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] | 5647 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5648 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5649 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5650 | last_hdr = 1; |
| 5651 | break; |
| 5652 | |
| 5653 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5654 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5655 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5656 | break; |
| 5657 | |
| 5658 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5659 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5660 | last_hdr = 1; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5661 | break; |
| 5662 | |
| 5663 | case ACT_REPLACE: |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 5664 | len = exp_replace(trash.area, |
| 5665 | trash.size, cur_ptr, |
| 5666 | exp->replace, pmatch); |
| 5667 | if (len < 0) |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 5668 | return -1; |
| 5669 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 5670 | delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len); |
| 5671 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5672 | /* FIXME: if the user adds a newline in the replacement, the |
| 5673 | * index will not be recalculated for now, and the new line |
| 5674 | * will not be counted as a new header. |
| 5675 | */ |
| 5676 | |
| 5677 | cur_end += delta; |
| 5678 | cur_next += delta; |
| 5679 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5680 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5681 | break; |
| 5682 | |
| 5683 | case ACT_REMOVE: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5684 | delta = b_rep_blk(&req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5685 | cur_next += delta; |
| 5686 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5687 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5688 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5689 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5690 | cur_hdr->len = 0; |
| 5691 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 5692 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5693 | break; |
| 5694 | |
| 5695 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5696 | } |
| 5697 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5698 | /* keep the link from this header to next one in case of later |
| 5699 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5700 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5701 | old_idx = cur_idx; |
| 5702 | } |
| 5703 | return 0; |
| 5704 | } |
| 5705 | |
| 5706 | |
| 5707 | /* Apply the filter to the request line. |
| 5708 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5709 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5710 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5711 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5712 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5713 | 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] | 5714 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5715 | char *cur_ptr, *cur_end; |
| 5716 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5717 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 5718 | int delta, len; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5719 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5720 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5721 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5722 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5723 | (exp->action == ACT_ALLOW || |
| 5724 | exp->action == ACT_DENY || |
| 5725 | exp->action == ACT_TARPIT)) |
| 5726 | return 0; |
| 5727 | else if (exp->action == ACT_REMOVE) |
| 5728 | return 0; |
| 5729 | |
| 5730 | done = 0; |
| 5731 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5732 | cur_ptr = ci_head(req); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5733 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5734 | |
| 5735 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5736 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 5737 | 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] | 5738 | switch (exp->action) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5739 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5740 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5741 | done = 1; |
| 5742 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5743 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5744 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5745 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5746 | done = 1; |
| 5747 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5748 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5749 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5750 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5751 | done = 1; |
| 5752 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5753 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5754 | case ACT_REPLACE: |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 5755 | len = exp_replace(trash.area, trash.size, |
| 5756 | cur_ptr, exp->replace, pmatch); |
| 5757 | if (len < 0) |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 5758 | return -1; |
| 5759 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 5760 | delta = b_rep_blk(&req->buf, cur_ptr, cur_end, trash.area, len); |
| 5761 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5762 | /* FIXME: if the user adds a newline in the replacement, the |
| 5763 | * index will not be recalculated for now, and the new line |
| 5764 | * will not be counted as a new header. |
| 5765 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5766 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5767 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5768 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 5769 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5770 | HTTP_MSG_RQMETH, |
| 5771 | cur_ptr, cur_end + 1, |
| 5772 | NULL, NULL); |
| 5773 | if (unlikely(!cur_end)) |
| 5774 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5775 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5776 | /* we have a full request and we know that we have either a CR |
| 5777 | * or an LF at <ptr>. |
| 5778 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5779 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5780 | 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] | 5781 | /* there is no point trying this regex on headers */ |
| 5782 | return 1; |
| 5783 | } |
| 5784 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5785 | return done; |
| 5786 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5787 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5788 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5789 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5790 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5791 | * 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] | 5792 | * 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] | 5793 | * unparsable request. Since it can manage the switch to another backend, it |
| 5794 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5795 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5796 | 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] | 5797 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 5798 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5799 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5800 | struct hdr_exp *exp; |
| 5801 | |
| 5802 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5803 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5804 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5805 | /* |
| 5806 | * The interleaving of transformations and verdicts |
| 5807 | * makes it difficult to decide to continue or stop |
| 5808 | * the evaluation. |
| 5809 | */ |
| 5810 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5811 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 5812 | break; |
| 5813 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5814 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5815 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5816 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5817 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5818 | |
| 5819 | /* if this filter had a condition, evaluate it now and skip to |
| 5820 | * next filter if the condition does not match. |
| 5821 | */ |
| 5822 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 5823 | 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] | 5824 | ret = acl_pass(ret); |
| 5825 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 5826 | ret = !ret; |
| 5827 | |
| 5828 | if (!ret) |
| 5829 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5830 | } |
| 5831 | |
| 5832 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5833 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5834 | if (unlikely(ret < 0)) |
| 5835 | return -1; |
| 5836 | |
| 5837 | if (likely(ret == 0)) { |
| 5838 | /* The filter did not match the request, it can be |
| 5839 | * iterated through all headers. |
| 5840 | */ |
Willy Tarreau | 34d4c3c | 2015-01-30 20:58:58 +0100 | [diff] [blame] | 5841 | if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0)) |
| 5842 | return -1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5843 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5844 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5845 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5846 | } |
| 5847 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5848 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5849 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 5850 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 5851 | * the first delimiter is updated if required. The function tries as much as |
| 5852 | * possible to respect the following principles : |
| 5853 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 5854 | * colon, in which case <next> is simply removed |
| 5855 | * - set exactly one space character after the new first delimiter, unless |
| 5856 | * there are not enough characters in the block being moved to do so. |
| 5857 | * - remove unneeded spaces before the previous delimiter and after the new |
| 5858 | * one. |
| 5859 | * |
| 5860 | * It is the caller's responsibility to ensure that : |
| 5861 | * - <from> points to a valid delimiter or the colon ; |
| 5862 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 5863 | * - there are non-space chars before <from> ; |
| 5864 | * - there is a CR/LF at or after <next>. |
| 5865 | */ |
Willy Tarreau | c5a4fd5 | 2018-12-11 11:42:27 +0100 | [diff] [blame] | 5866 | static int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5867 | { |
| 5868 | char *prev = *from; |
| 5869 | |
| 5870 | if (*prev == ':') { |
| 5871 | /* We're removing the first value, preserve the colon and add a |
| 5872 | * space if possible. |
| 5873 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 5874 | if (!HTTP_IS_CRLF(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5875 | next++; |
| 5876 | prev++; |
| 5877 | if (prev < next) |
| 5878 | *prev++ = ' '; |
| 5879 | |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 5880 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5881 | next++; |
| 5882 | } else { |
| 5883 | /* Remove useless spaces before the old delimiter. */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 5884 | while (HTTP_IS_SPHT(*(prev-1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5885 | prev--; |
| 5886 | *from = prev; |
| 5887 | |
| 5888 | /* copy the delimiter and if possible a space if we're |
| 5889 | * not at the end of the line. |
| 5890 | */ |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 5891 | if (!HTTP_IS_CRLF(*next)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5892 | *prev++ = *next++; |
| 5893 | if (prev + 1 < next) |
| 5894 | *prev++ = ' '; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 5895 | while (HTTP_IS_SPHT(*next)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5896 | next++; |
| 5897 | } |
| 5898 | } |
Willy Tarreau | e312802 | 2018-07-12 15:55:34 +0200 | [diff] [blame] | 5899 | return b_rep_blk(buf, prev, next, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5900 | } |
| 5901 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5902 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5903 | * 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] | 5904 | * desirable to call it only when needed. This code is quite complex because |
| 5905 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 5906 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5907 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5908 | void manage_client_side_cookies(struct stream *s, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5909 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5910 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 5911 | struct session *sess = s->sess; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5912 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5913 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5914 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 5915 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5916 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5917 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 5918 | old_idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 5919 | hdr_next = ci_head(req) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5920 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5921 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5922 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5923 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5924 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5925 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5926 | hdr_beg = hdr_next; |
| 5927 | hdr_end = hdr_beg + cur_hdr->len; |
| 5928 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5929 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5930 | /* We have one full header between hdr_beg and hdr_end, and the |
| 5931 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5932 | * "Cookie:" headers. |
| 5933 | */ |
| 5934 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5935 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5936 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5937 | old_idx = cur_idx; |
| 5938 | continue; |
| 5939 | } |
| 5940 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5941 | del_from = NULL; /* nothing to be deleted */ |
| 5942 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 5943 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5944 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 5945 | * attributes whose name begin with a '$', and associate them with |
| 5946 | * the right cookie, if we want to delete this cookie. |
| 5947 | * So there are 3 cases for each cookie read : |
| 5948 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 5949 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 5950 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 5951 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 5952 | * "special" cookie. |
| 5953 | * At the end of loop, if a "special" cookie remains, we may have to |
| 5954 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5955 | * *MUST* delete it. |
| 5956 | * |
| 5957 | * Note: RFC2965 is unclear about the processing of spaces around |
| 5958 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 5959 | * the RFC explicitly allows spaces before it, and not within the |
| 5960 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 5961 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 5962 | * after the equal sign too, resulting in some (rare) buggy |
| 5963 | * implementations trying to do that. So let's do what servers do. |
| 5964 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 5965 | * allowed quoted strings in values, with any possible character |
| 5966 | * after a backslash, including control chars and delimitors, which |
| 5967 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 5968 | * within values even without quotes. |
| 5969 | * |
| 5970 | * We have to keep multiple pointers in order to support cookie |
| 5971 | * removal at the beginning, middle or end of header without |
| 5972 | * corrupting the header. All of these headers are valid : |
| 5973 | * |
| 5974 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 5975 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 5976 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 5977 | * | | | | | | | | | |
| 5978 | * | | | | | | | | hdr_end <--+ |
| 5979 | * | | | | | | | +--> next |
| 5980 | * | | | | | | +----> val_end |
| 5981 | * | | | | | +-----------> val_beg |
| 5982 | * | | | | +--------------> equal |
| 5983 | * | | | +----------------> att_end |
| 5984 | * | | +---------------------> att_beg |
| 5985 | * | +--------------------------> prev |
| 5986 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5987 | */ |
| 5988 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5989 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 5990 | /* Iterate through all cookies on this line */ |
| 5991 | |
| 5992 | /* find att_beg */ |
| 5993 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 5994 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5995 | att_beg++; |
| 5996 | |
| 5997 | /* find att_end : this is the first character after the last non |
| 5998 | * space before the equal. It may be equal to hdr_end. |
| 5999 | */ |
| 6000 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6001 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6002 | while (equal < hdr_end) { |
| 6003 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6004 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6005 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6006 | continue; |
| 6007 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6008 | } |
| 6009 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6010 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6011 | * is between <att_beg> and <equal>, both may be identical. |
| 6012 | */ |
| 6013 | |
| 6014 | /* look for end of cookie if there is an equal sign */ |
| 6015 | if (equal < hdr_end && *equal == '=') { |
| 6016 | /* look for the beginning of the value */ |
| 6017 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6018 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6019 | val_beg++; |
| 6020 | |
| 6021 | /* find the end of the value, respecting quotes */ |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 6022 | next = http_find_cookie_value_end(val_beg, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6023 | |
| 6024 | /* make val_end point to the first white space or delimitor after the value */ |
| 6025 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6026 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6027 | val_end--; |
| 6028 | } else { |
| 6029 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6030 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6031 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6032 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6033 | * they will automatically be removed if a header before them is removed, |
| 6034 | * since they're supposed to be linked together. |
| 6035 | */ |
| 6036 | if (*att_beg == '$') |
| 6037 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6038 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6039 | /* Ignore cookies with no equal sign */ |
| 6040 | if (equal == next) { |
| 6041 | /* This is not our cookie, so we must preserve it. But if we already |
| 6042 | * scheduled another cookie for removal, we cannot remove the |
| 6043 | * complete header, but we can remove the previous block itself. |
| 6044 | */ |
| 6045 | preserve_hdr = 1; |
| 6046 | if (del_from != NULL) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6047 | int delta = del_hdr_value(&req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6048 | val_end += delta; |
| 6049 | next += delta; |
| 6050 | hdr_end += delta; |
| 6051 | hdr_next += delta; |
| 6052 | cur_hdr->len += delta; |
| 6053 | http_msg_move_end(&txn->req, delta); |
| 6054 | prev = del_from; |
| 6055 | del_from = NULL; |
| 6056 | } |
| 6057 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6058 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6059 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6060 | /* if there are spaces around the equal sign, we need to |
| 6061 | * strip them otherwise we'll get trouble for cookie captures, |
| 6062 | * or even for rewrites. Since this happens extremely rarely, |
| 6063 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6064 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6065 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6066 | int stripped_before = 0; |
| 6067 | int stripped_after = 0; |
| 6068 | |
| 6069 | if (att_end != equal) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6070 | stripped_before = b_rep_blk(&req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6071 | equal += stripped_before; |
| 6072 | val_beg += stripped_before; |
| 6073 | } |
| 6074 | |
| 6075 | if (val_beg > equal + 1) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6076 | 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] | 6077 | val_beg += stripped_after; |
| 6078 | stripped_before += stripped_after; |
| 6079 | } |
| 6080 | |
| 6081 | val_end += stripped_before; |
| 6082 | next += stripped_before; |
| 6083 | hdr_end += stripped_before; |
| 6084 | hdr_next += stripped_before; |
| 6085 | cur_hdr->len += stripped_before; |
| 6086 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6087 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6088 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6089 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6090 | /* First, let's see if we want to capture this cookie. We check |
| 6091 | * that we don't already have a client side cookie, because we |
| 6092 | * can only capture one. Also as an optimisation, we ignore |
| 6093 | * cookies shorter than the declared name. |
| 6094 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6095 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6096 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 6097 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6098 | int log_len = val_end - att_beg; |
| 6099 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6100 | if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6101 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6102 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6103 | if (log_len > sess->fe->capture_len) |
| 6104 | log_len = sess->fe->capture_len; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6105 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6106 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6107 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6108 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6109 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6110 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6111 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6112 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6113 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6114 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6115 | * For cookies in prefix mode, the form is : |
| 6116 | * |
| 6117 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6118 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6119 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 6120 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 6121 | struct server *srv = s->be->srv; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6122 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6123 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6124 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6125 | * have the server ID between val_beg and delim, and the original cookie between |
| 6126 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6127 | * |
| 6128 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6129 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6130 | * | || || | |+-> next |
| 6131 | * | || || | +--> val_end |
| 6132 | * | || || +---------> delim |
| 6133 | * | || |+------------> val_beg |
| 6134 | * | || +-------------> att_end = equal |
| 6135 | * | |+-----------------> att_beg |
| 6136 | * | +------------------> prev |
| 6137 | * +-------------------------> hdr_beg |
| 6138 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6139 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6140 | if (s->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6141 | for (delim = val_beg; delim < val_end; delim++) |
| 6142 | if (*delim == COOKIE_DELIM) |
| 6143 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6144 | } else { |
| 6145 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6146 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6147 | /* Now check if the cookie contains a date field, which would |
| 6148 | * appear after a vertical bar ('|') just after the server name |
| 6149 | * and before the delimiter. |
| 6150 | */ |
| 6151 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6152 | if (vbar1) { |
| 6153 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6154 | * right is the last seen date. It is a base64 encoded |
| 6155 | * 30-bit value representing the UNIX date since the |
| 6156 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6157 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6158 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6159 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6160 | if (val_end - vbar1 >= 5) { |
| 6161 | val = b64tos30(vbar1); |
| 6162 | if (val > 0) |
| 6163 | txn->cookie_last_date = val << 2; |
| 6164 | } |
| 6165 | /* look for a second vertical bar */ |
| 6166 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6167 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6168 | val = b64tos30(vbar1 + 1); |
| 6169 | if (val > 0) |
| 6170 | txn->cookie_first_date = val << 2; |
| 6171 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6172 | } |
| 6173 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6174 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6175 | /* if the cookie has an expiration date and the proxy wants to check |
| 6176 | * it, then we do that now. We first check if the cookie is too old, |
| 6177 | * then only if it has expired. We detect strict overflow because the |
| 6178 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6179 | * in the future are ignored if their offset is beyond one day. This |
| 6180 | * allows an admin to fix timezone issues without expiring everyone |
| 6181 | * and at the same time avoids keeping unwanted side effects for too |
| 6182 | * long. |
| 6183 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6184 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 6185 | (((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] | 6186 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6187 | txn->flags &= ~TX_CK_MASK; |
| 6188 | txn->flags |= TX_CK_OLD; |
| 6189 | delim = val_beg; // let's pretend we have not found the cookie |
| 6190 | txn->cookie_first_date = 0; |
| 6191 | txn->cookie_last_date = 0; |
| 6192 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6193 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 6194 | (((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] | 6195 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6196 | txn->flags &= ~TX_CK_MASK; |
| 6197 | txn->flags |= TX_CK_EXPIRED; |
| 6198 | delim = val_beg; // let's pretend we have not found the cookie |
| 6199 | txn->cookie_first_date = 0; |
| 6200 | txn->cookie_last_date = 0; |
| 6201 | } |
| 6202 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6203 | /* Here, we'll look for the first running server which supports the cookie. |
| 6204 | * This allows to share a same cookie between several servers, for example |
| 6205 | * to dedicate backup servers to specific servers only. |
| 6206 | * However, to prevent clients from sticking to cookie-less backup server |
| 6207 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6208 | * empty cookies and mark them as invalid. |
| 6209 | * The same behaviour is applied when persistence must be ignored. |
| 6210 | */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6211 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6212 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6213 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6214 | while (srv) { |
| 6215 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6216 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 6217 | if ((srv->cur_state != SRV_ST_STOPPED) || |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6218 | (s->be->options & PR_O_PERSIST) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6219 | (s->flags & SF_FORCE_PRST)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6220 | /* we found the server and we can use it */ |
| 6221 | txn->flags &= ~TX_CK_MASK; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 6222 | 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] | 6223 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6224 | s->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6225 | break; |
| 6226 | } else { |
| 6227 | /* we found a server, but it's down, |
| 6228 | * mark it as such and go on in case |
| 6229 | * another one is available. |
| 6230 | */ |
| 6231 | txn->flags &= ~TX_CK_MASK; |
| 6232 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6233 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6234 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6235 | srv = srv->next; |
| 6236 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6237 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6238 | 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] | 6239 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6240 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6241 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 6242 | txn->flags |= TX_CK_UNUSED; |
| 6243 | else |
| 6244 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6245 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6246 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6247 | /* depending on the cookie mode, we may have to either : |
| 6248 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6249 | * the server never sees it ; |
| 6250 | * - remove the server id from the cookie value, and tag the cookie as an |
Joseph Herlant | 5ba8025 | 2018-11-15 09:25:36 -0800 | [diff] [blame] | 6251 | * application cookie so that it does not get accidently removed later, |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6252 | * if we're in cookie prefix mode |
| 6253 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6254 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6255 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6256 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6257 | delta = b_rep_blk(&req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6258 | val_end += delta; |
| 6259 | next += delta; |
| 6260 | hdr_end += delta; |
| 6261 | hdr_next += delta; |
| 6262 | cur_hdr->len += delta; |
| 6263 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6264 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6265 | del_from = NULL; |
| 6266 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6267 | } |
| 6268 | else if (del_from == NULL && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6269 | (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] | 6270 | del_from = prev; |
| 6271 | } |
| 6272 | } else { |
| 6273 | /* This is not our cookie, so we must preserve it. But if we already |
| 6274 | * scheduled another cookie for removal, we cannot remove the |
| 6275 | * complete header, but we can remove the previous block itself. |
| 6276 | */ |
| 6277 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6278 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6279 | if (del_from != NULL) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6280 | int delta = del_hdr_value(&req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 6281 | if (att_beg >= del_from) |
| 6282 | att_beg += delta; |
| 6283 | if (att_end >= del_from) |
| 6284 | att_end += delta; |
| 6285 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6286 | val_end += delta; |
| 6287 | next += delta; |
| 6288 | hdr_end += delta; |
| 6289 | hdr_next += delta; |
| 6290 | cur_hdr->len += delta; |
| 6291 | http_msg_move_end(&txn->req, delta); |
| 6292 | prev = del_from; |
| 6293 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6294 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6295 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6296 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6297 | /* continue with next cookie on this header line */ |
| 6298 | att_beg = next; |
| 6299 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6300 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6301 | /* There are no more cookies on this line. |
| 6302 | * We may still have one (or several) marked for deletion at the |
| 6303 | * end of the line. We must do this now in two ways : |
| 6304 | * - if some cookies must be preserved, we only delete from the |
| 6305 | * mark to the end of line ; |
| 6306 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6307 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6308 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6309 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6310 | if (preserve_hdr) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6311 | delta = del_hdr_value(&req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6312 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6313 | cur_hdr->len += delta; |
| 6314 | } else { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6315 | delta = b_rep_blk(&req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6316 | |
| 6317 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6318 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6319 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6320 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6321 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6322 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6323 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6324 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6325 | } |
| 6326 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6327 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6328 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6329 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6330 | } |
| 6331 | |
| 6332 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6333 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6334 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6335 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6336 | 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] | 6337 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6338 | char *cur_ptr, *cur_end, *cur_next; |
| 6339 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6340 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6341 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6342 | int delta, len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6343 | |
| 6344 | last_hdr = 0; |
| 6345 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6346 | cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6347 | old_idx = 0; |
| 6348 | |
| 6349 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6350 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6351 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6352 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6353 | (exp->action == ACT_ALLOW || |
| 6354 | exp->action == ACT_DENY)) |
| 6355 | return 0; |
| 6356 | |
| 6357 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 6358 | if (!cur_idx) |
| 6359 | break; |
| 6360 | |
| 6361 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6362 | cur_ptr = cur_next; |
| 6363 | cur_end = cur_ptr + cur_hdr->len; |
| 6364 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6365 | |
| 6366 | /* Now we have one header between cur_ptr and cur_end, |
| 6367 | * and the next header starts at cur_next. |
| 6368 | */ |
| 6369 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6370 | 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] | 6371 | switch (exp->action) { |
| 6372 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6373 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6374 | last_hdr = 1; |
| 6375 | break; |
| 6376 | |
| 6377 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6378 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6379 | last_hdr = 1; |
| 6380 | break; |
| 6381 | |
| 6382 | case ACT_REPLACE: |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6383 | len = exp_replace(trash.area, |
| 6384 | trash.size, cur_ptr, |
| 6385 | exp->replace, pmatch); |
| 6386 | if (len < 0) |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6387 | return -1; |
| 6388 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6389 | delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len); |
| 6390 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6391 | /* FIXME: if the user adds a newline in the replacement, the |
| 6392 | * index will not be recalculated for now, and the new line |
| 6393 | * will not be counted as a new header. |
| 6394 | */ |
| 6395 | |
| 6396 | cur_end += delta; |
| 6397 | cur_next += delta; |
| 6398 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6399 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6400 | break; |
| 6401 | |
| 6402 | case ACT_REMOVE: |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6403 | delta = b_rep_blk(&rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6404 | cur_next += delta; |
| 6405 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6406 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6407 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6408 | txn->hdr_idx.used--; |
| 6409 | cur_hdr->len = 0; |
| 6410 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6411 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6412 | break; |
| 6413 | |
| 6414 | } |
| 6415 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6416 | |
| 6417 | /* keep the link from this header to next one in case of later |
| 6418 | * removal of next header. |
| 6419 | */ |
| 6420 | old_idx = cur_idx; |
| 6421 | } |
| 6422 | return 0; |
| 6423 | } |
| 6424 | |
| 6425 | |
| 6426 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 6427 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6428 | * or -1 if a replacement resulted in an invalid status line. |
| 6429 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6430 | 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] | 6431 | { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6432 | char *cur_ptr, *cur_end; |
| 6433 | int done; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6434 | struct http_txn *txn = s->txn; |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6435 | int delta, len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6436 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6437 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6438 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6439 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6440 | (exp->action == ACT_ALLOW || |
| 6441 | exp->action == ACT_DENY)) |
| 6442 | return 0; |
| 6443 | else if (exp->action == ACT_REMOVE) |
| 6444 | return 0; |
| 6445 | |
| 6446 | done = 0; |
| 6447 | |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6448 | cur_ptr = ci_head(rtr); |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6449 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6450 | |
| 6451 | /* Now we have the status line between cur_ptr and cur_end */ |
| 6452 | |
Willy Tarreau | 15a53a4 | 2015-01-21 13:39:42 +0100 | [diff] [blame] | 6453 | 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] | 6454 | switch (exp->action) { |
| 6455 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6456 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6457 | done = 1; |
| 6458 | break; |
| 6459 | |
| 6460 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6461 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6462 | done = 1; |
| 6463 | break; |
| 6464 | |
| 6465 | case ACT_REPLACE: |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6466 | len = exp_replace(trash.area, trash.size, |
| 6467 | cur_ptr, exp->replace, pmatch); |
| 6468 | if (len < 0) |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6469 | return -1; |
| 6470 | |
Willy Tarreau | 6e27be1 | 2018-08-22 04:46:47 +0200 | [diff] [blame] | 6471 | delta = b_rep_blk(&rtr->buf, cur_ptr, cur_end, trash.area, len); |
| 6472 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6473 | /* FIXME: if the user adds a newline in the replacement, the |
| 6474 | * index will not be recalculated for now, and the new line |
| 6475 | * will not be counted as a new header. |
| 6476 | */ |
| 6477 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6478 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6479 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6480 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 6481 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6482 | cur_ptr, cur_end + 1, |
| 6483 | NULL, NULL); |
| 6484 | if (unlikely(!cur_end)) |
| 6485 | return -1; |
| 6486 | |
| 6487 | /* we have a full respnse and we know that we have either a CR |
| 6488 | * or an LF at <ptr>. |
| 6489 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6490 | 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] | 6491 | 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] | 6492 | /* there is no point trying this regex on headers */ |
| 6493 | return 1; |
| 6494 | } |
| 6495 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6496 | return done; |
| 6497 | } |
| 6498 | |
| 6499 | |
| 6500 | |
| 6501 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6502 | * 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] | 6503 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 6504 | * unparsable response. |
| 6505 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6506 | 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] | 6507 | { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6508 | struct session *sess = s->sess; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6509 | struct http_txn *txn = s->txn; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6510 | struct hdr_exp *exp; |
| 6511 | |
| 6512 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6513 | int ret; |
| 6514 | |
| 6515 | /* |
| 6516 | * The interleaving of transformations and verdicts |
| 6517 | * makes it difficult to decide to continue or stop |
| 6518 | * the evaluation. |
| 6519 | */ |
| 6520 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6521 | if (txn->flags & TX_SVDENY) |
| 6522 | break; |
| 6523 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6524 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6525 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 6526 | exp->action == ACT_PASS)) { |
| 6527 | exp = exp->next; |
| 6528 | continue; |
| 6529 | } |
| 6530 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6531 | /* if this filter had a condition, evaluate it now and skip to |
| 6532 | * next filter if the condition does not match. |
| 6533 | */ |
| 6534 | if (exp->cond) { |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 6535 | 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] | 6536 | ret = acl_pass(ret); |
| 6537 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6538 | ret = !ret; |
| 6539 | if (!ret) |
| 6540 | continue; |
| 6541 | } |
| 6542 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6543 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6544 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6545 | if (unlikely(ret < 0)) |
| 6546 | return -1; |
| 6547 | |
| 6548 | if (likely(ret == 0)) { |
| 6549 | /* The filter did not match the response, it can be |
| 6550 | * iterated through all headers. |
| 6551 | */ |
Sasha Pachev | c600204 | 2014-05-26 12:33:48 -0600 | [diff] [blame] | 6552 | if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0)) |
| 6553 | return -1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6554 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6555 | } |
| 6556 | return 0; |
| 6557 | } |
| 6558 | |
| 6559 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6560 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6561 | * 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] | 6562 | * desirable to call it only when needed. This function is also used when we |
| 6563 | * 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] | 6564 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6565 | void manage_server_side_cookies(struct stream *s, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6566 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6567 | struct http_txn *txn = s->txn; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6568 | struct session *sess = s->sess; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6569 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6570 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6571 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6572 | char *hdr_beg, *hdr_end, *hdr_next; |
| 6573 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6574 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6575 | /* Iterate through the headers. |
| 6576 | * we start with the start line. |
| 6577 | */ |
| 6578 | old_idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6579 | hdr_next = ci_head(res) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6580 | |
| 6581 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 6582 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6583 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6584 | |
| 6585 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6586 | hdr_beg = hdr_next; |
| 6587 | hdr_end = hdr_beg + cur_hdr->len; |
| 6588 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6589 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6590 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6591 | * next header starts at hdr_next. We're only interested in |
| 6592 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6593 | */ |
| 6594 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6595 | is_cookie2 = 0; |
| 6596 | prev = hdr_beg + 10; |
| 6597 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6598 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6599 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 6600 | if (!val) { |
| 6601 | old_idx = cur_idx; |
| 6602 | continue; |
| 6603 | } |
| 6604 | is_cookie2 = 1; |
| 6605 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6606 | } |
| 6607 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6608 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 6609 | * <prev> points to the colon. |
| 6610 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6611 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6612 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6613 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 6614 | * check-cache is enabled) and we are not interested in checking |
| 6615 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6616 | */ |
Willy Tarreau | 53a09d5 | 2015-08-10 18:59:40 +0200 | [diff] [blame] | 6617 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6618 | return; |
| 6619 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6620 | /* OK so now we know we have to process this response cookie. |
| 6621 | * The format of the Set-Cookie header is slightly different |
| 6622 | * from the format of the Cookie header in that it does not |
| 6623 | * support the comma as a cookie delimiter (thus the header |
| 6624 | * cannot be folded) because the Expires attribute described in |
| 6625 | * the original Netscape's spec may contain an unquoted date |
| 6626 | * with a comma inside. We have to live with this because |
| 6627 | * many browsers don't support Max-Age and some browsers don't |
| 6628 | * support quoted strings. However the Set-Cookie2 header is |
| 6629 | * clean. |
| 6630 | * |
| 6631 | * We have to keep multiple pointers in order to support cookie |
| 6632 | * removal at the beginning, middle or end of header without |
| 6633 | * corrupting the header (in case of set-cookie2). A special |
| 6634 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 6635 | * fields after the first semi-colon. The <next> pointer points |
| 6636 | * either to the end of line (set-cookie) or next unquoted comma |
| 6637 | * (set-cookie2). All of these headers are valid : |
| 6638 | * |
| 6639 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 6640 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6641 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6642 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 6643 | * | | | | | | | | | | |
| 6644 | * | | | | | | | | +-> next hdr_end <--+ |
| 6645 | * | | | | | | | +------------> scav |
| 6646 | * | | | | | | +--------------> val_end |
| 6647 | * | | | | | +--------------------> val_beg |
| 6648 | * | | | | +----------------------> equal |
| 6649 | * | | | +------------------------> att_end |
| 6650 | * | | +----------------------------> att_beg |
| 6651 | * | +------------------------------> prev |
| 6652 | * +-----------------------------------------> hdr_beg |
| 6653 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6654 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6655 | for (; prev < hdr_end; prev = next) { |
| 6656 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6657 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6658 | /* find att_beg */ |
| 6659 | att_beg = prev + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6660 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6661 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6662 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6663 | /* find att_end : this is the first character after the last non |
| 6664 | * space before the equal. It may be equal to hdr_end. |
| 6665 | */ |
| 6666 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6667 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6668 | while (equal < hdr_end) { |
| 6669 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 6670 | break; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6671 | if (HTTP_IS_SPHT(*equal++)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6672 | continue; |
| 6673 | att_end = equal; |
| 6674 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6675 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6676 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6677 | * is between <att_beg> and <equal>, both may be identical. |
| 6678 | */ |
| 6679 | |
| 6680 | /* look for end of cookie if there is an equal sign */ |
| 6681 | if (equal < hdr_end && *equal == '=') { |
| 6682 | /* look for the beginning of the value */ |
| 6683 | val_beg = equal + 1; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6684 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6685 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6686 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6687 | /* find the end of the value, respecting quotes */ |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 6688 | next = http_find_cookie_value_end(val_beg, hdr_end); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6689 | |
| 6690 | /* make val_end point to the first white space or delimitor after the value */ |
| 6691 | val_end = next; |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 6692 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6693 | val_end--; |
| 6694 | } else { |
| 6695 | /* <equal> points to next comma, semi-colon or EOL */ |
| 6696 | val_beg = val_end = next = equal; |
| 6697 | } |
| 6698 | |
| 6699 | if (next < hdr_end) { |
| 6700 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 6701 | * a colon or semi-colon before the end. So skip all attr-value |
| 6702 | * pairs and look for the next comma. For Set-Cookie, since |
| 6703 | * commas are permitted in values, skip to the end. |
| 6704 | */ |
| 6705 | if (is_cookie2) |
Willy Tarreau | ab813a4 | 2018-09-10 18:41:28 +0200 | [diff] [blame] | 6706 | next = http_find_hdr_value_end(next, hdr_end); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6707 | else |
| 6708 | next = hdr_end; |
| 6709 | } |
| 6710 | |
| 6711 | /* Now everything is as on the diagram above */ |
| 6712 | |
| 6713 | /* Ignore cookies with no equal sign */ |
| 6714 | if (equal == val_end) |
| 6715 | continue; |
| 6716 | |
| 6717 | /* If there are spaces around the equal sign, we need to |
| 6718 | * strip them otherwise we'll get trouble for cookie captures, |
| 6719 | * or even for rewrites. Since this happens extremely rarely, |
| 6720 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6721 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6722 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6723 | int stripped_before = 0; |
| 6724 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6725 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6726 | if (att_end != equal) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6727 | stripped_before = b_rep_blk(&res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6728 | equal += stripped_before; |
| 6729 | val_beg += stripped_before; |
| 6730 | } |
| 6731 | |
| 6732 | if (val_beg > equal + 1) { |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6733 | 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] | 6734 | val_beg += stripped_after; |
| 6735 | stripped_before += stripped_after; |
| 6736 | } |
| 6737 | |
| 6738 | val_end += stripped_before; |
| 6739 | next += stripped_before; |
| 6740 | hdr_end += stripped_before; |
| 6741 | hdr_next += stripped_before; |
| 6742 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 6743 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6744 | } |
| 6745 | |
| 6746 | /* First, let's see if we want to capture this cookie. We check |
| 6747 | * that we don't already have a server side cookie, because we |
| 6748 | * can only capture one. Also as an optimisation, we ignore |
| 6749 | * cookies shorter than the declared name. |
| 6750 | */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6751 | if (sess->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6752 | txn->srv_cookie == NULL && |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6753 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 6754 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6755 | int log_len = val_end - att_beg; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6756 | if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6757 | ha_alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6758 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 6759 | else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 6760 | if (log_len > sess->fe->capture_len) |
| 6761 | log_len = sess->fe->capture_len; |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 6762 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 6763 | txn->srv_cookie[log_len] = 0; |
| 6764 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6765 | } |
| 6766 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6767 | srv = objt_server(s->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6768 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6769 | if (!(s->flags & SF_IGNORE_PRST) && |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6770 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 6771 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6772 | /* assume passive cookie by default */ |
| 6773 | txn->flags &= ~TX_SCK_MASK; |
| 6774 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6775 | |
| 6776 | /* If the cookie is in insert mode on a known server, we'll delete |
| 6777 | * this occurrence because we'll insert another one later. |
| 6778 | * 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] | 6779 | * a direct access. |
| 6780 | */ |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6781 | if (s->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 6782 | /* The "preserve" flag was set, we don't want to touch the |
| 6783 | * server's cookie. |
| 6784 | */ |
| 6785 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6786 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 6787 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6788 | /* this cookie must be deleted */ |
| 6789 | if (*prev == ':' && next == hdr_end) { |
| 6790 | /* whole header */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6791 | delta = b_rep_blk(&res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6792 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6793 | txn->hdr_idx.used--; |
| 6794 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6795 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6796 | hdr_next += delta; |
| 6797 | http_msg_move_end(&txn->rsp, delta); |
| 6798 | /* note: while both invalid now, <next> and <hdr_end> |
| 6799 | * are still equal, so the for() will stop as expected. |
| 6800 | */ |
| 6801 | } else { |
| 6802 | /* just remove the value */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6803 | int delta = del_hdr_value(&res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6804 | next = prev; |
| 6805 | hdr_end += delta; |
| 6806 | hdr_next += delta; |
| 6807 | cur_hdr->len += delta; |
| 6808 | http_msg_move_end(&txn->rsp, delta); |
| 6809 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6810 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6811 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6812 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6813 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6814 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6815 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6816 | * with this server since we know it. |
| 6817 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6818 | 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] | 6819 | next += delta; |
| 6820 | hdr_end += delta; |
| 6821 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6822 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6823 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6824 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6825 | txn->flags &= ~TX_SCK_MASK; |
| 6826 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6827 | } |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 6828 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6829 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6830 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6831 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6832 | 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] | 6833 | next += delta; |
| 6834 | hdr_end += delta; |
| 6835 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6836 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6837 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6838 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6839 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6840 | txn->flags &= ~TX_SCK_MASK; |
| 6841 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6842 | } |
| 6843 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6844 | /* that's done for this cookie, check the next one on the same |
| 6845 | * line when next != hdr_end (only if is_cookie2). |
| 6846 | */ |
| 6847 | } |
| 6848 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6849 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6850 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6851 | } |
| 6852 | |
| 6853 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6854 | /* |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 6855 | * Parses the Cache-Control and Pragma request header fields to determine if |
| 6856 | * the request may be served from the cache and/or if it is cacheable. Updates |
| 6857 | * s->txn->flags. |
| 6858 | */ |
| 6859 | void check_request_for_cacheability(struct stream *s, struct channel *chn) |
| 6860 | { |
| 6861 | struct http_txn *txn = s->txn; |
| 6862 | char *p1, *p2; |
| 6863 | char *cur_ptr, *cur_end, *cur_next; |
| 6864 | int pragma_found; |
| 6865 | int cc_found; |
| 6866 | int cur_idx; |
| 6867 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 6868 | if (IS_HTX_STRM(s)) |
| 6869 | return htx_check_request_for_cacheability(s, chn); |
| 6870 | |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 6871 | if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE) |
| 6872 | return; /* nothing more to do here */ |
| 6873 | |
| 6874 | cur_idx = 0; |
| 6875 | pragma_found = cc_found = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6876 | cur_next = ci_head(chn) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 6877 | |
| 6878 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6879 | struct hdr_idx_elem *cur_hdr; |
| 6880 | int val; |
| 6881 | |
| 6882 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6883 | cur_ptr = cur_next; |
| 6884 | cur_end = cur_ptr + cur_hdr->len; |
| 6885 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6886 | |
| 6887 | /* We have one full header between cur_ptr and cur_end, and the |
| 6888 | * next header starts at cur_next. |
| 6889 | */ |
| 6890 | |
| 6891 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 6892 | if (val) { |
| 6893 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 6894 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 6895 | pragma_found = 1; |
| 6896 | continue; |
| 6897 | } |
| 6898 | } |
| 6899 | |
William Lallemand | 8a16fe0 | 2018-05-22 11:04:33 +0200 | [diff] [blame] | 6900 | /* Don't use the cache and don't try to store if we found the |
| 6901 | * Authorization header */ |
| 6902 | val = http_header_match2(cur_ptr, cur_end, "Authorization", 13); |
| 6903 | if (val) { |
| 6904 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 6905 | txn->flags |= TX_CACHE_IGNORE; |
| 6906 | continue; |
| 6907 | } |
| 6908 | |
Willy Tarreau | 0ad8e0d | 2017-12-22 15:03:36 +0100 | [diff] [blame] | 6909 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 6910 | if (!val) |
| 6911 | continue; |
| 6912 | |
| 6913 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 6914 | cc_found = 1; |
| 6915 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
| 6916 | |
| 6917 | if (p1 >= cur_end) /* no more info */ |
| 6918 | continue; |
| 6919 | |
| 6920 | /* p1 is at the beginning of the value */ |
| 6921 | p2 = p1; |
| 6922 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
| 6923 | p2++; |
| 6924 | |
| 6925 | /* we have a complete value between p1 and p2. We don't check the |
| 6926 | * values after max-age, max-stale nor min-fresh, we simply don't |
| 6927 | * use the cache when they're specified. |
| 6928 | */ |
| 6929 | if (((p2 - p1 == 7) && strncasecmp(p1, "max-age", 7) == 0) || |
| 6930 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
| 6931 | ((p2 - p1 == 9) && strncasecmp(p1, "max-stale", 9) == 0) || |
| 6932 | ((p2 - p1 == 9) && strncasecmp(p1, "min-fresh", 9) == 0)) { |
| 6933 | txn->flags |= TX_CACHE_IGNORE; |
| 6934 | continue; |
| 6935 | } |
| 6936 | |
| 6937 | if ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) { |
| 6938 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 6939 | continue; |
| 6940 | } |
| 6941 | } |
| 6942 | |
| 6943 | /* RFC7234#5.4: |
| 6944 | * When the Cache-Control header field is also present and |
| 6945 | * understood in a request, Pragma is ignored. |
| 6946 | * When the Cache-Control header field is not present in a |
| 6947 | * request, caches MUST consider the no-cache request |
| 6948 | * pragma-directive as having the same effect as if |
| 6949 | * "Cache-Control: no-cache" were present. |
| 6950 | */ |
| 6951 | if (!cc_found && pragma_found) |
| 6952 | txn->flags |= TX_CACHE_IGNORE; |
| 6953 | } |
| 6954 | |
| 6955 | /* |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 6956 | * Check if response is cacheable or not. Updates s->txn->flags. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6957 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6958 | void check_response_for_cacheability(struct stream *s, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6959 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 6960 | struct http_txn *txn = s->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6961 | char *p1, *p2; |
| 6962 | |
| 6963 | char *cur_ptr, *cur_end, *cur_next; |
| 6964 | int cur_idx; |
| 6965 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 6966 | |
| 6967 | if (IS_HTX_STRM(s)) |
| 6968 | return htx_check_response_for_cacheability(s, rtr); |
| 6969 | |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 6970 | if (txn->status < 200) { |
| 6971 | /* do not try to cache interim responses! */ |
| 6972 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6973 | return; |
Willy Tarreau | 12b32f2 | 2017-12-21 16:08:09 +0100 | [diff] [blame] | 6974 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6975 | |
| 6976 | /* Iterate through the headers. |
| 6977 | * we start with the start line. |
| 6978 | */ |
| 6979 | cur_idx = 0; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 6980 | cur_next = ci_head(rtr) + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6981 | |
| 6982 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6983 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6984 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6985 | |
| 6986 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6987 | cur_ptr = cur_next; |
| 6988 | cur_end = cur_ptr + cur_hdr->len; |
| 6989 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6990 | |
| 6991 | /* 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] | 6992 | * next header starts at cur_next. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6993 | */ |
| 6994 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6995 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 6996 | if (val) { |
| 6997 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 6998 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 6999 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7000 | return; |
| 7001 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7002 | } |
| 7003 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7004 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7005 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7006 | continue; |
| 7007 | |
| 7008 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7009 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7010 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7011 | |
| 7012 | if (p1 >= cur_end) /* no more info */ |
| 7013 | continue; |
| 7014 | |
| 7015 | /* p1 is at the beginning of the value */ |
| 7016 | p2 = p1; |
| 7017 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7018 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7019 | p2++; |
| 7020 | |
| 7021 | /* we have a complete value between p1 and p2 */ |
| 7022 | if (p2 < cur_end && *p2 == '=') { |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7023 | if (((cur_end - p2) > 1 && (p2 - p1 == 7) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7024 | ((cur_end - p2) > 1 && (p2 - p1 == 8) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
| 7025 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7026 | continue; |
| 7027 | } |
| 7028 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7029 | /* we have something of the form no-cache="set-cookie" */ |
| 7030 | if ((cur_end - p1 >= 21) && |
| 7031 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7032 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7033 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7034 | continue; |
| 7035 | } |
| 7036 | |
| 7037 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7038 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
Willy Tarreau | 5b15f90 | 2013-07-04 12:46:56 +0200 | [diff] [blame] | 7039 | ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) || |
Willy Tarreau | d3900cc | 2017-12-22 15:35:11 +0100 | [diff] [blame] | 7040 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7041 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7042 | return; |
| 7043 | } |
| 7044 | |
| 7045 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7046 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7047 | continue; |
| 7048 | } |
| 7049 | } |
| 7050 | } |
| 7051 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7052 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7053 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7054 | * 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] | 7055 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7056 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7057 | * 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] | 7058 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7059 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7060 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7061 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7062 | 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] | 7063 | { |
| 7064 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7065 | struct http_msg *msg = &txn->req; |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7066 | const char *uri = ci_head(msg->chn)+ msg->sl.rq.u; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7067 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7068 | if (!uri_auth) |
| 7069 | return 0; |
| 7070 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7071 | 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] | 7072 | return 0; |
| 7073 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7074 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7075 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7076 | return 0; |
| 7077 | |
Willy Tarreau | 414e9bb | 2013-11-23 00:30:38 +0100 | [diff] [blame] | 7078 | if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7079 | return 0; |
| 7080 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7081 | return 1; |
| 7082 | } |
| 7083 | |
Willy Tarreau | 7ccdd8d | 2018-09-07 14:01:39 +0200 | [diff] [blame] | 7084 | /* Append the description of what is present in error snapshot <es> into <out>. |
| 7085 | * The description must be small enough to always fit in a trash. The output |
| 7086 | * buffer may be the trash so the trash must not be used inside this function. |
| 7087 | */ |
| 7088 | void http_show_error_snapshot(struct buffer *out, const struct error_snapshot *es) |
| 7089 | { |
Christopher Faulet | ed26fb8 | 2018-11-29 22:53:30 +0100 | [diff] [blame] | 7090 | chunk_appendf(out, |
Willy Tarreau | 7ccdd8d | 2018-09-07 14:01:39 +0200 | [diff] [blame] | 7091 | " stream #%d, stream flags 0x%08x, tx flags 0x%08x\n" |
| 7092 | " HTTP msg state %s(%d), msg flags 0x%08x\n" |
| 7093 | " HTTP chunk len %lld bytes, HTTP body len %lld bytes, channel flags 0x%08x :\n", |
| 7094 | es->ctx.http.sid, es->ctx.http.s_flags, es->ctx.http.t_flags, |
| 7095 | h1_msg_state_str(es->ctx.http.state), es->ctx.http.state, |
| 7096 | es->ctx.http.m_flags, es->ctx.http.m_clen, |
| 7097 | es->ctx.http.m_blen, es->ctx.http.b_flags); |
| 7098 | } |
| 7099 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7100 | /* |
| 7101 | * 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] | 7102 | * By default it tries to report the error position as msg->err_pos. However if |
| 7103 | * this one is not set, it will then report msg->next, which is the last known |
| 7104 | * 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] | 7105 | * displays buffers as a contiguous area starting at buf->p. The direction is |
| 7106 | * determined thanks to the channel's flags. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7107 | */ |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 7108 | void http_capture_bad_message(struct proxy *proxy, struct stream *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7109 | struct http_msg *msg, |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 7110 | enum h1_state state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7111 | { |
Willy Tarreau | ef3ca73 | 2018-09-07 15:47:35 +0200 | [diff] [blame] | 7112 | union error_snapshot_ctx ctx; |
| 7113 | long ofs; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7114 | |
Willy Tarreau | ef3ca73 | 2018-09-07 15:47:35 +0200 | [diff] [blame] | 7115 | /* http-specific part now */ |
| 7116 | ctx.http.sid = s->uniq_id; |
| 7117 | ctx.http.state = state; |
| 7118 | ctx.http.b_flags = msg->chn->flags; |
| 7119 | ctx.http.s_flags = s->flags; |
| 7120 | ctx.http.t_flags = s->txn->flags; |
| 7121 | ctx.http.m_flags = msg->flags; |
| 7122 | ctx.http.m_clen = msg->chunk_len; |
| 7123 | ctx.http.m_blen = msg->body_len; |
Willy Tarreau | 7480f32 | 2018-09-06 19:41:22 +0200 | [diff] [blame] | 7124 | |
Willy Tarreau | ef3ca73 | 2018-09-07 15:47:35 +0200 | [diff] [blame] | 7125 | ofs = msg->chn->total - ci_data(msg->chn); |
| 7126 | if (ofs < 0) |
| 7127 | ofs = 0; |
Willy Tarreau | 0b5b480 | 2018-09-07 13:49:44 +0200 | [diff] [blame] | 7128 | |
Willy Tarreau | fd9419d | 2018-09-07 18:01:03 +0200 | [diff] [blame] | 7129 | proxy_capture_error(proxy, !!(msg->chn->flags & CF_ISRESP), |
Willy Tarreau | ef3ca73 | 2018-09-07 15:47:35 +0200 | [diff] [blame] | 7130 | other_end, s->target, |
| 7131 | strm_sess(s), &msg->chn->buf, |
| 7132 | ofs, co_data(msg->chn), |
| 7133 | (msg->err_pos >= 0) ? msg->err_pos : msg->next, |
| 7134 | &ctx, http_show_error_snapshot); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7135 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7136 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7137 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 7138 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 7139 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 7140 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7141 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7142 | 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] | 7143 | { |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7144 | struct session *sess = strm_sess(s); |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7145 | int max; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 7146 | |
Willy Tarreau | f1fd9dc | 2014-04-24 20:47:57 +0200 | [diff] [blame] | 7147 | 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] | 7148 | dir, |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 7149 | 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] | 7150 | 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] | 7151 | |
| 7152 | for (max = 0; start + max < end; max++) |
| 7153 | if (start[max] == '\r' || start[max] == '\n') |
| 7154 | break; |
| 7155 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7156 | UBOUND(max, trash.size - trash.data - 3); |
| 7157 | trash.data += strlcpy2(trash.area + trash.data, start, max + 1); |
| 7158 | trash.area[trash.data++] = '\n'; |
| 7159 | shut_your_big_mouth_gcc(write(1, trash.area, trash.data)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7160 | } |
| 7161 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7162 | |
| 7163 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 7164 | * The hdr_idx is allocated as well. In case of allocation failure, everything |
| 7165 | * allocated is freed and NULL is returned. Otherwise the new transaction is |
| 7166 | * assigned to the stream and returned. |
| 7167 | */ |
| 7168 | struct http_txn *http_alloc_txn(struct stream *s) |
| 7169 | { |
| 7170 | struct http_txn *txn = s->txn; |
| 7171 | |
| 7172 | if (txn) |
| 7173 | return txn; |
| 7174 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7175 | txn = pool_alloc(pool_head_http_txn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7176 | if (!txn) |
| 7177 | return txn; |
| 7178 | |
| 7179 | txn->hdr_idx.size = global.tune.max_http_hdr; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7180 | txn->hdr_idx.v = pool_alloc(pool_head_hdr_idx); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7181 | if (!txn->hdr_idx.v) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7182 | pool_free(pool_head_http_txn, txn); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7183 | return NULL; |
| 7184 | } |
| 7185 | |
| 7186 | s->txn = txn; |
| 7187 | return txn; |
| 7188 | } |
| 7189 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 7190 | void http_txn_reset_req(struct http_txn *txn) |
| 7191 | { |
| 7192 | txn->req.flags = 0; |
| 7193 | txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */ |
| 7194 | txn->req.next = 0; |
| 7195 | txn->req.chunk_len = 0LL; |
| 7196 | txn->req.body_len = 0LL; |
| 7197 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7198 | } |
| 7199 | |
| 7200 | void http_txn_reset_res(struct http_txn *txn) |
| 7201 | { |
| 7202 | txn->rsp.flags = 0; |
| 7203 | txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 7204 | txn->rsp.next = 0; |
| 7205 | txn->rsp.chunk_len = 0LL; |
| 7206 | txn->rsp.body_len = 0LL; |
| 7207 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 7208 | } |
| 7209 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7210 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7211 | * 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] | 7212 | * the required fields are properly allocated and that we only need to (re)init |
| 7213 | * them. This should be used before processing any new request. |
| 7214 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7215 | void http_init_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7216 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7217 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7218 | struct proxy *fe = strm_fe(s); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 7219 | struct conn_stream *cs = objt_cs(s->si[0].end); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7220 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 7221 | txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) |
| 7222 | ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ) |
| 7223 | : 0); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7224 | txn->status = -1; |
Willy Tarreau | c9036c0 | 2019-01-11 19:38:25 +0100 | [diff] [blame] | 7225 | *(unsigned int *)txn->cache_hash = 0; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7226 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7227 | txn->cookie_first_date = 0; |
| 7228 | txn->cookie_last_date = 0; |
| 7229 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7230 | txn->srv_cookie = NULL; |
| 7231 | txn->cli_cookie = NULL; |
| 7232 | txn->uri = NULL; |
| 7233 | |
Thierry FOURNIER | fd50f0b | 2015-09-25 18:53:18 +0200 | [diff] [blame] | 7234 | http_txn_reset_req(txn); |
| 7235 | http_txn_reset_res(txn); |
| 7236 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7237 | txn->req.chn = &s->req; |
| 7238 | txn->rsp.chn = &s->res; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7239 | |
| 7240 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7241 | |
| 7242 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7243 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7244 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7245 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7246 | if (txn->hdr_idx.v) |
| 7247 | hdr_idx_init(&txn->hdr_idx); |
Thierry FOURNIER | 4834bc7 | 2015-06-06 19:29:07 +0200 | [diff] [blame] | 7248 | |
| 7249 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 7250 | vars_init(&s->vars_reqres, SCOPE_REQ); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7251 | } |
| 7252 | |
| 7253 | /* to be used at the end of a transaction */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7254 | void http_end_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7255 | { |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 7256 | struct http_txn *txn = s->txn; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7257 | struct proxy *fe = strm_fe(s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7258 | |
| 7259 | /* these ones will have been dynamically allocated */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7260 | pool_free(pool_head_requri, txn->uri); |
| 7261 | pool_free(pool_head_capture, txn->cli_cookie); |
| 7262 | pool_free(pool_head_capture, txn->srv_cookie); |
| 7263 | pool_free(pool_head_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7264 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7265 | s->unique_id = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7266 | txn->uri = NULL; |
| 7267 | txn->srv_cookie = NULL; |
| 7268 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7269 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 7270 | if (s->req_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7271 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7272 | for (h = fe->req_cap; h; h = h->next) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7273 | pool_free(h->pool, s->req_cap[h->index]); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 7274 | memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7275 | } |
| 7276 | |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 7277 | if (s->res_cap) { |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7278 | struct cap_hdr *h; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 7279 | for (h = fe->rsp_cap; h; h = h->next) |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7280 | pool_free(h->pool, s->res_cap[h->index]); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 7281 | memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7282 | } |
| 7283 | |
Willy Tarreau | cda7f3f | 2018-10-28 13:44:36 +0100 | [diff] [blame] | 7284 | if (!LIST_ISEMPTY(&s->vars_txn.head)) |
| 7285 | vars_prune(&s->vars_txn, s->sess, s); |
| 7286 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 7287 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7288 | } |
| 7289 | |
| 7290 | /* 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] | 7291 | void http_reset_txn(struct stream *s) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7292 | { |
| 7293 | http_end_txn(s); |
| 7294 | http_init_txn(s); |
| 7295 | |
Thierry FOURNIER | bc4c1ac | 2015-02-25 13:36:14 +0100 | [diff] [blame] | 7296 | /* reinitialise the current rule list pointer to NULL. We are sure that |
| 7297 | * any rulelist match the NULL pointer. |
| 7298 | */ |
| 7299 | s->current_rule_list = NULL; |
| 7300 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7301 | s->be = strm_fe(s); |
| 7302 | s->logs.logwait = strm_fe(s)->to_log; |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 7303 | s->logs.level = 0; |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7304 | stream_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7305 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7306 | /* re-init store persistence */ |
| 7307 | s->store_count = 0; |
Willy Tarreau | 90a7c03 | 2018-09-05 16:21:29 +0200 | [diff] [blame] | 7308 | s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1); |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7309 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7310 | s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7311 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7312 | /* We must trim any excess data from the response buffer, because we |
| 7313 | * may have blocked an invalid response from a server that we don't |
Joseph Herlant | 5ba8025 | 2018-11-15 09:25:36 -0800 | [diff] [blame] | 7314 | * want to accidently forward once we disable the analysers, nor do |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7315 | * we want those data to come along with next response. A typical |
| 7316 | * example of such data would be from a buggy server responding to |
| 7317 | * a HEAD with some data, or sending more than the advertised |
| 7318 | * content-length. |
| 7319 | */ |
Willy Tarreau | f37954d | 2018-06-15 18:31:02 +0200 | [diff] [blame] | 7320 | if (unlikely(ci_data(&s->res))) |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 7321 | b_set_data(&s->res.buf, co_data(&s->res)); |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7322 | |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 7323 | /* Now we can realign the response buffer */ |
Willy Tarreau | d5b343b | 2018-06-06 06:42:46 +0200 | [diff] [blame] | 7324 | c_realign_if_empty(&s->res); |
Christopher Faulet | c0c672a | 2017-03-28 11:51:33 +0200 | [diff] [blame] | 7325 | |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7326 | s->req.rto = strm_fe(s)->timeout.client; |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7327 | s->req.wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7328 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7329 | s->res.rto = TICK_ETERNITY; |
Willy Tarreau | d0d8da9 | 2015-04-04 02:10:38 +0200 | [diff] [blame] | 7330 | s->res.wto = strm_fe(s)->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7331 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7332 | s->req.rex = TICK_ETERNITY; |
| 7333 | s->req.wex = TICK_ETERNITY; |
| 7334 | s->req.analyse_exp = TICK_ETERNITY; |
| 7335 | s->res.rex = TICK_ETERNITY; |
| 7336 | s->res.wex = TICK_ETERNITY; |
| 7337 | s->res.analyse_exp = TICK_ETERNITY; |
Hongbo Long | e39683c | 2017-03-10 18:41:51 +0100 | [diff] [blame] | 7338 | s->si[1].hcto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7339 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7340 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7341 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 7342 | * takes the string from the variable 'replace' with length 'len', then modifies |
| 7343 | * the relevant part of the request line accordingly. Then it updates various |
| 7344 | * pointers to the next elements which were moved, and the total buffer length. |
| 7345 | * It finds the action to be performed in p[2], previously filled by function |
| 7346 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 7347 | * error, though this can be revisited when this code is finally exploited. |
| 7348 | * |
| 7349 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 7350 | * query string and 3 to replace uri. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7351 | * |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7352 | * In query string case, the mark question '?' must be set at the start of the |
| 7353 | * string by the caller, event if the replacement query string is empty. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7354 | */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7355 | int http_replace_req_line(int action, const char *replace, int len, |
| 7356 | struct proxy *px, struct stream *s) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7357 | { |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7358 | struct http_txn *txn = s->txn; |
| 7359 | char *cur_ptr, *cur_end; |
| 7360 | int offset = 0; |
| 7361 | int delta; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7362 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 7363 | if (IS_HTX_STRM(s)) |
| 7364 | return htx_req_replace_stline(action, replace, len, px, s); |
| 7365 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7366 | switch (action) { |
| 7367 | case 0: // method |
| 7368 | cur_ptr = ci_head(&s->req); |
| 7369 | cur_end = cur_ptr + txn->req.sl.rq.m_l; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7370 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7371 | /* adjust req line offsets and lengths */ |
| 7372 | delta = len - offset - (cur_end - cur_ptr); |
| 7373 | txn->req.sl.rq.m_l += delta; |
| 7374 | txn->req.sl.rq.u += delta; |
| 7375 | txn->req.sl.rq.v += delta; |
| 7376 | break; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 7377 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7378 | case 1: // path |
| 7379 | cur_ptr = http_txn_get_path(txn); |
| 7380 | if (!cur_ptr) |
| 7381 | cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7382 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7383 | cur_end = cur_ptr; |
| 7384 | while (cur_end < ci_head(&s->req) + txn->req.sl.rq.u + txn->req.sl.rq.u_l && *cur_end != '?') |
| 7385 | cur_end++; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7386 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7387 | /* adjust req line offsets and lengths */ |
| 7388 | delta = len - offset - (cur_end - cur_ptr); |
| 7389 | txn->req.sl.rq.u_l += delta; |
| 7390 | txn->req.sl.rq.v += delta; |
| 7391 | break; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7392 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7393 | case 2: // query |
| 7394 | offset = 1; |
| 7395 | cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u; |
| 7396 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
| 7397 | while (cur_ptr < cur_end && *cur_ptr != '?') |
| 7398 | cur_ptr++; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 7399 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7400 | /* skip the question mark or indicate that we must insert it |
| 7401 | * (but only if the format string is not empty then). |
| 7402 | */ |
| 7403 | if (cur_ptr < cur_end) |
| 7404 | cur_ptr++; |
| 7405 | else if (len > 1) |
| 7406 | offset = 0; |
Willy Tarreau | aae75e3 | 2013-03-29 12:31:49 +0100 | [diff] [blame] | 7407 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7408 | /* adjust req line offsets and lengths */ |
| 7409 | delta = len - offset - (cur_end - cur_ptr); |
| 7410 | txn->req.sl.rq.u_l += delta; |
| 7411 | txn->req.sl.rq.v += delta; |
| 7412 | break; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7413 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7414 | case 3: // uri |
| 7415 | cur_ptr = ci_head(&s->req) + txn->req.sl.rq.u; |
| 7416 | cur_end = cur_ptr + txn->req.sl.rq.u_l; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7417 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7418 | /* adjust req line offsets and lengths */ |
| 7419 | delta = len - offset - (cur_end - cur_ptr); |
| 7420 | txn->req.sl.rq.u_l += delta; |
| 7421 | txn->req.sl.rq.v += delta; |
| 7422 | break; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7423 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7424 | default: |
| 7425 | return -1; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7426 | } |
| 7427 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7428 | /* commit changes and adjust end of message */ |
| 7429 | delta = b_rep_blk(&s->req.buf, cur_ptr, cur_end, replace + offset, len - offset); |
| 7430 | txn->req.sl.rq.l += delta; |
| 7431 | txn->hdr_idx.v[0].len += delta; |
| 7432 | http_msg_move_end(&txn->req, delta); |
| 7433 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7434 | } |
| 7435 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7436 | /* This function replace the HTTP status code and the associated message. The |
| 7437 | * variable <status> contains the new status code. This function never fails. |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7438 | */ |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7439 | 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] | 7440 | { |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7441 | struct http_txn *txn = s->txn; |
| 7442 | char *cur_ptr, *cur_end; |
| 7443 | int delta; |
| 7444 | char *res; |
| 7445 | int c_l; |
| 7446 | const char *msg = reason; |
| 7447 | int msg_len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7448 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 7449 | if (IS_HTX_STRM(s)) |
| 7450 | return htx_res_set_status(status, reason, s); |
| 7451 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7452 | chunk_reset(&trash); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7453 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7454 | res = ultoa_o(status, trash.area, trash.size); |
| 7455 | c_l = res - trash.area; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7456 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7457 | trash.area[c_l] = ' '; |
| 7458 | trash.data = c_l + 1; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7459 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7460 | /* Do we have a custom reason format string? */ |
| 7461 | if (msg == NULL) |
| 7462 | msg = http_get_reason(status); |
| 7463 | msg_len = strlen(msg); |
| 7464 | strncpy(&trash.area[trash.data], msg, trash.size - trash.data); |
| 7465 | trash.data += msg_len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7466 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7467 | cur_ptr = ci_head(&s->res) + txn->rsp.sl.st.c; |
| 7468 | 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] | 7469 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7470 | /* commit changes and adjust message */ |
| 7471 | delta = b_rep_blk(&s->res.buf, cur_ptr, cur_end, trash.area, |
| 7472 | trash.data); |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 7473 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7474 | /* adjust res line offsets and lengths */ |
| 7475 | txn->rsp.sl.st.r += c_l - txn->rsp.sl.st.c_l; |
| 7476 | txn->rsp.sl.st.c_l = c_l; |
| 7477 | txn->rsp.sl.st.r_l = msg_len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7478 | |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 7479 | delta = trash.data - (cur_end - cur_ptr); |
| 7480 | txn->rsp.sl.st.l += delta; |
| 7481 | txn->hdr_idx.v[0].len += delta; |
| 7482 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7483 | } |
| 7484 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7485 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7486 | * Local variables: |
| 7487 | * c-indent-level: 8 |
| 7488 | * c-basic-offset: 8 |
| 7489 | * End: |
| 7490 | */ |