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