blob: 37bb2e219a7a5a50b49a8563a00a24ebc9575c6d [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 Denoyelle154bc7f2021-11-12 16:09:54 +010010
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +020011static ssize_t hq_interop_decode_qcs(struct qcs *qcs, struct buffer *b, int fin)
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010012{
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010013 struct htx *htx;
14 struct htx_sl *sl;
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010015 struct buffer htx_buf = BUF_NULL;
16 struct ist path;
Amaury Denoyelle62eef852022-06-03 16:40:34 +020017 char *ptr = b_head(b);
18 char *end = b_wrap(b);
19 size_t size = b_size(b);
20 size_t data = b_data(b);
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010021
22 b_alloc(&htx_buf);
23 htx = htx_from_buf(&htx_buf);
24
25 /* skip method */
Frédéric Lécailleafd373c2021-12-15 22:38:48 +010026 while (data && HTTP_IS_TOKEN(*ptr)) {
27 if (++ptr == end)
28 ptr -= size;
29 data--;
30 }
31
32 if (!data || !HTTP_IS_SPHT(*ptr)) {
33 fprintf(stderr, "truncated stream\n");
34 return 0;
35 }
36
37 if (++ptr == end)
38 ptr -= size;
39
40 if (!--data) {
41 fprintf(stderr, "truncated stream\n");
42 return 0;
43 }
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010044
45 /* extract path */
46 BUG_ON(HTTP_IS_LWS(*ptr));
47 path.ptr = ptr;
Frédéric Lécailleafd373c2021-12-15 22:38:48 +010048 while (data && !HTTP_IS_LWS(*ptr)) {
49 if (++ptr == end)
50 ptr -= size;
51 data--;
52 }
53
54 if (!data) {
55 fprintf(stderr, "truncated stream\n");
56 return 0;
57 }
58
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010059 BUG_ON(!HTTP_IS_LWS(*ptr));
60 path.len = ptr - path.ptr;
61
62 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, 0, ist("GET"), path, ist("HTTP/1.0"));
Amaury Denoyelleb48c59a2021-11-18 14:40:26 +010063 if (!sl)
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +020064 return -1;
Amaury Denoyelleb48c59a2021-11-18 14:40:26 +010065
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010066 sl->flags |= HTX_SL_F_BODYLESS;
67 sl->info.req.meth = find_http_meth("GET", 3);
68
69 htx_add_endof(htx, HTX_BLK_EOH);
70 htx_to_buf(htx, &htx_buf);
71
Amaury Denoyelle8d4ac482022-09-15 11:22:32 +020072 if (!qc_attach_sc(qcs, &htx_buf))
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +020073 return -1;
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010074
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010075 b_free(&htx_buf);
76
Amaury Denoyelledb443382021-11-30 11:23:29 +010077 if (fin)
78 htx->flags |= HTX_FL_EOM;
79
Amaury Denoyelle1f21ebd2022-06-07 17:30:55 +020080 return b_data(b);
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010081}
82
83static struct buffer *mux_get_buf(struct qcs *qcs)
84{
85 if (!b_size(&qcs->tx.buf))
86 b_alloc(&qcs->tx.buf);
87
88 return &qcs->tx.buf;
89}
90
Amaury Denoyelle9534e592022-09-19 17:14:27 +020091static size_t hq_interop_snd_buf(struct qcs *qcs, struct htx *htx,
92 size_t count)
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010093{
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +010094 enum htx_blk_type btype;
95 struct htx_blk *blk;
96 int32_t idx;
97 uint32_t bsize, fsize;
98 struct buffer *res, outbuf;
99 size_t total = 0;
100
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100101 res = mux_get_buf(qcs);
102 outbuf = b_make(b_tail(res), b_contig_space(res), 0, 0);
103
Amaury Denoyelle84ea8dc2021-12-03 14:40:01 +0100104 while (count && !htx_is_empty(htx) && !(qcs->flags & QC_SF_BLK_MROOM)) {
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100105 /* Not implemented : QUIC on backend side */
106 idx = htx_get_head(htx);
107 blk = htx_get_blk(htx, idx);
108 btype = htx_get_blk_type(blk);
109 fsize = bsize = htx_get_blksz(blk);
110
111 BUG_ON(btype == HTX_BLK_REQ_SL);
112
113 switch (btype) {
114 case HTX_BLK_DATA:
115 if (fsize > count)
116 fsize = count;
Amaury Denoyelle5ede40b2021-12-07 16:19:03 +0100117
Amaury Denoyelle1ac95442021-12-09 10:07:23 +0100118 if (b_room(&outbuf) < fsize)
119 fsize = b_room(&outbuf);
Amaury Denoyelle5ede40b2021-12-07 16:19:03 +0100120
121 if (!fsize) {
122 qcs->flags |= QC_SF_BLK_MROOM;
123 goto end;
124 }
125
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100126 b_putblk(&outbuf, htx_get_blk_ptr(htx, blk), fsize);
127 total += fsize;
128 count -= fsize;
129
130 if (fsize == bsize)
131 htx_remove_blk(htx, blk);
132 else
133 htx_cut_data_blk(htx, blk, fsize);
134 break;
135
Ilya Shipitsin5e87bcf2021-12-25 11:45:52 +0500136 /* only body is transferred on HTTP/0.9 */
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100137 case HTX_BLK_RES_SL:
138 case HTX_BLK_TLR:
139 case HTX_BLK_EOT:
140 default:
141 htx_remove_blk(htx, blk);
142 total += bsize;
143 count -= bsize;
144 break;
145 }
146 }
147
Amaury Denoyelle5ede40b2021-12-07 16:19:03 +0100148 end:
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100149 b_add(res, b_data(&outbuf));
150
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100151 return total;
152}
153
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200154static int hq_interop_attach(struct qcs *qcs, void *conn_ctx)
155{
156 qcs_wait_http_req(qcs);
157 return 0;
158}
159
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100160const struct qcc_app_ops hq_interop_ops = {
161 .decode_qcs = hq_interop_decode_qcs,
162 .snd_buf = hq_interop_snd_buf,
Amaury Denoyelle30e260e2022-08-03 11:17:57 +0200163 .attach = hq_interop_attach,
Amaury Denoyelle154bc7f2021-11-12 16:09:54 +0100164};