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