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