blob: 175b92decfd6d4d3b15115594f0ed89d76532178 [file] [log] [blame]
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +01001#include <haproxy/hq_interop.h>
2
3#include <import/ist.h>
4#include <haproxy/buf.h>
5#include <haproxy/connection.h>
6#include <haproxy/dynbuf.h>
7#include <haproxy/htx.h>
8#include <haproxy/http.h>
Amaury Denoyelle846cc042022-04-04 16:13:44 +02009#include <haproxy/mux_quic.h>
Amaury Denoyelle381d8132023-02-17 09:51:20 +010010#include <haproxy/qmux_http.h>
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010011
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +020012static ssize_t hq_interop_decode_qcs(struct qcs *qcs, struct buffer *b, int fin)
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010013{
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010014 struct htx *htx;
15 struct htx_sl *sl;
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010016 struct buffer htx_buf = BUF_NULL;
17 struct ist path;
Amaury Denoyelle62eef852022-06-03 16:40:34 +020018 char *ptr = b_head(b);
19 char *end = b_wrap(b);
20 size_t size = b_size(b);
21 size_t data = b_data(b);
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010022
Amaury Denoyelle381d8132023-02-17 09:51:20 +010023 if (!data && fin) {
24 /* FIN is notified with an empty STREAM frame. */
25 BUG_ON(!qcs->sd); /* sd must already be attached here */
26 qcs_http_handle_standalone_fin(qcs);
27 return 0;
28 }
29
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010030 b_alloc(&htx_buf);
31 htx = htx_from_buf(&htx_buf);
32
33 /* skip method */
Frédéric Lécailleafd373c2021-12-15 22:38:48 +010034 while (data && HTTP_IS_TOKEN(*ptr)) {
35 if (++ptr == end)
36 ptr -= size;
37 data--;
38 }
39
40 if (!data || !HTTP_IS_SPHT(*ptr)) {
41 fprintf(stderr, "truncated stream\n");
42 return 0;
43 }
44
45 if (++ptr == end)
46 ptr -= size;
47
48 if (!--data) {
49 fprintf(stderr, "truncated stream\n");
50 return 0;
51 }
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010052
53 /* extract path */
54 BUG_ON(HTTP_IS_LWS(*ptr));
55 path.ptr = ptr;
Frédéric Lécailleafd373c2021-12-15 22:38:48 +010056 while (data && !HTTP_IS_LWS(*ptr)) {
57 if (++ptr == end)
58 ptr -= size;
59 data--;
60 }
61
62 if (!data) {
63 fprintf(stderr, "truncated stream\n");
64 return 0;
65 }
66
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010067 BUG_ON(!HTTP_IS_LWS(*ptr));
68 path.len = ptr - path.ptr;
69
70 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, 0, ist("GET"), path, ist("HTTP/1.0"));
Amaury Denoyelleb48c59a2021-11-18 14:40:26 +010071 if (!sl)
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +020072 return -1;
Amaury Denoyelleb48c59a2021-11-18 14:40:26 +010073
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010074 sl->flags |= HTX_SL_F_BODYLESS;
75 sl->info.req.meth = find_http_meth("GET", 3);
76
77 htx_add_endof(htx, HTX_BLK_EOH);
78 htx_to_buf(htx, &htx_buf);
79
Amaury Denoyelle8d4ac482022-09-15 11:22:32 +020080 if (!qc_attach_sc(qcs, &htx_buf))
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +020081 return -1;
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010082
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010083 b_free(&htx_buf);
84
Amaury Denoyelledb443382021-11-30 11:23:29 +010085 if (fin)
86 htx->flags |= HTX_FL_EOM;
87
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +020088 return b_data(b);
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010089}
90
91static struct buffer *mux_get_buf(struct qcs *qcs)
92{
93 if (!b_size(&qcs->tx.buf))
94 b_alloc(&qcs->tx.buf);
95
96 return &qcs->tx.buf;
97}
98
Amaury Denoyelle9534e592022-09-19 17:14:27 +020099static size_t hq_interop_snd_buf(struct qcs *qcs, struct htx *htx,
100 size_t count)
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100101{
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100102 enum htx_blk_type btype;
103 struct htx_blk *blk;
104 int32_t idx;
105 uint32_t bsize, fsize;
106 struct buffer *res, outbuf;
107 size_t total = 0;
108
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100109 res = mux_get_buf(qcs);
110 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
111
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100112 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100113 /* Not implemented : QUIC on backend side */
114 idx = htx_get_head(htx);
115 blk = htx_get_blk(htx, idx);
116 btype = htx_get_blk_type(blk);
117 fsize = bsize = htx_get_blksz(blk);
118
119 BUG_ON(btype == HTX_BLK_REQ_SL);
120
121 switch (btype) {
122 case HTX_BLK_DATA:
123 if (fsize > count)
124 fsize = count;
Amaury Denoyelle5ede40b2021-12-07 16:19:03 +0100125
Amaury Denoyelle1ac95442021-12-09 10:07:23 +0100126 if (b_room(&outbuf) < fsize)
127 fsize = b_room(&outbuf);
Amaury Denoyelle5ede40b2021-12-07 16:19:03 +0100128
129 if (!fsize) {
130 qcs->flags |= QC_SF_BLK_MROOM;
131 goto end;
132 }
133
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100134 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
135 total += fsize;
136 count -= fsize;
137
138 if (fsize == bsize)
139 htx_remove_blk(htx, blk);
140 else
141 htx_cut_data_blk(htx, blk, fsize);
142 break;
143
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500144 /* only body is transferred on HTTP/0.9 */
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100145 case HTX_BLK_RES_SL:
146 case HTX_BLK_TLR:
147 case HTX_BLK_EOT:
148 default:
149 htx_remove_blk(htx, blk);
150 total += bsize;
151 count -= bsize;
152 break;
153 }
154 }
155
Amaury Denoyelle5ede40b2021-12-07 16:19:03 +0100156 end:
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100157 b_add(res, b_data(&outbuf));
158
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100159 return total;
160}
161
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200162static int hq_interop_attach(struct qcs *qcs, void *conn_ctx)
163{
164 qcs_wait_http_req(qcs);
165 return 0;
166}
167
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100168const struct qcc_app_ops hq_interop_ops = {
169 .decode_qcs = hq_interop_decode_qcs,
170 .snd_buf = hq_interop_snd_buf,
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200171 .attach = hq_interop_attach,
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100172};