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 | |
| 446 | LIST_DEL(&conn->list); |
| 447 | |
| 448 | if (h1c) { |
| 449 | if (!LIST_ISEMPTY(&h1c->buf_wait.list)) { |
| 450 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 451 | LIST_DEL(&h1c->buf_wait.list); |
| 452 | LIST_INIT(&h1c->buf_wait.list); |
| 453 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
| 454 | } |
| 455 | |
| 456 | h1_release_buf(h1c, &h1c->ibuf); |
| 457 | h1_release_buf(h1c, &h1c->obuf); |
| 458 | |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 459 | if (h1c->task) { |
| 460 | h1c->task->context = NULL; |
| 461 | task_wakeup(h1c->task, TASK_WOKEN_OTHER); |
| 462 | h1c->task = NULL; |
| 463 | } |
| 464 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 465 | if (h1c->wait_event.task) |
| 466 | tasklet_free(h1c->wait_event.task); |
| 467 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 468 | h1s_destroy(h1c->h1s); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 469 | if (h1c->wait_event.events != 0) |
| 470 | conn->xprt->unsubscribe(conn, h1c->wait_event.events, |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 471 | &h1c->wait_event); |
| 472 | pool_free(pool_head_h1c, h1c); |
| 473 | } |
| 474 | |
| 475 | conn->mux = NULL; |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 476 | conn->ctx = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 477 | |
| 478 | conn_stop_tracking(conn); |
| 479 | conn_full_close(conn); |
| 480 | if (conn->destroy_cb) |
| 481 | conn->destroy_cb(conn); |
| 482 | conn_free(conn); |
| 483 | } |
| 484 | |
| 485 | /******************************************************/ |
| 486 | /* functions below are for the H1 protocol processing */ |
| 487 | /******************************************************/ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 488 | /* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is |
| 489 | * greater or equal to 1.1 |
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 | 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] | 492 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 493 | const char *p = HTX_SL_REQ_VPTR(sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 494 | |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 495 | if ((HTX_SL_REQ_VLEN(sl) == 8) && |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 496 | (*(p + 5) > '1' || |
| 497 | (*(p + 5) == '1' && *(p + 7) >= '1'))) |
| 498 | h1m->flags |= H1_MF_VER_11; |
| 499 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 500 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 501 | /* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is |
| 502 | * greater or equal to 1.1 |
| 503 | */ |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 504 | 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] | 505 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 506 | const char *p = HTX_SL_RES_VPTR(sl); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 507 | |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 508 | if ((HTX_SL_RES_VLEN(sl) == 8) && |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 509 | (*(p + 5) > '1' || |
| 510 | (*(p + 5) == '1' && *(p + 7) >= '1'))) |
| 511 | h1m->flags |= H1_MF_VER_11; |
| 512 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 513 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 514 | /* |
| 515 | * Check the validity of the request version. If the version is valid, it |
| 516 | * returns 1. Otherwise, it returns 0. |
| 517 | */ |
| 518 | static int h1_process_req_vsn(struct h1s *h1s, struct h1m *h1m, union h1_sl sl) |
| 519 | { |
| 520 | struct h1c *h1c = h1s->h1c; |
| 521 | |
| 522 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 523 | * exactly one digit "." one digit. This check may be disabled using |
| 524 | * option accept-invalid-http-request. |
| 525 | */ |
| 526 | if (!(h1c->px->options2 & PR_O2_REQBUG_OK)) { |
| 527 | if (sl.rq.v.len != 8) |
| 528 | return 0; |
| 529 | |
| 530 | if (*(sl.rq.v.ptr + 4) != '/' || |
| 531 | !isdigit((unsigned char)*(sl.rq.v.ptr + 5)) || |
| 532 | *(sl.rq.v.ptr + 6) != '.' || |
| 533 | !isdigit((unsigned char)*(sl.rq.v.ptr + 7))) |
| 534 | return 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 535 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 536 | else if (!sl.rq.v.len) { |
| 537 | /* try to convert HTTP/0.9 requests to HTTP/1.0 */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 538 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 539 | /* RFC 1945 allows only GET for HTTP/0.9 requests */ |
| 540 | if (sl.rq.meth != HTTP_METH_GET) |
| 541 | return 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 542 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 543 | /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */ |
| 544 | if (!sl.rq.u.len) |
| 545 | return 0; |
| 546 | |
| 547 | /* Add HTTP version */ |
| 548 | sl.rq.v = ist("HTTP/1.0"); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 549 | return 1; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 550 | } |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 551 | |
| 552 | if ((sl.rq.v.len == 8) && |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 553 | ((*(sl.rq.v.ptr + 5) > '1') || |
| 554 | ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1')))) |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 555 | h1m->flags |= H1_MF_VER_11; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 556 | return 1; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 557 | } |
| 558 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 559 | /* |
| 560 | * Check the validity of the response version. If the version is valid, it |
| 561 | * returns 1. Otherwise, it returns 0. |
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 | 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] | 564 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 565 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 566 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 567 | /* RFC7230#2.6 has enforced the format of the HTTP version string to be |
| 568 | * exactly one digit "." one digit. This check may be disabled using |
| 569 | * option accept-invalid-http-request. |
| 570 | */ |
| 571 | if (!(h1c->px->options2 & PR_O2_RSPBUG_OK)) { |
| 572 | if (sl.st.v.len != 8) |
| 573 | return 0; |
| 574 | |
| 575 | if (*(sl.st.v.ptr + 4) != '/' || |
| 576 | !isdigit((unsigned char)*(sl.st.v.ptr + 5)) || |
| 577 | *(sl.st.v.ptr + 6) != '.' || |
| 578 | !isdigit((unsigned char)*(sl.st.v.ptr + 7))) |
| 579 | return 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 580 | } |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 581 | |
| 582 | if ((sl.st.v.len == 8) && |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 583 | ((*(sl.st.v.ptr + 5) > '1') || |
| 584 | ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1')))) |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 585 | h1m->flags |= H1_MF_VER_11; |
| 586 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 587 | return 1; |
| 588 | } |
| 589 | /* Remove all "Connection:" headers from the HTX message <htx> */ |
| 590 | static void h1_remove_conn_hdrs(struct h1m *h1m, struct htx *htx) |
| 591 | { |
| 592 | struct ist hdr = {.ptr = "Connection", .len = 10}; |
| 593 | struct http_hdr_ctx ctx; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 594 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 595 | while (http_find_header(htx, hdr, &ctx, 1)) |
| 596 | http_remove_header(htx, &ctx); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 597 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 598 | h1m->flags &= ~(H1_MF_CONN_KAL|H1_MF_CONN_CLO); |
| 599 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 600 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 601 | /* Add a "Connection:" header with the value <value> into the HTX message |
| 602 | * <htx>. |
| 603 | */ |
| 604 | static void h1_add_conn_hdr(struct h1m *h1m, struct htx *htx, struct ist value) |
| 605 | { |
| 606 | struct ist hdr = {.ptr = "Connection", .len = 10}; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 607 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 608 | http_add_header(htx, hdr, value); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /* Deduce the connection mode of the client connection, depending on the |
| 612 | * configuration and the H1 message flags. This function is called twice, the |
| 613 | * first time when the request is parsed and the second time when the response |
| 614 | * is parsed. |
| 615 | */ |
| 616 | static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m) |
| 617 | { |
| 618 | struct proxy *fe = h1s->h1c->px; |
| 619 | int flag = H1S_F_WANT_KAL; /* For client connection: server-close == keepalive */ |
| 620 | |
| 621 | /* Tunnel mode can only by set on the frontend */ |
| 622 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 623 | flag = H1S_F_WANT_TUN; |
| 624 | else if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) |
| 625 | flag = H1S_F_WANT_CLO; |
| 626 | |
| 627 | /* flags order: CLO > SCL > TUN > KAL */ |
| 628 | if ((h1s->flags & H1S_F_WANT_MSK) < flag) |
| 629 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag; |
| 630 | |
| 631 | if (h1m->flags & H1_MF_RESP) { |
| 632 | /* Either we've established an explicit tunnel, or we're |
| 633 | * switching the protocol. In both cases, we're very unlikely to |
| 634 | * understand the next protocols. We have to switch to tunnel |
| 635 | * mode, so that we transfer the request and responses then let |
| 636 | * this protocol pass unmodified. When we later implement |
| 637 | * specific parsers for such protocols, we'll want to check the |
| 638 | * Upgrade header which contains information about that protocol |
| 639 | * for responses with status 101 (eg: see RFC2817 about TLS). |
| 640 | */ |
| 641 | if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || |
| 642 | h1s->status == 101) |
| 643 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN; |
Christopher Faulet | bd44ca6 | 2018-11-29 09:55:22 +0100 | [diff] [blame] | 644 | else if (!(h1m->flags & H1_MF_XFER_LEN) || /* no length known => close */ |
| 645 | (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] | 646 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 647 | } |
| 648 | else { |
| 649 | if (h1s->flags & H1S_F_WANT_KAL && |
| 650 | (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */ |
| 651 | h1m->flags & H1_MF_CONN_CLO)) /* explicit close */ |
| 652 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 653 | } |
| 654 | |
| 655 | /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */ |
| 656 | if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED) |
| 657 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 658 | } |
| 659 | |
| 660 | /* Deduce the connection mode of the client connection, depending on the |
| 661 | * configuration and the H1 message flags. This function is called twice, the |
| 662 | * first time when the request is parsed and the second time when the response |
| 663 | * is parsed. |
| 664 | */ |
| 665 | static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m) |
| 666 | { |
Christopher Faulet | a1692f5 | 2018-11-22 10:19:50 +0100 | [diff] [blame] | 667 | struct h1c *h1c = h1s->h1c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 668 | struct session *sess = h1s->sess; |
Christopher Faulet | a1692f5 | 2018-11-22 10:19:50 +0100 | [diff] [blame] | 669 | struct proxy *be = h1c->px; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 670 | int flag = H1S_F_WANT_KAL; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 671 | int fe_flags = sess ? sess->fe->options : 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 672 | |
| 673 | /* Tunnel mode can only by set on the frontend */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 674 | if ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 675 | flag = H1S_F_WANT_TUN; |
| 676 | |
| 677 | /* For the server connection: server-close == httpclose */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 678 | if ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 679 | (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 680 | (fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_CLO || |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 681 | (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) |
| 682 | flag = H1S_F_WANT_CLO; |
| 683 | |
| 684 | /* flags order: CLO > SCL > TUN > KAL */ |
| 685 | if ((h1s->flags & H1S_F_WANT_MSK) < flag) |
| 686 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag; |
| 687 | |
| 688 | if (h1m->flags & H1_MF_RESP) { |
| 689 | /* Either we've established an explicit tunnel, or we're |
| 690 | * switching the protocol. In both cases, we're very unlikely to |
| 691 | * understand the next protocols. We have to switch to tunnel |
| 692 | * mode, so that we transfer the request and responses then let |
| 693 | * this protocol pass unmodified. When we later implement |
| 694 | * specific parsers for such protocols, we'll want to check the |
| 695 | * Upgrade header which contains information about that protocol |
| 696 | * for responses with status 101 (eg: see RFC2817 about TLS). |
| 697 | */ |
| 698 | if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || |
| 699 | h1s->status == 101) |
| 700 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN; |
| 701 | else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */ |
| 702 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 703 | else if (h1s->flags & H1S_F_WANT_KAL && |
| 704 | (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */ |
| 705 | h1m->flags & H1_MF_CONN_CLO)) /* explicit close */ |
| 706 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 707 | } |
Christopher Faulet | 7003378 | 2018-12-05 13:50:11 +0100 | [diff] [blame] | 708 | else { |
| 709 | if (h1s->flags & H1S_F_WANT_KAL && |
| 710 | (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */ |
| 711 | h1m->flags & H1_MF_CONN_CLO)) /* explicit close */ |
| 712 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 713 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 714 | |
| 715 | /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */ |
| 716 | if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED) |
| 717 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 718 | } |
| 719 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 720 | static void h1_update_req_conn_hdr(struct h1s *h1s, struct h1m *h1m, |
| 721 | struct htx *htx, struct ist *conn_val) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 722 | { |
| 723 | struct proxy *px = h1s->h1c->px; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 724 | |
| 725 | /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage" |
| 726 | * token is found |
| 727 | */ |
| 728 | 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] | 729 | return; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 730 | |
| 731 | 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] | 732 | if (h1m->flags & H1_MF_CONN_CLO) { |
| 733 | if (conn_val) |
| 734 | *conn_val = ist(""); |
| 735 | if (htx) |
| 736 | h1_remove_conn_hdrs(h1m, htx); |
| 737 | } |
| 738 | if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) { |
| 739 | if (conn_val) |
| 740 | *conn_val = ist("keep-alive"); |
| 741 | if (htx) |
| 742 | h1_add_conn_hdr(h1m, htx, ist("keep-alive")); |
| 743 | } |
Christopher Faulet | ce85149 | 2018-12-07 18:06:59 +0100 | [diff] [blame] | 744 | if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) == (H1_MF_VER_11|H1_MF_CONN_KAL)) { |
| 745 | if (conn_val) |
| 746 | *conn_val = ist(""); |
| 747 | if (htx) |
| 748 | h1_remove_conn_hdrs(h1m, htx); |
| 749 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 750 | } |
| 751 | else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 752 | if (h1m->flags & H1_MF_CONN_KAL) { |
| 753 | if (conn_val) |
| 754 | *conn_val = ist(""); |
| 755 | if (htx) |
| 756 | h1_remove_conn_hdrs(h1m, htx); |
| 757 | } |
| 758 | if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) { |
| 759 | if (conn_val) |
| 760 | *conn_val = ist("close"); |
| 761 | if (htx) |
| 762 | h1_add_conn_hdr(h1m, htx, ist("close")); |
| 763 | } |
Christopher Faulet | ce85149 | 2018-12-07 18:06:59 +0100 | [diff] [blame] | 764 | if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_CONN_CLO) { |
| 765 | if (conn_val) |
| 766 | *conn_val = ist(""); |
| 767 | if (htx) |
| 768 | h1_remove_conn_hdrs(h1m, htx); |
| 769 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 770 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 771 | } |
| 772 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 773 | static void h1_update_res_conn_hdr(struct h1s *h1s, struct h1m *h1m, |
| 774 | struct htx *htx, struct ist *conn_val) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 775 | { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 776 | /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage" |
| 777 | * token is found |
| 778 | */ |
| 779 | 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] | 780 | return; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 781 | |
| 782 | if (h1s->flags & H1S_F_WANT_KAL) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 783 | if (h1m->flags & H1_MF_CONN_CLO) { |
| 784 | if (conn_val) |
| 785 | *conn_val = ist(""); |
| 786 | if (htx) |
| 787 | h1_remove_conn_hdrs(h1m, htx); |
| 788 | } |
| 789 | if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) { |
| 790 | if (conn_val) |
| 791 | *conn_val = ist("keep-alive"); |
| 792 | if (htx) |
| 793 | h1_add_conn_hdr(h1m, htx, ist("keep-alive")); |
| 794 | } |
Christopher Faulet | ce85149 | 2018-12-07 18:06:59 +0100 | [diff] [blame] | 795 | if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) == (H1_MF_VER_11|H1_MF_CONN_KAL)) { |
| 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 | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1036 | if (h1m->state == H1_MSG_DONE) |
| 1037 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 1038 | goto error; |
| 1039 | |
| 1040 | h1_process_conn_mode(h1s, h1m, htx, NULL); |
| 1041 | |
| 1042 | /* If body length cannot be determined, set htx->extra to |
| 1043 | * ULLONG_MAX. This value is impossible in other cases. |
| 1044 | */ |
| 1045 | htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX); |
| 1046 | |
| 1047 | /* Recheck there is enough space to do headers rewritting */ |
| 1048 | if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite) |
| 1049 | goto error; |
| 1050 | |
| 1051 | *ofs += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1052 | end: |
| 1053 | return ret; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1054 | |
| 1055 | error: |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1056 | h1m->err_state = h1m->state; |
| 1057 | h1m->err_pos = h1m->next; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1058 | vsn_error: |
| 1059 | 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] | 1060 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1061 | ret = 0; |
| 1062 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | /* |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1066 | * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it |
| 1067 | * 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] | 1068 | * 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] | 1069 | * responsible to update the parser state <h1m>. |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1070 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1071 | 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] | 1072 | struct buffer *buf, size_t *ofs, size_t max, |
Christopher Faulet | afe5784 | 2019-01-21 11:55:02 +0100 | [diff] [blame] | 1073 | struct buffer *htxbuf, size_t reserve) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1074 | { |
Christopher Faulet | afe5784 | 2019-01-21 11:55:02 +0100 | [diff] [blame] | 1075 | uint32_t data_space; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1076 | size_t total = 0; |
| 1077 | int ret = 0; |
| 1078 | |
Christopher Faulet | afe5784 | 2019-01-21 11:55:02 +0100 | [diff] [blame] | 1079 | data_space = htx_free_data_space(htx); |
| 1080 | if (data_space <= reserve) |
| 1081 | goto end; |
| 1082 | data_space -= reserve; |
| 1083 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1084 | if (h1m->flags & H1_MF_XFER_LEN) { |
| 1085 | if (h1m->flags & H1_MF_CLEN) { |
| 1086 | /* content-length: read only h2m->body_len */ |
| 1087 | ret = max; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1088 | if (ret > data_space) |
| 1089 | ret = data_space; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1090 | if ((uint64_t)ret > h1m->curr_len) |
| 1091 | ret = h1m->curr_len; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1092 | if (ret > b_contig_data(buf, *ofs)) |
| 1093 | ret = b_contig_data(buf, *ofs); |
| 1094 | if (ret) { |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 1095 | /* very often with large files we'll face the following |
| 1096 | * situation : |
| 1097 | * - htx is empty and points to <htxbuf> |
| 1098 | * - ret == buf->data |
| 1099 | * - buf->head == sizeof(struct htx) |
| 1100 | * => we can swap the buffers and place an htx header into |
| 1101 | * the target buffer instead |
| 1102 | */ |
| 1103 | if (unlikely(htx_is_empty(htx) && ret == b_data(buf) && |
| 1104 | !*ofs && b_head_ofs(buf) == sizeof(struct htx))) { |
| 1105 | void *raw_area = buf->area; |
| 1106 | void *htx_area = htxbuf->area; |
| 1107 | struct htx_blk *blk; |
| 1108 | |
| 1109 | buf->area = htx_area; |
| 1110 | htxbuf->area = raw_area; |
| 1111 | htx = (struct htx *)htxbuf->area; |
| 1112 | htx->size = htxbuf->size - sizeof(*htx); |
| 1113 | htx_reset(htx); |
| 1114 | b_set_data(htxbuf, b_size(htxbuf)); |
| 1115 | |
| 1116 | blk = htx_add_blk(htx, HTX_BLK_DATA, ret); |
| 1117 | blk->info += ret; |
| 1118 | /* nothing else to do, the old buffer now contains an |
| 1119 | * empty pre-initialized HTX header |
| 1120 | */ |
| 1121 | } |
| 1122 | else if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret))) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1123 | goto end; |
| 1124 | h1m->curr_len -= ret; |
| 1125 | *ofs += ret; |
| 1126 | total += ret; |
| 1127 | } |
| 1128 | |
| 1129 | if (!h1m->curr_len) { |
| 1130 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 1131 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1132 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1133 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1134 | } |
| 1135 | else if (h1m->flags & H1_MF_CHNK) { |
| 1136 | new_chunk: |
| 1137 | /* te:chunked : parse chunks */ |
| 1138 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1139 | ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1140 | if (ret <= 0) |
| 1141 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1142 | h1m->state = H1_MSG_CHUNK_SIZE; |
| 1143 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1144 | max -= ret; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1145 | *ofs += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1146 | total += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
| 1150 | unsigned int chksz; |
| 1151 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1152 | ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1153 | if (ret <= 0) |
| 1154 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1155 | if (!chksz) { |
| 1156 | if (!htx_add_endof(htx, HTX_BLK_EOD)) |
| 1157 | goto end; |
Willy Tarreau | 34d2348 | 2019-01-03 17:46:56 +0100 | [diff] [blame] | 1158 | h1s->flags |= H1S_F_HAVE_I_EOD; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1159 | h1m->state = H1_MSG_TRAILERS; |
| 1160 | } |
| 1161 | else |
| 1162 | h1m->state = H1_MSG_DATA; |
| 1163 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1164 | h1m->curr_len = chksz; |
| 1165 | h1m->body_len += chksz; |
| 1166 | max -= ret; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1167 | *ofs += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1168 | total += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | if (h1m->state == H1_MSG_DATA) { |
| 1172 | ret = max; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1173 | if (ret > data_space) |
| 1174 | ret = data_space; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1175 | if ((uint64_t)ret > h1m->curr_len) |
| 1176 | ret = h1m->curr_len; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1177 | if (ret > b_contig_data(buf, *ofs)) |
| 1178 | ret = b_contig_data(buf, *ofs); |
| 1179 | if (ret) { |
| 1180 | if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret))) |
| 1181 | goto end; |
| 1182 | h1m->curr_len -= ret; |
| 1183 | max -= ret; |
| 1184 | *ofs += ret; |
| 1185 | total += ret; |
| 1186 | } |
| 1187 | if (!h1m->curr_len) { |
| 1188 | h1m->state = H1_MSG_CHUNK_CRLF; |
Christopher Faulet | afe5784 | 2019-01-21 11:55:02 +0100 | [diff] [blame] | 1189 | data_space = htx_free_data_space(htx); |
| 1190 | if (data_space <= reserve) |
| 1191 | goto end; |
| 1192 | data_space -= reserve; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1193 | goto new_chunk; |
| 1194 | } |
| 1195 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | if (h1m->state == H1_MSG_TRAILERS) { |
Christopher Faulet | 1727648 | 2018-11-22 11:44:35 +0100 | [diff] [blame] | 1199 | /* Trailers were alread parsed, only the EOM |
| 1200 | * need to be added */ |
Willy Tarreau | 34d2348 | 2019-01-03 17:46:56 +0100 | [diff] [blame] | 1201 | if (h1s->flags & H1S_F_HAVE_I_TLR) |
Christopher Faulet | 1727648 | 2018-11-22 11:44:35 +0100 | [diff] [blame] | 1202 | goto skip_tlr_parsing; |
| 1203 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1204 | ret = h1_measure_trailers(buf, *ofs, *ofs + max); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1205 | if (ret > data_space) |
| 1206 | ret = (htx_is_empty(htx) ? -1 : 0); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1207 | if (ret <= 0) |
| 1208 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1209 | |
| 1210 | /* Realing input buffer if tailers wrap. For now |
| 1211 | * this is a workaroung. Because trailers are |
| 1212 | * not split on CRLF, like headers, there is no |
| 1213 | * way to know where to split it when trailers |
| 1214 | * wrap. This is a limitation of |
| 1215 | * h1_measure_trailers. |
| 1216 | */ |
| 1217 | if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret)) |
| 1218 | b_slow_realign(buf, trash.area, 0); |
| 1219 | |
| 1220 | if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret))) |
| 1221 | goto end; |
Willy Tarreau | 34d2348 | 2019-01-03 17:46:56 +0100 | [diff] [blame] | 1222 | h1s->flags |= H1S_F_HAVE_I_TLR; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1223 | max -= ret; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1224 | *ofs += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1225 | total += ret; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1226 | |
Christopher Faulet | 1727648 | 2018-11-22 11:44:35 +0100 | [diff] [blame] | 1227 | skip_tlr_parsing: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1228 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 1229 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1230 | h1m->state = H1_MSG_DONE; |
| 1231 | } |
| 1232 | } |
| 1233 | else { |
| 1234 | /* XFER_LEN is set but not CLEN nor CHNK, it means there |
| 1235 | * is no body. Switch the message in DONE state |
| 1236 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1237 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 1238 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1239 | h1m->state = H1_MSG_DONE; |
| 1240 | } |
| 1241 | } |
| 1242 | else { |
| 1243 | /* no content length, read till SHUTW */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1244 | ret = max; |
| 1245 | if (ret > data_space) |
| 1246 | ret = data_space; |
| 1247 | if (ret > b_contig_data(buf, *ofs)) |
| 1248 | ret = b_contig_data(buf, *ofs); |
| 1249 | if (ret) { |
| 1250 | if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret))) |
| 1251 | goto end; |
| 1252 | |
Olivier Houchard | cf42d5a | 2018-12-04 17:41:58 +0100 | [diff] [blame] | 1253 | *ofs += ret; |
| 1254 | total = ret; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1255 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | end: |
| 1259 | if (ret < 0) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1260 | 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] | 1261 | h1m->err_state = h1m->state; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1262 | h1m->err_pos = *ofs + max + ret; |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1263 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1264 | return 0; |
| 1265 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1266 | /* update htx->extra, only when the body length is known */ |
| 1267 | if (h1m->flags & H1_MF_XFER_LEN) |
| 1268 | htx->extra = h1m->curr_len; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1269 | return total; |
| 1270 | } |
| 1271 | |
| 1272 | /* |
| 1273 | * Synchronize the request and the response before reseting them. Except for 1xx |
| 1274 | * responses, we wait that the request and the response are in DONE state and |
| 1275 | * that all data are forwarded for both. For 1xx responses, only the response is |
| 1276 | * reset, waiting the final one. Many 1xx messages can be sent. |
| 1277 | */ |
| 1278 | static void h1_sync_messages(struct h1c *h1c) |
| 1279 | { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1280 | struct h1s *h1s = h1c->h1s; |
| 1281 | |
| 1282 | if (!h1s) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1283 | return; |
| 1284 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1285 | if (h1s->res.state == H1_MSG_DONE && |
| 1286 | (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) && |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1287 | (conn_is_back(h1c->conn) || !b_data(&h1c->obuf))) { |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1288 | /* For 100-Continue response or any other informational 1xx |
| 1289 | * response which is non-final, don't reset the request, the |
| 1290 | * transaction is not finished. We take care the response was |
| 1291 | * transferred before. |
| 1292 | */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1293 | h1m_init_res(&h1s->res); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1294 | h1s->res.flags |= H1_MF_NO_PHDR; |
Christopher Faulet | cb55f48 | 2018-12-10 11:56:47 +0100 | [diff] [blame] | 1295 | h1c->flags &= ~H1C_F_IN_BUSY; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1296 | } |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1297 | else if (!b_data(&h1c->obuf) && |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1298 | h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { |
| 1299 | if (h1s->flags & H1S_F_WANT_TUN) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1300 | h1m_init_req(&h1s->req); |
| 1301 | h1m_init_res(&h1s->res); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1302 | h1s->req.state = H1_MSG_TUNNEL; |
| 1303 | h1s->res.state = H1_MSG_TUNNEL; |
Christopher Faulet | cb55f48 | 2018-12-10 11:56:47 +0100 | [diff] [blame] | 1304 | h1c->flags &= ~H1C_F_IN_BUSY; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1305 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | /* |
| 1310 | * 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] | 1311 | * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if |
| 1312 | * it couldn't proceed. |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1313 | */ |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1314 | 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] | 1315 | { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1316 | struct h1s *h1s = h1c->h1s; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1317 | struct h1m *h1m; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1318 | struct htx *htx; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1319 | size_t total = 0; |
| 1320 | size_t ret = 0; |
Christopher Faulet | afe5784 | 2019-01-21 11:55:02 +0100 | [diff] [blame] | 1321 | size_t count, rsv; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1322 | int errflag; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1323 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1324 | htx = htx_from_buf(buf); |
| 1325 | count = b_data(&h1c->ibuf); |
Willy Tarreau | 3a6190f | 2018-12-16 08:29:56 +0100 | [diff] [blame] | 1326 | if (!count) |
| 1327 | goto end; |
Christopher Faulet | afe5784 | 2019-01-21 11:55:02 +0100 | [diff] [blame] | 1328 | rsv = ((flags & CO_RFL_KEEP_RSV) ? global.tune.maxrewrite : 0); |
Willy Tarreau | 3a6190f | 2018-12-16 08:29:56 +0100 | [diff] [blame] | 1329 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1330 | if (!conn_is_back(h1c->conn)) { |
| 1331 | h1m = &h1s->req; |
| 1332 | errflag = H1S_F_REQ_ERROR; |
| 1333 | } |
| 1334 | else { |
| 1335 | h1m = &h1s->res; |
| 1336 | errflag = H1S_F_RES_ERROR; |
| 1337 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1338 | |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1339 | do { |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1340 | if (h1m->state <= H1_MSG_LAST_LF) { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1341 | ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1342 | if (!ret) |
| 1343 | break; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1344 | } |
| 1345 | else if (h1m->state <= H1_MSG_TRAILERS) { |
Christopher Faulet | afe5784 | 2019-01-21 11:55:02 +0100 | [diff] [blame] | 1346 | ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count, buf, rsv); |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 1347 | htx = htx_from_buf(buf); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1348 | if (!ret) |
| 1349 | break; |
| 1350 | } |
Christopher Faulet | cb55f48 | 2018-12-10 11:56:47 +0100 | [diff] [blame] | 1351 | else if (h1m->state == H1_MSG_DONE) { |
| 1352 | h1c->flags |= H1C_F_IN_BUSY; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1353 | break; |
Christopher Faulet | cb55f48 | 2018-12-10 11:56:47 +0100 | [diff] [blame] | 1354 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1355 | else if (h1m->state == H1_MSG_TUNNEL) { |
Christopher Faulet | afe5784 | 2019-01-21 11:55:02 +0100 | [diff] [blame] | 1356 | 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] | 1357 | htx = htx_from_buf(buf); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1358 | if (!ret) |
| 1359 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1360 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1361 | else { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1362 | h1s->flags |= errflag; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1363 | break; |
| 1364 | } |
| 1365 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1366 | count -= ret; |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1367 | } while (!(h1s->flags & errflag) && count); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1368 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1369 | if (h1s->flags & errflag) |
| 1370 | goto parsing_err; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1371 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1372 | b_del(&h1c->ibuf, total); |
| 1373 | |
| 1374 | end: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1375 | htx_to_buf(htx, buf); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1376 | |
Willy Tarreau | 45f2b89 | 2018-12-05 07:59:27 +0100 | [diff] [blame] | 1377 | 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] | 1378 | h1c->flags &= ~H1C_F_IN_FULL; |
| 1379 | tasklet_wakeup(h1c->wait_event.task); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1380 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1381 | |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 1382 | h1s->cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM); |
| 1383 | |
| 1384 | if (!b_data(&h1c->ibuf)) { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1385 | h1_release_buf(h1c, &h1c->ibuf); |
| 1386 | h1_sync_messages(h1c); |
Christopher Faulet | f6ce9d6 | 2018-12-10 15:30:06 +0100 | [diff] [blame] | 1387 | } |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 1388 | else if (!htx_is_empty(htx)) |
| 1389 | h1s->cs->flags |= CS_FL_RCV_MORE | CS_FL_WANT_ROOM; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1390 | |
Christopher Faulet | f6ce9d6 | 2018-12-10 15:30:06 +0100 | [diff] [blame] | 1391 | if ((h1s->cs->flags & CS_FL_REOS) && (!b_data(&h1c->ibuf) || htx_is_empty(htx))) { |
| 1392 | h1s->cs->flags |= CS_FL_EOS; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1393 | } |
Christopher Faulet | f6ce9d6 | 2018-12-10 15:30:06 +0100 | [diff] [blame] | 1394 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1395 | return total; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1396 | |
| 1397 | parsing_err: |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1398 | b_reset(&h1c->ibuf); |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1399 | htx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1400 | htx_to_buf(htx, buf); |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1401 | h1s->cs->flags |= CS_FL_EOS; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1402 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1403 | } |
| 1404 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1405 | /* |
| 1406 | * Process outgoing data. It parses data and transfer them from the channel buffer into |
| 1407 | * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or |
| 1408 | * 0 if it couldn't proceed. |
| 1409 | */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1410 | static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count) |
| 1411 | { |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1412 | struct h1s *h1s = h1c->h1s; |
| 1413 | struct h1m *h1m; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1414 | struct htx *chn_htx; |
| 1415 | struct htx_blk *blk; |
| 1416 | struct buffer *tmp; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1417 | size_t total = 0; |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1418 | 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] | 1419 | int errflag; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1420 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1421 | if (!count) |
| 1422 | goto end; |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 1423 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1424 | chn_htx = htx_from_buf(buf); |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 1425 | if (htx_is_empty(chn_htx)) |
| 1426 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1427 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1428 | if (!h1_get_buf(h1c, &h1c->obuf)) { |
| 1429 | h1c->flags |= H1C_F_OUT_ALLOC; |
| 1430 | goto end; |
| 1431 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1432 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1433 | if (!conn_is_back(h1c->conn)) { |
| 1434 | h1m = &h1s->res; |
| 1435 | errflag = H1S_F_RES_ERROR; |
| 1436 | } |
| 1437 | else { |
| 1438 | h1m = &h1s->req; |
| 1439 | errflag = H1S_F_REQ_ERROR; |
| 1440 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1441 | |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 1442 | /* the htx is non-empty thus has at least one block */ |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1443 | blk = htx_get_head_blk(chn_htx); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1444 | |
| 1445 | tmp = get_trash_chunk(); |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 1446 | |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1447 | /* Perform some optimizations to reduce the number of buffer copies. |
| 1448 | * First, if the mux's buffer is empty and the htx area contains |
| 1449 | * exactly one data block of the same size as the requested count, |
| 1450 | * then it's possible to simply swap the caller's buffer with the |
| 1451 | * mux's output buffer and adjust offsets and length to match the |
| 1452 | * entire DATA HTX block in the middle. In this case we perform a |
| 1453 | * true zero-copy operation from end-to-end. This is the situation |
| 1454 | * that happens all the time with large files. Second, if this is not |
| 1455 | * possible, but the mux's output buffer is empty, we still have an |
| 1456 | * opportunity to avoid the copy to the intermediary buffer, by making |
| 1457 | * the intermediary buffer's area point to the output buffer's area. |
| 1458 | * In this case we want to skip the HTX header to make sure that copies |
| 1459 | * remain aligned and that this operation remains possible all the |
| 1460 | * time. This goes for headers, data blocks and any data extracted from |
| 1461 | * the HTX blocks. |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 1462 | */ |
| 1463 | if (!b_data(&h1c->obuf)) { |
Olivier Houchard | 84cca66 | 2018-12-14 16:28:08 +0100 | [diff] [blame] | 1464 | h1c->obuf.head = sizeof(struct htx) + blk->addr; |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1465 | |
| 1466 | if (chn_htx->used == 1 && |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 1467 | htx_get_blk_type(blk) == HTX_BLK_DATA && |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1468 | htx_get_blk_value(chn_htx, blk).len == count) { |
| 1469 | void *old_area = h1c->obuf.area; |
| 1470 | |
| 1471 | h1c->obuf.area = buf->area; |
| 1472 | h1c->obuf.data = count; |
| 1473 | |
| 1474 | buf->area = old_area; |
| 1475 | buf->data = buf->head = 0; |
Christopher Faulet | adb2220 | 2018-12-12 10:32:09 +0100 | [diff] [blame] | 1476 | |
| 1477 | /* The message is chunked. We need to emit the chunk |
| 1478 | * size. We have at least the size of the struct htx to |
| 1479 | * write the chunk envelope. It should be enough. |
| 1480 | */ |
| 1481 | if (h1m->flags & H1_MF_CHNK) { |
| 1482 | h1_emit_chunk_size(&h1c->obuf, count); |
| 1483 | h1_emit_chunk_crlf(&h1c->obuf); |
| 1484 | } |
| 1485 | |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1486 | total += count; |
| 1487 | goto out; |
| 1488 | } |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 1489 | tmp->area = h1c->obuf.area + h1c->obuf.head; |
| 1490 | } |
| 1491 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1492 | tmp->size = b_room(&h1c->obuf); |
| 1493 | |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1494 | while (count && !(h1s->flags & errflag) && blk) { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1495 | struct htx_sl *sl; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1496 | struct ist n, v; |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1497 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1498 | uint32_t sz = htx_get_blksz(blk); |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1499 | uint32_t vlen; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1500 | |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1501 | vlen = sz; |
| 1502 | if (vlen > count) { |
| 1503 | if (type != HTX_BLK_DATA && type != HTX_BLK_TLR) |
| 1504 | goto copy; |
| 1505 | vlen = count; |
| 1506 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1507 | |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1508 | switch (type) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1509 | case HTX_BLK_UNUSED: |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1510 | break; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1511 | |
| 1512 | case HTX_BLK_REQ_SL: |
Christopher Faulet | 66229af | 2018-11-28 16:06:57 +0100 | [diff] [blame] | 1513 | h1m_init_req(h1m); |
| 1514 | h1m->flags |= H1_MF_NO_PHDR; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1515 | sl = htx_get_blk_ptr(chn_htx, blk); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1516 | h1s->meth = sl->info.req.meth; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1517 | h1_parse_req_vsn(h1m, sl); |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1518 | if (!htx_reqline_to_h1(sl, tmp)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1519 | goto copy; |
| 1520 | h1m->flags |= H1_MF_XFER_LEN; |
| 1521 | h1m->state = H1_MSG_HDR_FIRST; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1522 | break; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1523 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1524 | case HTX_BLK_RES_SL: |
Christopher Faulet | 66229af | 2018-11-28 16:06:57 +0100 | [diff] [blame] | 1525 | h1m_init_res(h1m); |
| 1526 | h1m->flags |= H1_MF_NO_PHDR; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1527 | sl = htx_get_blk_ptr(chn_htx, blk); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1528 | h1s->status = sl->info.res.status; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1529 | h1_parse_res_vsn(h1m, sl); |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1530 | if (!htx_stline_to_h1(sl, tmp)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1531 | goto copy; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1532 | if (sl->flags & HTX_SL_F_XFER_LEN) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1533 | h1m->flags |= H1_MF_XFER_LEN; |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1534 | if (sl->info.res.status < 200 && |
| 1535 | (sl->info.res.status == 100 || sl->info.res.status >= 102)) |
| 1536 | process_conn_mode = 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1537 | h1m->state = H1_MSG_HDR_FIRST; |
| 1538 | break; |
| 1539 | |
| 1540 | case HTX_BLK_HDR: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1541 | h1m->state = H1_MSG_HDR_NAME; |
| 1542 | n = htx_get_blk_name(chn_htx, blk); |
| 1543 | v = htx_get_blk_value(chn_htx, blk); |
| 1544 | |
| 1545 | if (isteqi(n, ist("transfer-encoding"))) |
| 1546 | h1_parse_xfer_enc_header(h1m, v); |
Willy Tarreau | 27cd223 | 2019-01-03 21:52:42 +0100 | [diff] [blame] | 1547 | else if (isteqi(n, ist("content-length"))) { |
| 1548 | if (h1_parse_cont_len_header(h1m, &v) <= 0) |
| 1549 | goto skip_hdr; |
| 1550 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1551 | else if (isteqi(n, ist("connection"))) { |
| 1552 | h1_parse_connection_header(h1m, v); |
| 1553 | h1_process_conn_mode(h1s, h1m, NULL, &v); |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1554 | process_conn_mode = 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1555 | if (!v.len) |
| 1556 | goto skip_hdr; |
| 1557 | } |
| 1558 | |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1559 | if (!htx_hdr_to_h1(n, v, tmp)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1560 | goto copy; |
| 1561 | skip_hdr: |
| 1562 | h1m->state = H1_MSG_HDR_L2_LWS; |
| 1563 | break; |
| 1564 | |
| 1565 | case HTX_BLK_PHDR: |
| 1566 | /* not implemented yet */ |
| 1567 | h1m->flags |= errflag; |
| 1568 | break; |
| 1569 | |
| 1570 | case HTX_BLK_EOH: |
Christopher Faulet | de68b13 | 2018-12-10 11:21:47 +0100 | [diff] [blame] | 1571 | if (h1m->state != H1_MSG_LAST_LF && process_conn_mode) { |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1572 | /* There is no "Connection:" header and |
| 1573 | * it the conn_mode must be |
| 1574 | * processed. So do it */ |
| 1575 | n = ist("Connection"); |
| 1576 | v = ist(""); |
| 1577 | h1_process_conn_mode(h1s, h1m, NULL, &v); |
| 1578 | process_conn_mode = 0; |
| 1579 | if (v.len) { |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1580 | if (!htx_hdr_to_h1(n, v, tmp)) |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 1581 | goto copy; |
| 1582 | } |
| 1583 | } |
Willy Tarreau | 4710d20 | 2019-01-03 17:39:54 +0100 | [diff] [blame] | 1584 | |
Christopher Faulet | 3949c9d | 2019-01-30 21:55:21 +0100 | [diff] [blame] | 1585 | if (h1s->status >= 200 && h1s->status != 204 && h1s->status != 304 && |
| 1586 | h1s->meth != HTTP_METH_HEAD && !(h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) && |
| 1587 | (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) == |
Willy Tarreau | 6195237 | 2019-01-03 21:27:19 +0100 | [diff] [blame] | 1588 | (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN)) { |
Willy Tarreau | 4710d20 | 2019-01-03 17:39:54 +0100 | [diff] [blame] | 1589 | /* chunking needed but header not seen */ |
| 1590 | if (!chunk_memcat(tmp, "transfer-encoding: chunked\r\n", 28)) |
| 1591 | goto copy; |
| 1592 | h1m->flags |= H1_MF_CHNK; |
| 1593 | } |
| 1594 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1595 | h1m->state = H1_MSG_LAST_LF; |
| 1596 | if (!chunk_memcat(tmp, "\r\n", 2)) |
| 1597 | goto copy; |
| 1598 | |
| 1599 | h1m->state = H1_MSG_DATA; |
| 1600 | break; |
| 1601 | |
| 1602 | case HTX_BLK_DATA: |
| 1603 | v = htx_get_blk_value(chn_htx, blk); |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1604 | v.len = vlen; |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1605 | if (!htx_data_to_h1(v, tmp, !!(h1m->flags & H1_MF_CHNK))) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1606 | goto copy; |
| 1607 | break; |
| 1608 | |
| 1609 | case HTX_BLK_EOD: |
| 1610 | if (!chunk_memcat(tmp, "0\r\n", 3)) |
| 1611 | goto copy; |
Willy Tarreau | 34d2348 | 2019-01-03 17:46:56 +0100 | [diff] [blame] | 1612 | h1s->flags |= H1S_F_HAVE_O_EOD; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1613 | h1m->state = H1_MSG_TRAILERS; |
| 1614 | break; |
| 1615 | |
| 1616 | case HTX_BLK_TLR: |
Willy Tarreau | 34d2348 | 2019-01-03 17:46:56 +0100 | [diff] [blame] | 1617 | if (!(h1s->flags & H1S_F_HAVE_O_EOD)) { |
Christopher Faulet | 3218821 | 2018-11-20 18:21:43 +0100 | [diff] [blame] | 1618 | if (!chunk_memcat(tmp, "0\r\n", 3)) |
| 1619 | goto copy; |
Willy Tarreau | 34d2348 | 2019-01-03 17:46:56 +0100 | [diff] [blame] | 1620 | h1s->flags |= H1S_F_HAVE_O_EOD; |
Christopher Faulet | 3218821 | 2018-11-20 18:21:43 +0100 | [diff] [blame] | 1621 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1622 | v = htx_get_blk_value(chn_htx, blk); |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1623 | v.len = vlen; |
Christopher Faulet | c59ff23 | 2018-12-03 13:58:44 +0100 | [diff] [blame] | 1624 | if (!htx_trailer_to_h1(v, tmp)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1625 | goto copy; |
Willy Tarreau | 34d2348 | 2019-01-03 17:46:56 +0100 | [diff] [blame] | 1626 | h1s->flags |= H1S_F_HAVE_O_TLR; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1627 | break; |
| 1628 | |
| 1629 | case HTX_BLK_EOM: |
Christopher Faulet | 3218821 | 2018-11-20 18:21:43 +0100 | [diff] [blame] | 1630 | if ((h1m->flags & H1_MF_CHNK)) { |
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 | } |
Willy Tarreau | 34d2348 | 2019-01-03 17:46:56 +0100 | [diff] [blame] | 1636 | if (!(h1s->flags & H1S_F_HAVE_O_TLR)) { |
Christopher Faulet | 3218821 | 2018-11-20 18:21:43 +0100 | [diff] [blame] | 1637 | if (!chunk_memcat(tmp, "\r\n", 2)) |
| 1638 | goto copy; |
Willy Tarreau | 34d2348 | 2019-01-03 17:46:56 +0100 | [diff] [blame] | 1639 | h1s->flags |= H1S_F_HAVE_O_TLR; |
Christopher Faulet | 3218821 | 2018-11-20 18:21:43 +0100 | [diff] [blame] | 1640 | } |
| 1641 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1642 | h1m->state = H1_MSG_DONE; |
| 1643 | break; |
| 1644 | |
| 1645 | case HTX_BLK_OOB: |
| 1646 | v = htx_get_blk_value(chn_htx, blk); |
| 1647 | if (!chunk_memcat(tmp, v.ptr, v.len)) |
| 1648 | goto copy; |
| 1649 | break; |
| 1650 | |
| 1651 | default: |
| 1652 | h1m->flags |= errflag; |
| 1653 | break; |
| 1654 | } |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 1655 | total += vlen; |
| 1656 | count -= vlen; |
| 1657 | if (sz == vlen) |
| 1658 | blk = htx_remove_blk(chn_htx, blk); |
| 1659 | else { |
| 1660 | htx_cut_data_blk(chn_htx, blk, vlen); |
| 1661 | break; |
| 1662 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1663 | } |
| 1664 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1665 | copy: |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 1666 | /* when the output buffer is empty, tmp shares the same area so that we |
| 1667 | * only have to update pointers and lengths. |
| 1668 | */ |
Willy Tarreau | b57af61 | 2019-01-23 20:43:53 +0100 | [diff] [blame] | 1669 | if (tmp->area == h1c->obuf.area + h1c->obuf.head) |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 1670 | h1c->obuf.data = tmp->data; |
| 1671 | else |
| 1672 | b_putblk(&h1c->obuf, tmp->area, tmp->data); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1673 | |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1674 | htx_to_buf(chn_htx, buf); |
| 1675 | out: |
Willy Tarreau | 45f2b89 | 2018-12-05 07:59:27 +0100 | [diff] [blame] | 1676 | if (!buf_room_for_htx_data(&h1c->obuf)) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1677 | h1c->flags |= H1C_F_OUT_FULL; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1678 | end: |
| 1679 | return total; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1680 | } |
| 1681 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1682 | /*********************************************************/ |
| 1683 | /* functions below are I/O callbacks from the connection */ |
| 1684 | /*********************************************************/ |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 1685 | static void h1_wake_stream_for_recv(struct h1s *h1s) |
| 1686 | { |
| 1687 | if (h1s && h1s->recv_wait) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 1688 | h1s->recv_wait->events &= ~SUB_RETRY_RECV; |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 1689 | tasklet_wakeup(h1s->recv_wait->task); |
| 1690 | h1s->recv_wait = NULL; |
| 1691 | } |
| 1692 | } |
| 1693 | static void h1_wake_stream_for_send(struct h1s *h1s) |
| 1694 | { |
| 1695 | if (h1s && h1s->send_wait) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 1696 | h1s->send_wait->events &= ~SUB_RETRY_SEND; |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 1697 | tasklet_wakeup(h1s->send_wait->task); |
| 1698 | h1s->send_wait = NULL; |
| 1699 | } |
| 1700 | } |
| 1701 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1702 | /* |
| 1703 | * Attempt to read data, and subscribe if none available |
| 1704 | */ |
| 1705 | static int h1_recv(struct h1c *h1c) |
| 1706 | { |
| 1707 | struct connection *conn = h1c->conn; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 1708 | struct h1s *h1s = h1c->h1s; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 1709 | size_t ret = 0, max; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1710 | int rcvd = 0; |
| 1711 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 1712 | if (h1c->wait_event.events & SUB_RETRY_RECV) |
Christopher Faulet | c386a88 | 2018-12-04 16:06:28 +0100 | [diff] [blame] | 1713 | return (b_data(&h1c->ibuf)); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1714 | |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 1715 | if (!h1_recv_allowed(h1c)) { |
| 1716 | rcvd = 1; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1717 | goto end; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 1718 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1719 | |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 1720 | if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1721 | rcvd = 1; |
| 1722 | goto end; |
| 1723 | } |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1724 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1725 | if (!h1_get_buf(h1c, &h1c->ibuf)) { |
| 1726 | h1c->flags |= H1C_F_IN_ALLOC; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1727 | goto end; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1728 | } |
| 1729 | |
Olivier Houchard | 29a22bc | 2018-12-04 18:16:45 +0100 | [diff] [blame] | 1730 | /* |
| 1731 | * If we only have a small amount of data, realign it, |
| 1732 | * it's probably cheaper than doing 2 recv() calls. |
| 1733 | */ |
| 1734 | if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128) |
| 1735 | b_slow_realign(&h1c->ibuf, trash.area, 0); |
| 1736 | |
Willy Tarreau | 45f2b89 | 2018-12-05 07:59:27 +0100 | [diff] [blame] | 1737 | max = buf_room_for_htx_data(&h1c->ibuf); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1738 | if (max) { |
| 1739 | h1c->flags &= ~H1C_F_IN_FULL; |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 1740 | |
Willy Tarreau | e0f24ee | 2018-12-14 10:51:23 +0100 | [diff] [blame] | 1741 | b_realign_if_empty(&h1c->ibuf); |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 1742 | if (!b_data(&h1c->ibuf)) { |
| 1743 | /* try to pre-align the buffer like the rxbufs will be |
| 1744 | * to optimize memory copies. |
| 1745 | */ |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 1746 | h1c->ibuf.head = sizeof(struct htx); |
| 1747 | } |
Willy Tarreau | c0960d1 | 2018-12-14 10:59:15 +0100 | [diff] [blame] | 1748 | ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1749 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1750 | if (ret > 0) { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1751 | rcvd = 1; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 1752 | if (h1s && h1s->cs) { |
Christopher Faulet | 37e3607 | 2018-12-04 15:54:12 +0100 | [diff] [blame] | 1753 | h1s->cs->flags |= (CS_FL_READ_PARTIAL|CS_FL_RCV_MORE); |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 1754 | if (h1s->csinfo.t_idle == -1) |
| 1755 | h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake; |
| 1756 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1757 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1758 | |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 1759 | if (!h1_recv_allowed(h1c) || !buf_room_for_htx_data(&h1c->ibuf)) { |
Christopher Faulet | 81d4843 | 2018-11-19 21:22:43 +0100 | [diff] [blame] | 1760 | rcvd = 1; |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 1761 | goto end; |
| 1762 | } |
| 1763 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 1764 | conn->xprt->subscribe(conn, SUB_RETRY_RECV, &h1c->wait_event); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1765 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1766 | end: |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 1767 | if (ret > 0 || (conn->flags & CO_FL_ERROR) || conn_xprt_read0_pending(conn)) |
| 1768 | h1_wake_stream_for_recv(h1s); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 1769 | |
Christopher Faulet | e6b3994 | 2018-12-07 09:42:49 +0100 | [diff] [blame] | 1770 | if (conn_xprt_read0_pending(conn) && h1s && h1s->cs) |
Olivier Houchard | 6a2d334 | 2018-12-06 17:41:26 +0100 | [diff] [blame] | 1771 | h1s->cs->flags |= CS_FL_REOS; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1772 | if (!b_data(&h1c->ibuf)) |
| 1773 | h1_release_buf(h1c, &h1c->ibuf); |
Willy Tarreau | 45f2b89 | 2018-12-05 07:59:27 +0100 | [diff] [blame] | 1774 | else if (!buf_room_for_htx_data(&h1c->ibuf)) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1775 | h1c->flags |= H1C_F_IN_FULL; |
| 1776 | return rcvd; |
| 1777 | } |
| 1778 | |
| 1779 | |
| 1780 | /* |
| 1781 | * Try to send data if possible |
| 1782 | */ |
| 1783 | static int h1_send(struct h1c *h1c) |
| 1784 | { |
| 1785 | struct connection *conn = h1c->conn; |
| 1786 | unsigned int flags = 0; |
| 1787 | size_t ret; |
| 1788 | int sent = 0; |
| 1789 | |
| 1790 | if (conn->flags & CO_FL_ERROR) |
| 1791 | return 0; |
| 1792 | |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 1793 | if (h1c->flags & H1C_F_CS_WAIT_CONN) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 1794 | if (!(h1c->wait_event.events & SUB_RETRY_SEND)) |
| 1795 | conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h1c->wait_event); |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 1796 | return 0; |
| 1797 | } |
| 1798 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1799 | if (!b_data(&h1c->obuf)) |
| 1800 | goto end; |
| 1801 | |
| 1802 | if (h1c->flags & H1C_F_OUT_FULL) |
| 1803 | flags |= CO_SFL_MSG_MORE; |
| 1804 | |
| 1805 | ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags); |
| 1806 | if (ret > 0) { |
| 1807 | h1c->flags &= ~H1C_F_OUT_FULL; |
| 1808 | b_del(&h1c->obuf, ret); |
| 1809 | sent = 1; |
| 1810 | } |
| 1811 | |
Christopher Faulet | 145aa47 | 2018-12-06 10:56:20 +0100 | [diff] [blame] | 1812 | if (conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH)) { |
| 1813 | /* error or output closed, nothing to send, clear the buffer to release it */ |
| 1814 | b_reset(&h1c->obuf); |
| 1815 | } |
| 1816 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1817 | end: |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 1818 | if (!(h1c->flags & H1C_F_OUT_FULL)) |
| 1819 | h1_wake_stream_for_send(h1c->h1s); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 1820 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1821 | /* We're done, no more to send */ |
| 1822 | if (!b_data(&h1c->obuf)) { |
| 1823 | h1_release_buf(h1c, &h1c->obuf); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1824 | h1_sync_messages(h1c); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1825 | if (h1c->flags & H1C_F_CS_SHUTW_NOW) |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 1826 | h1_shutw_conn(conn, CS_SHW_NORMAL); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1827 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 1828 | else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) |
| 1829 | conn->xprt->subscribe(conn, SUB_RETRY_SEND, &h1c->wait_event); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1830 | |
| 1831 | return sent; |
| 1832 | } |
| 1833 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1834 | |
| 1835 | /* callback called on any event by the connection handler. |
| 1836 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 1837 | * destruction of the connection. |
| 1838 | */ |
| 1839 | static int h1_process(struct h1c * h1c) |
| 1840 | { |
| 1841 | struct connection *conn = h1c->conn; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 1842 | struct h1s *h1s = h1c->h1s; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1843 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 1844 | if (!conn->ctx) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1845 | return -1; |
| 1846 | |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 1847 | if (h1c->flags & H1C_F_CS_WAIT_CONN) { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1848 | if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR))) |
| 1849 | goto end; |
| 1850 | h1c->flags &= ~H1C_F_CS_WAIT_CONN; |
Christopher Faulet | a0883e6 | 2018-12-11 16:26:50 +0100 | [diff] [blame] | 1851 | h1_wake_stream_for_send(h1s); |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 1852 | } |
| 1853 | |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 1854 | if (!h1s) { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1855 | if (h1c->flags & H1C_F_CS_ERROR || |
Olivier Houchard | 32d75ed | 2019-01-14 17:27:23 +0100 | [diff] [blame] | 1856 | conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH) || |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1857 | conn_xprt_read0_pending(conn)) |
| 1858 | goto release; |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 1859 | 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] | 1860 | if (!h1s_create(h1c, NULL, NULL)) |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1861 | goto release; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1862 | } |
Christopher Faulet | 1a7ad7a | 2018-12-04 16:10:44 +0100 | [diff] [blame] | 1863 | else |
Olivier Houchard | e728478 | 2018-12-06 18:54:54 +0100 | [diff] [blame] | 1864 | goto end; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 1865 | h1s = h1c->h1s; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1866 | } |
| 1867 | |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 1868 | if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1) |
| 1869 | h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake; |
| 1870 | |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 1871 | if (!b_data(&h1c->ibuf) && h1s && h1s->cs && h1s->cs->data_cb->wake && |
| 1872 | (conn_xprt_read0_pending(conn) || h1c->flags & H1C_F_CS_ERROR || |
Olivier Houchard | 32d75ed | 2019-01-14 17:27:23 +0100 | [diff] [blame] | 1873 | conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) { |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 1874 | int flags = 0; |
| 1875 | |
| 1876 | if (h1c->flags & H1C_F_CS_ERROR || conn->flags & CO_FL_ERROR) |
| 1877 | flags |= CS_FL_ERROR; |
| 1878 | if (conn_xprt_read0_pending(conn)) |
Christopher Faulet | 5f50f5e | 2018-12-07 11:39:55 +0100 | [diff] [blame] | 1879 | flags |= CS_FL_EOS; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 1880 | h1s->cs->flags |= flags; |
| 1881 | h1s->cs->data_cb->wake(h1s->cs); |
| 1882 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1883 | end: |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 1884 | if (h1c->task) { |
| 1885 | h1c->task->expire = TICK_ETERNITY; |
| 1886 | if (b_data(&h1c->obuf)) { |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 1887 | 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] | 1888 | ? h1c->shut_timeout |
| 1889 | : h1c->timeout)); |
| 1890 | task_queue(h1c->task); |
| 1891 | } |
| 1892 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1893 | return 0; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1894 | |
| 1895 | release: |
| 1896 | h1_release(conn); |
| 1897 | return -1; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status) |
| 1901 | { |
| 1902 | struct h1c *h1c = ctx; |
| 1903 | int ret = 0; |
| 1904 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 1905 | if (!(h1c->wait_event.events & SUB_RETRY_SEND)) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1906 | ret = h1_send(h1c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 1907 | if (!(h1c->wait_event.events & SUB_RETRY_RECV)) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1908 | ret |= h1_recv(h1c); |
Christopher Faulet | 81d4843 | 2018-11-19 21:22:43 +0100 | [diff] [blame] | 1909 | if (ret || !h1c->h1s) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1910 | h1_process(h1c); |
| 1911 | return NULL; |
| 1912 | } |
| 1913 | |
Olivier Houchard | 9a86fcb | 2018-12-11 16:47:14 +0100 | [diff] [blame] | 1914 | static void h1_reset(struct connection *conn) |
| 1915 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 1916 | struct h1c *h1c = conn->ctx; |
Olivier Houchard | 9a86fcb | 2018-12-11 16:47:14 +0100 | [diff] [blame] | 1917 | |
| 1918 | /* Reset the flags, and let the mux know we're waiting for a connection */ |
| 1919 | h1c->flags = H1C_F_CS_WAIT_CONN; |
| 1920 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1921 | |
| 1922 | static int h1_wake(struct connection *conn) |
| 1923 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 1924 | struct h1c *h1c = conn->ctx; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 1925 | int ret; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1926 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1927 | h1_send(h1c); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 1928 | ret = h1_process(h1c); |
| 1929 | if (ret == 0) { |
| 1930 | struct h1s *h1s = h1c->h1s; |
| 1931 | |
| 1932 | if (h1s && h1s->cs && h1s->cs->data_cb->wake) |
| 1933 | ret = h1s->cs->data_cb->wake(h1s->cs); |
| 1934 | } |
| 1935 | return ret; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1936 | } |
| 1937 | |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 1938 | /* Connection timeout management. The principle is that if there's no receipt |
| 1939 | * nor sending for a certain amount of time, the connection is closed. |
| 1940 | */ |
| 1941 | static struct task *h1_timeout_task(struct task *t, void *context, unsigned short state) |
| 1942 | { |
| 1943 | struct h1c *h1c = context; |
| 1944 | int expired = tick_is_expired(t->expire, now_ms); |
| 1945 | |
| 1946 | if (!expired && h1c) |
| 1947 | return t; |
| 1948 | |
| 1949 | task_delete(t); |
| 1950 | task_free(t); |
| 1951 | |
| 1952 | if (!h1c) { |
| 1953 | /* resources were already deleted */ |
| 1954 | return NULL; |
| 1955 | } |
| 1956 | |
| 1957 | h1c->task = NULL; |
| 1958 | /* If a stream is still attached to the mux, just set an error and wait |
| 1959 | * for the stream's timeout. Otherwise, release the mux. This is only ok |
| 1960 | * because same timeouts are used. |
| 1961 | */ |
| 1962 | if (h1c->h1s && h1c->h1s->cs) |
| 1963 | h1c->flags |= H1C_F_CS_ERROR; |
| 1964 | else |
| 1965 | h1_release(h1c->conn); |
| 1966 | return NULL; |
| 1967 | } |
| 1968 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1969 | /*******************************************/ |
| 1970 | /* functions below are used by the streams */ |
| 1971 | /*******************************************/ |
| 1972 | /* |
| 1973 | * Attach a new stream to a connection |
| 1974 | * (Used for outgoing connections) |
| 1975 | */ |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1976 | static struct conn_stream *h1_attach(struct connection *conn, struct session *sess) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1977 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 1978 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1979 | struct conn_stream *cs = NULL; |
| 1980 | struct h1s *h1s; |
| 1981 | |
| 1982 | if (h1c->flags & H1C_F_CS_ERROR) |
| 1983 | goto end; |
| 1984 | |
| 1985 | cs = cs_new(h1c->conn); |
| 1986 | if (!cs) |
| 1987 | goto end; |
| 1988 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1989 | h1s = h1s_create(h1c, cs, sess); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1990 | if (h1s == NULL) |
| 1991 | goto end; |
| 1992 | |
| 1993 | return cs; |
| 1994 | end: |
| 1995 | cs_free(cs); |
| 1996 | return NULL; |
| 1997 | } |
| 1998 | |
| 1999 | /* Retrieves a valid conn_stream from this connection, or returns NULL. For |
| 2000 | * this mux, it's easy as we can only store a single conn_stream. |
| 2001 | */ |
| 2002 | static const struct conn_stream *h1_get_first_cs(const struct connection *conn) |
| 2003 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2004 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2005 | struct h1s *h1s = h1c->h1s; |
| 2006 | |
| 2007 | if (h1s) |
| 2008 | return h1s->cs; |
| 2009 | |
| 2010 | return NULL; |
| 2011 | } |
| 2012 | |
| 2013 | static void h1_destroy(struct connection *conn) |
| 2014 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2015 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2016 | |
| 2017 | if (!h1c->h1s) |
| 2018 | h1_release(conn); |
| 2019 | } |
| 2020 | |
| 2021 | /* |
| 2022 | * Detach the stream from the connection and possibly release the connection. |
| 2023 | */ |
| 2024 | static void h1_detach(struct conn_stream *cs) |
| 2025 | { |
| 2026 | struct h1s *h1s = cs->ctx; |
| 2027 | struct h1c *h1c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2028 | struct session *sess; |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2029 | int has_keepalive; |
| 2030 | int is_not_first; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2031 | |
| 2032 | cs->ctx = NULL; |
| 2033 | if (!h1s) |
| 2034 | return; |
| 2035 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2036 | sess = h1s->sess; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2037 | h1c = h1s->h1c; |
| 2038 | h1s->cs = NULL; |
| 2039 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2040 | has_keepalive = h1s->flags & H1S_F_WANT_KAL; |
| 2041 | is_not_first = h1s->flags & H1S_F_NOT_FIRST; |
| 2042 | h1s_destroy(h1s); |
| 2043 | |
| 2044 | if (conn_is_back(h1c->conn) && has_keepalive && |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 2045 | !(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] | 2046 | /* Never ever allow to reuse a connection from a non-reuse backend */ |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 2047 | if ((h1c->px->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR) |
Christopher Faulet | 9400a39 | 2018-11-23 23:10:39 +0100 | [diff] [blame] | 2048 | h1c->conn->flags |= CO_FL_PRIVATE; |
| 2049 | |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 2050 | if (!(h1c->conn->owner)) { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 2051 | h1c->conn->owner = sess; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 2052 | if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) { |
| 2053 | h1c->conn->owner = NULL; |
| 2054 | if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn)) |
| 2055 | /* The server doesn't want it, let's kill the connection right away */ |
| 2056 | h1c->conn->mux->destroy(h1c->conn); |
| 2057 | else |
| 2058 | tasklet_wakeup(h1c->wait_event.task); |
| 2059 | return; |
| 2060 | |
| 2061 | } |
Olivier Houchard | 44d5914 | 2018-12-13 18:46:22 +0100 | [diff] [blame] | 2062 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 2063 | if (h1c->conn->owner == sess) { |
| 2064 | int ret = session_check_idle_conn(sess, h1c->conn); |
| 2065 | if (ret == -1) |
| 2066 | /* The connection got destroyed, let's leave */ |
| 2067 | return; |
| 2068 | else if (ret == 1) { |
| 2069 | /* The connection was added to the server list, |
| 2070 | * wake the task so we can subscribe to events |
| 2071 | */ |
| 2072 | tasklet_wakeup(h1c->wait_event.task); |
| 2073 | return; |
| 2074 | } |
| 2075 | } |
Christopher Faulet | 9400a39 | 2018-11-23 23:10:39 +0100 | [diff] [blame] | 2076 | /* 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] | 2077 | if (LIST_ISEMPTY(&h1c->conn->list)) { |
Christopher Faulet | 9400a39 | 2018-11-23 23:10:39 +0100 | [diff] [blame] | 2078 | struct server *srv = objt_server(h1c->conn->target); |
| 2079 | |
| 2080 | if (srv) { |
| 2081 | if (h1c->conn->flags & CO_FL_PRIVATE) |
| 2082 | LIST_ADD(&srv->priv_conns[tid], &h1c->conn->list); |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 2083 | else if (is_not_first) |
Christopher Faulet | 9400a39 | 2018-11-23 23:10:39 +0100 | [diff] [blame] | 2084 | LIST_ADD(&srv->safe_conns[tid], &h1c->conn->list); |
| 2085 | else |
| 2086 | LIST_ADD(&srv->idle_conns[tid], &h1c->conn->list); |
| 2087 | } |
| 2088 | } |
| 2089 | } |
| 2090 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2091 | /* 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] | 2092 | if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTDOWN)) || |
Olivier Houchard | 7ccff1a | 2018-12-03 16:33:19 +0100 | [diff] [blame] | 2093 | (h1c->conn->flags & CO_FL_ERROR) || !h1c->conn->owner) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2094 | h1_release(h1c->conn); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2095 | else { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2096 | tasklet_wakeup(h1c->wait_event.task); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 2097 | if (h1c->task) { |
| 2098 | h1c->task->expire = TICK_ETERNITY; |
| 2099 | if (b_data(&h1c->obuf)) { |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 2100 | 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] | 2101 | ? h1c->shut_timeout |
| 2102 | : h1c->timeout)); |
| 2103 | task_queue(h1c->task); |
| 2104 | } |
| 2105 | } |
| 2106 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | |
| 2110 | static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 2111 | { |
| 2112 | struct h1s *h1s = cs->ctx; |
| 2113 | |
| 2114 | if (!h1s) |
| 2115 | return; |
| 2116 | |
Olivier Houchard | d2e88c7 | 2018-12-19 15:55:23 +0100 | [diff] [blame] | 2117 | if ((h1s->flags & H1S_F_WANT_KAL) && |
| 2118 | !(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] | 2119 | return; |
| 2120 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2121 | /* NOTE: Be sure to handle abort (cf. h2_shutr) */ |
| 2122 | if (cs->flags & CS_FL_SHR) |
| 2123 | return; |
| 2124 | if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr) |
| 2125 | cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN)); |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 2126 | if ((cs->conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) |
| 2127 | 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] | 2128 | } |
| 2129 | |
| 2130 | static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 2131 | { |
| 2132 | struct h1s *h1s = cs->ctx; |
| 2133 | struct h1c *h1c; |
| 2134 | |
| 2135 | if (!h1s) |
| 2136 | return; |
| 2137 | h1c = h1s->h1c; |
| 2138 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2139 | if ((h1s->flags & H1S_F_WANT_KAL) && |
Olivier Houchard | d2e88c7 | 2018-12-19 15:55:23 +0100 | [diff] [blame] | 2140 | !(h1c->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) && |
| 2141 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2142 | h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) |
| 2143 | return; |
| 2144 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2145 | h1c->flags |= H1C_F_CS_SHUTW_NOW; |
| 2146 | if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf)) |
| 2147 | return; |
| 2148 | |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 2149 | h1_shutw_conn(cs->conn, mode); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2150 | } |
| 2151 | |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 2152 | 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] | 2153 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 2154 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2155 | |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 2156 | conn_xprt_shutw(conn); |
| 2157 | conn_sock_shutw(conn, (mode == CS_SHW_NORMAL)); |
| 2158 | if ((conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) == (CO_FL_SOCK_RD_SH|CO_FL_SOCK_WR_SH)) |
| 2159 | 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] | 2160 | } |
| 2161 | |
| 2162 | /* Called from the upper layer, to unsubscribe to events */ |
| 2163 | static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 2164 | { |
| 2165 | struct wait_event *sw; |
| 2166 | struct h1s *h1s = cs->ctx; |
| 2167 | |
| 2168 | if (!h1s) |
| 2169 | return 0; |
| 2170 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2171 | if (event_type & SUB_RETRY_RECV) { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2172 | sw = param; |
| 2173 | if (h1s->recv_wait == sw) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2174 | sw->events &= ~SUB_RETRY_RECV; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2175 | h1s->recv_wait = NULL; |
| 2176 | } |
| 2177 | } |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2178 | if (event_type & SUB_RETRY_SEND) { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2179 | sw = param; |
| 2180 | if (h1s->send_wait == sw) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2181 | sw->events &= ~SUB_RETRY_SEND; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2182 | h1s->send_wait = NULL; |
| 2183 | } |
| 2184 | } |
| 2185 | return 0; |
| 2186 | } |
| 2187 | |
| 2188 | /* Called from the upper layer, to subscribe to events, such as being able to send */ |
| 2189 | static int h1_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 2190 | { |
| 2191 | struct wait_event *sw; |
| 2192 | struct h1s *h1s = cs->ctx; |
| 2193 | |
| 2194 | if (!h1s) |
| 2195 | return -1; |
| 2196 | |
| 2197 | switch (event_type) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2198 | case SUB_RETRY_RECV: |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2199 | sw = param; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2200 | if (!(sw->events & SUB_RETRY_RECV)) { |
| 2201 | sw->events |= SUB_RETRY_RECV; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2202 | sw->handle = h1s; |
| 2203 | h1s->recv_wait = sw; |
| 2204 | } |
| 2205 | return 0; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2206 | case SUB_RETRY_SEND: |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2207 | sw = param; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2208 | if (!(sw->events & SUB_RETRY_SEND)) { |
| 2209 | sw->events |= SUB_RETRY_SEND; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2210 | sw->handle = h1s; |
| 2211 | h1s->send_wait = sw; |
| 2212 | } |
| 2213 | return 0; |
| 2214 | default: |
| 2215 | break; |
| 2216 | } |
| 2217 | return -1; |
| 2218 | } |
| 2219 | |
| 2220 | /* Called from the upper layer, to receive data */ |
| 2221 | static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 2222 | { |
| 2223 | struct h1s *h1s = cs->ctx; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2224 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2225 | size_t ret = 0; |
| 2226 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2227 | if (!(h1c->flags & H1C_F_IN_ALLOC)) |
| 2228 | ret = h1_process_input(h1c, buf, flags); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2229 | |
| 2230 | if (flags & CO_RFL_BUF_FLUSH) |
| 2231 | h1s->flags |= H1S_F_BUF_FLUSH; |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 2232 | else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) { |
| 2233 | h1s->flags &= ~H1S_F_SPLICED_DATA; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2234 | if (!(h1c->wait_event.events & SUB_RETRY_RECV)) |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2235 | tasklet_wakeup(h1c->wait_event.task); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2236 | } |
| 2237 | return ret; |
| 2238 | } |
| 2239 | |
| 2240 | |
| 2241 | /* Called from the upper layer, to send data */ |
| 2242 | static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 2243 | { |
| 2244 | struct h1s *h1s = cs->ctx; |
| 2245 | struct h1c *h1c; |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2246 | size_t total = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2247 | |
| 2248 | if (!h1s) |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2249 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2250 | |
| 2251 | h1c = h1s->h1c; |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 2252 | if (h1c->flags & H1C_F_CS_WAIT_CONN) |
| 2253 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2254 | |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2255 | while (total != count) { |
| 2256 | size_t ret = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2257 | |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2258 | if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC))) |
| 2259 | ret = h1_process_output(h1c, buf, count); |
| 2260 | if (!ret) |
| 2261 | break; |
| 2262 | total += ret; |
| 2263 | if (!h1_send(h1c)) |
| 2264 | break; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2265 | } |
Christopher Faulet | f96c322 | 2018-11-20 18:38:01 +0100 | [diff] [blame] | 2266 | |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 2267 | return total; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2268 | } |
| 2269 | |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2270 | #if defined(CONFIG_HAP_LINUX_SPLICE) |
| 2271 | /* Send and get, using splicing */ |
| 2272 | static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count) |
| 2273 | { |
| 2274 | struct h1s *h1s = cs->ctx; |
| 2275 | struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res); |
| 2276 | int ret = 0; |
| 2277 | |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 2278 | if (b_data(&h1s->h1c->ibuf)) { |
| 2279 | h1s->flags |= H1S_F_BUF_FLUSH; |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2280 | goto end; |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | h1s->flags &= ~H1S_F_BUF_FLUSH; |
| 2284 | h1s->flags |= H1S_F_SPLICED_DATA; |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2285 | if (h1m->state == H1_MSG_DATA && count > h1m->curr_len) |
| 2286 | count = h1m->curr_len; |
| 2287 | ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count); |
| 2288 | if (h1m->state == H1_MSG_DATA && ret > 0) |
| 2289 | h1m->curr_len -= ret; |
| 2290 | end: |
| 2291 | return ret; |
| 2292 | |
| 2293 | } |
| 2294 | |
| 2295 | static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe) |
| 2296 | { |
| 2297 | struct h1s *h1s = cs->ctx; |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2298 | int ret = 0; |
| 2299 | |
| 2300 | if (b_data(&h1s->h1c->obuf)) |
| 2301 | goto end; |
| 2302 | |
| 2303 | ret = cs->conn->xprt->snd_pipe(cs->conn, pipe); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2304 | end: |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 2305 | if (pipe->data) { |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 2306 | if (!(h1s->h1c->wait_event.events & SUB_RETRY_SEND)) |
| 2307 | 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] | 2308 | } |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2309 | return ret; |
| 2310 | } |
| 2311 | #endif |
| 2312 | |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 2313 | /* for debugging with CLI's "show fd" command */ |
| 2314 | static void h1_show_fd(struct buffer *msg, struct connection *conn) |
| 2315 | { |
| 2316 | struct h1c *h1c = conn->ctx; |
| 2317 | struct h1s *h1s = h1c->h1s; |
| 2318 | |
Christopher Faulet | f376a31 | 2019-01-04 15:16:06 +0100 | [diff] [blame] | 2319 | chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u", |
| 2320 | h1c->flags, h1c->wait_event.events, |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 2321 | (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf), |
| 2322 | (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf), |
| 2323 | (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf), |
| 2324 | (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf)); |
| 2325 | |
| 2326 | if (h1s) { |
| 2327 | char *method; |
| 2328 | |
| 2329 | if (h1s->meth < HTTP_METH_OTHER) |
| 2330 | method = http_known_methods[h1s->meth].ptr; |
| 2331 | else |
| 2332 | method = "UNKNOWN"; |
| 2333 | chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .req.state=%s .res.state=%s" |
| 2334 | " .meth=%s status=%d", |
| 2335 | h1s, h1s->flags, |
| 2336 | h1m_state_str(h1s->req.state), |
| 2337 | h1m_state_str(h1s->res.state), method, h1s->status); |
| 2338 | if (h1s->cs) |
| 2339 | chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p", |
| 2340 | h1s->cs->flags, h1s->cs->data); |
| 2341 | } |
| 2342 | } |
| 2343 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2344 | /****************************************/ |
| 2345 | /* MUX initialization and instanciation */ |
| 2346 | /****************************************/ |
| 2347 | |
| 2348 | /* The mux operations */ |
Willy Tarreau | f77a158 | 2019-01-10 10:00:08 +0100 | [diff] [blame] | 2349 | static const struct mux_ops mux_h1_ops = { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2350 | .init = h1_init, |
| 2351 | .wake = h1_wake, |
| 2352 | .attach = h1_attach, |
| 2353 | .get_first_cs = h1_get_first_cs, |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 2354 | .get_cs_info = h1_get_cs_info, |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2355 | .detach = h1_detach, |
| 2356 | .destroy = h1_destroy, |
| 2357 | .avail_streams = h1_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 2358 | .used_streams = h1_used_streams, |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2359 | .rcv_buf = h1_rcv_buf, |
| 2360 | .snd_buf = h1_snd_buf, |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2361 | #if defined(CONFIG_HAP_LINUX_SPLICE) |
| 2362 | .rcv_pipe = h1_rcv_pipe, |
| 2363 | .snd_pipe = h1_snd_pipe, |
| 2364 | #endif |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2365 | .subscribe = h1_subscribe, |
| 2366 | .unsubscribe = h1_unsubscribe, |
| 2367 | .shutr = h1_shutr, |
| 2368 | .shutw = h1_shutw, |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 2369 | .show_fd = h1_show_fd, |
Olivier Houchard | 9a86fcb | 2018-12-11 16:47:14 +0100 | [diff] [blame] | 2370 | .reset = h1_reset, |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2371 | .flags = MX_FL_NONE, |
| 2372 | .name = "h1", |
| 2373 | }; |
| 2374 | |
| 2375 | |
| 2376 | /* this mux registers default HTX proto */ |
| 2377 | static struct mux_proto_list mux_proto_htx = |
| 2378 | { .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops }; |
| 2379 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2380 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx); |
| 2381 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2382 | /* |
| 2383 | * Local variables: |
| 2384 | * c-indent-level: 8 |
| 2385 | * c-basic-offset: 8 |
| 2386 | * End: |
| 2387 | */ |