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