lsxpack_header.h revision a5fa05f9
1/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc. See LICENSE. */ 2#ifndef LSXPACK_HEADER_H_v203 3#define LSXPACK_HEADER_H_v203 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 25enum lsxpack_flag 26{ 27 LSXPACK_HPACK_IDX = 1, 28 LSXPACK_QPACK_IDX = 2, 29 LSXPACK_APP_IDX = 4, 30 LSXPACK_NAME_HASH = 8, 31 LSXPACK_NAMEVAL_HASH = 16, 32 LSXPACK_VAL_MATCHED = 32, 33 LSXPACK_NEVER_INDEX = 64, 34}; 35 36/** 37 * When header are decoded, it should be stored to @buf starting from @name_offset, 38 * <name>: <value>\r\n 39 * So, it can be used directly as HTTP/1.1 header. there are 4 extra characters 40 * added. 41 * 42 * limitation: we currently does not support total header size > 64KB. 43 */ 44 45struct lsxpack_header 46{ 47 char *buf; /* the buffer for headers */ 48 const char *name_ptr; /* the name pointer can be optionally set for encoding */ 49 uint32_t name_hash; /* hash value for name */ 50 uint32_t nameval_hash; /* hash value for name + value */ 51 lsxpack_strlen_t name_offset; /* the offset for name in the buffer */ 52 lsxpack_strlen_t name_len; /* the length of name */ 53 lsxpack_strlen_t val_offset; /* the offset for value in the buffer */ 54 lsxpack_strlen_t val_len; /* the length of value */ 55 uint8_t hpack_index; /* HPACK static table index */ 56 uint8_t qpack_index; /* QPACK static table index */ 57 uint8_t app_index; /* APP header index */ 58 enum lsxpack_flag flags:8; /* combination of lsxpack_flag */ 59 uint8_t indexed_type; /* control to disable index or not */ 60 uint8_t dec_overhead; /* num of extra bytes written to decoded buffer */ 61}; 62 63typedef struct lsxpack_header lsxpack_header_t; 64 65 66static inline void 67lsxpack_header_set_idx(lsxpack_header_t *hdr, int hpack_idx, 68 const char *val, size_t val_len) 69{ 70 memset(hdr, 0, sizeof(*hdr)); 71 hdr->buf = (char *)val; 72 hdr->hpack_index = hpack_idx; 73 assert(hpack_idx != 0); 74 hdr->flags = LSXPACK_HPACK_IDX; 75 assert(val_len <= LSXPACK_MAX_STRLEN); 76 hdr->val_len = val_len; 77} 78 79 80static inline void 81lsxpack_header_set_ptr(lsxpack_header_t *hdr, 82 const char *name, size_t name_len, 83 const char *val, size_t val_len) 84{ 85 memset(hdr, 0, sizeof(*hdr)); 86 hdr->buf = (char *)val; 87 assert(val_len <= LSXPACK_MAX_STRLEN); 88 hdr->val_len = val_len; 89 hdr->name_ptr = name; 90 assert(name_len <= LSXPACK_MAX_STRLEN); 91 hdr->name_len = name_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_ptr ? hdr->name_ptr 148 : (hdr->name_len) ? hdr->buf + hdr->name_offset 149 : NULL; 150} 151 152 153static inline const char * 154lsxpack_header_get_value(const lsxpack_header_t *hdr) 155{ return hdr->buf + hdr->val_offset; } 156 157static inline size_t 158lsxpack_header_get_dec_size(const lsxpack_header_t *hdr) 159{ return hdr->name_len + hdr->val_len + hdr->dec_overhead; } 160 161#ifdef __cplusplus 162} 163#endif 164 165#endif //LSXPACK_HEADER_H_v203 166