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