lsquic_headers_stream.c revision c51ce338
1/* Copyright (c) 2017 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * HEADERS stream logic
4 */
5
6#include <assert.h>
7#include <errno.h>
8#include <stdarg.h>
9#include <stdlib.h>
10#include <string.h>
11#include <sys/queue.h>
12
13#include "lsquic_types.h"
14#include "lsquic_frame_common.h"
15#include "lsquic_frame_reader.h"
16#include "lsquic_frame_writer.h"
17#include "lsquic_arr.h"
18#include "lsquic_hpack_enc.h"
19#include "lsquic_hpack_dec.h"
20#include "lsquic.h"
21
22#include "lsquic_headers_stream.h"
23
24#define MAX_HEADERS_SIZE (64 * 1024)
25#define MAX_HEADER_TABLE_SIZE (512 * 1024)
26
27#define LSQUIC_LOGGER_MODULE LSQLM_HEADERS
28#define LSQUIC_LOG_CONN_ID lsquic_conn_id(lsquic_stream_conn(hs->hs_stream))
29#include "lsquic_logger.h"
30
31static const struct frame_reader_callbacks frame_callbacks;
32
33struct headers_stream
34{
35    struct lsquic_stream               *hs_stream;
36    struct lsquic_frame_reader         *hs_fr;
37    struct lsquic_frame_writer         *hs_fw;
38    const struct headers_stream_callbacks
39                                       *hs_callbacks;
40    const struct lsquic_engine_settings
41                                       *hs_settings;
42    void                               *hs_cb_ctx;
43    struct lsquic_mm                   *hs_mm;
44    struct lsquic_henc                  hs_henc;
45    struct lsquic_hdec                  hs_hdec;
46    enum {
47            HS_IS_SERVER    = (1 << 0),
48            HS_HENC_INITED  = (1 << 1),
49    }                                   hs_flags;
50};
51
52
53int
54lsquic_headers_stream_send_settings (struct headers_stream *hs,
55        const struct lsquic_http2_setting *settings, unsigned n_settings)
56{
57    if (0 == lsquic_frame_writer_write_settings(hs->hs_fw, settings,
58                                                            n_settings))
59    {
60        lsquic_stream_wantwrite(hs->hs_stream,
61            lsquic_frame_writer_have_leftovers(hs->hs_fw));
62        return 0;
63    }
64    else
65    {
66        LSQ_WARN("Could not write settings to stream: %s",
67                                                        strerror(errno));
68        return -1;
69    }
70}
71
72
73static lsquic_stream_ctx_t *
74headers_on_new_stream (void *stream_if_ctx, lsquic_stream_t *stream)
75{
76    struct headers_stream *hs = stream_if_ctx;
77    lsquic_hdec_init(&hs->hs_hdec);
78    if (0 != lsquic_henc_init(&hs->hs_henc))
79    {
80        LSQ_WARN("could not initialize HPACK encoder: %s", strerror(errno));
81        return NULL;
82    }
83    hs->hs_flags |= HS_HENC_INITED;
84    hs->hs_stream = stream;
85    LSQ_DEBUG("stream created");
86    hs->hs_fr = lsquic_frame_reader_new((hs->hs_flags & HS_IS_SERVER) ? FRF_SERVER : 0,
87                                MAX_HEADERS_SIZE, hs->hs_mm,
88                                stream, lsquic_stream_read, &hs->hs_hdec,
89                                &frame_callbacks, hs);
90    if (!hs->hs_fr)
91    {
92        LSQ_WARN("could not create frame reader: %s", strerror(errno));
93        hs->hs_callbacks->hsc_on_conn_error(hs->hs_cb_ctx);
94        return NULL;
95    }
96    hs->hs_fw = lsquic_frame_writer_new(hs->hs_mm, stream, 0, &hs->hs_henc,
97                                lsquic_stream_write,
98                                (hs->hs_flags & HS_IS_SERVER));
99    if (!hs->hs_fw)
100    {
101        LSQ_WARN("could not create frame writer: %s", strerror(errno));
102        hs->hs_callbacks->hsc_on_conn_error(hs->hs_cb_ctx);
103        return NULL;
104    }
105    lsquic_stream_wantread(stream, 1);
106    return (lsquic_stream_ctx_t *) hs;
107}
108
109
110static void
111headers_on_read (lsquic_stream_t *stream, struct lsquic_stream_ctx *ctx)
112{
113    struct headers_stream *hs = (struct headers_stream *) ctx;
114    if (0 != lsquic_frame_reader_read(hs->hs_fr))
115    {
116        LSQ_ERROR("frame reader failed");
117        hs->hs_callbacks->hsc_on_conn_error(hs->hs_cb_ctx);
118    }
119}
120
121
122static void
123headers_on_write (lsquic_stream_t *stream, struct lsquic_stream_ctx *ctx)
124{
125    struct headers_stream *hs = (struct headers_stream *) ctx;
126    assert(lsquic_frame_writer_have_leftovers(hs->hs_fw));
127    int s = lsquic_frame_writer_flush(hs->hs_fw);
128    if (0 == s)
129    {
130        LSQ_DEBUG("flushed");
131        lsquic_stream_wantwrite(stream,
132            lsquic_frame_writer_have_leftovers(hs->hs_fw));
133    }
134    else
135    {
136        LSQ_WARN("Error writing to stream: %s", strerror(errno));
137        hs->hs_callbacks->hsc_on_conn_error(hs->hs_cb_ctx);
138    }
139}
140
141
142static void
143headers_on_close (lsquic_stream_t *stream, struct lsquic_stream_ctx *ctx)
144{
145    struct headers_stream *hs = (struct headers_stream *) ctx;
146    LSQ_DEBUG("stream closed");
147}
148
149
150int
151lsquic_headers_stream_send_headers (struct headers_stream *hs,
152    uint32_t stream_id, const struct lsquic_http_headers *headers, int eos,
153    unsigned weight)
154{
155    LSQ_DEBUG("received compressed headers to send");
156    int s;
157    s = lsquic_frame_writer_write_headers(hs->hs_fw, stream_id, headers, eos,
158                                                                        weight);
159    if (0 == s)
160    {
161        lsquic_stream_wantwrite(hs->hs_stream,
162            lsquic_frame_writer_have_leftovers(hs->hs_fw));
163    }
164    else
165        LSQ_INFO("Error writing headers: %s", strerror(errno));
166    return s;
167}
168
169
170int
171lsquic_headers_stream_send_priority (struct headers_stream *hs,
172    uint32_t stream_id, int exclusive, uint32_t dep_stream_id, unsigned weight)
173{
174    LSQ_DEBUG("received priority to send");
175    int s;
176    if (stream_id == dep_stream_id)
177    {
178        LSQ_INFO("stream cannot depend on itself"); /* RFC 7540, Sec. 5.3.1. */
179        return -1;
180    }
181    s = lsquic_frame_writer_write_priority(hs->hs_fw, stream_id, exclusive,
182                                                        dep_stream_id, weight);
183    if (0 == s)
184    {
185        lsquic_stream_wantwrite(hs->hs_stream,
186            lsquic_frame_writer_have_leftovers(hs->hs_fw));
187    }
188    else
189        LSQ_INFO("Error writing priority frame: %s", strerror(errno));
190    return s;
191}
192
193
194struct headers_stream *
195lsquic_headers_stream_new (int is_server, struct lsquic_mm *mm,
196                           const struct lsquic_engine_settings *settings,
197                           const struct headers_stream_callbacks *callbacks,
198                           void *cb_ctx)
199{
200    struct headers_stream *hs = calloc(1, sizeof(*hs));
201    if (!hs)
202        return NULL;
203    hs->hs_callbacks = callbacks;
204    hs->hs_cb_ctx    = cb_ctx;
205    if (is_server)
206        hs->hs_flags = HS_IS_SERVER;
207    else
208        hs->hs_flags = 0;
209    hs->hs_settings  = settings;
210    hs->hs_mm        = mm;
211    return hs;
212}
213
214
215void
216lsquic_headers_stream_destroy (struct headers_stream *hs)
217{
218    if (hs->hs_fr)
219        lsquic_frame_reader_destroy(hs->hs_fr);
220    if (hs->hs_fw)
221        lsquic_frame_writer_destroy(hs->hs_fw);
222    if (hs->hs_flags & HS_HENC_INITED)
223        lsquic_henc_cleanup(&hs->hs_henc);
224    lsquic_hdec_cleanup(&hs->hs_hdec);
225    free(hs);
226}
227
228
229static const struct lsquic_stream_if headers_stream_if =
230{
231    .on_new_stream = headers_on_new_stream,
232    .on_read       = headers_on_read,
233    .on_write      = headers_on_write,
234    .on_close      = headers_on_close,
235};
236
237
238const struct lsquic_stream_if *const lsquic_headers_stream_if =
239                                                &headers_stream_if;
240
241
242static void
243headers_on_incoming_headers (void *ctx, struct uncompressed_headers *uh)
244{
245    struct headers_stream *hs = ctx;
246    hs->hs_callbacks->hsc_on_headers(hs->hs_cb_ctx, uh);
247}
248
249
250static void
251headers_on_push_promise (void *ctx, struct uncompressed_headers *uh)
252{
253    struct headers_stream *hs = ctx;
254    hs->hs_callbacks->hsc_on_push_promise(hs->hs_cb_ctx, uh);
255}
256
257
258static void
259headers_on_priority (void *ctx, uint32_t stream_id, int exclusive,
260                     uint32_t dep_stream_id, unsigned weight)
261{
262    struct headers_stream *hs = ctx;
263    hs->hs_callbacks->hsc_on_priority(hs->hs_cb_ctx, stream_id, exclusive,
264                                                    dep_stream_id, weight);
265}
266
267
268static void
269headers_on_error (void *ctx, uint32_t stream_id, enum frame_reader_error err)
270{
271    struct headers_stream *hs = ctx;
272    switch (err)
273    {
274    case FR_ERR_DUPLICATE_PSEH:
275    case FR_ERR_INCOMPL_REQ_PSEH:
276    case FR_ERR_UNNEC_REQ_PSEH:
277    case FR_ERR_INCOMPL_RESP_PSEH:
278    case FR_ERR_UNNEC_RESP_PSEH:
279    case FR_ERR_UNKNOWN_PSEH:
280    case FR_ERR_UPPERCASE_HEADER:
281    case FR_ERR_MISPLACED_PSEH:
282    case FR_ERR_MISSING_PSEH:
283    case FR_ERR_DECOMPRESS:
284    case FR_ERR_HEADERS_TOO_LARGE:
285    case FR_ERR_SELF_DEP_STREAM:
286        LSQ_INFO("error %u is a stream error (stream %u)", err, stream_id);
287        hs->hs_callbacks->hsc_on_stream_error(hs->hs_cb_ctx, stream_id);
288        break;
289    case FR_ERR_INVALID_FRAME_SIZE:
290    case FR_ERR_NONZERO_STREAM_ID:
291    case FR_ERR_UNEXPECTED_PUSH:
292    case FR_ERR_ZERO_STREAM_ID:
293    case FR_ERR_NOMEM:
294    case FR_ERR_EXPECTED_CONTIN:
295        LSQ_INFO("error %u is a connection error (stream %u)", err, stream_id);
296        hs->hs_callbacks->hsc_on_conn_error(hs->hs_cb_ctx);
297        break;
298    }
299}
300
301
302static void
303headers_on_settings (void *ctx, uint16_t setting_id, uint32_t setting_value)
304{
305    struct headers_stream *hs = ctx;
306    switch (setting_id)
307    {
308    case SETTINGS_HEADER_TABLE_SIZE:
309        if (setting_value > MAX_HEADER_TABLE_SIZE)
310        {
311            LSQ_INFO("tried to update table size to %u, which is larger than "
312                "allowed maximum of %u bytes", setting_value,
313                MAX_HEADER_TABLE_SIZE);
314            hs->hs_callbacks->hsc_on_conn_error(hs->hs_cb_ctx);
315        }
316        else
317        {
318            LSQ_INFO("update hpack table size to %u", setting_value);
319            lsquic_henc_set_max_capacity(&hs->hs_henc, setting_value);
320        }
321        break;
322    case SETTINGS_MAX_HEADER_LIST_SIZE:
323        LSQ_INFO("set max header list size to %u", setting_value);
324        lsquic_frame_writer_max_header_list_size(hs->hs_fw, setting_value);
325        break;
326    case SETTINGS_ENABLE_PUSH:
327        LSQ_INFO("got setting enable_push: %u", setting_value);
328        if (hs->hs_flags & HS_IS_SERVER)
329        {
330            if (setting_value <= 1)
331                hs->hs_callbacks->hsc_on_enable_push(hs->hs_cb_ctx,
332                                                            setting_value);
333            else
334            {
335                LSQ_INFO("invalid value of enable_push");
336                hs->hs_callbacks->hsc_on_conn_error(hs->hs_cb_ctx);
337            }
338        }
339        else
340        {
341            LSQ_INFO("it is an error to receive enable_push setting in "
342                     "client mode");
343            hs->hs_callbacks->hsc_on_conn_error(hs->hs_cb_ctx);
344        }
345        break;
346    case SETTINGS_MAX_CONCURRENT_STREAMS:
347    case SETTINGS_INITIAL_WINDOW_SIZE:
348    case SETTINGS_MAX_FRAME_SIZE:
349        /* [draft-ietf-quic-http-00], Section 3 */
350        LSQ_INFO("Specifying setting 0x%X is a QUIC error", setting_id);
351        hs->hs_callbacks->hsc_on_conn_error(hs->hs_cb_ctx);
352        break;
353    default:
354        LSQ_INFO("Ignoring unknown setting 0x%X; value 0x%X", setting_id,
355                                                                setting_value);
356        break;
357    }
358}
359
360
361int
362lsquic_headers_stream_push_promise (struct headers_stream *hs,
363                        uint32_t stream_id, uint32_t promised_stream_id,
364                        const struct iovec *path, const struct iovec *host,
365                        const struct lsquic_http_headers *headers)
366{
367    int s;
368    LSQ_DEBUG("promising stream %u in response to stream %u",
369                                            promised_stream_id, stream_id);
370    s = lsquic_frame_writer_write_promise(hs->hs_fw, stream_id,
371                                    promised_stream_id, path, host, headers);
372    if (0 == s)
373    {
374        lsquic_stream_wantwrite(hs->hs_stream,
375            lsquic_frame_writer_have_leftovers(hs->hs_fw));
376    }
377    else
378        LSQ_INFO("Error writing push promise: %s", strerror(errno));
379    return s;
380}
381
382
383size_t
384lsquic_headers_stream_mem_used (const struct headers_stream *hs)
385{
386    size_t size;
387
388    size = sizeof(*hs);
389    size += lsquic_frame_reader_mem_used(hs->hs_fr);
390    size += lsquic_frame_writer_mem_used(hs->hs_fw);
391    size -= sizeof(hs->hs_hdec);
392    size += lsquic_hdec_mem_used(&hs->hs_hdec);
393    if (hs->hs_flags & HS_HENC_INITED)
394    {
395        size -= sizeof(hs->hs_henc);
396        size += lsquic_henc_mem_used(&hs->hs_henc);
397    }
398
399    return size;
400}
401
402
403struct lsquic_stream *
404lsquic_headers_stream_get_stream (const struct headers_stream *hs)
405{
406    return hs->hs_stream;
407}
408
409
410static const struct frame_reader_callbacks frame_callbacks = {
411    .frc_on_headers      = headers_on_incoming_headers,
412    .frc_on_push_promise = headers_on_push_promise,
413    .frc_on_error        = headers_on_error,
414    .frc_on_settings     = headers_on_settings,
415    .frc_on_priority     = headers_on_priority,
416};
417