1a74702c6SGeorge Wang/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
2758aff32SDmitri Tikhonov#include <inttypes.h>
3758aff32SDmitri Tikhonov#include <stdio.h>
4758aff32SDmitri Tikhonov#include <stdlib.h>
5758aff32SDmitri Tikhonov
6758aff32SDmitri Tikhonov#include "lsquic_int_types.h"
7758aff32SDmitri Tikhonov#include "lsquic_qpack_exp.h"
8758aff32SDmitri Tikhonov
9758aff32SDmitri Tikhonov
10758aff32SDmitri Tikhonovstruct qpack_exp_record *
11758aff32SDmitri Tikhonovlsquic_qpack_exp_new (void)
12758aff32SDmitri Tikhonov{
13758aff32SDmitri Tikhonov    return calloc(1, sizeof(struct qpack_exp_record));
14758aff32SDmitri Tikhonov}
15758aff32SDmitri Tikhonov
16758aff32SDmitri Tikhonov
17758aff32SDmitri Tikhonovvoid
18758aff32SDmitri Tikhonovlsquic_qpack_exp_destroy (struct qpack_exp_record *exp)
19758aff32SDmitri Tikhonov{
20758aff32SDmitri Tikhonov    free(exp->qer_user_agent);
21758aff32SDmitri Tikhonov    free(exp);
22758aff32SDmitri Tikhonov}
23758aff32SDmitri Tikhonov
24758aff32SDmitri Tikhonov
25758aff32SDmitri Tikhonovstatic const char *const flag2tag[] = {
26758aff32SDmitri Tikhonov    [QER_SERVER|QER_ENCODER]    = "server",
27758aff32SDmitri Tikhonov    [QER_SERVER|0]              = "user-agent",
28758aff32SDmitri Tikhonov    [0         |QER_ENCODER]    = "user-agent",
29758aff32SDmitri Tikhonov    [0         |0]              = "server",
30758aff32SDmitri Tikhonov};
31758aff32SDmitri Tikhonov
32758aff32SDmitri Tikhonov
33758aff32SDmitri Tikhonovint
34758aff32SDmitri Tikhonovlsquic_qpack_exp_to_xml (const struct qpack_exp_record *exp, char *buf,
35758aff32SDmitri Tikhonov                                                                size_t buf_sz)
36758aff32SDmitri Tikhonov{
37758aff32SDmitri Tikhonov    const char *const tag = flag2tag[exp->qer_flags & (QER_SERVER|QER_ENCODER)];
38758aff32SDmitri Tikhonov
39758aff32SDmitri Tikhonov    return snprintf(buf, buf_sz,
40758aff32SDmitri Tikhonov        "<qpack-exp>"
41758aff32SDmitri Tikhonov            "<role>%s</role>"
42758aff32SDmitri Tikhonov            "<duration units=\"ms\">%"PRIu64"</duration>"
43758aff32SDmitri Tikhonov            "<hblock-count>%u</hblock-count>"
44758aff32SDmitri Tikhonov            "<hblock-size>%u</hblock-size>"
45758aff32SDmitri Tikhonov            "<peer-max-size>%u</peer-max-size>"
46758aff32SDmitri Tikhonov            "<used-max-size>%u</used-max-size>"
47758aff32SDmitri Tikhonov            "<peer-max-blocked>%u</peer-max-blocked>"
48758aff32SDmitri Tikhonov            "<used-max-blocked>%u</used-max-blocked>"
49758aff32SDmitri Tikhonov            "<comp-ratio>%.3f</comp-ratio>"
50758aff32SDmitri Tikhonov            /* We just print unescaped string... */
51758aff32SDmitri Tikhonov            "<%s>%s</%s>"
52758aff32SDmitri Tikhonov        "</qpack-exp>"
53758aff32SDmitri Tikhonov        ,   exp->qer_flags & QER_ENCODER ? "encoder" : "decoder"
54758aff32SDmitri Tikhonov        ,   (exp->qer_last_req - exp->qer_first_req) / 1000 /* Milliseconds */
55758aff32SDmitri Tikhonov        ,   exp->qer_hblock_count
56758aff32SDmitri Tikhonov        ,   exp->qer_hblock_size
57758aff32SDmitri Tikhonov        ,   exp->qer_peer_max_size
58758aff32SDmitri Tikhonov        ,   exp->qer_used_max_size
59758aff32SDmitri Tikhonov        ,   exp->qer_peer_max_blocked
60758aff32SDmitri Tikhonov        ,   exp->qer_used_max_blocked
61758aff32SDmitri Tikhonov        ,   exp->qer_comp_ratio
62758aff32SDmitri Tikhonov        ,   tag
63758aff32SDmitri Tikhonov        ,   exp->qer_user_agent ? exp->qer_user_agent : ""
64758aff32SDmitri Tikhonov        ,   tag
65758aff32SDmitri Tikhonov        );
66758aff32SDmitri Tikhonov}
67