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