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