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