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