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