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