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