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