1/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc. See LICENSE. */ 2#ifndef LSXPACK_HEADER_H_v206 3#define LSXPACK_HEADER_H_v206 4 5#ifdef __cplusplus 6extern "C" { 7#endif 8 9#include <assert.h> 10#include <stdint.h> 11#include <string.h> 12 13#ifndef LSXPACK_MAX_STRLEN 14#define LSXPACK_MAX_STRLEN UINT16_MAX 15#endif 16 17#if LSXPACK_MAX_STRLEN == UINT16_MAX 18typedef uint16_t lsxpack_strlen_t; 19#elif LSXPACK_MAX_STRLEN == UINT32_MAX 20typedef uint32_t lsxpack_strlen_t; 21#else 22#error unexpected LSXPACK_MAX_STRLEN 23#endif 24 25#define LSXPACK_DEL ((char *)NULL) 26 27enum lsxpack_flag 28{ 29 LSXPACK_HPACK_VAL_MATCHED = 1, 30 LSXPACK_QPACK_IDX = 2, 31 LSXPACK_APP_IDX = 4, 32 LSXPACK_NAME_HASH = 8, 33 LSXPACK_NAMEVAL_HASH = 16, 34 LSXPACK_VAL_MATCHED = 32, 35 LSXPACK_NEVER_INDEX = 64, 36}; 37 38/** 39 * When header are decoded, it should be stored to @buf starting from @name_offset, 40 * <name>: <value>\r\n 41 * So, it can be used directly as HTTP/1.1 header. there are 4 extra characters 42 * added. 43 * 44 * limitation: we currently does not support total header size > 64KB. 45 */ 46 47struct lsxpack_header 48{ 49 char *buf; /* the buffer for headers */ 50 uint32_t name_hash; /* hash value for name */ 51 uint32_t nameval_hash; /* hash value for name + value */ 52 lsxpack_strlen_t name_offset; /* the offset for name in the buffer */ 53 lsxpack_strlen_t name_len; /* the length of name */ 54 lsxpack_strlen_t val_offset; /* the offset for value in the buffer */ 55 lsxpack_strlen_t val_len; /* the length of value */ 56 uint16_t chain_next_idx; /* mainly for cookie value chain */ 57 uint8_t hpack_index; /* HPACK static table index */ 58 uint8_t qpack_index; /* QPACK static table index */ 59 uint8_t app_index; /* APP header index */ 60 enum lsxpack_flag flags:8; /* combination of lsxpack_flag */ 61 uint8_t indexed_type; /* control to disable index or not */ 62 uint8_t dec_overhead; /* num of extra bytes written to decoded buffer */ 63}; 64 65typedef struct lsxpack_header lsxpack_header_t; 66 67 68static inline void 69lsxpack_header_set_idx(lsxpack_header_t *hdr, int hpack_idx, 70 const char *val, size_t val_len) 71{ 72 memset(hdr, 0, sizeof(*hdr)); 73 hdr->buf = (char *)val; 74 hdr->hpack_index = hpack_idx; 75 assert(hpack_idx != 0); 76 assert(val_len <= LSXPACK_MAX_STRLEN); 77 hdr->val_len = val_len; 78} 79 80 81static inline void 82lsxpack_header_set_qpack_idx(lsxpack_header_t *hdr, int qpack_idx, 83 const char *val, size_t val_len) 84{ 85 memset(hdr, 0, sizeof(*hdr)); 86 hdr->buf = (char *)val; 87 hdr->qpack_index = qpack_idx; 88 assert(qpack_idx != -1); 89 hdr->flags = LSXPACK_QPACK_IDX; 90 assert(val_len <= LSXPACK_MAX_STRLEN); 91 hdr->val_len = val_len; 92} 93 94 95static inline void 96lsxpack_header_set_offset(lsxpack_header_t *hdr, const char *buf, 97 size_t name_offset, size_t name_len, 98 size_t val_len) 99{ 100 memset(hdr, 0, sizeof(*hdr)); 101 hdr->buf = (char *)buf; 102 hdr->name_offset = name_offset; 103 assert(name_len <= LSXPACK_MAX_STRLEN); 104 hdr->name_len = name_len; 105 assert(name_offset + name_len + 2 <= LSXPACK_MAX_STRLEN); 106 hdr->val_offset = name_offset + name_len + 2; 107 assert(val_len <= LSXPACK_MAX_STRLEN); 108 hdr->val_len = val_len; 109} 110 111 112static inline void 113lsxpack_header_set_offset2(lsxpack_header_t *hdr, const char *buf, 114 size_t name_offset, size_t name_len, 115 size_t val_offset, size_t val_len) 116{ 117 memset(hdr, 0, sizeof(*hdr)); 118 hdr->buf = (char *)buf; 119 hdr->name_offset = name_offset; 120 assert(name_len <= LSXPACK_MAX_STRLEN); 121 hdr->name_len = name_len; 122 assert(val_offset <= LSXPACK_MAX_STRLEN); 123 hdr->val_offset = val_offset; 124 assert(val_len <= LSXPACK_MAX_STRLEN); 125 hdr->val_len = val_len; 126} 127 128 129static inline void 130lsxpack_header_prepare_decode(lsxpack_header_t *hdr, 131 char *out, size_t offset, size_t len) 132{ 133 memset(hdr, 0, sizeof(*hdr)); 134 hdr->buf = out; 135 assert(offset <= LSXPACK_MAX_STRLEN); 136 hdr->name_offset = offset; 137 if (len > LSXPACK_MAX_STRLEN) 138 hdr->val_len = LSXPACK_MAX_STRLEN; 139 else 140 hdr->val_len = len; 141} 142 143 144static inline const char * 145lsxpack_header_get_name(const lsxpack_header_t *hdr) 146{ 147 return (hdr->name_len)? hdr->buf + hdr->name_offset : NULL; 148} 149 150 151static inline const char * 152lsxpack_header_get_value(const lsxpack_header_t *hdr) 153{ return hdr->buf + hdr->val_offset; } 154 155static inline size_t 156lsxpack_header_get_dec_size(const lsxpack_header_t *hdr) 157{ return hdr->name_len + hdr->val_len + hdr->dec_overhead; } 158 159static inline void 160lsxpack_header_mark_val_changed(lsxpack_header_t *hdr) 161{ 162 hdr->flags = (enum lsxpack_flag)(hdr->flags & 163 ~(LSXPACK_HPACK_VAL_MATCHED|LSXPACK_VAL_MATCHED|LSXPACK_NAMEVAL_HASH)); 164} 165#ifdef __cplusplus 166} 167#endif 168 169#endif //LSXPACK_HEADER_H_v206 170