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 | { |
Christopher Faulet | a1692f5 | 2018-11-22 10:19:50 +0100 | [diff] [blame^] | 597 | struct h1c *h1c = h1s->h1c; |
| 598 | struct session *sess = h1c->conn->owner; |
| 599 | struct proxy *fe = sess->fe; |
| 600 | struct proxy *be = h1c->px; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 601 | int flag = H1S_F_WANT_KAL; |
| 602 | |
| 603 | /* Tunnel mode can only by set on the frontend */ |
| 604 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 605 | flag = H1S_F_WANT_TUN; |
| 606 | |
| 607 | /* For the server connection: server-close == httpclose */ |
| 608 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
| 609 | (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
| 610 | (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO || |
| 611 | (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) |
| 612 | flag = H1S_F_WANT_CLO; |
| 613 | |
| 614 | /* flags order: CLO > SCL > TUN > KAL */ |
| 615 | if ((h1s->flags & H1S_F_WANT_MSK) < flag) |
| 616 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | flag; |
| 617 | |
| 618 | if (h1m->flags & H1_MF_RESP) { |
| 619 | /* Either we've established an explicit tunnel, or we're |
| 620 | * switching the protocol. In both cases, we're very unlikely to |
| 621 | * understand the next protocols. We have to switch to tunnel |
| 622 | * mode, so that we transfer the request and responses then let |
| 623 | * this protocol pass unmodified. When we later implement |
| 624 | * specific parsers for such protocols, we'll want to check the |
| 625 | * Upgrade header which contains information about that protocol |
| 626 | * for responses with status 101 (eg: see RFC2817 about TLS). |
| 627 | */ |
| 628 | if ((h1s->meth == HTTP_METH_CONNECT && h1s->status == 200) || |
| 629 | h1s->status == 101) |
| 630 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN; |
| 631 | else if (!(h1m->flags & H1_MF_XFER_LEN)) /* no length known => close */ |
| 632 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 633 | else if (h1s->flags & H1S_F_WANT_KAL && |
| 634 | (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || /* no KA in HTTP/1.0 */ |
| 635 | h1m->flags & H1_MF_CONN_CLO)) /* explicit close */ |
| 636 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 637 | } |
| 638 | |
| 639 | /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */ |
| 640 | if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED) |
| 641 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 642 | } |
| 643 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 644 | static void h1_update_req_conn_hdr(struct h1s *h1s, struct h1m *h1m, |
| 645 | struct htx *htx, struct ist *conn_val) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 646 | { |
| 647 | struct proxy *px = h1s->h1c->px; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 648 | |
| 649 | /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage" |
| 650 | * token is found |
| 651 | */ |
| 652 | 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] | 653 | return; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 654 | |
| 655 | 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] | 656 | if (h1m->flags & H1_MF_CONN_CLO) { |
| 657 | if (conn_val) |
| 658 | *conn_val = ist(""); |
| 659 | if (htx) |
| 660 | h1_remove_conn_hdrs(h1m, htx); |
| 661 | } |
| 662 | if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) { |
| 663 | if (conn_val) |
| 664 | *conn_val = ist("keep-alive"); |
| 665 | if (htx) |
| 666 | h1_add_conn_hdr(h1m, htx, ist("keep-alive")); |
| 667 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 668 | } |
| 669 | else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 670 | if (h1m->flags & H1_MF_CONN_KAL) { |
| 671 | if (conn_val) |
| 672 | *conn_val = ist(""); |
| 673 | if (htx) |
| 674 | h1_remove_conn_hdrs(h1m, htx); |
| 675 | } |
| 676 | if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) { |
| 677 | if (conn_val) |
| 678 | *conn_val = ist("close"); |
| 679 | if (htx) |
| 680 | h1_add_conn_hdr(h1m, htx, ist("close")); |
| 681 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 682 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 683 | } |
| 684 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 685 | static void h1_update_res_conn_hdr(struct h1s *h1s, struct h1m *h1m, |
| 686 | struct htx *htx, struct ist *conn_val) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 687 | { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 688 | /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage" |
| 689 | * token is found |
| 690 | */ |
| 691 | 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] | 692 | return; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 693 | |
| 694 | if (h1s->flags & H1S_F_WANT_KAL) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 695 | if (h1m->flags & H1_MF_CONN_CLO) { |
| 696 | if (conn_val) |
| 697 | *conn_val = ist(""); |
| 698 | if (htx) |
| 699 | h1_remove_conn_hdrs(h1m, htx); |
| 700 | } |
| 701 | if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))) { |
| 702 | if (conn_val) |
| 703 | *conn_val = ist("keep-alive"); |
| 704 | if (htx) |
| 705 | h1_add_conn_hdr(h1m, htx, ist("keep-alive")); |
| 706 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 707 | } |
| 708 | else { /* H1S_F_WANT_CLO */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 709 | if (h1m->flags & H1_MF_CONN_KAL) { |
| 710 | if (conn_val) |
| 711 | *conn_val = ist(""); |
| 712 | if (htx) |
| 713 | h1_remove_conn_hdrs(h1m, htx); |
| 714 | } |
| 715 | if ((h1m->flags & (H1_MF_VER_11|H1_MF_CONN_CLO)) == H1_MF_VER_11) { |
| 716 | if (conn_val) |
| 717 | *conn_val = ist("close"); |
| 718 | if (htx) |
| 719 | h1_add_conn_hdr(h1m, htx, ist("close")); |
| 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 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 723 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 724 | /* Set the right connection mode and update "Connection:" header if |
| 725 | * needed. <htx> and <conn_val> can be NULL. When <htx> is not NULL, the HTX |
| 726 | * message is updated accordingly. When <conn_val> is not NULL, it is set with |
| 727 | * the new header value. |
| 728 | */ |
| 729 | static void h1_process_conn_mode(struct h1s *h1s, struct h1m *h1m, |
| 730 | struct htx *htx, struct ist *conn_val) |
| 731 | { |
| 732 | if (!conn_is_back(h1s->h1c->conn)) { |
| 733 | h1_set_cli_conn_mode(h1s, h1m); |
| 734 | if (h1m->flags & H1_MF_RESP) |
| 735 | h1_update_res_conn_hdr(h1s, h1m, htx, conn_val); |
| 736 | } |
| 737 | else { |
| 738 | h1_set_srv_conn_mode(h1s, h1m); |
| 739 | if (!(h1m->flags & H1_MF_RESP)) |
| 740 | h1_update_req_conn_hdr(h1s, h1m, htx, conn_val); |
| 741 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 742 | } |
| 743 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 744 | /* |
| 745 | * 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] | 746 | * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR |
| 747 | * 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] | 748 | * responsibile to update the parser state <h1m>. |
| 749 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 750 | 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] | 751 | struct buffer *buf, size_t *ofs, size_t max) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 752 | { |
| 753 | struct http_hdr hdrs[MAX_HTTP_HDR]; |
| 754 | union h1_sl sl; |
| 755 | int ret = 0; |
| 756 | |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 757 | if (!max) |
| 758 | goto end; |
| 759 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 760 | /* Realing input buffer if necessary */ |
| 761 | if (b_head(buf) + b_data(buf) > b_wrap(buf)) |
| 762 | b_slow_realign(buf, trash.area, 0); |
| 763 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 764 | 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] | 765 | hdrs, sizeof(hdrs)/sizeof(hdrs[0]), h1m, &sl); |
| 766 | if (ret <= 0) { |
| 767 | /* Incomplete or invalid message. If the buffer is full, it's an |
| 768 | * error because headers are too large to be handled by the |
| 769 | * parser. */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 770 | if (ret < 0 || (!ret && b_full(buf))) |
| 771 | goto error; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 772 | goto end; |
| 773 | } |
| 774 | |
| 775 | /* messages headers fully parsed, do some checks to prepare the body |
| 776 | * parsing. |
| 777 | */ |
| 778 | |
| 779 | /* Be sure to keep some space to do headers rewritting */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 780 | if (ret > (b_size(buf) - global.tune.maxrewrite)) |
| 781 | goto error; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 782 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 783 | /* Save the request's method or the response's status, check if the body |
| 784 | * length is known and check the VSN validity */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 785 | if (!(h1m->flags & H1_MF_RESP)) { |
| 786 | h1s->meth = sl.rq.meth; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 787 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 788 | /* Request have always a known length */ |
| 789 | h1m->flags |= H1_MF_XFER_LEN; |
| 790 | if (!(h1m->flags & H1_MF_CHNK) && !h1m->body_len) |
| 791 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 792 | |
| 793 | if (!h1_process_req_vsn(h1s, h1m, sl)) { |
| 794 | h1m->err_pos = sl.rq.v.ptr - b_head(buf); |
| 795 | h1m->err_state = h1m->state; |
| 796 | goto vsn_error; |
| 797 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 798 | } |
| 799 | else { |
| 800 | h1s->status = sl.st.status; |
| 801 | |
| 802 | if ((h1s->meth == HTTP_METH_HEAD) || |
| 803 | (h1s->status >= 100 && h1s->status < 200) || |
| 804 | (h1s->status == 204) || (h1s->status == 304) || |
| 805 | (h1s->meth == HTTP_METH_CONNECT && h1s->status == 200)) { |
| 806 | h1m->flags &= ~(H1_MF_CLEN|H1_MF_CHNK); |
| 807 | h1m->flags |= H1_MF_XFER_LEN; |
| 808 | h1m->curr_len = h1m->body_len = 0; |
| 809 | h1m->state = H1_MSG_DONE; |
| 810 | } |
| 811 | else if (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 812 | h1m->flags |= H1_MF_XFER_LEN; |
| 813 | if ((h1m->flags & H1_MF_CLEN) && !h1m->body_len) |
| 814 | h1m->state = H1_MSG_DONE; |
| 815 | } |
| 816 | else |
| 817 | h1m->state = H1_MSG_TUNNEL; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 818 | |
| 819 | if (!h1_process_res_vsn(h1s, h1m, sl)) { |
| 820 | h1m->err_pos = sl.st.v.ptr - b_head(buf); |
| 821 | h1m->err_state = h1m->state; |
| 822 | goto vsn_error; |
| 823 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 824 | } |
| 825 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 826 | if (!(h1m->flags & H1_MF_RESP)) { |
| 827 | if (!htx_add_reqline(htx, sl) || !htx_add_all_headers(htx, hdrs)) |
| 828 | goto error; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 829 | } |
| 830 | else { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 831 | if (!htx_add_resline(htx, sl) || !htx_add_all_headers(htx, hdrs)) |
| 832 | goto error; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 833 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 834 | if (h1m->state == H1_MSG_DONE) |
| 835 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 836 | goto error; |
| 837 | |
| 838 | h1_process_conn_mode(h1s, h1m, htx, NULL); |
| 839 | |
| 840 | /* If body length cannot be determined, set htx->extra to |
| 841 | * ULLONG_MAX. This value is impossible in other cases. |
| 842 | */ |
| 843 | htx->extra = ((h1m->flags & H1_MF_XFER_LEN) ? h1m->curr_len : ULLONG_MAX); |
| 844 | |
| 845 | /* Recheck there is enough space to do headers rewritting */ |
| 846 | if (htx_used_space(htx) > b_size(buf) - global.tune.maxrewrite) |
| 847 | goto error; |
| 848 | |
| 849 | *ofs += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 850 | end: |
| 851 | return ret; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 852 | |
| 853 | error: |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 854 | h1m->err_state = h1m->state; |
| 855 | h1m->err_pos = h1m->next; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 856 | vsn_error: |
| 857 | 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] | 858 | ret = 0; |
| 859 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | /* |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 863 | * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it |
| 864 | * 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] | 865 | * and filling h1s->err_pos and h1s->err_state fields. This functions is |
| 866 | * responsibile to update the parser state <h1m>. |
| 867 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 868 | 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] | 869 | struct buffer *buf, size_t *ofs, size_t max) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 870 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 871 | uint32_t data_space = htx_free_data_space(htx); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 872 | size_t total = 0; |
| 873 | int ret = 0; |
| 874 | |
| 875 | if (h1m->flags & H1_MF_XFER_LEN) { |
| 876 | if (h1m->flags & H1_MF_CLEN) { |
| 877 | /* content-length: read only h2m->body_len */ |
| 878 | ret = max; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 879 | if (ret > data_space) |
| 880 | ret = data_space; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 881 | if ((uint64_t)ret > h1m->curr_len) |
| 882 | ret = h1m->curr_len; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 883 | if (ret > b_contig_data(buf, *ofs)) |
| 884 | ret = b_contig_data(buf, *ofs); |
| 885 | if (ret) { |
| 886 | if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret))) |
| 887 | goto end; |
| 888 | h1m->curr_len -= ret; |
| 889 | *ofs += ret; |
| 890 | total += ret; |
| 891 | } |
| 892 | |
| 893 | if (!h1m->curr_len) { |
| 894 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 895 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 896 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 897 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 898 | } |
| 899 | else if (h1m->flags & H1_MF_CHNK) { |
| 900 | new_chunk: |
| 901 | /* te:chunked : parse chunks */ |
| 902 | if (h1m->state == H1_MSG_CHUNK_CRLF) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 903 | ret = h1_skip_chunk_crlf(buf, *ofs, *ofs + max); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 904 | if (ret <= 0) |
| 905 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 906 | h1m->state = H1_MSG_CHUNK_SIZE; |
| 907 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 908 | max -= ret; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 909 | *ofs += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 910 | total += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | if (h1m->state == H1_MSG_CHUNK_SIZE) { |
| 914 | unsigned int chksz; |
| 915 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 916 | ret = h1_parse_chunk_size(buf, *ofs, *ofs + max, &chksz); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 917 | if (ret <= 0) |
| 918 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 919 | if (!chksz) { |
| 920 | if (!htx_add_endof(htx, HTX_BLK_EOD)) |
| 921 | goto end; |
| 922 | h1m->state = H1_MSG_TRAILERS; |
| 923 | } |
| 924 | else |
| 925 | h1m->state = H1_MSG_DATA; |
| 926 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 927 | h1m->curr_len = chksz; |
| 928 | h1m->body_len += chksz; |
| 929 | max -= ret; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 930 | *ofs += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 931 | total += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | if (h1m->state == H1_MSG_DATA) { |
| 935 | ret = max; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 936 | if (ret > data_space) |
| 937 | ret = data_space; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 938 | if ((uint64_t)ret > h1m->curr_len) |
| 939 | ret = h1m->curr_len; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 940 | if (ret > b_contig_data(buf, *ofs)) |
| 941 | ret = b_contig_data(buf, *ofs); |
| 942 | if (ret) { |
| 943 | if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret))) |
| 944 | goto end; |
| 945 | h1m->curr_len -= ret; |
| 946 | max -= ret; |
| 947 | *ofs += ret; |
| 948 | total += ret; |
| 949 | } |
| 950 | if (!h1m->curr_len) { |
| 951 | h1m->state = H1_MSG_CHUNK_CRLF; |
| 952 | goto new_chunk; |
| 953 | } |
| 954 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | if (h1m->state == H1_MSG_TRAILERS) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 958 | ret = h1_measure_trailers(buf, *ofs, *ofs + max); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 959 | if (ret > data_space) |
| 960 | ret = (htx_is_empty(htx) ? -1 : 0); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 961 | if (ret <= 0) |
| 962 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 963 | |
| 964 | /* Realing input buffer if tailers wrap. For now |
| 965 | * this is a workaroung. Because trailers are |
| 966 | * not split on CRLF, like headers, there is no |
| 967 | * way to know where to split it when trailers |
| 968 | * wrap. This is a limitation of |
| 969 | * h1_measure_trailers. |
| 970 | */ |
| 971 | if (b_peek(buf, *ofs) > b_peek(buf, *ofs + ret)) |
| 972 | b_slow_realign(buf, trash.area, 0); |
| 973 | |
| 974 | if (!htx_add_trailer(htx, ist2(b_peek(buf, *ofs), ret))) |
| 975 | goto end; |
| 976 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 977 | max -= ret; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 978 | *ofs += ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 979 | total += ret; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 980 | |
| 981 | /* FIXME: if it fails here, this is a problem, |
| 982 | * because there is no way to return here. */ |
| 983 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 984 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 985 | h1m->state = H1_MSG_DONE; |
| 986 | } |
| 987 | } |
| 988 | else { |
| 989 | /* XFER_LEN is set but not CLEN nor CHNK, it means there |
| 990 | * is no body. Switch the message in DONE state |
| 991 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 992 | if (!htx_add_endof(htx, HTX_BLK_EOM)) |
| 993 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 994 | h1m->state = H1_MSG_DONE; |
| 995 | } |
| 996 | } |
| 997 | else { |
| 998 | /* no content length, read till SHUTW */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 999 | ret = max; |
| 1000 | if (ret > data_space) |
| 1001 | ret = data_space; |
| 1002 | if (ret > b_contig_data(buf, *ofs)) |
| 1003 | ret = b_contig_data(buf, *ofs); |
| 1004 | if (ret) { |
| 1005 | if (!htx_add_data(htx, ist2(b_peek(buf, *ofs), ret))) |
| 1006 | goto end; |
| 1007 | |
| 1008 | *ofs += max; |
| 1009 | total = max; |
| 1010 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | end: |
| 1014 | if (ret < 0) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1015 | 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] | 1016 | h1m->err_state = h1m->state; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1017 | h1m->err_pos = *ofs + max + ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1018 | return 0; |
| 1019 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1020 | /* update htx->extra, only when the body length is known */ |
| 1021 | if (h1m->flags & H1_MF_XFER_LEN) |
| 1022 | htx->extra = h1m->curr_len; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1023 | return total; |
| 1024 | } |
| 1025 | |
| 1026 | /* |
| 1027 | * Synchronize the request and the response before reseting them. Except for 1xx |
| 1028 | * responses, we wait that the request and the response are in DONE state and |
| 1029 | * that all data are forwarded for both. For 1xx responses, only the response is |
| 1030 | * reset, waiting the final one. Many 1xx messages can be sent. |
| 1031 | */ |
| 1032 | static void h1_sync_messages(struct h1c *h1c) |
| 1033 | { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1034 | struct h1s *h1s = h1c->h1s; |
| 1035 | |
| 1036 | if (!h1s) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1037 | return; |
| 1038 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1039 | if (h1s->res.state == H1_MSG_DONE && |
| 1040 | (h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) && |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1041 | (conn_is_back(h1c->conn) || !b_data(&h1c->obuf))) { |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1042 | /* For 100-Continue response or any other informational 1xx |
| 1043 | * response which is non-final, don't reset the request, the |
| 1044 | * transaction is not finished. We take care the response was |
| 1045 | * transferred before. |
| 1046 | */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1047 | h1m_init_res(&h1s->res); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1048 | h1s->res.flags |= H1_MF_NO_PHDR; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1049 | } |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1050 | else if (!b_data(&h1c->obuf) && |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1051 | h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { |
| 1052 | if (h1s->flags & H1S_F_WANT_TUN) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1053 | h1m_init_req(&h1s->req); |
| 1054 | h1m_init_res(&h1s->res); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1055 | h1s->req.state = H1_MSG_TUNNEL; |
| 1056 | h1s->res.state = H1_MSG_TUNNEL; |
| 1057 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | /* |
| 1062 | * 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] | 1063 | * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if |
| 1064 | * it couldn't proceed. |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1065 | */ |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1066 | 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] | 1067 | { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1068 | struct h1s *h1s = h1c->h1s; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1069 | struct h1m *h1m; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1070 | struct htx *htx; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1071 | size_t total = 0; |
| 1072 | size_t ret = 0; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1073 | size_t count, max; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1074 | int errflag; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1075 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1076 | htx = htx_from_buf(buf); |
| 1077 | count = b_data(&h1c->ibuf); |
| 1078 | max = htx_free_space(htx); |
| 1079 | if (flags & CO_RFL_KEEP_RSV) { |
| 1080 | if (max < global.tune.maxrewrite) |
| 1081 | goto end; |
| 1082 | max -= global.tune.maxrewrite; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1083 | } |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1084 | if (count > max) |
| 1085 | count = max; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1086 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1087 | if (!conn_is_back(h1c->conn)) { |
| 1088 | h1m = &h1s->req; |
| 1089 | errflag = H1S_F_REQ_ERROR; |
| 1090 | } |
| 1091 | else { |
| 1092 | h1m = &h1s->res; |
| 1093 | errflag = H1S_F_RES_ERROR; |
| 1094 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1095 | |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1096 | do { |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1097 | if (h1m->state <= H1_MSG_LAST_LF) { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1098 | ret = h1_process_headers(h1s, h1m, htx, &h1c->ibuf, &total, count); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1099 | if (!ret) |
| 1100 | break; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1101 | } |
| 1102 | else if (h1m->state <= H1_MSG_TRAILERS) { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1103 | ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1104 | if (!ret) |
| 1105 | break; |
| 1106 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1107 | else if (h1m->state == H1_MSG_DONE) |
| 1108 | break; |
| 1109 | else if (h1m->state == H1_MSG_TUNNEL) { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1110 | ret = h1_process_data(h1s, h1m, htx, &h1c->ibuf, &total, count); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1111 | if (!ret) |
| 1112 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1113 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1114 | else { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1115 | h1s->flags |= errflag; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1116 | break; |
| 1117 | } |
| 1118 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1119 | count -= ret; |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1120 | } while (!(h1s->flags & errflag) && count); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1121 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1122 | if (h1s->flags & errflag) |
| 1123 | goto parsing_err; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1124 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1125 | b_del(&h1c->ibuf, total); |
| 1126 | |
| 1127 | end: |
| 1128 | if (htx_is_not_empty(htx)) |
| 1129 | b_set_data(buf, b_size(buf)); |
| 1130 | else { |
| 1131 | htx_reset(htx); |
| 1132 | b_set_data(buf, 0); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1133 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1134 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1135 | if (h1c->flags & H1C_F_IN_FULL && b_room(&h1c->ibuf)) { |
| 1136 | h1c->flags &= ~H1C_F_IN_FULL; |
| 1137 | tasklet_wakeup(h1c->wait_event.task); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1138 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1139 | |
Christopher Faulet | 9c38840 | 2018-11-19 21:54:26 +0100 | [diff] [blame] | 1140 | if (b_data(&h1c->ibuf)) { |
| 1141 | if (!htx_is_empty(htx)) |
| 1142 | h1s->cs->flags |= CS_FL_RCV_MORE; |
| 1143 | } |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1144 | else { |
| 1145 | h1_release_buf(h1c, &h1c->ibuf); |
| 1146 | h1_sync_messages(h1c); |
| 1147 | |
| 1148 | h1s->cs->flags &= ~CS_FL_RCV_MORE; |
| 1149 | if (h1s->cs->flags & CS_FL_REOS) |
| 1150 | h1s->cs->flags |= CS_FL_EOS; |
| 1151 | } |
| 1152 | return total; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1153 | |
| 1154 | parsing_err: |
| 1155 | // FIXME: create an error snapshot here |
| 1156 | b_reset(&h1c->ibuf); |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1157 | htx->flags |= HTX_FL_PARSING_ERROR; |
| 1158 | b_set_data(buf, b_size(buf)); |
| 1159 | h1s->cs->flags |= CS_FL_EOS; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1160 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1161 | } |
| 1162 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1163 | /* |
| 1164 | * Process outgoing data. It parses data and transfer them from the channel buffer into |
| 1165 | * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or |
| 1166 | * 0 if it couldn't proceed. |
| 1167 | */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1168 | static size_t h1_process_output(struct h1c *h1c, struct buffer *buf, size_t count) |
| 1169 | { |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1170 | struct h1s *h1s = h1c->h1s; |
| 1171 | struct h1m *h1m; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1172 | struct htx *chn_htx; |
| 1173 | struct htx_blk *blk; |
| 1174 | struct buffer *tmp; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1175 | size_t total = 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1176 | int errflag; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1177 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1178 | if (!count) |
| 1179 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1180 | chn_htx = htx_from_buf(buf); |
| 1181 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1182 | if (!h1_get_buf(h1c, &h1c->obuf)) { |
| 1183 | h1c->flags |= H1C_F_OUT_ALLOC; |
| 1184 | goto end; |
| 1185 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1186 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1187 | if (!conn_is_back(h1c->conn)) { |
| 1188 | h1m = &h1s->res; |
| 1189 | errflag = H1S_F_RES_ERROR; |
| 1190 | } |
| 1191 | else { |
| 1192 | h1m = &h1s->req; |
| 1193 | errflag = H1S_F_REQ_ERROR; |
| 1194 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1195 | |
| 1196 | |
| 1197 | tmp = get_trash_chunk(); |
| 1198 | tmp->size = b_room(&h1c->obuf); |
| 1199 | |
| 1200 | blk = htx_get_head_blk(chn_htx); |
| 1201 | while (!(h1s->flags & errflag) && blk) { |
| 1202 | union htx_sl *sl; |
| 1203 | struct ist n, v; |
| 1204 | uint32_t sz = htx_get_blksz(blk); |
| 1205 | |
| 1206 | if (total + sz > count) |
| 1207 | goto copy; |
| 1208 | |
| 1209 | switch (htx_get_blk_type(blk)) { |
| 1210 | case HTX_BLK_UNUSED: |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1211 | break; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1212 | |
| 1213 | case HTX_BLK_REQ_SL: |
Christopher Faulet | 66229af | 2018-11-28 16:06:57 +0100 | [diff] [blame] | 1214 | h1m_init_req(h1m); |
| 1215 | h1m->flags |= H1_MF_NO_PHDR; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1216 | sl = htx_get_blk_ptr(chn_htx, blk); |
| 1217 | h1s->meth = sl->rq.meth; |
| 1218 | h1_parse_req_vsn(h1m, sl); |
| 1219 | if (!htx_reqline_to_str(sl, tmp)) |
| 1220 | goto copy; |
| 1221 | h1m->flags |= H1_MF_XFER_LEN; |
| 1222 | h1m->state = H1_MSG_HDR_FIRST; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1223 | break; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1224 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1225 | case HTX_BLK_RES_SL: |
Christopher Faulet | 66229af | 2018-11-28 16:06:57 +0100 | [diff] [blame] | 1226 | h1m_init_res(h1m); |
| 1227 | h1m->flags |= H1_MF_NO_PHDR; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1228 | sl = htx_get_blk_ptr(chn_htx, blk); |
| 1229 | h1s->status = sl->st.status; |
| 1230 | h1_parse_res_vsn(h1m, sl); |
| 1231 | if (!htx_stline_to_str(sl, tmp)) |
| 1232 | goto copy; |
| 1233 | if (chn_htx->extra != ULLONG_MAX) |
| 1234 | h1m->flags |= H1_MF_XFER_LEN; |
| 1235 | h1m->state = H1_MSG_HDR_FIRST; |
| 1236 | break; |
| 1237 | |
| 1238 | case HTX_BLK_HDR: |
| 1239 | if (h1m->state == H1_MSG_HDR_FIRST) { |
| 1240 | struct http_hdr_ctx ctx; |
| 1241 | |
| 1242 | n = ist("Connection"); |
| 1243 | v = ist(""); |
| 1244 | |
| 1245 | /* If there is no "Connection:" header, |
| 1246 | * process conn_mode now and add the |
| 1247 | * right one. |
| 1248 | */ |
| 1249 | ctx.blk = blk; |
Christopher Faulet | 5999b86 | 2018-11-27 10:46:09 +0100 | [diff] [blame] | 1250 | ctx.value = ist(NULL); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1251 | if (http_find_header(chn_htx, n, &ctx, 1)) |
| 1252 | goto process_hdr; |
| 1253 | h1_process_conn_mode(h1s, h1m, NULL, &v); |
| 1254 | if (!v.len) |
| 1255 | goto process_hdr; |
| 1256 | |
| 1257 | if (!htx_hdr_to_str(n, v, tmp)) |
| 1258 | goto copy; |
| 1259 | } |
| 1260 | process_hdr: |
| 1261 | h1m->state = H1_MSG_HDR_NAME; |
| 1262 | n = htx_get_blk_name(chn_htx, blk); |
| 1263 | v = htx_get_blk_value(chn_htx, blk); |
| 1264 | |
| 1265 | if (isteqi(n, ist("transfer-encoding"))) |
| 1266 | h1_parse_xfer_enc_header(h1m, v); |
| 1267 | else if (isteqi(n, ist("connection"))) { |
| 1268 | h1_parse_connection_header(h1m, v); |
| 1269 | h1_process_conn_mode(h1s, h1m, NULL, &v); |
| 1270 | if (!v.len) |
| 1271 | goto skip_hdr; |
| 1272 | } |
| 1273 | |
| 1274 | if (!htx_hdr_to_str(n, v, tmp)) |
| 1275 | goto copy; |
| 1276 | skip_hdr: |
| 1277 | h1m->state = H1_MSG_HDR_L2_LWS; |
| 1278 | break; |
| 1279 | |
| 1280 | case HTX_BLK_PHDR: |
| 1281 | /* not implemented yet */ |
| 1282 | h1m->flags |= errflag; |
| 1283 | break; |
| 1284 | |
| 1285 | case HTX_BLK_EOH: |
| 1286 | h1m->state = H1_MSG_LAST_LF; |
| 1287 | if (!chunk_memcat(tmp, "\r\n", 2)) |
| 1288 | goto copy; |
| 1289 | |
| 1290 | h1m->state = H1_MSG_DATA; |
| 1291 | break; |
| 1292 | |
| 1293 | case HTX_BLK_DATA: |
| 1294 | v = htx_get_blk_value(chn_htx, blk); |
| 1295 | if (!htx_data_to_str(v, tmp, !!(h1m->flags & H1_MF_CHNK))) |
| 1296 | goto copy; |
| 1297 | break; |
| 1298 | |
| 1299 | case HTX_BLK_EOD: |
| 1300 | if (!chunk_memcat(tmp, "0\r\n", 3)) |
| 1301 | goto copy; |
| 1302 | h1m->state = H1_MSG_TRAILERS; |
| 1303 | break; |
| 1304 | |
| 1305 | case HTX_BLK_TLR: |
| 1306 | v = htx_get_blk_value(chn_htx, blk); |
| 1307 | if (!htx_trailer_to_str(v, tmp)) |
| 1308 | goto copy; |
| 1309 | break; |
| 1310 | |
| 1311 | case HTX_BLK_EOM: |
| 1312 | /* if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(tmp, "\r\n", 2)) */ |
| 1313 | /* goto copy; */ |
| 1314 | h1m->state = H1_MSG_DONE; |
| 1315 | break; |
| 1316 | |
| 1317 | case HTX_BLK_OOB: |
| 1318 | v = htx_get_blk_value(chn_htx, blk); |
| 1319 | if (!chunk_memcat(tmp, v.ptr, v.len)) |
| 1320 | goto copy; |
| 1321 | break; |
| 1322 | |
| 1323 | default: |
| 1324 | h1m->flags |= errflag; |
| 1325 | break; |
| 1326 | } |
| 1327 | total += sz; |
| 1328 | blk = htx_remove_blk(chn_htx, blk); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1329 | } |
| 1330 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1331 | copy: |
| 1332 | b_putblk(&h1c->obuf, tmp->area, tmp->data); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1333 | |
| 1334 | if (b_full(&h1c->obuf)) |
| 1335 | h1c->flags |= H1C_F_OUT_FULL; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1336 | if (htx_is_empty(chn_htx)) { |
| 1337 | htx_reset(chn_htx); |
| 1338 | b_set_data(buf, 0); |
| 1339 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1340 | end: |
| 1341 | return total; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1342 | } |
| 1343 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1344 | /*********************************************************/ |
| 1345 | /* functions below are I/O callbacks from the connection */ |
| 1346 | /*********************************************************/ |
| 1347 | /* |
| 1348 | * Attempt to read data, and subscribe if none available |
| 1349 | */ |
| 1350 | static int h1_recv(struct h1c *h1c) |
| 1351 | { |
| 1352 | struct connection *conn = h1c->conn; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 1353 | struct h1s *h1s = h1c->h1s; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1354 | size_t ret, max; |
| 1355 | int rcvd = 0; |
| 1356 | |
| 1357 | if (h1c->wait_event.wait_reason & SUB_CAN_RECV) |
| 1358 | return 0; |
| 1359 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1360 | if (!h1_recv_allowed(h1c)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1361 | goto end; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1362 | |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 1363 | if (h1s && (h1s->flags & (H1S_F_BUF_FLUSH|H1S_F_SPLICED_DATA))) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1364 | rcvd = 1; |
| 1365 | goto end; |
| 1366 | } |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1367 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1368 | if (!h1_get_buf(h1c, &h1c->ibuf)) { |
| 1369 | h1c->flags |= H1C_F_IN_ALLOC; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1370 | goto end; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | ret = 0; |
| 1374 | max = b_room(&h1c->ibuf); |
| 1375 | if (max) { |
| 1376 | h1c->flags &= ~H1C_F_IN_FULL; |
| 1377 | ret = conn->xprt->rcv_buf(conn, &h1c->ibuf, max, 0); |
| 1378 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1379 | if (ret > 0) { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1380 | rcvd = 1; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 1381 | if (h1s && h1s->cs) { |
| 1382 | h1s->cs->flags |= CS_FL_READ_PARTIAL; |
| 1383 | if (h1s->csinfo.t_idle == -1) |
| 1384 | h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake; |
| 1385 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1386 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1387 | |
| 1388 | if (h1_recv_allowed(h1c)) |
| 1389 | conn->xprt->subscribe(conn, SUB_CAN_RECV, &h1c->wait_event); |
Christopher Faulet | 81d4843 | 2018-11-19 21:22:43 +0100 | [diff] [blame] | 1390 | else |
| 1391 | rcvd = 1; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1392 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1393 | end: |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1394 | if (!b_data(&h1c->ibuf)) |
| 1395 | h1_release_buf(h1c, &h1c->ibuf); |
| 1396 | else if (b_full(&h1c->ibuf)) |
| 1397 | h1c->flags |= H1C_F_IN_FULL; |
| 1398 | return rcvd; |
| 1399 | } |
| 1400 | |
| 1401 | |
| 1402 | /* |
| 1403 | * Try to send data if possible |
| 1404 | */ |
| 1405 | static int h1_send(struct h1c *h1c) |
| 1406 | { |
| 1407 | struct connection *conn = h1c->conn; |
| 1408 | unsigned int flags = 0; |
| 1409 | size_t ret; |
| 1410 | int sent = 0; |
| 1411 | |
| 1412 | if (conn->flags & CO_FL_ERROR) |
| 1413 | return 0; |
| 1414 | |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 1415 | if (h1c->flags & H1C_F_CS_WAIT_CONN) { |
| 1416 | if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND)) |
| 1417 | conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event); |
| 1418 | return 0; |
| 1419 | } |
| 1420 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1421 | if (!b_data(&h1c->obuf)) |
| 1422 | goto end; |
| 1423 | |
| 1424 | if (h1c->flags & H1C_F_OUT_FULL) |
| 1425 | flags |= CO_SFL_MSG_MORE; |
| 1426 | |
| 1427 | ret = conn->xprt->snd_buf(conn, &h1c->obuf, b_data(&h1c->obuf), flags); |
| 1428 | if (ret > 0) { |
| 1429 | h1c->flags &= ~H1C_F_OUT_FULL; |
| 1430 | b_del(&h1c->obuf, ret); |
| 1431 | sent = 1; |
| 1432 | } |
| 1433 | |
| 1434 | end: |
| 1435 | /* We're done, no more to send */ |
| 1436 | if (!b_data(&h1c->obuf)) { |
| 1437 | h1_release_buf(h1c, &h1c->obuf); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1438 | h1_sync_messages(h1c); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1439 | if (h1c->flags & H1C_F_CS_SHUTW_NOW) |
| 1440 | h1_shutw_conn(conn); |
| 1441 | } |
| 1442 | else if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND)) |
| 1443 | conn->xprt->subscribe(conn, SUB_CAN_SEND, &h1c->wait_event); |
| 1444 | |
| 1445 | return sent; |
| 1446 | } |
| 1447 | |
| 1448 | |
| 1449 | static void h1_wake_stream(struct h1c *h1c) |
| 1450 | { |
| 1451 | struct connection *conn = h1c->conn; |
| 1452 | struct h1s *h1s = h1c->h1s; |
| 1453 | uint32_t flags = 0; |
| 1454 | int dont_wake = 0; |
| 1455 | |
| 1456 | if (!h1s || !h1s->cs) |
| 1457 | return; |
| 1458 | |
| 1459 | if ((h1c->flags & H1C_F_CS_ERROR) || (conn->flags & CO_FL_ERROR)) |
| 1460 | flags |= CS_FL_ERROR; |
| 1461 | if (conn_xprt_read0_pending(conn)) |
| 1462 | flags |= CS_FL_REOS; |
| 1463 | |
| 1464 | h1s->cs->flags |= flags; |
| 1465 | if (h1s->recv_wait) { |
| 1466 | h1s->recv_wait->wait_reason &= ~SUB_CAN_RECV; |
| 1467 | tasklet_wakeup(h1s->recv_wait->task); |
| 1468 | h1s->recv_wait = NULL; |
| 1469 | dont_wake = 1; |
| 1470 | } |
| 1471 | if (h1s->send_wait) { |
| 1472 | h1s->send_wait->wait_reason &= ~SUB_CAN_SEND; |
| 1473 | tasklet_wakeup(h1s->send_wait->task); |
| 1474 | h1s->send_wait = NULL; |
| 1475 | dont_wake = 1; |
| 1476 | } |
| 1477 | if (!dont_wake && h1s->cs->data_cb->wake) |
| 1478 | h1s->cs->data_cb->wake(h1s->cs); |
| 1479 | } |
| 1480 | |
| 1481 | /* callback called on any event by the connection handler. |
| 1482 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 1483 | * destruction of the connection. |
| 1484 | */ |
| 1485 | static int h1_process(struct h1c * h1c) |
| 1486 | { |
| 1487 | struct connection *conn = h1c->conn; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 1488 | struct h1s *h1s = h1c->h1s; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1489 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1490 | if (!conn->mux_ctx) |
| 1491 | return -1; |
| 1492 | |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 1493 | if (h1c->flags & H1C_F_CS_WAIT_CONN) { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1494 | if (!(conn->flags & (CO_FL_CONNECTED|CO_FL_ERROR))) |
| 1495 | goto end; |
| 1496 | h1c->flags &= ~H1C_F_CS_WAIT_CONN; |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 1497 | } |
| 1498 | |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 1499 | if (!h1s) { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1500 | if (h1c->flags & H1C_F_CS_ERROR || |
| 1501 | conn->flags & CO_FL_ERROR || |
| 1502 | conn_xprt_read0_pending(conn)) |
| 1503 | goto release; |
Christopher Faulet | 81d4843 | 2018-11-19 21:22:43 +0100 | [diff] [blame] | 1504 | 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] | 1505 | if (!h1s_create(h1c, NULL)) |
| 1506 | goto release; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1507 | } |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 1508 | h1s = h1c->h1s; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1509 | } |
| 1510 | |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 1511 | if (b_data(&h1c->ibuf) && h1s->csinfo.t_idle == -1) |
| 1512 | h1s->csinfo.t_idle = tv_ms_elapsed(&h1s->csinfo.tv_create, &now) - h1s->csinfo.t_handshake; |
| 1513 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1514 | h1_wake_stream(h1c); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1515 | end: |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1516 | return 0; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1517 | |
| 1518 | release: |
| 1519 | h1_release(conn); |
| 1520 | return -1; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | static struct task *h1_io_cb(struct task *t, void *ctx, unsigned short status) |
| 1524 | { |
| 1525 | struct h1c *h1c = ctx; |
| 1526 | int ret = 0; |
| 1527 | |
| 1528 | if (!(h1c->wait_event.wait_reason & SUB_CAN_SEND)) |
| 1529 | ret = h1_send(h1c); |
| 1530 | if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV)) |
| 1531 | ret |= h1_recv(h1c); |
Christopher Faulet | 81d4843 | 2018-11-19 21:22:43 +0100 | [diff] [blame] | 1532 | if (ret || !h1c->h1s) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1533 | h1_process(h1c); |
| 1534 | return NULL; |
| 1535 | } |
| 1536 | |
| 1537 | |
| 1538 | static int h1_wake(struct connection *conn) |
| 1539 | { |
| 1540 | struct h1c *h1c = conn->mux_ctx; |
| 1541 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1542 | h1_send(h1c); |
| 1543 | return h1_process(h1c); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1544 | } |
| 1545 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1546 | /*******************************************/ |
| 1547 | /* functions below are used by the streams */ |
| 1548 | /*******************************************/ |
| 1549 | /* |
| 1550 | * Attach a new stream to a connection |
| 1551 | * (Used for outgoing connections) |
| 1552 | */ |
| 1553 | static struct conn_stream *h1_attach(struct connection *conn) |
| 1554 | { |
| 1555 | struct h1c *h1c = conn->mux_ctx; |
| 1556 | struct conn_stream *cs = NULL; |
| 1557 | struct h1s *h1s; |
| 1558 | |
| 1559 | if (h1c->flags & H1C_F_CS_ERROR) |
| 1560 | goto end; |
| 1561 | |
| 1562 | cs = cs_new(h1c->conn); |
| 1563 | if (!cs) |
| 1564 | goto end; |
| 1565 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1566 | h1s = h1s_create(h1c, cs); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1567 | if (h1s == NULL) |
| 1568 | goto end; |
| 1569 | |
| 1570 | return cs; |
| 1571 | end: |
| 1572 | cs_free(cs); |
| 1573 | return NULL; |
| 1574 | } |
| 1575 | |
| 1576 | /* Retrieves a valid conn_stream from this connection, or returns NULL. For |
| 1577 | * this mux, it's easy as we can only store a single conn_stream. |
| 1578 | */ |
| 1579 | static const struct conn_stream *h1_get_first_cs(const struct connection *conn) |
| 1580 | { |
| 1581 | struct h1c *h1c = conn->mux_ctx; |
| 1582 | struct h1s *h1s = h1c->h1s; |
| 1583 | |
| 1584 | if (h1s) |
| 1585 | return h1s->cs; |
| 1586 | |
| 1587 | return NULL; |
| 1588 | } |
| 1589 | |
| 1590 | static void h1_destroy(struct connection *conn) |
| 1591 | { |
| 1592 | struct h1c *h1c = conn->mux_ctx; |
| 1593 | |
| 1594 | if (!h1c->h1s) |
| 1595 | h1_release(conn); |
| 1596 | } |
| 1597 | |
| 1598 | /* |
| 1599 | * Detach the stream from the connection and possibly release the connection. |
| 1600 | */ |
| 1601 | static void h1_detach(struct conn_stream *cs) |
| 1602 | { |
| 1603 | struct h1s *h1s = cs->ctx; |
| 1604 | struct h1c *h1c; |
| 1605 | |
| 1606 | cs->ctx = NULL; |
| 1607 | if (!h1s) |
| 1608 | return; |
| 1609 | |
| 1610 | h1c = h1s->h1c; |
| 1611 | h1s->cs = NULL; |
| 1612 | |
| 1613 | h1s_destroy(h1s); |
| 1614 | |
| 1615 | /* We don't want to close right now unless the connection is in error */ |
| 1616 | if ((h1c->flags & (H1C_F_CS_ERROR|H1C_F_CS_SHUTW)) || |
| 1617 | (h1c->conn->flags & CO_FL_ERROR)) |
| 1618 | h1_release(h1c->conn); |
| 1619 | else |
| 1620 | tasklet_wakeup(h1c->wait_event.task); |
| 1621 | } |
| 1622 | |
| 1623 | |
| 1624 | static void h1_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 1625 | { |
| 1626 | struct h1s *h1s = cs->ctx; |
| 1627 | |
| 1628 | if (!h1s) |
| 1629 | return; |
| 1630 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1631 | if ((h1s->flags & H1S_F_WANT_KAL) && !(cs->flags & (CS_FL_REOS|CS_FL_EOS))) |
| 1632 | return; |
| 1633 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1634 | /* NOTE: Be sure to handle abort (cf. h2_shutr) */ |
| 1635 | if (cs->flags & CS_FL_SHR) |
| 1636 | return; |
| 1637 | if (conn_xprt_ready(cs->conn) && cs->conn->xprt->shutr) |
| 1638 | cs->conn->xprt->shutr(cs->conn, (mode == CS_SHR_DRAIN)); |
| 1639 | if (cs->flags & CS_FL_SHW) { |
| 1640 | h1s->h1c->flags = (h1s->h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW; |
| 1641 | conn_full_close(cs->conn); |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | static void h1_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 1646 | { |
| 1647 | struct h1s *h1s = cs->ctx; |
| 1648 | struct h1c *h1c; |
| 1649 | |
| 1650 | if (!h1s) |
| 1651 | return; |
| 1652 | h1c = h1s->h1c; |
| 1653 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1654 | if ((h1s->flags & H1S_F_WANT_KAL) && |
| 1655 | !(cs->flags & (CS_FL_REOS|CS_FL_EOS)) && |
| 1656 | h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) |
| 1657 | return; |
| 1658 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1659 | h1c->flags |= H1C_F_CS_SHUTW_NOW; |
| 1660 | if ((cs->flags & CS_FL_SHW) || b_data(&h1c->obuf)) |
| 1661 | return; |
| 1662 | |
| 1663 | h1_shutw_conn(cs->conn); |
| 1664 | } |
| 1665 | |
| 1666 | static void h1_shutw_conn(struct connection *conn) |
| 1667 | { |
| 1668 | struct h1c *h1c = conn->mux_ctx; |
| 1669 | |
| 1670 | if (conn_xprt_ready(conn) && conn->xprt->shutw) |
| 1671 | conn->xprt->shutw(conn, 1); |
| 1672 | if (!(conn->flags & CO_FL_SOCK_RD_SH)) |
| 1673 | conn_sock_shutw(conn, 1); |
| 1674 | else { |
| 1675 | h1c->flags = (h1c->flags & ~H1C_F_CS_SHUTW_NOW) | H1C_F_CS_SHUTW; |
| 1676 | conn_full_close(conn); |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | /* Called from the upper layer, to unsubscribe to events */ |
| 1681 | static int h1_unsubscribe(struct conn_stream *cs, int event_type, void *param) |
| 1682 | { |
| 1683 | struct wait_event *sw; |
| 1684 | struct h1s *h1s = cs->ctx; |
| 1685 | |
| 1686 | if (!h1s) |
| 1687 | return 0; |
| 1688 | |
| 1689 | if (event_type & SUB_CAN_RECV) { |
| 1690 | sw = param; |
| 1691 | if (h1s->recv_wait == sw) { |
| 1692 | sw->wait_reason &= ~SUB_CAN_RECV; |
| 1693 | h1s->recv_wait = NULL; |
| 1694 | } |
| 1695 | } |
| 1696 | if (event_type & SUB_CAN_SEND) { |
| 1697 | sw = param; |
| 1698 | if (h1s->send_wait == sw) { |
| 1699 | sw->wait_reason &= ~SUB_CAN_SEND; |
| 1700 | h1s->send_wait = NULL; |
| 1701 | } |
| 1702 | } |
| 1703 | return 0; |
| 1704 | } |
| 1705 | |
| 1706 | /* Called from the upper layer, to subscribe to events, such as being able to send */ |
| 1707 | static int h1_subscribe(struct conn_stream *cs, int event_type, void *param) |
| 1708 | { |
| 1709 | struct wait_event *sw; |
| 1710 | struct h1s *h1s = cs->ctx; |
| 1711 | |
| 1712 | if (!h1s) |
| 1713 | return -1; |
| 1714 | |
| 1715 | switch (event_type) { |
| 1716 | case SUB_CAN_RECV: |
| 1717 | sw = param; |
| 1718 | if (!(sw->wait_reason & SUB_CAN_RECV)) { |
| 1719 | sw->wait_reason |= SUB_CAN_RECV; |
| 1720 | sw->handle = h1s; |
| 1721 | h1s->recv_wait = sw; |
| 1722 | } |
| 1723 | return 0; |
| 1724 | case SUB_CAN_SEND: |
| 1725 | sw = param; |
| 1726 | if (!(sw->wait_reason & SUB_CAN_SEND)) { |
| 1727 | sw->wait_reason |= SUB_CAN_SEND; |
| 1728 | sw->handle = h1s; |
| 1729 | h1s->send_wait = sw; |
| 1730 | } |
| 1731 | return 0; |
| 1732 | default: |
| 1733 | break; |
| 1734 | } |
| 1735 | return -1; |
| 1736 | } |
| 1737 | |
| 1738 | /* Called from the upper layer, to receive data */ |
| 1739 | static size_t h1_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 1740 | { |
| 1741 | struct h1s *h1s = cs->ctx; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1742 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1743 | size_t ret = 0; |
| 1744 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1745 | if (!(h1c->flags & H1C_F_IN_ALLOC)) |
| 1746 | ret = h1_process_input(h1c, buf, flags); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1747 | |
| 1748 | if (flags & CO_RFL_BUF_FLUSH) |
| 1749 | h1s->flags |= H1S_F_BUF_FLUSH; |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1750 | else if (ret > 0 || (h1s->flags & H1S_F_SPLICED_DATA)) { |
| 1751 | h1s->flags &= ~H1S_F_SPLICED_DATA; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1752 | if (!(h1c->wait_event.wait_reason & SUB_CAN_RECV)) |
| 1753 | tasklet_wakeup(h1c->wait_event.task); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1754 | } |
| 1755 | return ret; |
| 1756 | } |
| 1757 | |
| 1758 | |
| 1759 | /* Called from the upper layer, to send data */ |
| 1760 | static size_t h1_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags) |
| 1761 | { |
| 1762 | struct h1s *h1s = cs->ctx; |
| 1763 | struct h1c *h1c; |
| 1764 | size_t ret = 0; |
| 1765 | |
| 1766 | if (!h1s) |
| 1767 | return ret; |
| 1768 | |
| 1769 | h1c = h1s->h1c; |
| 1770 | |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 1771 | if (h1c->flags & H1C_F_CS_WAIT_CONN) |
| 1772 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1773 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1774 | if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC))) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1775 | ret = h1_process_output(h1c, buf, count); |
| 1776 | if (ret > 0) { |
| 1777 | h1_send(h1c); |
| 1778 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1779 | /* We need to do that because of the infinite forwarding. <buf> |
| 1780 | * contains HTX messages so when infinite forwarding is enabled, |
| 1781 | * count is equal to the buffer size. From outside, the buffer |
| 1782 | * appears as full. |
| 1783 | */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1784 | if (!b_data(buf)) |
| 1785 | ret = count; |
| 1786 | } |
| 1787 | return ret; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1788 | } |
| 1789 | |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1790 | #if defined(CONFIG_HAP_LINUX_SPLICE) |
| 1791 | /* Send and get, using splicing */ |
| 1792 | static int h1_rcv_pipe(struct conn_stream *cs, struct pipe *pipe, unsigned int count) |
| 1793 | { |
| 1794 | struct h1s *h1s = cs->ctx; |
| 1795 | struct h1m *h1m = (!conn_is_back(cs->conn) ? &h1s->req : &h1s->res); |
| 1796 | int ret = 0; |
| 1797 | |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1798 | if (b_data(&h1s->h1c->ibuf)) { |
| 1799 | h1s->flags |= H1S_F_BUF_FLUSH; |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1800 | goto end; |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | h1s->flags &= ~H1S_F_BUF_FLUSH; |
| 1804 | h1s->flags |= H1S_F_SPLICED_DATA; |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1805 | if (h1m->state == H1_MSG_DATA && count > h1m->curr_len) |
| 1806 | count = h1m->curr_len; |
| 1807 | ret = cs->conn->xprt->rcv_pipe(cs->conn, pipe, count); |
| 1808 | if (h1m->state == H1_MSG_DATA && ret > 0) |
| 1809 | h1m->curr_len -= ret; |
| 1810 | end: |
| 1811 | return ret; |
| 1812 | |
| 1813 | } |
| 1814 | |
| 1815 | static int h1_snd_pipe(struct conn_stream *cs, struct pipe *pipe) |
| 1816 | { |
| 1817 | struct h1s *h1s = cs->ctx; |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1818 | int ret = 0; |
| 1819 | |
| 1820 | if (b_data(&h1s->h1c->obuf)) |
| 1821 | goto end; |
| 1822 | |
| 1823 | ret = cs->conn->xprt->snd_pipe(cs->conn, pipe); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1824 | end: |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1825 | if (pipe->data) { |
| 1826 | if (!(h1s->h1c->wait_event.wait_reason & SUB_CAN_SEND)) |
| 1827 | cs->conn->xprt->subscribe(cs->conn, SUB_CAN_SEND, &h1s->h1c->wait_event); |
| 1828 | } |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1829 | return ret; |
| 1830 | } |
| 1831 | #endif |
| 1832 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1833 | /****************************************/ |
| 1834 | /* MUX initialization and instanciation */ |
| 1835 | /****************************************/ |
| 1836 | |
| 1837 | /* The mux operations */ |
| 1838 | const struct mux_ops mux_h1_ops = { |
| 1839 | .init = h1_init, |
| 1840 | .wake = h1_wake, |
| 1841 | .attach = h1_attach, |
| 1842 | .get_first_cs = h1_get_first_cs, |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 1843 | .get_cs_info = h1_get_cs_info, |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1844 | .detach = h1_detach, |
| 1845 | .destroy = h1_destroy, |
| 1846 | .avail_streams = h1_avail_streams, |
| 1847 | .rcv_buf = h1_rcv_buf, |
| 1848 | .snd_buf = h1_snd_buf, |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 1849 | #if defined(CONFIG_HAP_LINUX_SPLICE) |
| 1850 | .rcv_pipe = h1_rcv_pipe, |
| 1851 | .snd_pipe = h1_snd_pipe, |
| 1852 | #endif |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1853 | .subscribe = h1_subscribe, |
| 1854 | .unsubscribe = h1_unsubscribe, |
| 1855 | .shutr = h1_shutr, |
| 1856 | .shutw = h1_shutw, |
| 1857 | .flags = MX_FL_NONE, |
| 1858 | .name = "h1", |
| 1859 | }; |
| 1860 | |
| 1861 | |
| 1862 | /* this mux registers default HTX proto */ |
| 1863 | static struct mux_proto_list mux_proto_htx = |
| 1864 | { .token = IST(""), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops }; |
| 1865 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1866 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_htx); |
| 1867 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1868 | /* |
| 1869 | * Local variables: |
| 1870 | * c-indent-level: 8 |
| 1871 | * c-basic-offset: 8 |
| 1872 | * End: |
| 1873 | */ |