Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTT/1 mux-demux for connections |
| 3 | * |
| 4 | * Copyright 2018 Christopher Faulet <cfaulet@haproxy.com> |
| 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 | #include <common/cfgparse.h> |
| 13 | #include <common/config.h> |
Willy Tarreau | afba57a | 2018-12-11 13:44:24 +0100 | [diff] [blame] | 14 | #include <common/h1.h> |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 15 | #include <common/h2.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 16 | #include <common/htx.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 17 | #include <common/initcall.h> |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 18 | |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 19 | #include <ebistree.h> |
| 20 | |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 21 | #include <types/pipe.h> |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 22 | #include <types/proxy.h> |
| 23 | #include <types/session.h> |
| 24 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 25 | #include <proto/connection.h> |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 26 | #include <proto/http_htx.h> |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 27 | #include <proto/log.h> |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 28 | #include <proto/session.h> |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 29 | #include <proto/stream.h> |
| 30 | #include <proto/stream_interface.h> |
| 31 | |
| 32 | /* |
| 33 | * H1 Connection flags (32 bits) |
| 34 | */ |
| 35 | #define H1C_F_NONE 0x00000000 |
| 36 | |
| 37 | /* Flags indicating why writing output data are blocked */ |
| 38 | #define H1C_F_OUT_ALLOC 0x00000001 /* mux is blocked on lack of output buffer */ |
| 39 | #define H1C_F_OUT_FULL 0x00000002 /* mux is blocked on output buffer full */ |
| 40 | /* 0x00000004 - 0x00000008 unused */ |
| 41 | |
| 42 | /* Flags indicating why reading input data are blocked. */ |
| 43 | #define H1C_F_IN_ALLOC 0x00000010 /* mux is blocked on lack of input buffer */ |
| 44 | #define H1C_F_IN_FULL 0x00000020 /* mux is blocked on input buffer full */ |
Christopher Faulet | f36e72b | 2019-12-05 11:18:31 +0100 | [diff] [blame] | 45 | #define H1C_F_IN_BUSY 0x00000040 /* mux is blocked on input waiting the other side */ |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 46 | /* 0x00000040 - 0x00000400 unused */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 47 | |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 48 | #define H1C_F_CS_WAIT_CONN 0x00000800 /* waiting for the connection establishment */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 49 | #define H1C_F_CS_ERROR 0x00001000 /* connection must be closed ASAP because an error occurred */ |
| 50 | #define H1C_F_CS_SHUTW_NOW 0x00002000 /* connection must be shut down for writes ASAP */ |
Christopher Faulet | f36e72b | 2019-12-05 11:18:31 +0100 | [diff] [blame] | 51 | #define H1C_F_CS_SHUTDOWN 0x00004000 /* connection is shut down */ |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 52 | #define H1C_F_CS_IDLE 0x00008000 /* connection is idle and may be reused |
| 53 | * (exclusive to all H1C_F_CS flags and never set when an h1s is attached) */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 54 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 55 | #define H1C_F_WAIT_NEXT_REQ 0x00010000 /* waiting for the next request to start, use keep-alive timeout */ |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 56 | #define H1C_F_UPG_H2C 0x00020000 /* set if an upgrade to h2 should be done */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 57 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 58 | /* |
| 59 | * H1 Stream flags (32 bits) |
| 60 | */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 61 | #define H1S_F_NONE 0x00000000 |
| 62 | #define H1S_F_ERROR 0x00000001 /* An error occurred on the H1 stream */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 63 | #define H1S_F_REQ_ERROR 0x00000002 /* An error occurred during the request parsing/xfer */ |
| 64 | #define H1S_F_RES_ERROR 0x00000004 /* An error occurred during the response parsing/xfer */ |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 65 | #define H1S_F_REOS 0x00000008 /* End of input stream seen even if not delivered yet */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 66 | #define H1S_F_WANT_KAL 0x00000010 |
| 67 | #define H1S_F_WANT_TUN 0x00000020 |
| 68 | #define H1S_F_WANT_CLO 0x00000040 |
| 69 | #define H1S_F_WANT_MSK 0x00000070 |
| 70 | #define H1S_F_NOT_FIRST 0x00000080 /* The H1 stream is not the first one */ |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 71 | #define H1S_F_BUF_FLUSH 0x00000100 /* Flush input buffer and don't read more data */ |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 72 | #define H1S_F_SPLICED_DATA 0x00000200 /* Set when the kernel splicing is in used */ |
Willy Tarreau | 34d2348 | 2019-01-03 17:46:56 +0100 | [diff] [blame] | 73 | #define H1S_F_HAVE_I_TLR 0x00000800 /* Set during input process to know the trailers were processed */ |
Willy Tarreau | 6203815 | 2019-08-23 09:29:29 +0200 | [diff] [blame] | 74 | #define H1S_F_APPEND_EOM 0x00001000 /* Send EOM to the HTX buffer */ |
| 75 | /* 0x00002000 .. 0x00002000 unused */ |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 76 | #define H1S_F_HAVE_O_CONN 0x00004000 /* Set during output process to know connection mode was processed */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 77 | |
| 78 | /* H1 connection descriptor */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 79 | struct h1c { |
| 80 | struct connection *conn; |
| 81 | struct proxy *px; |
| 82 | uint32_t flags; /* Connection flags: H1C_F_* */ |
| 83 | |
| 84 | struct buffer ibuf; /* Input buffer to store data before parsing */ |
| 85 | struct buffer obuf; /* Output buffer to store data after reformatting */ |
| 86 | |
| 87 | struct buffer_wait buf_wait; /* Wait list for buffer allocation */ |
| 88 | struct wait_event wait_event; /* To be used if we're waiting for I/Os */ |
| 89 | |
| 90 | struct h1s *h1s; /* H1 stream descriptor */ |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 91 | struct task *task; /* timeout management task */ |
| 92 | int timeout; /* idle timeout duration in ticks */ |
| 93 | int shut_timeout; /* idle timeout duration in ticks after stream shutdown */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | /* H1 stream descriptor */ |
| 97 | struct h1s { |
| 98 | struct h1c *h1c; |
| 99 | struct conn_stream *cs; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 100 | struct cs_info csinfo; /* CS info, only used for client connections */ |
| 101 | uint32_t flags; /* Connection flags: H1S_F_* */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 102 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 103 | struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */ |
| 104 | struct wait_event *send_wait; /* Address of the wait_event the conn_stream associated is waiting on */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 105 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 106 | struct session *sess; /* Associated session */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 107 | struct h1m req; |
| 108 | struct h1m res; |
| 109 | |
| 110 | enum http_meth_t meth; /* HTTP resquest method */ |
| 111 | uint16_t status; /* HTTP response status */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 112 | }; |
| 113 | |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 114 | /* Map of headers used to convert outgoing headers */ |
| 115 | struct h1_hdrs_map { |
| 116 | char *name; |
| 117 | struct eb_root map; |
| 118 | }; |
| 119 | |
| 120 | /* An entry in a headers map */ |
| 121 | struct h1_hdr_entry { |
| 122 | struct ist name; |
| 123 | struct ebpt_node node; |
| 124 | }; |
| 125 | |
| 126 | /* Declare the headers map */ |
| 127 | static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT }; |
| 128 | |
| 129 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 130 | /* the h1c and h1s pools */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 131 | DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c)); |
| 132 | DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s)); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 133 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 134 | static int h1_recv(struct h1c *h1c); |
| 135 | static int h1_send(struct h1c *h1c); |
| 136 | static int h1_process(struct h1c *h1c); |
| 137 | static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short state); |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 138 | static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 139 | static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 140 | |
| 141 | /*****************************************************/ |
| 142 | /* functions below are for dynamic buffer management */ |
| 143 | /*****************************************************/ |
| 144 | /* |
Christopher Faulet | 7caf150 | 2019-12-05 12:30:55 +0100 | [diff] [blame] | 145 | * Indicates whether or not we may receive data. The rules are the following : |
| 146 | * - if an error or a shutdown for reads was detected on the connection we |
| 147 | must not attempt to receive |
| 148 | * - if the input buffer failed to be allocated or is full , we must not try |
| 149 | * to receive |
| 150 | * - if he input processing is busy waiting for the output side, we may |
| 151 | * attemp to receive |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 152 | * - otherwise must may not attempt to receive |
| 153 | */ |
| 154 | static inline int h1_recv_allowed(const struct h1c *h1c) |
| 155 | { |
Christopher Faulet | 7caf150 | 2019-12-05 12:30:55 +0100 | [diff] [blame] | 156 | if (h1c->flags & H1C_F_CS_ERROR) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 157 | return 0; |
| 158 | |
Christopher Faulet | 7caf150 | 2019-12-05 12:30:55 +0100 | [diff] [blame] | 159 | if (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH)) |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 160 | return 0; |
| 161 | |
Christopher Faulet | cb55f48 | 2018-12-10 11:56:47 +0100 | [diff] [blame] | 162 | if (!(h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_BUSY))) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 163 | return 1; |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Tries to grab a buffer and to re-enables processing on mux <target>. The h1 |
| 170 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 171 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 172 | * impossible to wake up and we prefer to be woken up later. |
| 173 | */ |
| 174 | static int h1_buf_available(void *target) |
| 175 | { |
| 176 | struct h1c *h1c = target; |
| 177 | |
| 178 | if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc_margin(&h1c->ibuf, 0)) { |
| 179 | h1c->flags &= ~H1C_F_IN_ALLOC; |
| 180 | if (h1_recv_allowed(h1c)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 181 | tasklet_wakeup(h1c->wait_event.tasklet); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 182 | return 1; |
| 183 | } |
| 184 | |
| 185 | if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc_margin(&h1c->obuf, 0)) { |
| 186 | h1c->flags &= ~H1C_F_OUT_ALLOC; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 187 | tasklet_wakeup(h1c->wait_event.tasklet); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 188 | return 1; |
| 189 | } |
| 190 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Allocate a buffer. If if fails, it adds the mux in buffer wait queue. |
| 196 | */ |
| 197 | static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr) |
| 198 | { |
| 199 | struct buffer *buf = NULL; |
| 200 | |
| 201 | if (likely(LIST_ISEMPTY(&h1c->buf_wait.list)) && |
| 202 | unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) { |
| 203 | h1c->buf_wait.target = h1c; |
| 204 | h1c->buf_wait.wakeup_cb = h1_buf_available; |
| 205 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 206 | LIST_ADDQ(&buffer_wq, &h1c->buf_wait.list); |
| 207 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 208 | __conn_xprt_stop_recv(h1c->conn); |
| 209 | } |
| 210 | return buf; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Release a buffer, if any, and try to wake up entities waiting in the buffer |
| 215 | * wait queue. |
| 216 | */ |
| 217 | static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr) |
| 218 | { |
| 219 | if (bptr->size) { |
| 220 | b_free(bptr); |
| 221 | offer_buffers(h1c->buf_wait.target, tasks_run_queue); |
| 222 | } |
| 223 | } |
| 224 | |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 225 | /* returns the number of streams in use on a connection to figure if it's idle |
| 226 | * or not. We rely on H1C_F_CS_IDLE to know if the connection is in-use or |
| 227 | * not. This flag is only set when no H1S is attached and when the previous |
| 228 | * stream, if any, was fully terminated without any error and in K/A mode. |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 229 | */ |
| 230 | static int h1_used_streams(struct connection *conn) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 231 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 232 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 233 | |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 234 | return ((h1c->flags & H1C_F_CS_IDLE) ? 0 : 1); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 237 | /* returns the number of streams still available on a connection */ |
| 238 | static int h1_avail_streams(struct connection *conn) |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 239 | { |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 240 | return 1 - h1_used_streams(conn); |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 241 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 242 | |
Christopher Faulet | b576206 | 2020-08-05 11:31:16 +0200 | [diff] [blame^] | 243 | /* Refresh the h1c task timeout if necessary */ |
| 244 | static void h1_refresh_timeout(struct h1c *h1c) |
| 245 | { |
| 246 | if (h1c->task) { |
| 247 | h1c->task->expire = TICK_ETERNITY; |
| 248 | if ((!h1c->h1s && !conn_is_back(h1c->conn)) || b_data(&h1c->obuf)) { |
| 249 | /* front connections waiting for a stream, as well as any connection with |
| 250 | * pending data, need a timeout. |
| 251 | */ |
| 252 | h1c->task->expire = tick_add(now_ms, ((h1c->flags & (H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN)) |
| 253 | ? h1c->shut_timeout |
| 254 | : h1c->timeout)); |
| 255 | task_queue(h1c->task); |
| 256 | } |
| 257 | } |
| 258 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 259 | /*****************************************************************/ |
| 260 | /* functions below are dedicated to the mux setup and management */ |
| 261 | /*****************************************************************/ |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 262 | |
| 263 | /* returns non-zero if there are input data pending for stream h1s. */ |
| 264 | static inline size_t h1s_data_pending(const struct h1s *h1s) |
| 265 | { |
| 266 | const struct h1m *h1m; |
| 267 | |
| 268 | h1m = conn_is_back(h1s->h1c->conn) ? &h1s->res : &h1s->req; |
| 269 | if (h1m->state == H1_MSG_DONE) |
| 270 | return 0; // data not for this stream (e.g. pipelining) |
| 271 | |
| 272 | return b_data(&h1s->h1c->ibuf); |
| 273 | } |
| 274 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 275 | static struct conn_stream *h1s_new_cs(struct h1s *h1s) |
| 276 | { |
| 277 | struct conn_stream *cs; |
| 278 | |
| 279 | cs = cs_new(h1s->h1c->conn); |
| 280 | if (!cs) |
| 281 | goto err; |
| 282 | h1s->cs = cs; |
| 283 | cs->ctx = h1s; |
| 284 | |
| 285 | if (h1s->flags & H1S_F_NOT_FIRST) |
| 286 | cs->flags |= CS_FL_NOT_FIRST; |
| 287 | |
Willy Tarreau | 7e9dd73 | 2020-01-17 16:19:34 +0100 | [diff] [blame] | 288 | if (global.tune.options & GTUNE_USE_SPLICE) |
| 289 | cs->flags |= CS_FL_MAY_SPLICE; |
| 290 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 291 | if (stream_create_from_cs(cs) < 0) |
| 292 | goto err; |
| 293 | return cs; |
| 294 | |
| 295 | err: |
| 296 | cs_free(cs); |
| 297 | h1s->cs = NULL; |
| 298 | return NULL; |
| 299 | } |
| 300 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 301 | static struct h1s *h1s_create(struct h1c *h1c, struct conn_stream *cs, struct session *sess) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 302 | { |
| 303 | struct h1s *h1s; |
| 304 | |
| 305 | h1s = pool_alloc(pool_head_h1s); |
| 306 | if (!h1s) |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 307 | goto fail; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 308 | |
| 309 | h1s->h1c = h1c; |
| 310 | h1c->h1s = h1s; |
| 311 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 312 | h1s->sess = sess; |
| 313 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 314 | h1s->cs = NULL; |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 315 | h1s->flags = H1S_F_WANT_KAL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 316 | |
| 317 | h1s->recv_wait = NULL; |
| 318 | h1s->send_wait = NULL; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 319 | |
| 320 | h1m_init_req(&h1s->req); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 321 | h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 322 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 323 | h1m_init_res(&h1s->res); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 324 | h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 325 | |
| 326 | h1s->status = 0; |
| 327 | h1s->meth = HTTP_METH_OTHER; |
| 328 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 329 | if (h1c->flags & H1C_F_WAIT_NEXT_REQ) |
| 330 | h1s->flags |= H1S_F_NOT_FIRST; |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 331 | h1c->flags &= ~(H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 332 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 333 | if (!conn_is_back(h1c->conn)) { |
| 334 | if (h1c->px->options2 & PR_O2_REQBUG_OK) |
| 335 | h1s->req.err_pos = -1; |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 336 | |
| 337 | /* For frontend connections we should always have a session */ |
| 338 | if (!sess) |
| 339 | sess = h1c->conn->owner; |
| 340 | |
Dave Pirotte | cf67ec5 | 2019-07-10 13:57:38 +0000 | [diff] [blame] | 341 | /* Timers for subsequent sessions on the same HTTP 1.x connection |
| 342 | * measure from `now`, not from the connection accept time */ |
| 343 | if (h1s->flags & H1S_F_NOT_FIRST) { |
| 344 | h1s->csinfo.create_date = date; |
| 345 | h1s->csinfo.tv_create = now; |
| 346 | h1s->csinfo.t_handshake = 0; |
| 347 | h1s->csinfo.t_idle = -1; |
| 348 | } |
| 349 | else { |
| 350 | h1s->csinfo.create_date = sess->accept_date; |
| 351 | h1s->csinfo.tv_create = sess->tv_accept; |
| 352 | h1s->csinfo.t_handshake = sess->t_handshake; |
| 353 | h1s->csinfo.t_idle = -1; |
| 354 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 355 | } |
| 356 | else { |
| 357 | if (h1c->px->options2 & PR_O2_RSPBUG_OK) |
| 358 | h1s->res.err_pos = -1; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 359 | |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 360 | h1s->csinfo.create_date = date; |
| 361 | h1s->csinfo.tv_create = now; |
| 362 | h1s->csinfo.t_handshake = 0; |
| 363 | h1s->csinfo.t_idle = -1; |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 364 | } |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 365 | |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 366 | /* If a conn_stream already exists, attach it to this H1S. Otherwise we |
| 367 | * create a new one. |
| 368 | */ |
| 369 | if (cs) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 370 | cs->ctx = h1s; |
| 371 | h1s->cs = cs; |
| 372 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 373 | else { |
| 374 | cs = h1s_new_cs(h1s); |
| 375 | if (!cs) |
| 376 | goto fail; |
| 377 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 378 | return h1s; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 379 | |
| 380 | fail: |
| 381 | pool_free(pool_head_h1s, h1s); |
| 382 | return NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | static void h1s_destroy(struct h1s *h1s) |
| 386 | { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 387 | if (h1s) { |
| 388 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 389 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 390 | h1c->h1s = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 391 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 392 | if (h1s->recv_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 393 | h1s->recv_wait->events &= ~SUB_RETRY_RECV; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 394 | if (h1s->send_wait != NULL) |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 395 | h1s->send_wait->events &= ~SUB_RETRY_SEND; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 396 | |
Christopher Faulet | cb55f48 | 2018-12-10 11:56:47 +0100 | [diff] [blame] | 397 | h1c->flags &= ~H1C_F_IN_BUSY; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 398 | if (h1s->flags & (H1S_F_REQ_ERROR|H1S_F_RES_ERROR)) |
| 399 | h1c->flags |= H1C_F_CS_ERROR; |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 400 | |
| 401 | if (!(h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW_NOW|H1C_F_CS_SHUTDOWN)) && /* No error/shutdown on h1c */ |
| 402 | !(h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) && /* No error/shutdown on conn */ |
| 403 | (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */ |
| 404 | h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */ |
| 405 | h1c->flags |= (H1C_F_CS_IDLE|H1C_F_WAIT_NEXT_REQ); |
| 406 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 407 | pool_free(pool_head_h1s, h1s); |
| 408 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 409 | } |
| 410 | |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 411 | static const struct cs_info *h1_get_cs_info(struct conn_stream *cs) |
| 412 | { |
| 413 | struct h1s *h1s = cs->ctx; |
| 414 | |
| 415 | if (h1s && !conn_is_back(cs->conn)) |
| 416 | return &h1s->csinfo; |
| 417 | return NULL; |
| 418 | } |
| 419 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 420 | /* |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 421 | * Initialize the mux once it's attached. It is expected that conn->ctx points |
| 422 | * to the existing conn_stream (for outgoing connections or for incoming onces |
| 423 | * during a mux upgrade) or NULL (for incoming ones during the connexion |
| 424 | * establishment). <input> is always used as Input buffer and may contain |
| 425 | * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on |
| 426 | * error. |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 427 | */ |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 428 | static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess, |
| 429 | struct buffer *input) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 430 | { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 431 | struct h1c *h1c; |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 432 | struct task *t = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 433 | |
| 434 | h1c = pool_alloc(pool_head_h1c); |
| 435 | if (!h1c) |
| 436 | goto fail_h1c; |
| 437 | h1c->conn = conn; |
| 438 | h1c->px = proxy; |
| 439 | |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 440 | h1c->flags = H1C_F_CS_IDLE; |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 441 | h1c->ibuf = *input; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 442 | h1c->obuf = BUF_NULL; |
| 443 | h1c->h1s = NULL; |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 444 | h1c->task = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 445 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 446 | LIST_INIT(&h1c->buf_wait.list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 447 | h1c->wait_event.tasklet = tasklet_new(); |
| 448 | if (!h1c->wait_event.tasklet) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 449 | goto fail; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 450 | h1c->wait_event.tasklet->process = h1_io_cb; |
| 451 | h1c->wait_event.tasklet->context = h1c; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 452 | h1c->wait_event.events = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 453 | |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 454 | if (conn_is_back(conn)) { |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 455 | h1c->shut_timeout = h1c->timeout = proxy->timeout.server; |
| 456 | if (tick_isset(proxy->timeout.serverfin)) |
| 457 | h1c->shut_timeout = proxy->timeout.serverfin; |
| 458 | } else { |
| 459 | h1c->shut_timeout = h1c->timeout = proxy->timeout.client; |
| 460 | if (tick_isset(proxy->timeout.clientfin)) |
| 461 | h1c->shut_timeout = proxy->timeout.clientfin; |
| 462 | } |
| 463 | if (tick_isset(h1c->timeout)) { |
| 464 | t = task_new(tid_bit); |
| 465 | if (!t) |
| 466 | goto fail; |
| 467 | |
| 468 | h1c->task = t; |
| 469 | t->process = h1_timeout_task; |
| 470 | t->context = h1c; |
| 471 | t->expire = tick_add(now_ms, h1c->timeout); |
| 472 | } |
| 473 | |
Olivier Houchard | 985234d | 2019-06-13 17:54:33 +0200 | [diff] [blame] | 474 | if (!(conn->flags & CO_FL_CONNECTED) || (conn->flags & CO_FL_HANDSHAKE)) |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 475 | h1c->flags |= H1C_F_CS_WAIT_CONN; |
| 476 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 477 | /* Always Create a new H1S */ |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 478 | if (!h1s_create(h1c, conn->ctx, sess)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 479 | goto fail; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 480 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 481 | conn->ctx = h1c; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 482 | |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 483 | |
| 484 | if (t) |
| 485 | task_queue(t); |
| 486 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 487 | /* Try to read, if nothing is available yet we'll just subscribe */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 488 | tasklet_wakeup(h1c->wait_event.tasklet); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 489 | |
| 490 | /* mux->wake will be called soon to complete the operation */ |
| 491 | return 0; |
| 492 | |
| 493 | fail: |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 494 | task_destroy(t); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 495 | if (h1c->wait_event.tasklet) |
| 496 | tasklet_free(h1c->wait_event.tasklet); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 497 | pool_free(pool_head_h1c, h1c); |
| 498 | fail_h1c: |
| 499 | return -1; |
| 500 | } |
| 501 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 502 | /* release function. This one should be called to free all resources allocated |
| 503 | * to the mux. |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 504 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 505 | static void h1_release(struct h1c *h1c) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 506 | { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 507 | struct connection *conn = NULL; |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 508 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 509 | if (h1c) { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 510 | /* The connection must be aattached to this mux to be released */ |
| 511 | if (h1c->conn && h1c->conn->ctx == h1c) |
| 512 | conn = h1c->conn; |
| 513 | |
| 514 | if (conn && h1c->flags & H1C_F_UPG_H2C) { |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 515 | h1c->flags &= ~H1C_F_UPG_H2C; |
Olivier Houchard | 13f556e | 2019-07-03 13:08:18 +0200 | [diff] [blame] | 516 | /* Make sure we're no longer subscribed to anything */ |
| 517 | if (h1c->wait_event.events) |
| 518 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, |
| 519 | h1c->wait_event.events, &h1c->wait_event); |
Olivier Houchard | 45c4437 | 2019-06-11 14:06:23 +0200 | [diff] [blame] | 520 | if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTX) != -1) { |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 521 | /* connection successfully upgraded to H2, this |
| 522 | * mux was already released */ |
| 523 | return; |
| 524 | } |
| 525 | sess_log(conn->owner); /* Log if the upgrade failed */ |
| 526 | } |
| 527 | |
Olivier Houchard | 45c4437 | 2019-06-11 14:06:23 +0200 | [diff] [blame] | 528 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 529 | if (!LIST_ISEMPTY(&h1c->buf_wait.list)) { |
| 530 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 531 | LIST_DEL(&h1c->buf_wait.list); |
| 532 | LIST_INIT(&h1c->buf_wait.list); |
| 533 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 534 | } |
| 535 | |
| 536 | h1_release_buf(h1c, &h1c->ibuf); |
| 537 | h1_release_buf(h1c, &h1c->obuf); |
| 538 | |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 539 | if (h1c->task) { |
| 540 | h1c->task->context = NULL; |
| 541 | task_wakeup(h1c->task, TASK_WOKEN_OTHER); |
| 542 | h1c->task = NULL; |
| 543 | } |
| 544 | |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 545 | if (h1c->wait_event.tasklet) |
| 546 | tasklet_free(h1c->wait_event.tasklet); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 547 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 548 | h1s_destroy(h1c->h1s); |
Olivier Houchard | 0e07937 | 2019-04-15 17:51:16 +0200 | [diff] [blame] | 549 | if (conn && h1c->wait_event.events != 0) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 550 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events, |
Olivier Houchard | 0e07937 | 2019-04-15 17:51:16 +0200 | [diff] [blame] | 551 | &h1c->wait_event); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 552 | pool_free(pool_head_h1c, h1c); |
| 553 | } |
| 554 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 555 | if (conn) { |
| 556 | conn->mux = NULL; |
| 557 | conn->ctx = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 558 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 559 | conn_stop_tracking(conn); |
| 560 | conn_full_close(conn); |
| 561 | if (conn->destroy_cb) |
| 562 | conn->destroy_cb(conn); |
| 563 | conn_free(conn); |
| 564 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | /******************************************************/ |
| 568 | /* functions below are for the H1 protocol processing */ |
| 569 | /******************************************************/ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 570 | /* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is |
| 571 | * greater or equal to 1.1 |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 572 | */ |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 573 | static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 574 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 575 | const char *p = HTX_SL_REQ_VPTR(sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 576 | |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 577 | if ((HTX_SL_REQ_VLEN(sl) == 8) && |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 578 | (*(p + 5) > '1' || |
| 579 | (*(p + 5) == '1' && *(p + 7) >= '1'))) |
| 580 | h1m->flags |= H1_MF_VER_11; |
| 581 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 582 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 583 | /* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is |
| 584 | * greater or equal to 1.1 |
| 585 | */ |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 586 | static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 587 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 588 | const char *p = HTX_SL_RES_VPTR(sl); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 589 | |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 590 | if ((HTX_SL_RES_VLEN(sl) == 8) && |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 591 | (*(p + 5) > '1' || |
| 592 | (*(p + 5) == '1' && *(p + 7) >= '1'))) |
| 593 | h1m->flags |= H1_MF_VER_11; |
| 594 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 595 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 596 | /* |
| 597 | * Check the validity of the request version. If the version is valid, it |
| 598 | * returns 1. Otherwise, it returns 0. |
| 599 | */ |
| 600 | static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl) |
| 601 | { |
| 602 | struct h1c *h1c = h1s->h1c; |
| 603 | |
| 604 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 605 | * exactly one digit "." one digit. This check may be disabled using |
| 606 | * option accept-invalid-http-request. |
| 607 | */ |
| 608 | if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) { |
| 609 | if (sl.rq.v.len != 8) |
| 610 | return 0; |
| 611 | |
| 612 | if (*(sl.rq.v.ptr + 4) != '/' || |
| 613 | !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) || |
| 614 | *(sl.rq.v.ptr + 6) != '.' || |
| 615 | !isdigit((unsigned char)*(sl.rq.v.ptr + 7))) |
| 616 | return 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 617 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 618 | else if (!sl.rq.v.len) { |
| 619 | /* try to convert HTTP/0.9 requests to HTTP/1.0 */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 620 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 621 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 622 | if (sl.rq.meth != HTTP_METH_GET) |
| 623 | return 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 624 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 625 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 626 | if (!sl.rq.u.len) |
| 627 | return 0; |
| 628 | |
| 629 | /* Add HTTP version */ |
| 630 | sl.rq.v = ist("HTTP/1.0"); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 631 | return 1; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 632 | } |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 633 | |
| 634 | if ((sl.rq.v.len == 8) && |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 635 | ((*(sl.rq.v.ptr + 5) > '1') || |
| 636 | ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1')))) |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 637 | h1m->flags |= H1_MF_VER_11; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 638 | return 1; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 639 | } |
| 640 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 641 | /* |
| 642 | * Check the validity of the response version. If the version is valid, it |
| 643 | * returns 1. Otherwise, it returns 0. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 644 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 645 | static int h1_process_res_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 646 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 647 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 648 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 649 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 650 | * exactly one digit "." one digit. This check may be disabled using |
| 651 | * option accept-invalid-http-request. |
| 652 | */ |
| 653 | if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) { |
| 654 | if (sl.st.v.len != 8) |
| 655 | return 0; |
| 656 | |
| 657 | if (*(sl.st.v.ptr + 4) != '/' || |
| 658 | !isdigit((unsigned char)*(sl.st.v.ptr + 5)) || |
| 659 | *(sl.st.v.ptr + 6) != '.' || |
| 660 | !isdigit((unsigned char)*(sl.st.v.ptr + 7))) |
| 661 | return 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 662 | } |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 663 | |
| 664 | if ((sl.st.v.len == 8) && |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 665 | ((*(sl.st.v.ptr + 5) > '1') || |
| 666 | ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1')))) |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 667 | h1m->flags |= H1_MF_VER_11; |
| 668 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 669 | return 1; |
| 670 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 671 | |
| 672 | /* Deduce the connection mode of the client connection, depending on the |
| 673 | * configuration and the H1 message flags. This function is called twice, the |
| 674 | * first time when the request is parsed and the second time when the response |
| 675 | * is parsed. |
| 676 | */ |
| 677 | static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m) |
| 678 | { |
| 679 | struct proxy *fe = h1s->h1c->px; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 680 | |
| 681 | if (h1m->flags & H1_MF_RESP) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 682 | /* Output direction: second pass */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 683 | if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 684 | h1s->status == 101) { |
| 685 | /* Either we've established an explicit tunnel, or we're |
| 686 | * switching the protocol. In both cases, we're very unlikely to |
| 687 | * understand the next protocols. We have to switch to tunnel |
| 688 | * mode, so that we transfer the request and responses then let |
| 689 | * this protocol pass unmodified. When we later implement |
| 690 | * specific parsers for such protocols, we'll want to check the |
| 691 | * Upgrade header which contains information about that protocol |
| 692 | * for responses with status 101 (eg: see RFC2817 about TLS). |
| 693 | */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 694 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN; |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 695 | } |
| 696 | else if (h1s->flags & H1S_F_WANT_KAL) { |
| 697 | /* By default the client is in KAL mode. CLOSE mode mean |
| 698 | * it is imposed by the client itself. So only change |
| 699 | * KAL mode here. */ |
| 700 | if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) { |
| 701 | /* no length known or explicit close => close */ |
| 702 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 703 | } |
| 704 | else if (!(h1m->flags & H1_MF_CONN_KAL) && |
| 705 | (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) { |
| 706 | /* no explict keep-alive and option httpclose => close */ |
| 707 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 708 | } |
| 709 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 710 | } |
| 711 | else { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 712 | /* Input direction: first pass */ |
| 713 | if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) { |
| 714 | /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 715 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 716 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */ |
| 720 | if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED) |
| 721 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 722 | } |
| 723 | |
| 724 | /* Deduce the connection mode of the client connection, depending on the |
| 725 | * configuration and the H1 message flags. This function is called twice, the |
| 726 | * first time when the request is parsed and the second time when the response |
| 727 | * is parsed. |
| 728 | */ |
| 729 | static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m) |
| 730 | { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 731 | struct session *sess = h1s->sess; |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 732 | struct proxy *be = h1s->h1c->px; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 733 | int fe_flags = sess ? sess->fe->options : 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 734 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 735 | if (h1m->flags & H1_MF_RESP) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 736 | /* Input direction: second pass */ |
| 737 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 738 | if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 739 | h1s->status == 101) { |
| 740 | /* Either we've established an explicit tunnel, or we're |
| 741 | * switching the protocol. In both cases, we're very unlikely to |
| 742 | * understand the next protocols. We have to switch to tunnel |
| 743 | * mode, so that we transfer the request and responses then let |
| 744 | * this protocol pass unmodified. When we later implement |
| 745 | * specific parsers for such protocols, we'll want to check the |
| 746 | * Upgrade header which contains information about that protocol |
| 747 | * for responses with status 101 (eg: see RFC2817 about TLS). |
| 748 | */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 749 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN; |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 750 | } |
| 751 | else if (h1s->flags & H1S_F_WANT_KAL) { |
| 752 | /* By default the server is in KAL mode. CLOSE mode mean |
| 753 | * it is imposed by haproxy itself. So only change KAL |
| 754 | * mode here. */ |
| 755 | if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO || |
| 756 | !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){ |
| 757 | /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */ |
| 758 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 759 | } |
| 760 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 761 | } |
Christopher Faulet | 7003378 | 2018-12-05 13:50:11 +0100 | [diff] [blame] | 762 | else { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 763 | /* Output direction: first pass */ |
| 764 | if (h1m->flags & H1_MF_CONN_CLO) { |
| 765 | /* explicit close => close */ |
Christopher Faulet | 7003378 | 2018-12-05 13:50:11 +0100 | [diff] [blame] | 766 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 767 | } |
| 768 | else if (!(h1m->flags & H1_MF_CONN_KAL) && |
| 769 | ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
| 770 | (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
| 771 | (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO || |
| 772 | (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) { |
| 773 | /* no explicit keep-alive option httpclose/server-close => close */ |
| 774 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 775 | } |
Christopher Faulet | 7003378 | 2018-12-05 13:50:11 +0100 | [diff] [blame] | 776 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 777 | |
| 778 | /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */ |
| 779 | if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED) |
| 780 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 781 | } |
| 782 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 783 | static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 784 | { |
| 785 | struct proxy *px = h1s->h1c->px; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 786 | |
| 787 | /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage" |
| 788 | * token is found |
| 789 | */ |
| 790 | if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 791 | return; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 792 | |
| 793 | if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 794 | if (!(h1m->flags & H1_MF_VER_11)) |
| 795 | *conn_val = ist("keep-alive"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 796 | } |
| 797 | else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */ |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 798 | if (h1m->flags & H1_MF_VER_11) |
| 799 | *conn_val = ist("close"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 800 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 801 | } |
| 802 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 803 | static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 804 | { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 805 | /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage" |
| 806 | * token is found |
| 807 | */ |
| 808 | if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 809 | return; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 810 | |
| 811 | if (h1s->flags & H1S_F_WANT_KAL) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 812 | if (!(h1m->flags & H1_MF_VER_11) || |
| 813 | !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) |
| 814 | *conn_val = ist("keep-alive"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 815 | } |
| 816 | else { /* H1S_F_WANT_CLO */ |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 817 | if (h1m->flags & H1_MF_VER_11) |
| 818 | *conn_val = ist("close"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 819 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 820 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 821 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 822 | static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 823 | { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 824 | if (!conn_is_back(h1s->h1c->conn)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 825 | h1_set_cli_conn_mode(h1s, h1m); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 826 | else |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 827 | h1_set_srv_conn_mode(h1s, h1m); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 828 | } |
| 829 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 830 | static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val) |
| 831 | { |
| 832 | if (!conn_is_back(h1s->h1c->conn)) |
| 833 | h1_set_cli_conn_mode(h1s, h1m); |
| 834 | else |
| 835 | h1_set_srv_conn_mode(h1s, h1m); |
| 836 | |
| 837 | if (!(h1m->flags & H1_MF_RESP)) |
| 838 | h1_update_req_conn_value(h1s, h1m, conn_val); |
| 839 | else |
| 840 | h1_update_res_conn_value(h1s, h1m, conn_val); |
| 841 | } |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 842 | |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 843 | /* Try to adjust the case of the message header name using the global map |
| 844 | * <hdrs_map>. |
| 845 | */ |
| 846 | static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name) |
| 847 | { |
| 848 | struct ebpt_node *node; |
| 849 | struct h1_hdr_entry *entry; |
| 850 | |
| 851 | /* No entry in the map, do nothing */ |
| 852 | if (eb_is_empty(&hdrs_map.map)) |
| 853 | return; |
| 854 | |
| 855 | /* No conversion fo the request headers */ |
| 856 | if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV)) |
| 857 | return; |
| 858 | |
| 859 | /* No conversion fo the response headers */ |
| 860 | if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI)) |
| 861 | return; |
| 862 | |
| 863 | node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len); |
| 864 | if (!node) |
| 865 | return; |
| 866 | entry = container_of(node, struct h1_hdr_entry, node); |
| 867 | name->ptr = entry->name.ptr; |
| 868 | name->len = entry->name.len; |
| 869 | } |
| 870 | |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 871 | /* Append the description of what is present in error snapshot <es> into <out>. |
| 872 | * The description must be small enough to always fit in a buffer. The output |
| 873 | * buffer may be the trash so the trash must not be used inside this function. |
| 874 | */ |
| 875 | static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es) |
| 876 | { |
| 877 | chunk_appendf(out, |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 878 | " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n" |
| 879 | " H1 msg state %s(%d), H1 msg flags 0x%08x\n" |
| 880 | " H1 chunk len %lld bytes, H1 body len %lld bytes :\n", |
| 881 | es->ctx.h1.c_flags, es->ctx.h1.s_flags, |
| 882 | h1m_state_str(es->ctx.h1.state), es->ctx.h1.state, |
| 883 | es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen); |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 884 | } |
| 885 | /* |
| 886 | * Capture a bad request or response and archive it in the proxy's structure. |
| 887 | * By default it tries to report the error position as h1m->err_pos. However if |
| 888 | * this one is not set, it will then report h1m->next, which is the last known |
| 889 | * parsing point. The function is able to deal with wrapping buffers. It always |
| 890 | * displays buffers as a contiguous area starting at buf->p. The direction is |
| 891 | * determined thanks to the h1m's flags. |
| 892 | */ |
| 893 | static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s, |
| 894 | struct h1m *h1m, struct buffer *buf) |
| 895 | { |
| 896 | struct session *sess = h1c->conn->owner; |
| 897 | struct proxy *proxy = h1c->px; |
| 898 | struct proxy *other_end = sess->fe; |
| 899 | union error_snapshot_ctx ctx; |
| 900 | |
Willy Tarreau | 598d7fc | 2018-12-18 18:10:38 +0100 | [diff] [blame] | 901 | if (h1s->cs->data && !(h1m->flags & H1_MF_RESP)) |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 902 | other_end = si_strm(h1s->cs->data)->be; |
| 903 | |
| 904 | /* http-specific part now */ |
| 905 | ctx.h1.state = h1m->state; |
| 906 | ctx.h1.c_flags = h1c->flags; |
| 907 | ctx.h1.s_flags = h1s->flags; |
| 908 | ctx.h1.m_flags = h1m->flags; |
| 909 | ctx.h1.m_clen = h1m->curr_len; |
| 910 | ctx.h1.m_blen = h1m->body_len; |
| 911 | |
| 912 | proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end, |
| 913 | h1c->conn->target, sess, buf, 0, 0, |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 914 | (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next, |
| 915 | &ctx, h1_show_error_snapshot); |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 916 | } |
| 917 | |
Christopher Faulet | adb2220 | 2018-12-12 10:32:09 +0100 | [diff] [blame] | 918 | /* Emit the chunksize followed by a CRLF in front of data of the buffer |
| 919 | * <buf>. It goes backwards and starts with the byte before the buffer's |
| 920 | * head. The caller is responsible for ensuring there is enough room left before |
| 921 | * the buffer's head for the string. |
| 922 | */ |
| 923 | static void h1_emit_chunk_size(struct buffer *buf, size_t chksz) |
| 924 | { |
| 925 | char *beg, *end; |
| 926 | |
| 927 | beg = end = b_head(buf); |
| 928 | *--beg = '\n'; |
| 929 | *--beg = '\r'; |
| 930 | do { |
| 931 | *--beg = hextab[chksz & 0xF]; |
| 932 | } while (chksz >>= 4); |
| 933 | buf->head -= (end - beg); |
| 934 | b_add(buf, end - beg); |
| 935 | } |
| 936 | |
| 937 | /* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for |
| 938 | * ensuring there is enough room left in the buffer for the string. */ |
| 939 | static void h1_emit_chunk_crlf(struct buffer *buf) |
| 940 | { |
| 941 | *(b_peek(buf, b_data(buf))) = '\r'; |
| 942 | *(b_peek(buf, b_data(buf) + 1)) = '\n'; |
| 943 | b_add(buf, 2); |
| 944 | } |
| 945 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 946 | /* |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 947 | * Switch the request to tunnel mode. This function must only be called for |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 948 | * CONNECT requests. On the client side, if the response is not finished, the |
| 949 | * mux is mark as busy on input. |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 950 | */ |
| 951 | static void h1_set_req_tunnel_mode(struct h1s *h1s) |
| 952 | { |
| 953 | h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK); |
| 954 | h1s->req.state = H1_MSG_TUNNEL; |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 955 | if (!conn_is_back(h1s->h1c->conn) && h1s->res.state < H1_MSG_DONE) |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 956 | h1s->h1c->flags |= H1C_F_IN_BUSY; |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 957 | else if (h1s->h1c->flags & H1C_F_IN_BUSY) { |
| 958 | h1s->h1c->flags &= ~H1C_F_IN_BUSY; |
| 959 | tasklet_wakeup(h1s->h1c->wait_event.tasklet); |
| 960 | } |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | /* |
| 964 | * Switch the response to tunnel mode. This function must only be called on |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 965 | * successfull replies to CONNECT requests or on protocol switching. In this |
| 966 | * last case, this function takes care to switch the request to tunnel mode if |
| 967 | * possible. On the server side, if the request is not finished, the mux is mark |
| 968 | * as busy on input. |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 969 | */ |
| 970 | static void h1_set_res_tunnel_mode(struct h1s *h1s) |
| 971 | { |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 972 | /* On protocol switching, switch the request to tunnel mode if it is in |
| 973 | * DONE state. Otherwise we will wait the end of the request to switch |
| 974 | * it in tunnel mode. |
| 975 | */ |
| 976 | if (h1s->status == 101 && h1s->req.state == H1_MSG_DONE) { |
| 977 | h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK); |
| 978 | h1s->req.state = H1_MSG_TUNNEL; |
| 979 | } |
| 980 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 981 | h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK); |
| 982 | h1s->res.state = H1_MSG_TUNNEL; |
| 983 | if (conn_is_back(h1s->h1c->conn) && h1s->req.state < H1_MSG_DONE) |
| 984 | h1s->h1c->flags |= H1C_F_IN_BUSY; |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 985 | else if (h1s->h1c->flags & H1C_F_IN_BUSY) { |
| 986 | h1s->h1c->flags &= ~H1C_F_IN_BUSY; |
| 987 | tasklet_wakeup(h1s->h1c->wait_event.tasklet); |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 988 | } |
| 989 | } |
| 990 | |
Christopher Faulet | 82f0160 | 2019-05-24 16:50:16 +0200 | [diff] [blame] | 991 | /* Estimate the size of the HTX headers after the parsing, including the EOH. */ |
| 992 | static size_t h1_eval_htx_hdrs_size(struct http_hdr *hdrs) |
| 993 | { |
| 994 | size_t sz = 0; |
| 995 | int i; |
| 996 | |
| 997 | for (i = 0; hdrs[i].n.len; i++) |
| 998 | sz += sizeof(struct htx_blk) + hdrs[i].n.len + hdrs[i].v.len; |
| 999 | sz += sizeof(struct htx_blk) + 1; |
| 1000 | return sz; |
| 1001 | } |
| 1002 | |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1003 | /* Estimate the size of the HTX request after the parsing. */ |
| 1004 | static size_t h1_eval_htx_req_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs) |
| 1005 | { |
| 1006 | size_t sz; |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1007 | |
| 1008 | /* size of the HTX start-line */ |
Christopher Faulet | 1d76cef | 2019-09-03 16:16:50 +0200 | [diff] [blame] | 1009 | sz = sizeof(struct htx_blk) + sizeof(struct htx_sl) + h1sl->rq.m.len + h1sl->rq.u.len + h1sl->rq.v.len; |
Christopher Faulet | 82f0160 | 2019-05-24 16:50:16 +0200 | [diff] [blame] | 1010 | sz += h1_eval_htx_hdrs_size(hdrs); |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1011 | return sz; |
| 1012 | } |
| 1013 | |
| 1014 | /* Estimate the size of the HTX response after the parsing. */ |
| 1015 | static size_t h1_eval_htx_res_size(struct h1m *h1m, union h1_sl *h1sl, struct http_hdr *hdrs) |
| 1016 | { |
| 1017 | size_t sz; |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1018 | |
| 1019 | /* size of the HTX start-line */ |
Christopher Faulet | 1d76cef | 2019-09-03 16:16:50 +0200 | [diff] [blame] | 1020 | sz = sizeof(struct htx_blk) + sizeof(struct htx_sl) + h1sl->st.v.len + h1sl->st.c.len + h1sl->st.r.len; |
Christopher Faulet | 82f0160 | 2019-05-24 16:50:16 +0200 | [diff] [blame] | 1021 | sz += h1_eval_htx_hdrs_size(hdrs); |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1022 | return sz; |
| 1023 | } |
| 1024 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1025 | /* |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1026 | * Add the EOM in the HTX message and switch the message to the DONE state. It |
| 1027 | * returns the number of bytes parsed if > 0, or 0 if iet couldn't proceed. This |
| 1028 | * functions is responsible to update the parser state <h1m>. It also add the |
| 1029 | * flag CS_FL_EOI on the CS. |
| 1030 | */ |
| 1031 | static size_t h1_process_eom(struct h1s *h1s, struct h1m *h1m, struct htx *htx, size_t max) |
| 1032 | { |
Willy Tarreau | 6203815 | 2019-08-23 09:29:29 +0200 | [diff] [blame] | 1033 | if (max < sizeof(struct htx_blk) + 1 || !htx_add_endof(htx, HTX_BLK_EOM)) { |
| 1034 | h1s->flags |= H1S_F_APPEND_EOM; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1035 | return 0; |
Willy Tarreau | 6203815 | 2019-08-23 09:29:29 +0200 | [diff] [blame] | 1036 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1037 | |
Willy Tarreau | 6203815 | 2019-08-23 09:29:29 +0200 | [diff] [blame] | 1038 | h1s->flags &= ~H1S_F_APPEND_EOM; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1039 | h1m->state = H1_MSG_DONE; |
| 1040 | h1s->cs->flags |= CS_FL_EOI; |
| 1041 | return (sizeof(struct htx_blk) + 1); |
| 1042 | } |
| 1043 | |
| 1044 | /* |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1045 | * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1046 | * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR |
| 1047 | * flag and filling h1s->err_pos and h1s->err_state fields. This functions is |
Joseph Herlant | 30bc509 | 2018-11-25 10:52:20 -0800 | [diff] [blame] | 1048 | * responsible to update the parser state <h1m>. |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1049 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1050 | static size_t h1_process_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx, |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1051 | struct buffer *buf, size_t *ofs, size_t max) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1052 | { |
Christopher Faulet | a39d8ad | 2019-05-15 15:54:39 +0200 | [diff] [blame] | 1053 | struct htx_sl *sl; |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 1054 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1055 | union h1_sl h1sl; |
| 1056 | unsigned int flags = HTX_SL_F_NONE; |
Christopher Faulet | a39d8ad | 2019-05-15 15:54:39 +0200 | [diff] [blame] | 1057 | size_t used; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1058 | int ret = 0; |
| 1059 | |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1060 | if (!max || !b_data(buf)) |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1061 | goto end; |
| 1062 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1063 | /* Realing input buffer if necessary */ |
| 1064 | if (b_head(buf) + b_data(buf) > b_wrap(buf)) |
| 1065 | b_slow_realign(buf, trash.area, 0); |
| 1066 | |
Christopher Faulet | 842f41f | 2019-09-25 09:10:46 +0200 | [diff] [blame] | 1067 | if (!(h1s->flags & H1S_F_NOT_FIRST) && !(h1m->flags & H1_MF_RESP)) { |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 1068 | /* Try to match H2 preface before parsing the request headers. */ |
| 1069 | ret = b_isteq(buf, 0, b_data(buf), ist(H2_CONN_PREFACE)); |
| 1070 | if (ret > 0) |
| 1071 | goto h2c_upgrade; |
| 1072 | } |
| 1073 | |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1074 | ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf), |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1075 | hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &h1sl); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1076 | if (ret <= 0) { |
Willy Tarreau | 9a408ab | 2019-08-23 08:11:36 +0200 | [diff] [blame] | 1077 | /* Incomplete or invalid message. If the input buffer only |
| 1078 | * contains headers and is full, which is detected by it being |
| 1079 | * full and the offset to be zero, it's an error because |
| 1080 | * headers are too large to be handled by the parser. */ |
| 1081 | if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf))) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1082 | goto error; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1083 | goto end; |
| 1084 | } |
| 1085 | |
Christopher Faulet | b4bad50 | 2019-10-11 14:22:00 +0200 | [diff] [blame] | 1086 | if (h1m->err_pos >= 0) { |
| 1087 | /* Maybe we found an error during the parsing while we were |
| 1088 | * configured not to block on that, so we have to capture it |
| 1089 | * now. |
| 1090 | */ |
| 1091 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
| 1092 | } |
| 1093 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1094 | /* messages headers fully parsed, do some checks to prepare the body |
| 1095 | * parsing. |
| 1096 | */ |
| 1097 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1098 | /* Save the request's method or the response's status, check if the body |
| 1099 | * length is known and check the VSN validity */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1100 | if (!(h1m->flags & H1_MF_RESP)) { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1101 | h1s->meth = h1sl.rq.meth; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1102 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1103 | /* By default, request have always a known length */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1104 | h1m->flags |= H1_MF_XFER_LEN; |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1105 | |
| 1106 | if (h1s->meth == HTTP_METH_CONNECT) { |
| 1107 | /* Switch CONNECT requests to tunnel mode */ |
| 1108 | h1_set_req_tunnel_mode(h1s); |
| 1109 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1110 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1111 | if (!h1_process_req_vsn(h1s, h1m, h1sl)) { |
| 1112 | h1m->err_pos = h1sl.rq.v.ptr - b_head(buf); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1113 | h1m->err_state = h1m->state; |
| 1114 | goto vsn_error; |
| 1115 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1116 | } |
| 1117 | else { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1118 | h1s->status = h1sl.st.status; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1119 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1120 | if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || |
| 1121 | h1s->status == 101) { |
| 1122 | /* Switch successfull replies to CONNECT requests and |
| 1123 | * protocol switching to tunnel mode. */ |
| 1124 | h1_set_res_tunnel_mode(h1s); |
| 1125 | } |
| 1126 | else if ((h1s->meth == HTTP_METH_HEAD) || |
| 1127 | (h1s->status >= 100 && h1s->status < 200) || |
| 1128 | (h1s->status == 204) || (h1s->status == 304)) { |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1129 | /* Responses known to have no body. */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1130 | h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK); |
| 1131 | h1m->flags |= H1_MF_XFER_LEN; |
| 1132 | h1m->curr_len = h1m->body_len = 0; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1133 | } |
| 1134 | else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1135 | /* Responses with a known body length. */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1136 | h1m->flags |= H1_MF_XFER_LEN; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1137 | } |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1138 | else { |
| 1139 | /* Responses with an unknown body length */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1140 | h1m->state = H1_MSG_TUNNEL; |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1141 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1142 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1143 | if (!h1_process_res_vsn(h1s, h1m, h1sl)) { |
| 1144 | h1m->err_pos = h1sl.st.v.ptr - b_head(buf); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1145 | h1m->err_state = h1m->state; |
| 1146 | goto vsn_error; |
| 1147 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1148 | } |
| 1149 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1150 | /* Set HTX start-line flags */ |
| 1151 | if (h1m->flags & H1_MF_VER_11) |
| 1152 | flags |= HTX_SL_F_VER_11; |
| 1153 | if (h1m->flags & H1_MF_XFER_ENC) |
| 1154 | flags |= HTX_SL_F_XFER_ENC; |
| 1155 | if (h1m->flags & H1_MF_XFER_LEN) { |
| 1156 | flags |= HTX_SL_F_XFER_LEN; |
| 1157 | if (h1m->flags & H1_MF_CHNK) |
| 1158 | flags |= HTX_SL_F_CHNK; |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1159 | else if (h1m->flags & H1_MF_CLEN) { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1160 | flags |= HTX_SL_F_CLEN; |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1161 | if (h1m->body_len == 0) |
| 1162 | flags |= HTX_SL_F_BODYLESS; |
| 1163 | } |
| 1164 | else |
Christopher Faulet | b2db4fa | 2018-11-27 16:51:09 +0100 | [diff] [blame] | 1165 | flags |= HTX_SL_F_BODYLESS; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1166 | } |
| 1167 | |
Christopher Faulet | a39d8ad | 2019-05-15 15:54:39 +0200 | [diff] [blame] | 1168 | used = htx_used_space(htx); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1169 | if (!(h1m->flags & H1_MF_RESP)) { |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1170 | if (h1_eval_htx_req_size(h1m, &h1sl, hdrs) > max) { |
| 1171 | if (htx_is_empty(htx)) |
| 1172 | goto error; |
| 1173 | h1m_init_req(h1m); |
| 1174 | h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
| 1175 | ret = 0; |
| 1176 | goto end; |
| 1177 | } |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1178 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1179 | sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v); |
| 1180 | if (!sl || !htx_add_all_headers(htx, hdrs)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1181 | goto error; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1182 | sl->info.req.meth = h1s->meth; |
Christopher Faulet | 42993a8 | 2019-06-14 10:31:25 +0200 | [diff] [blame] | 1183 | |
| 1184 | /* Check if the uri contains an explicit scheme and if it is |
| 1185 | * "http" or "https". */ |
| 1186 | if (h1sl.rq.u.len && h1sl.rq.u.ptr[0] != '/') { |
| 1187 | sl->flags |= HTX_SL_F_HAS_SCHM; |
| 1188 | if (h1sl.rq.u.len > 4 && (h1sl.rq.u.ptr[0] | 0x20) == 'h') |
| 1189 | sl->flags |= ((h1sl.rq.u.ptr[4] == ':') ? HTX_SL_F_SCHM_HTTP : HTX_SL_F_SCHM_HTTPS); |
| 1190 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1191 | } |
| 1192 | else { |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1193 | if (h1_eval_htx_res_size(h1m, &h1sl, hdrs) > max) { |
| 1194 | if (htx_is_empty(htx)) |
| 1195 | goto error; |
| 1196 | h1m_init_res(h1m); |
| 1197 | h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
| 1198 | ret = 0; |
| 1199 | goto end; |
| 1200 | } |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1201 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1202 | flags |= HTX_SL_F_IS_RESP; |
| 1203 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, h1sl.st.v, h1sl.st.c, h1sl.st.r); |
| 1204 | if (!sl || !htx_add_all_headers(htx, hdrs)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1205 | goto error; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1206 | sl->info.res.status = h1s->status; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1207 | } |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1208 | |
Christopher Faulet | a39d8ad | 2019-05-15 15:54:39 +0200 | [diff] [blame] | 1209 | /* Set bytes used in the HTX mesage for the headers now */ |
| 1210 | sl->hdrs_bytes = htx_used_space(htx) - used; |
| 1211 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1212 | h1_process_input_conn_mode(h1s, h1m, htx); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1213 | |
| 1214 | /* If body length cannot be determined, set htx->extra to |
| 1215 | * ULLONG_MAX. This value is impossible in other cases. |
| 1216 | */ |
| 1217 | htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1218 | *ofs += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1219 | end: |
| 1220 | return ret; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1221 | |
| 1222 | error: |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1223 | h1m->err_state = h1m->state; |
| 1224 | h1m->err_pos = h1m->next; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1225 | vsn_error: |
| 1226 | h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR); |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1227 | h1s->cs->flags |= CS_FL_EOI; |
| 1228 | htx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1229 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1230 | ret = 0; |
| 1231 | goto end; |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 1232 | |
| 1233 | h2c_upgrade: |
| 1234 | h1s->h1c->flags |= H1C_F_UPG_H2C; |
Christopher Faulet | 8e9e3ef | 2019-05-17 09:14:10 +0200 | [diff] [blame] | 1235 | h1s->cs->flags |= CS_FL_EOI; |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 1236 | htx->flags |= HTX_FL_UPGRADE; |
| 1237 | ret = 0; |
| 1238 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | /* |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1242 | * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it |
| 1243 | * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1244 | * and filling h1s->err_pos and h1s->err_state fields. This functions is |
Joseph Herlant | 30bc509 | 2018-11-25 10:52:20 -0800 | [diff] [blame] | 1245 | * responsible to update the parser state <h1m>. |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1246 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1247 | static size_t h1_process_data(struct h1s *h1s, struct h1m *h1m, struct htx *htx, |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 1248 | struct buffer *buf, size_t *ofs, size_t max, |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1249 | struct buffer *htxbuf) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1250 | { |
| 1251 | size_t total = 0; |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1252 | int32_t ret = 0; |
Christopher Faulet | afe5784 | 2019-01-21 11:55:02 +0100 | [diff] [blame] | 1253 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1254 | if (h1m->flags & H1_MF_XFER_LEN) { |
| 1255 | if (h1m->flags & H1_MF_CLEN) { |
| 1256 | /* content-length: read only h2m->body_len */ |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1257 | ret = htx_get_max_blksz(htx, max); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1258 | if ((uint64_t)ret > h1m->curr_len) |
| 1259 | ret = h1m->curr_len; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1260 | if (ret > b_contig_data(buf, *ofs)) |
| 1261 | ret = b_contig_data(buf, *ofs); |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1262 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1263 | if (ret) { |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 1264 | /* very often with large files we'll face the following |
| 1265 | * situation : |
| 1266 | * - htx is empty and points to <htxbuf> |
| 1267 | * - ret == buf->data |
| 1268 | * - buf->head == sizeof(struct htx) |
| 1269 | * => we can swap the buffers and place an htx header into |
| 1270 | * the target buffer instead |
| 1271 | */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 1272 | int32_t try = ret; |
| 1273 | |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 1274 | if (unlikely(htx_is_empty(htx) && ret == b_data(buf) && |
| 1275 | !*ofs && b_head_ofs(buf) == sizeof(struct htx))) { |
| 1276 | void *raw_area = buf->area; |
| 1277 | void *htx_area = htxbuf->area; |
| 1278 | struct htx_blk *blk; |
| 1279 | |
| 1280 | buf->area = htx_area; |
| 1281 | htxbuf->area = raw_area; |
| 1282 | htx = (struct htx *)htxbuf->area; |
| 1283 | htx->size = htxbuf->size - sizeof(*htx); |
| 1284 | htx_reset(htx); |
| 1285 | b_set_data(htxbuf, b_size(htxbuf)); |
| 1286 | |
| 1287 | blk = htx_add_blk(htx, HTX_BLK_DATA, ret); |
| 1288 | blk->info += ret; |
| 1289 | /* nothing else to do, the old buffer now contains an |
| 1290 | * empty pre-initialized HTX header |
| 1291 | */ |
| 1292 | } |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 1293 | else { |
| 1294 | ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try)); |
| 1295 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1296 | h1m->curr_len -= ret; |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1297 | max -= sizeof(struct htx_blk) + ret; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1298 | *ofs += ret; |
| 1299 | total += ret; |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 1300 | if (ret < try) |
| 1301 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | if (!h1m->curr_len) { |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1305 | if (!h1_process_eom(h1s, h1m, htx, max)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1306 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1307 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1308 | } |
| 1309 | else if (h1m->flags & H1_MF_CHNK) { |
| 1310 | new_chunk: |
| 1311 | /* te:chunked : parse chunks */ |
| 1312 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1313 | ret = h1_skip_chunk_crlf(buf, *ofs, b_data(buf)); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1314 | if (ret <= 0) |
| 1315 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1316 | h1m->state = H1_MSG_CHUNK_SIZE; |
| 1317 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1318 | *ofs += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1319 | total += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
| 1323 | unsigned int chksz; |
| 1324 | |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1325 | ret = h1_parse_chunk_size(buf, *ofs, b_data(buf), &chksz); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1326 | if (ret <= 0) |
| 1327 | goto end; |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 1328 | h1m->state = ((!chksz) ? H1_MSG_TRAILERS : H1_MSG_DATA); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1329 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1330 | h1m->curr_len = chksz; |
| 1331 | h1m->body_len += chksz; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1332 | *ofs += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1333 | total += ret; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1334 | |
| 1335 | if (!h1m->curr_len) |
| 1336 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | if (h1m->state == H1_MSG_DATA) { |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1340 | ret = htx_get_max_blksz(htx, max); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1341 | if ((uint64_t)ret > h1m->curr_len) |
| 1342 | ret = h1m->curr_len; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1343 | if (ret > b_contig_data(buf, *ofs)) |
| 1344 | ret = b_contig_data(buf, *ofs); |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1345 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1346 | if (ret) { |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 1347 | int32_t try = ret; |
| 1348 | |
| 1349 | ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1350 | h1m->curr_len -= ret; |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1351 | max -= sizeof(struct htx_blk) + ret; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1352 | *ofs += ret; |
| 1353 | total += ret; |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 1354 | if (ret < try) |
| 1355 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1356 | } |
| 1357 | if (!h1m->curr_len) { |
| 1358 | h1m->state = H1_MSG_CHUNK_CRLF; |
| 1359 | goto new_chunk; |
| 1360 | } |
| 1361 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1362 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1363 | } |
| 1364 | else { |
| 1365 | /* XFER_LEN is set but not CLEN nor CHNK, it means there |
| 1366 | * is no body. Switch the message in DONE state |
| 1367 | */ |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1368 | if (!h1_process_eom(h1s, h1m, htx, max)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1369 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | else { |
| 1373 | /* no content length, read till SHUTW */ |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1374 | ret = htx_get_max_blksz(htx, max); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1375 | if (ret > b_contig_data(buf, *ofs)) |
| 1376 | ret = b_contig_data(buf, *ofs); |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1377 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1378 | if (ret) { |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 1379 | int32_t try = ret; |
| 1380 | |
| 1381 | ret = htx_add_data(htx, ist2(b_peek(buf, *ofs), try)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1382 | |
Olivier Houchard | cf42d5a | 2018-12-04 17:41:58 +0100 | [diff] [blame] | 1383 | *ofs += ret; |
| 1384 | total = ret; |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 1385 | if (ret < try) |
| 1386 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1387 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | end: |
| 1391 | if (ret < 0) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1392 | h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR); |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1393 | h1s->cs->flags |= CS_FL_EOI; |
| 1394 | htx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1395 | h1m->err_state = h1m->state; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1396 | h1m->err_pos = *ofs + max + ret; |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1397 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1398 | return 0; |
| 1399 | } |
Willy Tarreau | 7e9dd73 | 2020-01-17 16:19:34 +0100 | [diff] [blame] | 1400 | |
Christopher Faulet | afadc9a | 2020-07-03 14:51:15 +0200 | [diff] [blame] | 1401 | if (h1s->cs && !(h1m->flags & H1_MF_CHNK) && |
| 1402 | ((h1m->state == H1_MSG_DATA && h1m->curr_len) || (h1m->state == H1_MSG_TUNNEL))) |
Willy Tarreau | 7e9dd73 | 2020-01-17 16:19:34 +0100 | [diff] [blame] | 1403 | h1s->cs->flags |= CS_FL_MAY_SPLICE; |
| 1404 | else if (h1s->cs) |
| 1405 | h1s->cs->flags &= ~CS_FL_MAY_SPLICE; |
| 1406 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1407 | /* update htx->extra, only when the body length is known */ |
| 1408 | if (h1m->flags & H1_MF_XFER_LEN) |
| 1409 | htx->extra = h1m->curr_len; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1410 | return total; |
| 1411 | } |
| 1412 | |
| 1413 | /* |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1414 | * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if |
| 1415 | * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR |
| 1416 | * flag and filling h1s->err_pos and h1s->err_state fields. This functions is |
| 1417 | * responsible to update the parser state <h1m>. |
| 1418 | */ |
| 1419 | static size_t h1_process_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx, |
| 1420 | struct buffer *buf, size_t *ofs, size_t max) |
| 1421 | { |
Christopher Faulet | e4ab11b | 2019-06-11 15:05:37 +0200 | [diff] [blame] | 1422 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1423 | struct h1m tlr_h1m; |
| 1424 | int ret = 0; |
| 1425 | |
| 1426 | if (!max || !b_data(buf)) |
| 1427 | goto end; |
| 1428 | |
| 1429 | /* Realing input buffer if necessary */ |
| 1430 | if (b_peek(buf, *ofs) > b_tail(buf)) |
| 1431 | b_slow_realign(buf, trash.area, 0); |
| 1432 | |
| 1433 | tlr_h1m.flags = (H1_MF_NO_PHDR|H1_MF_HDRS_ONLY); |
| 1434 | ret = h1_headers_to_hdr_list(b_peek(buf, *ofs), b_tail(buf), |
| 1435 | hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &tlr_h1m, NULL); |
| 1436 | if (ret <= 0) { |
Willy Tarreau | 9a408ab | 2019-08-23 08:11:36 +0200 | [diff] [blame] | 1437 | /* Incomplete or invalid trailers. If the input buffer only |
| 1438 | * contains trailers and is full, which is detected by it being |
| 1439 | * full and the offset to be zero, it's an error because |
| 1440 | * trailers are too large to be handled by the parser. */ |
| 1441 | if (ret < 0 || (!ret && !*ofs && !buf_room_for_htx_data(buf))) |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1442 | goto error; |
| 1443 | goto end; |
| 1444 | } |
| 1445 | |
| 1446 | /* messages trailers fully parsed. */ |
| 1447 | if (h1_eval_htx_hdrs_size(hdrs) > max) { |
| 1448 | if (htx_is_empty(htx)) |
| 1449 | goto error; |
| 1450 | ret = 0; |
| 1451 | goto end; |
| 1452 | } |
| 1453 | |
| 1454 | if (!htx_add_all_trailers(htx, hdrs)) |
| 1455 | goto error; |
| 1456 | |
| 1457 | *ofs += ret; |
| 1458 | h1s->flags |= H1S_F_HAVE_I_TLR; |
| 1459 | end: |
| 1460 | return ret; |
| 1461 | |
| 1462 | error: |
| 1463 | h1m->err_state = h1m->state; |
| 1464 | h1m->err_pos = h1m->next; |
| 1465 | h1s->flags |= (!(h1m->flags & H1_MF_RESP) ? H1S_F_REQ_ERROR : H1S_F_RES_ERROR); |
| 1466 | h1s->cs->flags |= CS_FL_EOI; |
| 1467 | htx->flags |= HTX_FL_PARSING_ERROR; |
| 1468 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
| 1469 | ret = 0; |
| 1470 | goto end; |
| 1471 | } |
| 1472 | |
| 1473 | /* |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1474 | * Process incoming data. It parses data and transfer them from h1c->ibuf into |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1475 | * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if |
| 1476 | * it couldn't proceed. |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1477 | */ |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1478 | static size_t h1_process_input(struct h1c *h1c, struct buffer *buf, size_t count) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1479 | { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1480 | struct h1s *h1s = h1c->h1s; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1481 | struct h1m *h1m; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1482 | struct htx *htx; |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1483 | size_t ret, data; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1484 | size_t total = 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1485 | int errflag; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1486 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1487 | htx = htx_from_buf(buf); |
Willy Tarreau | 3a6190f | 2018-12-16 08:29:56 +0100 | [diff] [blame] | 1488 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1489 | if (!conn_is_back(h1c->conn)) { |
| 1490 | h1m = &h1s->req; |
| 1491 | errflag = H1S_F_REQ_ERROR; |
| 1492 | } |
| 1493 | else { |
| 1494 | h1m = &h1s->res; |
| 1495 | errflag = H1S_F_RES_ERROR; |
| 1496 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1497 | |
Christopher Faulet | 7402776 | 2019-02-26 14:45:05 +0100 | [diff] [blame] | 1498 | data = htx->data; |
Christopher Faulet | cd9f6a7 | 2019-07-04 21:22:34 +0200 | [diff] [blame] | 1499 | if (h1s->flags & errflag) |
| 1500 | goto end; |
| 1501 | |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1502 | do { |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1503 | size_t used = htx_used_space(htx); |
| 1504 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1505 | if (h1m->state <= H1_MSG_LAST_LF) { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1506 | ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1507 | if (!ret) |
| 1508 | break; |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1509 | if ((h1m->flags & H1_MF_RESP) && |
| 1510 | h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) { |
| 1511 | h1m_init_res(&h1s->res); |
| 1512 | h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
| 1513 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1514 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1515 | else if (h1m->state < H1_MSG_TRAILERS) { |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1516 | ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf); |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 1517 | htx = htx_from_buf(buf); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1518 | if (!ret) |
| 1519 | break; |
| 1520 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1521 | else if (h1m->state == H1_MSG_TRAILERS) { |
| 1522 | if (!(h1s->flags & H1S_F_HAVE_I_TLR)) { |
| 1523 | ret = h1_process_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count); |
| 1524 | if (!ret) |
| 1525 | break; |
| 1526 | } |
Christopher Faulet | 1021e72 | 2019-09-03 21:55:14 +0200 | [diff] [blame] | 1527 | else if (!h1_process_eom(h1s, h1m, htx, count)) |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1528 | break; |
| 1529 | } |
Christopher Faulet | cb55f48 | 2018-12-10 11:56:47 +0100 | [diff] [blame] | 1530 | else if (h1m->state == H1_MSG_DONE) { |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1531 | if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) |
| 1532 | h1_set_req_tunnel_mode(h1s); |
Christopher Faulet | affe0cb | 2020-07-10 10:01:26 +0200 | [diff] [blame] | 1533 | else if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) { |
Christopher Faulet | 2f320ee | 2019-04-16 20:26:53 +0200 | [diff] [blame] | 1534 | h1c->flags |= H1C_F_IN_BUSY; |
Christopher Faulet | affe0cb | 2020-07-10 10:01:26 +0200 | [diff] [blame] | 1535 | break; |
| 1536 | } |
| 1537 | else |
| 1538 | break; |
Christopher Faulet | cb55f48 | 2018-12-10 11:56:47 +0100 | [diff] [blame] | 1539 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1540 | else if (h1m->state == H1_MSG_TUNNEL) { |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1541 | ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf); |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 1542 | htx = htx_from_buf(buf); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1543 | if (!ret) |
| 1544 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1545 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1546 | else { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1547 | h1s->flags |= errflag; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1548 | break; |
| 1549 | } |
| 1550 | |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1551 | count -= htx_used_space(htx) - used; |
Christopher Faulet | 98e2bc2 | 2019-09-03 16:26:15 +0200 | [diff] [blame] | 1552 | } while (!(h1s->flags & errflag)); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1553 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1554 | if (h1s->flags & errflag) |
| 1555 | goto parsing_err; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1556 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1557 | b_del(&h1c->ibuf, total); |
| 1558 | |
| 1559 | end: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1560 | htx_to_buf(htx, buf); |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1561 | ret = htx->data - data; |
Willy Tarreau | 45f2b89 | 2018-12-05 07:59:27 +0100 | [diff] [blame] | 1562 | if (h1c->flags & H1C_F_IN_FULL && buf_room_for_htx_data(&h1c->ibuf)) { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1563 | h1c->flags &= ~H1C_F_IN_FULL; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 1564 | tasklet_wakeup(h1c->wait_event.tasklet); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1565 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1566 | |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 1567 | h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
| 1568 | |
Christopher Faulet | cdc90e9 | 2019-03-28 13:28:46 +0100 | [diff] [blame] | 1569 | if (!b_data(&h1c->ibuf)) |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1570 | h1_release_buf(h1c, &h1c->ibuf); |
Christopher Faulet | a882a46 | 2019-12-06 15:59:05 +0100 | [diff] [blame] | 1571 | |
| 1572 | if ((h1s_data_pending(h1s) && !htx_is_empty(htx)) || (h1s->flags & H1S_F_APPEND_EOM)) |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 1573 | h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1574 | |
Willy Tarreau | 6203815 | 2019-08-23 09:29:29 +0200 | [diff] [blame] | 1575 | if (((h1s->flags & (H1S_F_REOS|H1S_F_APPEND_EOM)) == H1S_F_REOS) && |
| 1576 | (!h1s_data_pending(h1s) || htx_is_empty(htx))) { |
Christopher Faulet | f6ce9d6 | 2018-12-10 15:30:06 +0100 | [diff] [blame] | 1577 | h1s->cs->flags |= CS_FL_EOS; |
Christopher Faulet | 2692238 | 2019-03-08 15:13:41 +0100 | [diff] [blame] | 1578 | if (h1m->state > H1_MSG_LAST_LF && h1m->state < H1_MSG_DONE) |
Christopher Faulet | b8d2ee0 | 2019-02-25 15:29:51 +0100 | [diff] [blame] | 1579 | h1s->cs->flags |= CS_FL_ERROR; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1580 | } |
Christopher Faulet | f6ce9d6 | 2018-12-10 15:30:06 +0100 | [diff] [blame] | 1581 | |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1582 | return ret; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1583 | |
| 1584 | parsing_err: |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1585 | b_reset(&h1c->ibuf); |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1586 | htx_to_buf(htx, buf); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1587 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1588 | } |
| 1589 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1590 | /* |
| 1591 | * Process outgoing data. It parses data and transfer them from the channel buffer into |
| 1592 | * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or |
| 1593 | * 0 if it couldn't proceed. |
| 1594 | */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1595 | static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count) |
| 1596 | { |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1597 | struct h1s *h1s = h1c->h1s; |
| 1598 | struct h1m *h1m; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1599 | struct htx *chn_htx; |
| 1600 | struct htx_blk *blk; |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1601 | struct buffer tmp; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1602 | size_t total = 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1603 | int errflag; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1604 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1605 | if (!count) |
| 1606 | goto end; |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 1607 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1608 | chn_htx = htxbuf(buf); |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 1609 | if (htx_is_empty(chn_htx)) |
| 1610 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1611 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1612 | if (!h1_get_buf(h1c, &h1c->obuf)) { |
| 1613 | h1c->flags |= H1C_F_OUT_ALLOC; |
| 1614 | goto end; |
| 1615 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1616 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1617 | if (!conn_is_back(h1c->conn)) { |
| 1618 | h1m = &h1s->res; |
| 1619 | errflag = H1S_F_RES_ERROR; |
| 1620 | } |
| 1621 | else { |
| 1622 | h1m = &h1s->req; |
| 1623 | errflag = H1S_F_REQ_ERROR; |
| 1624 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1625 | |
Christopher Faulet | cd9f6a7 | 2019-07-04 21:22:34 +0200 | [diff] [blame] | 1626 | if (h1s->flags & errflag) |
| 1627 | goto end; |
| 1628 | |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 1629 | /* the htx is non-empty thus has at least one block */ |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1630 | blk = htx_get_head_blk(chn_htx); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1631 | |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1632 | /* Perform some optimizations to reduce the number of buffer copies. |
| 1633 | * First, if the mux's buffer is empty and the htx area contains |
| 1634 | * exactly one data block of the same size as the requested count, |
| 1635 | * then it's possible to simply swap the caller's buffer with the |
| 1636 | * mux's output buffer and adjust offsets and length to match the |
| 1637 | * entire DATA HTX block in the middle. In this case we perform a |
| 1638 | * true zero-copy operation from end-to-end. This is the situation |
| 1639 | * that happens all the time with large files. Second, if this is not |
| 1640 | * possible, but the mux's output buffer is empty, we still have an |
| 1641 | * opportunity to avoid the copy to the intermediary buffer, by making |
| 1642 | * the intermediary buffer's area point to the output buffer's area. |
| 1643 | * In this case we want to skip the HTX header to make sure that copies |
| 1644 | * remain aligned and that this operation remains possible all the |
| 1645 | * time. This goes for headers, data blocks and any data extracted from |
| 1646 | * the HTX blocks. |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 1647 | */ |
| 1648 | if (!b_data(&h1c->obuf)) { |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1649 | if (chn_htx->used == 1 && |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 1650 | htx_get_blk_type(blk) == HTX_BLK_DATA && |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1651 | htx_get_blk_value(chn_htx, blk).len == count) { |
| 1652 | void *old_area = h1c->obuf.area; |
| 1653 | |
| 1654 | h1c->obuf.area = buf->area; |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1655 | h1c->obuf.head = sizeof(struct htx) + blk->addr; |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1656 | h1c->obuf.data = count; |
| 1657 | |
| 1658 | buf->area = old_area; |
| 1659 | buf->data = buf->head = 0; |
Christopher Faulet | adb2220 | 2018-12-12 10:32:09 +0100 | [diff] [blame] | 1660 | |
| 1661 | /* The message is chunked. We need to emit the chunk |
| 1662 | * size. We have at least the size of the struct htx to |
| 1663 | * write the chunk envelope. It should be enough. |
| 1664 | */ |
| 1665 | if (h1m->flags & H1_MF_CHNK) { |
| 1666 | h1_emit_chunk_size(&h1c->obuf, count); |
| 1667 | h1_emit_chunk_crlf(&h1c->obuf); |
| 1668 | } |
| 1669 | |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1670 | total += count; |
| 1671 | goto out; |
| 1672 | } |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1673 | tmp.area = h1c->obuf.area + h1c->obuf.head; |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 1674 | } |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1675 | else |
| 1676 | tmp.area = trash.area; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1677 | |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1678 | tmp.data = 0; |
| 1679 | tmp.size = b_room(&h1c->obuf); |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1680 | while (count && !(h1s->flags & errflag) && blk) { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1681 | struct htx_sl *sl; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1682 | struct ist n, v; |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1683 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1684 | uint32_t sz = htx_get_blksz(blk); |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1685 | uint32_t vlen; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1686 | |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1687 | vlen = sz; |
| 1688 | if (vlen > count) { |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1689 | if (type != HTX_BLK_DATA) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1690 | goto full; |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1691 | vlen = count; |
| 1692 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1693 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1694 | if (type == HTX_BLK_UNUSED) |
| 1695 | goto nextblk; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1696 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1697 | switch (h1m->state) { |
| 1698 | case H1_MSG_RQBEFORE: |
| 1699 | if (type != HTX_BLK_REQ_SL) |
| 1700 | goto error; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1701 | sl = htx_get_blk_ptr(chn_htx, blk); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1702 | h1s->meth = sl->info.req.meth; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1703 | h1_parse_req_vsn(h1m, sl); |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1704 | if (!htx_reqline_to_h1(sl, &tmp)) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1705 | goto full; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1706 | h1m->flags |= H1_MF_XFER_LEN; |
Christopher Faulet | 1f890dd | 2019-02-18 10:33:16 +0100 | [diff] [blame] | 1707 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 1708 | h1m->flags |= H1_MF_CLEN; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1709 | h1m->state = H1_MSG_HDR_FIRST; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1710 | break; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1711 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1712 | case H1_MSG_RPBEFORE: |
| 1713 | if (type != HTX_BLK_RES_SL) |
| 1714 | goto error; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1715 | sl = htx_get_blk_ptr(chn_htx, blk); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1716 | h1s->status = sl->info.res.status; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1717 | h1_parse_res_vsn(h1m, sl); |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1718 | if (!htx_stline_to_h1(sl, &tmp)) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1719 | goto full; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1720 | if (sl->flags & HTX_SL_F_XFER_LEN) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1721 | h1m->flags |= H1_MF_XFER_LEN; |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1722 | if (sl->info.res.status < 200 && |
| 1723 | (sl->info.res.status == 100 || sl->info.res.status >= 102)) |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 1724 | h1s->flags |= H1S_F_HAVE_O_CONN; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1725 | h1m->state = H1_MSG_HDR_FIRST; |
| 1726 | break; |
| 1727 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1728 | case H1_MSG_HDR_FIRST: |
| 1729 | case H1_MSG_HDR_NAME: |
| 1730 | case H1_MSG_HDR_L2_LWS: |
| 1731 | if (type == HTX_BLK_EOH) |
| 1732 | goto last_lf; |
| 1733 | if (type != HTX_BLK_HDR) |
| 1734 | goto error; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1735 | h1m->state = H1_MSG_HDR_NAME; |
| 1736 | n = htx_get_blk_name(chn_htx, blk); |
| 1737 | v = htx_get_blk_value(chn_htx, blk); |
| 1738 | |
| 1739 | if (isteqi(n, ist("transfer-encoding"))) |
| 1740 | h1_parse_xfer_enc_header(h1m, v); |
Willy Tarreau | 27cd223 | 2019-01-03 21:52:42 +0100 | [diff] [blame] | 1741 | else if (isteqi(n, ist("content-length"))) { |
Christopher Faulet | 5220ef2 | 2019-03-27 15:44:56 +0100 | [diff] [blame] | 1742 | /* Only skip C-L header with invalid value. */ |
| 1743 | if (h1_parse_cont_len_header(h1m, &v) < 0) |
Willy Tarreau | 27cd223 | 2019-01-03 21:52:42 +0100 | [diff] [blame] | 1744 | goto skip_hdr; |
| 1745 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1746 | else if (isteqi(n, ist("connection"))) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1747 | h1_parse_connection_header(h1m, &v); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1748 | if (!v.len) |
| 1749 | goto skip_hdr; |
| 1750 | } |
| 1751 | |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 1752 | /* Try to adjust the case of the header name */ |
| 1753 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 1754 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1755 | if (!htx_hdr_to_h1(n, v, &tmp)) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1756 | goto full; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1757 | skip_hdr: |
| 1758 | h1m->state = H1_MSG_HDR_L2_LWS; |
| 1759 | break; |
| 1760 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1761 | case H1_MSG_LAST_LF: |
| 1762 | if (type != HTX_BLK_EOH) |
| 1763 | goto error; |
| 1764 | last_lf: |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 1765 | h1m->state = H1_MSG_LAST_LF; |
| 1766 | if (!(h1s->flags & H1S_F_HAVE_O_CONN)) { |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1767 | /* There is no "Connection:" header and |
| 1768 | * it the conn_mode must be |
| 1769 | * processed. So do it */ |
Christopher Faulet | 661bfc3 | 2019-06-17 14:07:46 +0200 | [diff] [blame] | 1770 | n = ist("connection"); |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1771 | v = ist(""); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1772 | h1_process_output_conn_mode(h1s, h1m, &v); |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1773 | if (v.len) { |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 1774 | /* Try to adjust the case of the header name */ |
| 1775 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 1776 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1777 | if (!htx_hdr_to_h1(n, v, &tmp)) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1778 | goto full; |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1779 | } |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 1780 | h1s->flags |= H1S_F_HAVE_O_CONN; |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1781 | } |
Willy Tarreau | 4710d20 | 2019-01-03 17:39:54 +0100 | [diff] [blame] | 1782 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1783 | if ((h1s->meth != HTTP_METH_CONNECT && |
| 1784 | (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) == |
Christopher Faulet | 1f890dd | 2019-02-18 10:33:16 +0100 | [diff] [blame] | 1785 | (H1_MF_VER_11|H1_MF_XFER_LEN)) || |
| 1786 | (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 && |
| 1787 | h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) && |
| 1788 | (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) == |
| 1789 | (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) { |
Willy Tarreau | 4710d20 | 2019-01-03 17:39:54 +0100 | [diff] [blame] | 1790 | /* chunking needed but header not seen */ |
Christopher Faulet | e5addb0 | 2019-10-04 10:23:51 +0200 | [diff] [blame] | 1791 | n = ist("transfer-encoding"); |
| 1792 | v = ist("chunked"); |
| 1793 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 1794 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
| 1795 | if (!htx_hdr_to_h1(n, v, &tmp)) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1796 | goto full; |
Willy Tarreau | 4710d20 | 2019-01-03 17:39:54 +0100 | [diff] [blame] | 1797 | h1m->flags |= H1_MF_CHNK; |
| 1798 | } |
| 1799 | |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1800 | if (!chunk_memcat(&tmp, "\r\n", 2)) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1801 | goto full; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1802 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1803 | if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) { |
| 1804 | /* a CONNECT request is sent to the server. Switch it to tunnel mode. */ |
| 1805 | h1_set_req_tunnel_mode(h1s); |
| 1806 | } |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1807 | else if ((h1m->flags & H1_MF_RESP) && |
| 1808 | ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || h1s->status == 101)) { |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1809 | /* a successfull reply to a CONNECT or a protocol switching is sent |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1810 | * to the client. Switch the response to tunnel mode. |
| 1811 | */ |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1812 | h1_set_res_tunnel_mode(h1s); |
| 1813 | } |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1814 | else if ((h1m->flags & H1_MF_RESP) && |
| 1815 | h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) { |
| 1816 | h1m_init_res(&h1s->res); |
| 1817 | h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 1818 | h1s->flags &= ~H1S_F_HAVE_O_CONN; |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1819 | } |
Christopher Faulet | 33d58b5 | 2019-07-01 16:17:30 +0200 | [diff] [blame] | 1820 | else if ((h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_HEAD) |
| 1821 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1822 | else |
| 1823 | h1m->state = H1_MSG_DATA; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1824 | break; |
| 1825 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1826 | case H1_MSG_DATA: |
Christopher Faulet | aaa2506 | 2019-07-04 17:12:12 +0200 | [diff] [blame] | 1827 | case H1_MSG_TUNNEL: |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1828 | if (type == HTX_BLK_EOM) { |
| 1829 | /* Chunked message without explicit trailers */ |
| 1830 | if (h1m->flags & H1_MF_CHNK) { |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1831 | if (!chunk_memcat(&tmp, "0\r\n\r\n", 5)) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1832 | goto full; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1833 | } |
| 1834 | goto done; |
| 1835 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1836 | else if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) { |
Christopher Faulet | 5213168 | 2019-06-27 17:40:14 +0200 | [diff] [blame] | 1837 | /* If the message is not chunked, never |
| 1838 | * add the last chunk. */ |
| 1839 | if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3)) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1840 | goto full; |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1841 | goto trailers; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1842 | } |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1843 | else if (type != HTX_BLK_DATA) |
| 1844 | goto error; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1845 | v = htx_get_blk_value(chn_htx, blk); |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1846 | v.len = vlen; |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1847 | if (!htx_data_to_h1(v, &tmp, !!(h1m->flags & H1_MF_CHNK))) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1848 | goto full; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1849 | break; |
| 1850 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1851 | case H1_MSG_TRAILERS: |
| 1852 | if (type == HTX_BLK_EOM) |
| 1853 | goto done; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1854 | else if (type != HTX_BLK_TLR && type != HTX_BLK_EOT) |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1855 | goto error; |
| 1856 | trailers: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1857 | h1m->state = H1_MSG_TRAILERS; |
Christopher Faulet | 5213168 | 2019-06-27 17:40:14 +0200 | [diff] [blame] | 1858 | /* If the message is not chunked, ignore |
| 1859 | * trailers. It may happen with H2 messages. */ |
| 1860 | if (!(h1m->flags & H1_MF_CHNK)) |
| 1861 | break; |
| 1862 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1863 | if (type == HTX_BLK_EOT) { |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1864 | if (!chunk_memcat(&tmp, "\r\n", 2)) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1865 | goto full; |
Christopher Faulet | 3218821 | 2018-11-20 18:21:43 +0100 | [diff] [blame] | 1866 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1867 | else { // HTX_BLK_TLR |
| 1868 | n = htx_get_blk_name(chn_htx, blk); |
| 1869 | v = htx_get_blk_value(chn_htx, blk); |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 1870 | |
| 1871 | /* Try to adjust the case of the header name */ |
| 1872 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 1873 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1874 | if (!htx_hdr_to_h1(n, v, &tmp)) |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1875 | goto full; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1876 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1877 | break; |
| 1878 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1879 | case H1_MSG_DONE: |
| 1880 | if (type != HTX_BLK_EOM) |
| 1881 | goto error; |
| 1882 | done: |
| 1883 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1884 | if (!(h1m->flags & H1_MF_RESP) && h1s->status == 101) |
| 1885 | h1_set_req_tunnel_mode(h1s); |
| 1886 | else if (h1s->h1c->flags & H1C_F_IN_BUSY) { |
Christopher Faulet | cd67bff | 2019-06-14 16:54:15 +0200 | [diff] [blame] | 1887 | h1s->h1c->flags &= ~H1C_F_IN_BUSY; |
| 1888 | tasklet_wakeup(h1s->h1c->wait_event.tasklet); |
| 1889 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1890 | break; |
| 1891 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1892 | default: |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1893 | error: |
Christopher Faulet | c431df8 | 2019-06-26 15:16:28 +0200 | [diff] [blame] | 1894 | /* Unexpected error during output processing */ |
| 1895 | chn_htx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | a2ea158 | 2019-05-28 10:35:18 +0200 | [diff] [blame] | 1896 | h1s->flags |= errflag; |
Christopher Faulet | c431df8 | 2019-06-26 15:16:28 +0200 | [diff] [blame] | 1897 | h1c->flags |= H1C_F_CS_ERROR; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1898 | break; |
| 1899 | } |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1900 | |
| 1901 | nextblk: |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1902 | total += vlen; |
| 1903 | count -= vlen; |
| 1904 | if (sz == vlen) |
| 1905 | blk = htx_remove_blk(chn_htx, blk); |
| 1906 | else { |
| 1907 | htx_cut_data_blk(chn_htx, blk, vlen); |
| 1908 | break; |
| 1909 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1910 | } |
| 1911 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1912 | copy: |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 1913 | /* when the output buffer is empty, tmp shares the same area so that we |
| 1914 | * only have to update pointers and lengths. |
| 1915 | */ |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1916 | if (tmp.area == h1c->obuf.area + h1c->obuf.head) |
| 1917 | h1c->obuf.data = tmp.data; |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 1918 | else |
Christopher Faulet | 65fc1dc | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1919 | b_putblk(&h1c->obuf, tmp.area, tmp.data); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1920 | |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1921 | htx_to_buf(chn_htx, buf); |
| 1922 | out: |
Willy Tarreau | 45f2b89 | 2018-12-05 07:59:27 +0100 | [diff] [blame] | 1923 | if (!buf_room_for_htx_data(&h1c->obuf)) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1924 | h1c->flags |= H1C_F_OUT_FULL; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1925 | end: |
| 1926 | return total; |
Christopher Faulet | 85fc6ef | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 1927 | |
| 1928 | full: |
| 1929 | h1c->flags |= H1C_F_OUT_FULL; |
| 1930 | goto copy; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1931 | } |
| 1932 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1933 | /*********************************************************/ |
| 1934 | /* functions below are I/O callbacks from the connection */ |
| 1935 | /*********************************************************/ |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 1936 | static void h1_wake_stream_for_recv(struct h1s *h1s) |
| 1937 | { |
| 1938 | if (h1s && h1s->recv_wait) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 1939 | h1s->recv_wait->events &= ~SUB_RETRY_RECV; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 1940 | tasklet_wakeup(h1s->recv_wait->tasklet); |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 1941 | h1s->recv_wait = NULL; |
| 1942 | } |
| 1943 | } |
| 1944 | static void h1_wake_stream_for_send(struct h1s *h1s) |
| 1945 | { |
| 1946 | if (h1s && h1s->send_wait) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 1947 | h1s->send_wait->events &= ~SUB_RETRY_SEND; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 1948 | tasklet_wakeup(h1s->send_wait->tasklet); |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 1949 | h1s->send_wait = NULL; |
| 1950 | } |
| 1951 | } |
| 1952 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1953 | /* |
| 1954 | * Attempt to read data, and subscribe if none available |
| 1955 | */ |
| 1956 | static int h1_recv(struct h1c *h1c) |
| 1957 | { |
| 1958 | struct connection *conn = h1c->conn; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 1959 | struct h1s *h1s = h1c->h1s; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 1960 | size_t ret = 0, max; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1961 | int rcvd = 0; |
| 1962 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 1963 | if (h1c->wait_event.events & SUB_RETRY_RECV) |
Christopher Faulet | c386a88 | 2018-12-04 16:06:28 +0100 | [diff] [blame] | 1964 | return (b_data(&h1c->ibuf)); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1965 | |
Olivier Houchard | 5b06cb3 | 2019-08-13 15:21:59 +0200 | [diff] [blame] | 1966 | if (!(conn->flags & CO_FL_ERROR) && h1c->flags & H1C_F_CS_WAIT_CONN) |
Willy Tarreau | d58f27f | 2019-06-03 10:12:22 +0200 | [diff] [blame] | 1967 | return 0; |
Willy Tarreau | d58f27f | 2019-06-03 10:12:22 +0200 | [diff] [blame] | 1968 | |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 1969 | if (!h1_recv_allowed(h1c)) { |
| 1970 | rcvd = 1; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1971 | goto end; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 1972 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1973 | |
Christopher Faulet | f7d5ff3 | 2019-04-16 13:55:08 +0200 | [diff] [blame] | 1974 | if (!h1_get_buf(h1c, &h1c->ibuf)) { |
| 1975 | h1c->flags |= H1C_F_IN_ALLOC; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1976 | goto end; |
| 1977 | } |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1978 | |
Christopher Faulet | f7d5ff3 | 2019-04-16 13:55:08 +0200 | [diff] [blame] | 1979 | if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) { |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 1980 | if (!h1s_data_pending(h1s)) |
Christopher Faulet | f7d5ff3 | 2019-04-16 13:55:08 +0200 | [diff] [blame] | 1981 | h1_wake_stream_for_recv(h1s); |
| 1982 | rcvd = 1; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1983 | goto end; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1984 | } |
| 1985 | |
Olivier Houchard | 29a22bc | 2018-12-04 18:16:45 +0100 | [diff] [blame] | 1986 | /* |
| 1987 | * If we only have a small amount of data, realign it, |
| 1988 | * it's probably cheaper than doing 2 recv() calls. |
| 1989 | */ |
| 1990 | if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128) |
| 1991 | b_slow_realign(&h1c->ibuf, trash.area, 0); |
| 1992 | |
Willy Tarreau | 45f2b89 | 2018-12-05 07:59:27 +0100 | [diff] [blame] | 1993 | max = buf_room_for_htx_data(&h1c->ibuf); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1994 | if (max) { |
| 1995 | h1c->flags &= ~H1C_F_IN_FULL; |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 1996 | |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 1997 | b_realign_if_empty(&h1c->ibuf); |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 1998 | if (!b_data(&h1c->ibuf)) { |
| 1999 | /* try to pre-align the buffer like the rxbufs will be |
| 2000 | * to optimize memory copies. |
| 2001 | */ |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 2002 | h1c->ibuf.head = sizeof(struct htx); |
| 2003 | } |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2004 | ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, 0); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2005 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 2006 | if (ret > 0) { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2007 | rcvd = 1; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 2008 | if (h1s && h1s->cs) { |
Christopher Faulet | 37e3607 | 2018-12-04 15:54:12 +0100 | [diff] [blame] | 2009 | h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE); |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 2010 | if (h1s->csinfo.t_idle == -1) |
| 2011 | h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake; |
| 2012 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 2013 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2014 | |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 2015 | if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) { |
Christopher Faulet | 81d4843 | 2018-11-19 21:22:43 +0100 | [diff] [blame] | 2016 | rcvd = 1; |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 2017 | goto end; |
| 2018 | } |
| 2019 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2020 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2021 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2022 | end: |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 2023 | if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn)) |
| 2024 | h1_wake_stream_for_recv(h1s); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2025 | |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 2026 | if (conn_xprt_read0_pending(conn) && h1s) { |
| 2027 | h1s->flags |= H1S_F_REOS; |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 2028 | rcvd = 1; |
| 2029 | } |
| 2030 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2031 | if (!b_data(&h1c->ibuf)) |
| 2032 | h1_release_buf(h1c, &h1c->ibuf); |
Willy Tarreau | 45f2b89 | 2018-12-05 07:59:27 +0100 | [diff] [blame] | 2033 | else if (!buf_room_for_htx_data(&h1c->ibuf)) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2034 | h1c->flags |= H1C_F_IN_FULL; |
| 2035 | return rcvd; |
| 2036 | } |
| 2037 | |
| 2038 | |
| 2039 | /* |
| 2040 | * Try to send data if possible |
| 2041 | */ |
| 2042 | static int h1_send(struct h1c *h1c) |
| 2043 | { |
| 2044 | struct connection *conn = h1c->conn; |
| 2045 | unsigned int flags = 0; |
| 2046 | size_t ret; |
| 2047 | int sent = 0; |
| 2048 | |
| 2049 | if (conn->flags & CO_FL_ERROR) |
| 2050 | return 0; |
| 2051 | |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 2052 | if (h1c->flags & H1C_F_CS_WAIT_CONN) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2053 | if (!(h1c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2054 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event); |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 2055 | return 0; |
| 2056 | } |
| 2057 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2058 | if (!b_data(&h1c->obuf)) |
| 2059 | goto end; |
| 2060 | |
| 2061 | if (h1c->flags & H1C_F_OUT_FULL) |
| 2062 | flags |= CO_SFL_MSG_MORE; |
| 2063 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2064 | ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2065 | if (ret > 0) { |
| 2066 | h1c->flags &= ~H1C_F_OUT_FULL; |
| 2067 | b_del(&h1c->obuf, ret); |
| 2068 | sent = 1; |
| 2069 | } |
| 2070 | |
Christopher Faulet | 145aa47 | 2018-12-06 10:56:20 +0100 | [diff] [blame] | 2071 | if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) { |
| 2072 | /* error or output closed, nothing to send, clear the buffer to release it */ |
| 2073 | b_reset(&h1c->obuf); |
| 2074 | } |
| 2075 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2076 | end: |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 2077 | if (!(h1c->flags & H1C_F_OUT_FULL)) |
| 2078 | h1_wake_stream_for_send(h1c->h1s); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2079 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2080 | /* We're done, no more to send */ |
| 2081 | if (!b_data(&h1c->obuf)) { |
| 2082 | h1_release_buf(h1c, &h1c->obuf); |
| 2083 | if (h1c->flags & H1C_F_CS_SHUTW_NOW) |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 2084 | h1_shutw_conn(conn, CS_SHW_NORMAL); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2085 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2086 | else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2087 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2088 | |
| 2089 | return sent; |
| 2090 | } |
| 2091 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2092 | |
| 2093 | /* callback called on any event by the connection handler. |
| 2094 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2095 | * destruction of the connection. |
| 2096 | */ |
| 2097 | static int h1_process(struct h1c * h1c) |
| 2098 | { |
| 2099 | struct connection *conn = h1c->conn; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 2100 | struct h1s *h1s = h1c->h1s; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2101 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2102 | if (!conn->ctx) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2103 | return -1; |
| 2104 | |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 2105 | if (h1c->flags & H1C_F_CS_WAIT_CONN) { |
Olivier Houchard | 690e0f0 | 2019-06-11 16:37:24 +0200 | [diff] [blame] | 2106 | if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR)) || |
Olivier Houchard | 6063003 | 2019-06-13 17:37:00 +0200 | [diff] [blame] | 2107 | (!(conn->flags & CO_FL_ERROR) && (conn->flags & CO_FL_HANDSHAKE))) |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2108 | goto end; |
| 2109 | h1c->flags &= ~H1C_F_CS_WAIT_CONN; |
Christopher Faulet | a0883e6 | 2018-12-11 16:26:50 +0100 | [diff] [blame] | 2110 | h1_wake_stream_for_send(h1s); |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 2111 | } |
| 2112 | |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 2113 | if (!h1s) { |
Christopher Faulet | 470438c | 2019-07-11 15:40:25 +0200 | [diff] [blame] | 2114 | if (h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN) || |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 2115 | conn->flags & (CO_FL_ERROR|CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2116 | goto release; |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 2117 | if (!conn_is_back(conn) && (h1c->flags & H1C_F_CS_IDLE)) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2118 | if (!h1s_create(h1c, NULL, NULL)) |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2119 | goto release; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2120 | } |
Christopher Faulet | 1a7ad7a | 2018-12-04 16:10:44 +0100 | [diff] [blame] | 2121 | else |
Olivier Houchard | e728478 | 2018-12-06 18:54:54 +0100 | [diff] [blame] | 2122 | goto end; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 2123 | h1s = h1c->h1s; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2124 | } |
| 2125 | |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 2126 | if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1) |
| 2127 | h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake; |
| 2128 | |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 2129 | if (conn_xprt_read0_pending(conn)) |
| 2130 | h1s->flags |= H1S_F_REOS; |
| 2131 | |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 2132 | if (!h1s_data_pending(h1s) && h1s && h1s->cs && h1s->cs->data_cb->wake && |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 2133 | (h1s->flags & H1S_F_REOS || h1c->flags & H1C_F_CS_ERROR || |
Olivier Houchard | 32d75ed | 2019-01-14 17:27:23 +0100 | [diff] [blame] | 2134 | conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) { |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2135 | if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR) |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 2136 | h1s->cs->flags |= CS_FL_ERROR; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2137 | h1s->cs->data_cb->wake(h1s->cs); |
| 2138 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 2139 | end: |
Christopher Faulet | b576206 | 2020-08-05 11:31:16 +0200 | [diff] [blame^] | 2140 | h1_refresh_timeout(h1c); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2141 | return 0; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2142 | |
| 2143 | release: |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2144 | h1_release(h1c); |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2145 | return -1; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status) |
| 2149 | { |
| 2150 | struct h1c *h1c = ctx; |
| 2151 | int ret = 0; |
| 2152 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2153 | if (!(h1c->wait_event.events & SUB_RETRY_SEND)) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2154 | ret = h1_send(h1c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2155 | if (!(h1c->wait_event.events & SUB_RETRY_RECV)) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2156 | ret |= h1_recv(h1c); |
Christopher Faulet | 81d4843 | 2018-11-19 21:22:43 +0100 | [diff] [blame] | 2157 | if (ret || !h1c->h1s) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2158 | h1_process(h1c); |
| 2159 | return NULL; |
| 2160 | } |
| 2161 | |
Olivier Houchard | 9a86fcb | 2018-12-11 16:47:14 +0100 | [diff] [blame] | 2162 | static void h1_reset(struct connection *conn) |
| 2163 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2164 | struct h1c *h1c = conn->ctx; |
Olivier Houchard | 9a86fcb | 2018-12-11 16:47:14 +0100 | [diff] [blame] | 2165 | |
| 2166 | /* Reset the flags, and let the mux know we're waiting for a connection */ |
| 2167 | h1c->flags = H1C_F_CS_WAIT_CONN; |
| 2168 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2169 | |
| 2170 | static int h1_wake(struct connection *conn) |
| 2171 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2172 | struct h1c *h1c = conn->ctx; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2173 | int ret; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2174 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2175 | h1_send(h1c); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2176 | ret = h1_process(h1c); |
| 2177 | if (ret == 0) { |
| 2178 | struct h1s *h1s = h1c->h1s; |
| 2179 | |
| 2180 | if (h1s && h1s->cs && h1s->cs->data_cb->wake) |
| 2181 | ret = h1s->cs->data_cb->wake(h1s->cs); |
| 2182 | } |
| 2183 | return ret; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2184 | } |
| 2185 | |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2186 | /* Connection timeout management. The principle is that if there's no receipt |
| 2187 | * nor sending for a certain amount of time, the connection is closed. |
| 2188 | */ |
| 2189 | static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state) |
| 2190 | { |
| 2191 | struct h1c *h1c = context; |
| 2192 | int expired = tick_is_expired(t->expire, now_ms); |
| 2193 | |
| 2194 | if (!expired && h1c) |
| 2195 | return t; |
| 2196 | |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 2197 | task_destroy(t); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2198 | |
| 2199 | if (!h1c) { |
| 2200 | /* resources were already deleted */ |
| 2201 | return NULL; |
| 2202 | } |
| 2203 | |
| 2204 | h1c->task = NULL; |
| 2205 | /* If a stream is still attached to the mux, just set an error and wait |
| 2206 | * for the stream's timeout. Otherwise, release the mux. This is only ok |
| 2207 | * because same timeouts are used. |
| 2208 | */ |
| 2209 | if (h1c->h1s && h1c->h1s->cs) |
| 2210 | h1c->flags |= H1C_F_CS_ERROR; |
| 2211 | else |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2212 | h1_release(h1c); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2213 | return NULL; |
| 2214 | } |
| 2215 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2216 | /*******************************************/ |
| 2217 | /* functions below are used by the streams */ |
| 2218 | /*******************************************/ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2219 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2220 | /* |
| 2221 | * Attach a new stream to a connection |
| 2222 | * (Used for outgoing connections) |
| 2223 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2224 | static struct conn_stream *h1_attach(struct connection *conn, struct session *sess) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2225 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2226 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2227 | struct conn_stream *cs = NULL; |
| 2228 | struct h1s *h1s; |
| 2229 | |
| 2230 | if (h1c->flags & H1C_F_CS_ERROR) |
| 2231 | goto end; |
| 2232 | |
| 2233 | cs = cs_new(h1c->conn); |
| 2234 | if (!cs) |
| 2235 | goto end; |
| 2236 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2237 | h1s = h1s_create(h1c, cs, sess); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2238 | if (h1s == NULL) |
| 2239 | goto end; |
| 2240 | |
| 2241 | return cs; |
| 2242 | end: |
| 2243 | cs_free(cs); |
| 2244 | return NULL; |
| 2245 | } |
| 2246 | |
| 2247 | /* Retrieves a valid conn_stream from this connection, or returns NULL. For |
| 2248 | * this mux, it's easy as we can only store a single conn_stream. |
| 2249 | */ |
| 2250 | static const struct conn_stream *h1_get_first_cs(const struct connection *conn) |
| 2251 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2252 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2253 | struct h1s *h1s = h1c->h1s; |
| 2254 | |
| 2255 | if (h1s) |
| 2256 | return h1s->cs; |
| 2257 | |
| 2258 | return NULL; |
| 2259 | } |
| 2260 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2261 | static void h1_destroy(void *ctx) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2262 | { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2263 | struct h1c *h1c = ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2264 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 2265 | if (!h1c->h1s || !h1c->conn || h1c->conn->ctx != h1c) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2266 | h1_release(h1c); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | /* |
| 2270 | * Detach the stream from the connection and possibly release the connection. |
| 2271 | */ |
| 2272 | static void h1_detach(struct conn_stream *cs) |
| 2273 | { |
| 2274 | struct h1s *h1s = cs->ctx; |
| 2275 | struct h1c *h1c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2276 | struct session *sess; |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2277 | int is_not_first; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2278 | |
| 2279 | cs->ctx = NULL; |
| 2280 | if (!h1s) |
| 2281 | return; |
| 2282 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2283 | sess = h1s->sess; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2284 | h1c = h1s->h1c; |
| 2285 | h1s->cs = NULL; |
| 2286 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2287 | is_not_first = h1s->flags & H1S_F_NOT_FIRST; |
| 2288 | h1s_destroy(h1s); |
| 2289 | |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 2290 | if (conn_is_back(h1c->conn) && (h1c->flags & H1C_F_CS_IDLE)) { |
Christopher Faulet | 0bf28f8 | 2019-07-19 14:51:06 +0200 | [diff] [blame] | 2291 | /* If there are any excess server data in the input buffer, |
| 2292 | * release it and close the connection ASAP (some data may |
| 2293 | * remain in the output buffer). This happens if a server sends |
| 2294 | * invalid responses. So in such case, we don't want to reuse |
| 2295 | * the connection |
Christopher Faulet | 39e679b | 2019-07-19 11:34:08 +0200 | [diff] [blame] | 2296 | */ |
Christopher Faulet | 0bf28f8 | 2019-07-19 14:51:06 +0200 | [diff] [blame] | 2297 | if (b_data(&h1c->ibuf)) { |
| 2298 | h1_release_buf(h1c, &h1c->ibuf); |
Christopher Faulet | e31a0f1 | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 2299 | h1c->flags = (h1c->flags & ~H1C_F_CS_IDLE) | H1C_F_CS_SHUTW_NOW; |
Christopher Faulet | 0bf28f8 | 2019-07-19 14:51:06 +0200 | [diff] [blame] | 2300 | goto release; |
| 2301 | } |
Christopher Faulet | 39e679b | 2019-07-19 11:34:08 +0200 | [diff] [blame] | 2302 | |
Christopher Faulet | 9400a39 | 2018-11-23 23:10:39 +0100 | [diff] [blame] | 2303 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 2304 | if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
Christopher Faulet | 9400a39 | 2018-11-23 23:10:39 +0100 | [diff] [blame] | 2305 | h1c->conn->flags |= CO_FL_PRIVATE; |
| 2306 | |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 2307 | if (!(h1c->conn->owner)) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2308 | h1c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 2309 | if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) { |
| 2310 | h1c->conn->owner = NULL; |
| 2311 | if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn)) |
| 2312 | /* The server doesn't want it, let's kill the connection right away */ |
Olivier Houchard | 109ba13 | 2020-02-14 13:23:45 +0100 | [diff] [blame] | 2313 | h1c->conn->mux->destroy(h1c); |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 2314 | else |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2315 | tasklet_wakeup(h1c->wait_event.tasklet); |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 2316 | return; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 2317 | } |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 2318 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 2319 | if (h1c->conn->owner == sess) { |
| 2320 | int ret = session_check_idle_conn(sess, h1c->conn); |
| 2321 | if (ret == -1) |
| 2322 | /* The connection got destroyed, let's leave */ |
| 2323 | return; |
| 2324 | else if (ret == 1) { |
| 2325 | /* The connection was added to the server list, |
| 2326 | * wake the task so we can subscribe to events |
| 2327 | */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2328 | tasklet_wakeup(h1c->wait_event.tasklet); |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 2329 | return; |
| 2330 | } |
| 2331 | } |
Christopher Faulet | 9400a39 | 2018-11-23 23:10:39 +0100 | [diff] [blame] | 2332 | /* we're in keep-alive with an idle connection, monitor it if not already done */ |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 2333 | if (LIST_ISEMPTY(&h1c->conn->list)) { |
Christopher Faulet | 9400a39 | 2018-11-23 23:10:39 +0100 | [diff] [blame] | 2334 | struct server *srv = objt_server(h1c->conn->target); |
| 2335 | |
| 2336 | if (srv) { |
| 2337 | if (h1c->conn->flags & CO_FL_PRIVATE) |
| 2338 | LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list); |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2339 | else if (is_not_first) |
Christopher Faulet | 9400a39 | 2018-11-23 23:10:39 +0100 | [diff] [blame] | 2340 | LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list); |
| 2341 | else |
| 2342 | LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list); |
| 2343 | } |
| 2344 | } |
| 2345 | } |
| 2346 | |
Christopher Faulet | 0bf28f8 | 2019-07-19 14:51:06 +0200 | [diff] [blame] | 2347 | release: |
Christopher Faulet | 9fa93f6 | 2019-06-28 17:41:42 +0200 | [diff] [blame] | 2348 | /* We don't want to close right now unless the connection is in error or shut down for writes */ |
Christopher Faulet | 470438c | 2019-07-11 15:40:25 +0200 | [diff] [blame] | 2349 | if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN|H1C_F_UPG_H2C)) || |
| 2350 | (h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) || |
| 2351 | ((h1c->flags & H1C_F_CS_SHUTW_NOW) && !b_data(&h1c->obuf)) || |
| 2352 | !h1c->conn->owner) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 2353 | h1_release(h1c); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2354 | else { |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 2355 | tasklet_wakeup(h1c->wait_event.tasklet); |
Christopher Faulet | b576206 | 2020-08-05 11:31:16 +0200 | [diff] [blame^] | 2356 | h1_refresh_timeout(h1c); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2357 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2358 | } |
| 2359 | |
| 2360 | |
| 2361 | static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 2362 | { |
| 2363 | struct h1s *h1s = cs->ctx; |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 2364 | struct h1c *h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2365 | |
| 2366 | if (!h1s) |
| 2367 | return; |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 2368 | h1c = h1s->h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2369 | |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 2370 | if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) |
| 2371 | goto do_shutr; |
| 2372 | |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 2373 | if ((h1c->flags & H1C_F_UPG_H2C) || (h1s->flags & H1S_F_WANT_KAL)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2374 | return; |
| 2375 | |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 2376 | do_shutr: |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2377 | /* NOTE: Be sure to handle abort (cf. h2_shutr) */ |
| 2378 | if (cs->flags & CS_FL_SHR) |
| 2379 | return; |
| 2380 | if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2381 | cs->conn->xprt->shutr(cs->conn, cs->conn->xprt_ctx, |
Christopher Faulet | f36e72b | 2019-12-05 11:18:31 +0100 | [diff] [blame] | 2382 | (mode == CS_SHR_DRAIN)); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 2386 | { |
| 2387 | struct h1s *h1s = cs->ctx; |
| 2388 | struct h1c *h1c; |
| 2389 | |
| 2390 | if (!h1s) |
| 2391 | return; |
| 2392 | h1c = h1s->h1c; |
| 2393 | |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 2394 | if ((cs->flags & CS_FL_KILL_CONN) || (h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) |
| 2395 | goto do_shutw; |
Olivier Houchard | d2e88c7 | 2018-12-19 15:55:23 +0100 | [diff] [blame] | 2396 | |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 2397 | if ((h1c->flags & H1C_F_UPG_H2C) || |
| 2398 | ((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2399 | return; |
| 2400 | |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 2401 | do_shutw: |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2402 | h1c->flags |= H1C_F_CS_SHUTW_NOW; |
| 2403 | if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf)) |
| 2404 | return; |
| 2405 | |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 2406 | h1_shutw_conn(cs->conn, mode); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2407 | } |
| 2408 | |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 2409 | static void h1_shutw_conn(struct connection *conn, enum cs_shw_mode mode) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2410 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2411 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2412 | |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 2413 | conn_xprt_shutw(conn); |
| 2414 | conn_sock_shutw(conn, (mode == CS_SHW_NORMAL)); |
Christopher Faulet | f36e72b | 2019-12-05 11:18:31 +0100 | [diff] [blame] | 2415 | h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTDOWN; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | /* Called from the upper layer, to unsubscribe to events */ |
| 2419 | static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 2420 | { |
| 2421 | struct wait_event *sw; |
| 2422 | struct h1s *h1s = cs->ctx; |
| 2423 | |
| 2424 | if (!h1s) |
| 2425 | return 0; |
| 2426 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2427 | if (event_type & SUB_RETRY_RECV) { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2428 | sw = param; |
Olivier Houchard | 00b8f7c | 2019-05-14 18:02:23 +0200 | [diff] [blame] | 2429 | BUG_ON(h1s->recv_wait != sw); |
| 2430 | sw->events &= ~SUB_RETRY_RECV; |
| 2431 | h1s->recv_wait = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2432 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2433 | if (event_type & SUB_RETRY_SEND) { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2434 | sw = param; |
Olivier Houchard | 00b8f7c | 2019-05-14 18:02:23 +0200 | [diff] [blame] | 2435 | BUG_ON(h1s->send_wait != sw); |
| 2436 | sw->events &= ~SUB_RETRY_SEND; |
| 2437 | h1s->send_wait = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2438 | } |
| 2439 | return 0; |
| 2440 | } |
| 2441 | |
| 2442 | /* Called from the upper layer, to subscribe to events, such as being able to send */ |
| 2443 | static int h1_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 2444 | { |
| 2445 | struct wait_event *sw; |
| 2446 | struct h1s *h1s = cs->ctx; |
| 2447 | |
| 2448 | if (!h1s) |
| 2449 | return -1; |
| 2450 | |
| 2451 | switch (event_type) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2452 | case SUB_RETRY_RECV: |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2453 | sw = param; |
Olivier Houchard | 00b8f7c | 2019-05-14 18:02:23 +0200 | [diff] [blame] | 2454 | BUG_ON(h1s->recv_wait != NULL || (sw->events & SUB_RETRY_RECV)); |
| 2455 | sw->events |= SUB_RETRY_RECV; |
| 2456 | h1s->recv_wait = sw; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2457 | return 0; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2458 | case SUB_RETRY_SEND: |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2459 | sw = param; |
Olivier Houchard | 00b8f7c | 2019-05-14 18:02:23 +0200 | [diff] [blame] | 2460 | BUG_ON(h1s->send_wait != NULL || (sw->events & SUB_RETRY_SEND)); |
| 2461 | sw->events |= SUB_RETRY_SEND; |
| 2462 | h1s->send_wait = sw; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2463 | return 0; |
| 2464 | default: |
| 2465 | break; |
| 2466 | } |
| 2467 | return -1; |
| 2468 | } |
| 2469 | |
| 2470 | /* Called from the upper layer, to receive data */ |
| 2471 | static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 2472 | { |
| 2473 | struct h1s *h1s = cs->ctx; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2474 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2475 | size_t ret = 0; |
| 2476 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2477 | if (!(h1c->flags & H1C_F_IN_ALLOC)) |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 2478 | ret = h1_process_input(h1c, buf, count); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2479 | |
Christopher Faulet | e18777b | 2019-04-16 16:46:36 +0200 | [diff] [blame] | 2480 | if (flags & CO_RFL_BUF_FLUSH) { |
| 2481 | struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res); |
| 2482 | |
Christopher Faulet | afadc9a | 2020-07-03 14:51:15 +0200 | [diff] [blame] | 2483 | if (h1m->state == H1_MSG_TUNNEL || (h1m->state == H1_MSG_DATA && h1m->curr_len)) |
Christopher Faulet | e18777b | 2019-04-16 16:46:36 +0200 | [diff] [blame] | 2484 | h1s->flags |= H1S_F_BUF_FLUSH; |
| 2485 | } |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 2486 | else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) { |
Christopher Faulet | 0528ae2 | 2020-07-03 15:12:00 +0200 | [diff] [blame] | 2487 | if (ret) |
| 2488 | h1s->flags &= ~H1S_F_SPLICED_DATA; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2489 | if (!(h1c->wait_event.events & SUB_RETRY_RECV)) |
Christopher Faulet | d2a762f | 2020-07-24 16:31:14 +0200 | [diff] [blame] | 2490 | tasklet_wakeup(h1c->wait_event.tasklet); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2491 | } |
| 2492 | return ret; |
| 2493 | } |
| 2494 | |
| 2495 | |
| 2496 | /* Called from the upper layer, to send data */ |
| 2497 | static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 2498 | { |
| 2499 | struct h1s *h1s = cs->ctx; |
| 2500 | struct h1c *h1c; |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2501 | size_t total = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2502 | |
| 2503 | if (!h1s) |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2504 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2505 | |
| 2506 | h1c = h1s->h1c; |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 2507 | if (h1c->flags & H1C_F_CS_WAIT_CONN) |
| 2508 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2509 | |
Christopher Faulet | c31872f | 2019-06-04 22:09:36 +0200 | [diff] [blame] | 2510 | while (count) { |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2511 | size_t ret = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2512 | |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2513 | if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC))) |
| 2514 | ret = h1_process_output(h1c, buf, count); |
| 2515 | if (!ret) |
| 2516 | break; |
| 2517 | total += ret; |
Christopher Faulet | c31872f | 2019-06-04 22:09:36 +0200 | [diff] [blame] | 2518 | count -= ret; |
Olivier Houchard | a101286 | 2020-01-15 19:13:32 +0100 | [diff] [blame] | 2519 | if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c)) |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2520 | break; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2521 | } |
Christopher Faulet | b576206 | 2020-08-05 11:31:16 +0200 | [diff] [blame^] | 2522 | h1_refresh_timeout(h1c); |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2523 | return total; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2524 | } |
| 2525 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 2526 | #if defined(USE_LINUX_SPLICE) |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2527 | /* Send and get, using splicing */ |
| 2528 | static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count) |
| 2529 | { |
| 2530 | struct h1s *h1s = cs->ctx; |
| 2531 | struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res); |
| 2532 | int ret = 0; |
| 2533 | |
Christopher Faulet | 2c411be | 2019-11-05 16:24:27 +0100 | [diff] [blame] | 2534 | if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) { |
Christopher Faulet | e18777b | 2019-04-16 16:46:36 +0200 | [diff] [blame] | 2535 | h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA); |
Christopher Faulet | 1146f97 | 2019-05-29 14:35:24 +0200 | [diff] [blame] | 2536 | if (!(h1s->h1c->wait_event.events & SUB_RETRY_RECV)) |
| 2537 | cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_RECV, &h1s->h1c->wait_event); |
Christopher Faulet | e18777b | 2019-04-16 16:46:36 +0200 | [diff] [blame] | 2538 | goto end; |
| 2539 | } |
| 2540 | |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 2541 | if (h1s_data_pending(h1s)) { |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 2542 | h1s->flags |= H1S_F_BUF_FLUSH; |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2543 | goto end; |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 2544 | } |
| 2545 | |
| 2546 | h1s->flags &= ~H1S_F_BUF_FLUSH; |
| 2547 | h1s->flags |= H1S_F_SPLICED_DATA; |
Christopher Faulet | 8e8168a | 2020-07-03 15:02:25 +0200 | [diff] [blame] | 2548 | |
| 2549 | if (!h1_recv_allowed(h1s->h1c)) |
| 2550 | goto end; |
| 2551 | |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2552 | if (h1m->state == H1_MSG_DATA && count > h1m->curr_len) |
| 2553 | count = h1m->curr_len; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2554 | ret = cs->conn->xprt->rcv_pipe(cs->conn, cs->conn->xprt_ctx, pipe, count); |
Christopher Faulet | 290988a | 2020-07-24 16:13:12 +0200 | [diff] [blame] | 2555 | if (ret <= 0) |
| 2556 | h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA); |
| 2557 | else if (h1m->state == H1_MSG_DATA) { |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2558 | h1m->curr_len -= ret; |
Christopher Faulet | e18777b | 2019-04-16 16:46:36 +0200 | [diff] [blame] | 2559 | if (!h1m->curr_len) |
| 2560 | h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA); |
| 2561 | } |
| 2562 | |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2563 | end: |
Christopher Faulet | 038ad81 | 2019-04-17 11:03:22 +0200 | [diff] [blame] | 2564 | if (conn_xprt_read0_pending(cs->conn)) { |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 2565 | h1s->flags |= H1S_F_REOS; |
Christopher Faulet | ac4778c | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 2566 | h1s->flags &= ~(H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA); |
Christopher Faulet | 038ad81 | 2019-04-17 11:03:22 +0200 | [diff] [blame] | 2567 | } |
Willy Tarreau | 7e9dd73 | 2020-01-17 16:19:34 +0100 | [diff] [blame] | 2568 | |
Christopher Faulet | e0ca6ad | 2020-07-07 10:56:40 +0200 | [diff] [blame] | 2569 | if ((h1s->flags & H1S_F_REOS) || |
| 2570 | (h1m->state != H1_MSG_TUNNEL && h1m->state != H1_MSG_DATA) || |
Christopher Faulet | afadc9a | 2020-07-03 14:51:15 +0200 | [diff] [blame] | 2571 | (h1m->state == H1_MSG_DATA && !h1m->curr_len)) |
Willy Tarreau | 7e9dd73 | 2020-01-17 16:19:34 +0100 | [diff] [blame] | 2572 | cs->flags &= ~CS_FL_MAY_SPLICE; |
| 2573 | |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2574 | return ret; |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2575 | } |
| 2576 | |
| 2577 | static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe) |
| 2578 | { |
| 2579 | struct h1s *h1s = cs->ctx; |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2580 | int ret = 0; |
| 2581 | |
| 2582 | if (b_data(&h1s->h1c->obuf)) |
| 2583 | goto end; |
| 2584 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2585 | ret = cs->conn->xprt->snd_pipe(cs->conn, cs->conn->xprt_ctx, pipe); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2586 | end: |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 2587 | if (pipe->data) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2588 | if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2589 | cs->conn->xprt->subscribe(cs->conn, cs->conn->xprt_ctx, SUB_RETRY_SEND, &h1s->h1c->wait_event); |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 2590 | } |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2591 | return ret; |
| 2592 | } |
| 2593 | #endif |
| 2594 | |
Olivier Houchard | 198d129 | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 2595 | static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output) |
| 2596 | { |
| 2597 | int ret = 0; |
| 2598 | switch (mux_ctl) { |
| 2599 | case MUX_STATUS: |
| 2600 | if (conn->flags & CO_FL_CONNECTED) |
| 2601 | ret |= MUX_STATUS_READY; |
| 2602 | return ret; |
| 2603 | default: |
| 2604 | return -1; |
| 2605 | } |
| 2606 | } |
| 2607 | |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 2608 | /* for debugging with CLI's "show fd" command */ |
| 2609 | static void h1_show_fd(struct buffer *msg, struct connection *conn) |
| 2610 | { |
| 2611 | struct h1c *h1c = conn->ctx; |
| 2612 | struct h1s *h1s = h1c->h1s; |
| 2613 | |
Christopher Faulet | f376a31 | 2019-01-04 15:16:06 +0100 | [diff] [blame] | 2614 | chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u", |
| 2615 | h1c->flags, h1c->wait_event.events, |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 2616 | (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf), |
| 2617 | (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf), |
| 2618 | (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf), |
| 2619 | (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf)); |
| 2620 | |
| 2621 | if (h1s) { |
| 2622 | char *method; |
| 2623 | |
| 2624 | if (h1s->meth < HTTP_METH_OTHER) |
| 2625 | method = http_known_methods[h1s->meth].ptr; |
| 2626 | else |
| 2627 | method = "UNKNOWN"; |
| 2628 | chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s" |
| 2629 | " .meth=%s status=%d", |
| 2630 | h1s, h1s->flags, |
| 2631 | h1m_state_str(h1s->req.state), |
| 2632 | h1m_state_str(h1s->res.state), method, h1s->status); |
| 2633 | if (h1s->cs) |
| 2634 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 2635 | h1s->cs->flags, h1s->cs->data); |
| 2636 | } |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 2637 | } |
| 2638 | |
| 2639 | |
| 2640 | /* Add an entry in the headers map. Returns -1 on error and 0 on success. */ |
| 2641 | static int add_hdr_case_adjust(const char *from, const char *to, char **err) |
| 2642 | { |
| 2643 | struct h1_hdr_entry *entry; |
| 2644 | |
| 2645 | /* Be sure there is a non-empty <to> */ |
| 2646 | if (!strlen(to)) { |
| 2647 | memprintf(err, "expect <to>"); |
| 2648 | return -1; |
| 2649 | } |
| 2650 | |
| 2651 | /* Be sure only the case differs between <from> and <to> */ |
| 2652 | if (strcasecmp(from, to)) { |
| 2653 | memprintf(err, "<from> and <to> must not differ execpt the case"); |
| 2654 | return -1; |
| 2655 | } |
| 2656 | |
| 2657 | /* Be sure <from> does not already existsin the tree */ |
| 2658 | if (ebis_lookup(&hdrs_map.map, from)) { |
| 2659 | memprintf(err, "duplicate entry '%s'", from); |
| 2660 | return -1; |
| 2661 | } |
| 2662 | |
| 2663 | /* Create the entry and insert it in the tree */ |
| 2664 | entry = malloc(sizeof(*entry)); |
| 2665 | if (!entry) { |
| 2666 | memprintf(err, "out of memory"); |
| 2667 | return -1; |
| 2668 | } |
| 2669 | |
| 2670 | entry->node.key = strdup(from); |
| 2671 | entry->name.ptr = strdup(to); |
| 2672 | entry->name.len = strlen(to); |
| 2673 | if (!entry->node.key || !entry->name.ptr) { |
| 2674 | free(entry->node.key); |
| 2675 | free(entry->name.ptr); |
| 2676 | free(entry); |
| 2677 | memprintf(err, "out of memory"); |
| 2678 | return -1; |
| 2679 | } |
| 2680 | ebis_insert(&hdrs_map.map, &entry->node); |
| 2681 | return 0; |
| 2682 | } |
| 2683 | |
| 2684 | static void h1_hdeaders_case_adjust_deinit() |
| 2685 | { |
| 2686 | struct ebpt_node *node, *next; |
| 2687 | struct h1_hdr_entry *entry; |
| 2688 | |
| 2689 | node = ebpt_first(&hdrs_map.map); |
| 2690 | while (node) { |
| 2691 | next = ebpt_next(node); |
| 2692 | ebpt_delete(node); |
| 2693 | entry = container_of(node, struct h1_hdr_entry, node); |
| 2694 | free(entry->node.key); |
| 2695 | free(entry->name.ptr); |
| 2696 | free(entry); |
| 2697 | node = next; |
| 2698 | } |
| 2699 | free(hdrs_map.name); |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 2700 | } |
| 2701 | |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 2702 | static int cfg_h1_headers_case_adjust_postparser() |
| 2703 | { |
| 2704 | FILE *file = NULL; |
| 2705 | char *c, *key_beg, *key_end, *value_beg, *value_end; |
| 2706 | char *err; |
| 2707 | int rc, line = 0, err_code = 0; |
| 2708 | |
| 2709 | if (!hdrs_map.name) |
| 2710 | goto end; |
| 2711 | |
| 2712 | file = fopen(hdrs_map.name, "r"); |
| 2713 | if (!file) { |
| 2714 | ha_alert("config : h1-outgoing-headers-case-adjust-file '%s': failed to open file.\n", |
| 2715 | hdrs_map.name); |
| 2716 | err_code |= ERR_ALERT | ERR_FATAL; |
| 2717 | goto end; |
| 2718 | } |
| 2719 | |
| 2720 | /* now parse all lines. The file may contain only two header name per |
| 2721 | * line, separated by spaces. All heading and trailing spaces will be |
| 2722 | * ignored. Lines starting with a # are ignored. |
| 2723 | */ |
| 2724 | while (fgets(trash.area, trash.size, file) != NULL) { |
| 2725 | line++; |
| 2726 | c = trash.area; |
| 2727 | |
| 2728 | /* strip leading spaces and tabs */ |
| 2729 | while (*c == ' ' || *c == '\t') |
| 2730 | c++; |
| 2731 | |
| 2732 | /* ignore emptu lines, or lines beginning with a dash */ |
| 2733 | if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n') |
| 2734 | continue; |
| 2735 | |
| 2736 | /* look for the end of the key */ |
| 2737 | key_beg = c; |
| 2738 | while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r') |
| 2739 | c++; |
| 2740 | key_end = c; |
| 2741 | |
| 2742 | /* strip middle spaces and tabs */ |
| 2743 | while (*c == ' ' || *c == '\t') |
| 2744 | c++; |
| 2745 | |
| 2746 | /* look for the end of the value, it is the end of the line */ |
| 2747 | value_beg = c; |
| 2748 | while (*c && *c != '\n' && *c != '\r') |
| 2749 | c++; |
| 2750 | value_end = c; |
| 2751 | |
| 2752 | /* trim possibly trailing spaces and tabs */ |
| 2753 | while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t')) |
| 2754 | value_end--; |
| 2755 | |
| 2756 | /* set final \0 and check entries */ |
| 2757 | *key_end = '\0'; |
| 2758 | *value_end = '\0'; |
| 2759 | |
| 2760 | err = NULL; |
| 2761 | rc = add_hdr_case_adjust(key_beg, value_beg, &err); |
| 2762 | if (rc < 0) { |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 2763 | ha_alert("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n", |
| 2764 | hdrs_map.name, err, line); |
| 2765 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | 27f2017 | 2019-07-30 16:51:42 +0200 | [diff] [blame] | 2766 | free(err); |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 2767 | goto end; |
| 2768 | } |
| 2769 | if (rc > 0) { |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 2770 | ha_warning("config : h1-outgoing-headers-case-adjust-file '%s' : %s at line %d.\n", |
| 2771 | hdrs_map.name, err, line); |
| 2772 | err_code |= ERR_WARN; |
Christopher Faulet | 27f2017 | 2019-07-30 16:51:42 +0200 | [diff] [blame] | 2773 | free(err); |
Christopher Faulet | a99ff4d | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 2774 | } |
| 2775 | } |
| 2776 | |
| 2777 | end: |
| 2778 | if (file) |
| 2779 | fclose(file); |
| 2780 | hap_register_post_deinit(h1_hdeaders_case_adjust_deinit); |
| 2781 | return err_code; |
| 2782 | } |
| 2783 | |
| 2784 | |
| 2785 | /* config parser for global "h1-outgoing-header-case-adjust" */ |
| 2786 | static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx, |
| 2787 | struct proxy *defpx, const char *file, int line, |
| 2788 | char **err) |
| 2789 | { |
| 2790 | if (too_many_args(2, args, err, NULL)) |
| 2791 | return -1; |
| 2792 | if (!*(args[1]) || !*(args[2])) { |
| 2793 | memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]); |
| 2794 | return -1; |
| 2795 | } |
| 2796 | return add_hdr_case_adjust(args[1], args[2], err); |
| 2797 | } |
| 2798 | |
| 2799 | /* config parser for global "h1-outgoing-headers-case-adjust-file" */ |
| 2800 | static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx, |
| 2801 | struct proxy *defpx, const char *file, int line, |
| 2802 | char **err) |
| 2803 | { |
| 2804 | if (too_many_args(1, args, err, NULL)) |
| 2805 | return -1; |
| 2806 | if (!*(args[1])) { |
| 2807 | memprintf(err, "'%s' expects <file> as argument.", args[0]); |
| 2808 | return -1; |
| 2809 | } |
| 2810 | free(hdrs_map.name); |
| 2811 | hdrs_map.name = strdup(args[1]); |
| 2812 | return 0; |
| 2813 | } |
| 2814 | |
| 2815 | |
| 2816 | /* config keyword parsers */ |
| 2817 | static struct cfg_kw_list cfg_kws = {{ }, { |
| 2818 | { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust }, |
| 2819 | { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file }, |
| 2820 | { 0, NULL, NULL }, |
| 2821 | } |
| 2822 | }; |
| 2823 | |
| 2824 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 2825 | REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser); |
| 2826 | |
| 2827 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2828 | /****************************************/ |
| 2829 | /* MUX initialization and instanciation */ |
| 2830 | /****************************************/ |
| 2831 | |
| 2832 | /* The mux operations */ |
Willy Tarreau | f77a158 | 2019-01-10 10:00:08 +0100 | [diff] [blame] | 2833 | static const struct mux_ops mux_h1_ops = { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2834 | .init = h1_init, |
| 2835 | .wake = h1_wake, |
| 2836 | .attach = h1_attach, |
| 2837 | .get_first_cs = h1_get_first_cs, |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 2838 | .get_cs_info = h1_get_cs_info, |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2839 | .detach = h1_detach, |
| 2840 | .destroy = h1_destroy, |
| 2841 | .avail_streams = h1_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 2842 | .used_streams = h1_used_streams, |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2843 | .rcv_buf = h1_rcv_buf, |
| 2844 | .snd_buf = h1_snd_buf, |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 2845 | #if defined(USE_LINUX_SPLICE) |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2846 | .rcv_pipe = h1_rcv_pipe, |
| 2847 | .snd_pipe = h1_snd_pipe, |
| 2848 | #endif |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2849 | .subscribe = h1_subscribe, |
| 2850 | .unsubscribe = h1_unsubscribe, |
| 2851 | .shutr = h1_shutr, |
| 2852 | .shutw = h1_shutw, |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 2853 | .show_fd = h1_show_fd, |
Olivier Houchard | 9a86fcb | 2018-12-11 16:47:14 +0100 | [diff] [blame] | 2854 | .reset = h1_reset, |
Olivier Houchard | 198d129 | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 2855 | .ctl = h1_ctl, |
Christopher Faulet | 9f38f5a | 2019-04-03 09:53:32 +0200 | [diff] [blame] | 2856 | .flags = MX_FL_HTX, |
Willy Tarreau | 0a7a4fb | 2019-05-22 11:36:54 +0200 | [diff] [blame] | 2857 | .name = "H1", |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2858 | }; |
| 2859 | |
| 2860 | |
| 2861 | /* this mux registers default HTX proto */ |
| 2862 | static struct mux_proto_list mux_proto_htx = |
| 2863 | { .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops }; |
| 2864 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2865 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx); |
| 2866 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2867 | /* |
| 2868 | * Local variables: |
| 2869 | * c-indent-level: 8 |
| 2870 | * c-basic-offset: 8 |
| 2871 | * End: |
| 2872 | */ |