1/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * lsquic_frame_reader.h -- Read HTTP frames from stream
4 */
5
6#ifndef LSQUIC_FRAME_READER_H
7#define LSQUIC_FRAME_READER_H 1
8
9#include <stddef.h>
10#include <stdint.h>
11#ifdef WIN32
12#include <vc_compat.h>
13#endif
14
15struct lshpack_dec;
16struct lsquic_mm;
17struct lsquic_stream;
18struct lsquic_frame_reader;
19struct lsquic_hset_if;
20struct uncompressed_headers;
21#if LSQUIC_CONN_STATS
22struct conn_stats;
23#endif
24
25
26enum frame_reader_flags
27{
28    FRF_SERVER      = (1 << 0),
29    FRF_HAVE_PREV   = (1 << 1),
30};
31
32
33/* Frame reader may hit some error conditions which are reported using
34 * callback fc_on_error.  These codes are later mapped stream- or
35 * connection-level errors.
36 */
37enum frame_reader_error
38{
39    FR_ERR_BAD_HEADER,
40    FR_ERR_OTHER_ERROR,
41
42    FR_ERR_DECOMPRESS,
43    FR_ERR_INVALID_FRAME_SIZE,  /* E.g. a SETTINGS frame length is not a multiple
44                                 * of 6 (RFC 7540, Section 6.5.1).
45                                 */
46    FR_ERR_NONZERO_STREAM_ID,
47    FR_ERR_ZERO_STREAM_ID,
48    FR_ERR_SELF_DEP_STREAM,     /* A stream in priority frame cannot depend on
49                                 * itself (RFC 7540, Section 5.3.1).
50                                 */
51    FR_ERR_UNEXPECTED_PUSH,
52    FR_ERR_EXPECTED_CONTIN,     /* Expected continuation frame. */
53};
54
55
56struct frame_reader_callbacks
57{
58    void (*frc_on_headers)      (void *frame_cb_ctx, struct uncompressed_headers *);
59    void (*frc_on_push_promise) (void *frame_cb_ctx, struct uncompressed_headers *);
60    void (*frc_on_settings)     (void *frame_cb_ctx, uint16_t setting_id,
61                                 uint32_t setting_value);
62    void (*frc_on_priority)     (void *frame_cb_ctx, lsquic_stream_id_t stream_id,
63                                 int exclusive, lsquic_stream_id_t dep_stream_id,
64                                 unsigned weight);
65    void (*frc_on_error)        (void *frame_cb_ctx, lsquic_stream_id_t stream_id,
66                                 enum frame_reader_error);
67};
68
69typedef ssize_t (*fr_stream_read_f)(struct lsquic_stream *, void *, size_t);
70
71struct lsquic_frame_reader *
72lsquic_frame_reader_new (enum frame_reader_flags, unsigned max_headers_sz,
73                         struct lsquic_mm *, struct lsquic_stream *,
74                         fr_stream_read_f, struct lshpack_dec *,
75                         const struct frame_reader_callbacks *, void *fr_cb_ctx,
76#if LSQUIC_CONN_STATS
77                         struct conn_stats *conn_stats,
78#endif
79                         const struct lsquic_hset_if *, void *hsi_ctx);
80
81int
82lsquic_frame_reader_read (struct lsquic_frame_reader *);
83
84void
85lsquic_frame_reader_destroy (struct lsquic_frame_reader *);
86
87size_t
88lsquic_frame_reader_mem_used (const struct lsquic_frame_reader *);
89
90#endif
91