blob: 73b00389cb1a62a78708a299aa7b59242dc3921c [file] [log] [blame]
Amaury Denoyelled80fbca2022-09-19 17:02:28 +02001#include <haproxy/qmux_http.h>
2
3#include <haproxy/api-t.h>
4#include <haproxy/htx.h>
5#include <haproxy/qmux_trace.h>
6
7/* QUIC MUX rcv_buf operation using HTX data. Received data from stream <qcs>
8 * will be transferred as HTX in <buf>. Output buffer is expected to be of
9 * length <count>. <fin> will be set to signal the last data to receive on this
10 * stream.
11 *
12 * Return the size in bytes of transferred data.
13 */
14size_t qcs_http_rcv_buf(struct qcs *qcs, struct buffer *buf, size_t count,
15 char *fin)
16{
17 struct htx *qcs_htx = NULL;
18 struct htx *cs_htx = NULL;
19 size_t ret = 0;
20
21 TRACE_ENTER(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
22
23 *fin = 0;
24 qcs_htx = htx_from_buf(&qcs->rx.app_buf);
25 if (htx_is_empty(qcs_htx)) {
26 /* Set buffer data to 0 as HTX is empty. */
27 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
28 goto end;
29 }
30
31 ret = qcs_htx->data;
32
33 cs_htx = htx_from_buf(buf);
34 if (htx_is_empty(cs_htx) && htx_used_space(qcs_htx) <= count) {
35 /* EOM will be copied to cs_htx via b_xfer(). */
36 if (qcs_htx->flags & HTX_FL_EOM)
37 *fin = 1;
38
39 htx_to_buf(cs_htx, buf);
40 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
41 b_xfer(buf, &qcs->rx.app_buf, b_data(&qcs->rx.app_buf));
42 goto end;
43 }
44
45 htx_xfer_blks(cs_htx, qcs_htx, count, HTX_BLK_UNUSED);
46 BUG_ON(qcs_htx->flags & HTX_FL_PARSING_ERROR);
47
48 /* Copy EOM from src to dst buffer if all data copied. */
49 if (htx_is_empty(qcs_htx) && (qcs_htx->flags & HTX_FL_EOM)) {
50 cs_htx->flags |= HTX_FL_EOM;
51 *fin = 1;
52 }
53
54 cs_htx->extra = qcs_htx->extra ? (qcs_htx->data + qcs_htx->extra) : 0;
55 htx_to_buf(cs_htx, buf);
56 htx_to_buf(qcs_htx, &qcs->rx.app_buf);
57 ret -= qcs_htx->data;
58
59 end:
60 TRACE_LEAVE(QMUX_EV_STRM_RECV, qcs->qcc->conn, qcs);
61
62 return ret;
63}
Amaury Denoyelle9534e592022-09-19 17:14:27 +020064
65/* QUIC MUX snd_buf operation using HTX data. HTX data will be transferred from
66 * <buf> to <qcs> stream buffer. Input buffer is expected to be of length
67 * <count>. <fin> will be set to signal the last data to send for this stream.
68 *
69 * Return the size in bytes of transferred data.
70 */
71size_t qcs_http_snd_buf(struct qcs *qcs, struct buffer *buf, size_t count,
72 char *fin)
73{
74 struct htx *htx;
75 size_t ret;
76
77 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
78
79 htx = htx_from_buf(buf);
80
Amaury Denoyelle24962dd2023-04-24 17:50:23 +020081 if (htx->extra && htx->extra == HTX_UNKOWN_PAYLOAD_LENGTH)
82 qcs->flags |= QC_SF_UNKNOWN_PL_LENGTH;
83
Amaury Denoyelle9534e592022-09-19 17:14:27 +020084 ret = qcs->qcc->app_ops->snd_buf(qcs, htx, count);
85 *fin = (htx->flags & HTX_FL_EOM) && htx_is_empty(htx);
86
87 htx_to_buf(htx, buf);
88
89 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
90
91 return ret;
92}
Amaury Denoyelle0ed617a2022-09-20 14:46:40 +020093
94/* QUIC MUX snd_buf reset. HTX data stored in <buf> of length <count> will be
95 * cleared. This can be used when data should not be transmitted any longer.
96 *
97 * Return the size in bytes of cleared data.
98 */
99size_t qcs_http_reset_buf(struct qcs *qcs, struct buffer *buf, size_t count)
100{
101 struct htx *htx;
102
103 TRACE_ENTER(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
104
105 htx = htx_from_buf(buf);
106 htx_reset(htx);
107 htx_to_buf(htx, buf);
108
109 TRACE_LEAVE(QMUX_EV_STRM_SEND, qcs->qcc->conn, qcs);
110
111 return count;
112}
Amaury Denoyelle381d8132023-02-17 09:51:20 +0100113
114/* Utility function which can be used by app layer an empty STREAM frame is
115 * received with FIN bit set for <qcs> stream. It will ensure that HTX EOM is
116 * properly inserted in <qcs> app_buf.
117 */
118void qcs_http_handle_standalone_fin(struct qcs *qcs)
119{
120 struct buffer *appbuf;
121 struct htx *htx = NULL;
122
123 appbuf = qc_get_buf(qcs, &qcs->rx.app_buf);
124 BUG_ON(!appbuf);
125
126 htx = htx_from_buf(appbuf);
127 if (htx_is_empty(htx)) {
128 if (!htx_add_endof(htx, HTX_BLK_EOT)) {
129 ABORT_NOW(); /* cannot happen for empty HTX message. */
130 }
131 }
132 htx->flags |= HTX_FL_EOM;
133 htx_to_buf(htx, appbuf);
134}