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