]> git.ipfire.org Git - thirdparty/squid.git/blob - src/http/RegisteredHeaders.cc
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / http / RegisteredHeaders.cc
1 /*
2 * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #include "squid.h"
10 #include "RegisteredHeaders.h"
11
12 #include <ostream>
13 #include <vector>
14
15 namespace Http
16 {
17 /* glue to code generated by gperf */
18 #include "http/RegisteredHeadersHash.cci"
19
20 HeaderTableRecord::HeaderTableRecord() :
21 name(""), id(HdrType::BAD_HDR), type(HdrFieldType::ftInvalid),
22 list(false), request(false), reply(false), hopbyhop(false), denied304(false)
23 {}
24
25 HeaderTableRecord::HeaderTableRecord(const char *n) :
26 name(n), id(HdrType::BAD_HDR), type(HdrFieldType::ftInvalid),
27 list(false), request(false), reply(false), hopbyhop(false), denied304(false)
28 {}
29
30 HeaderTableRecord::HeaderTableRecord(const char *n, HdrType theId, HdrFieldType theType, int theKind) :
31 name(n), id(theId), type(theType),
32 list(theKind & HdrKind::ListHeader), request(theKind & HdrKind::RequestHeader),
33 reply(theKind & HdrKind::ReplyHeader), hopbyhop(theKind & HdrKind::HopByHopHeader),
34 denied304(theKind & HdrKind::Denied304Header)
35 {}
36
37 const HeaderTableRecord&
38 HeaderLookupTable_t::lookup (const char *buf, const std::size_t len) const {
39 const HeaderTableRecord *r = HttpHeaderHashTable::lookup(buf, len);
40 if (!r || r->id == Http::HdrType::OTHER)
41 return BadHdr;
42 return *r;
43 }
44 const HeaderTableRecord HeaderLookupTable_t::BadHdr {"*INVALID*:", Http::HdrType::BAD_HDR, Http::HdrFieldType::ftInvalid, HdrKind::None};
45
46 HeaderLookupTable_t::HeaderLookupTable_t()
47 {
48 initCache();
49 }
50
51 void
52 HeaderLookupTable_t::initCache()
53 {
54 idCache.resize(TOTAL_KEYWORDS);
55 for (int j = MIN_HASH_VALUE; j <= MAX_HASH_VALUE; ++j) { //MAX_HASH_VALUE is exported by gperf
56 if (HttpHeaderDefinitionsTable[j].name[0] != '\0') { //some slots are empty
57 idCache[static_cast<int>(HttpHeaderDefinitionsTable[j].id)] =
58 & HttpHeaderDefinitionsTable[j];
59 }
60 }
61 //check after the fact. The cache array must be full
62 for (auto e : idCache) {
63 assert(e->name);
64 }
65 }
66 const HeaderLookupTable_t HeaderLookupTable;
67
68 }; /* namespace Http */
69
70 std::ostream&
71 operator<< (std::ostream &s, Http::HdrType id)
72 {
73 if (Http::any_HdrType_enum_value(id))
74 s << Http::HeaderLookupTable.lookup(id).name << '[' << static_cast<int>(id) << ']';
75 else
76 s << "Invalid-Header[" << static_cast<int>(id) << ']';
77 return s;
78 }
79