]> git.ipfire.org Git - thirdparty/squid.git/blame - src/HttpHeaderTools.h
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / HttpHeaderTools.h
CommitLineData
bbc27441 1/*
77b1029d 2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
bbc27441
AJ
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
3b07476b
CT
9#ifndef SQUID_HTTPHEADERTOOLS_H
10#define SQUID_HTTPHEADERTOOLS_H
11
6f58d7d7 12#include "acl/forward.h"
f4698e0b 13#include "format/Format.h"
582c2af2 14#include "HttpHeader.h"
d5f18517 15#include "sbuf/forward.h"
f4698e0b 16
620bc212 17#include <functional>
f4698e0b 18#include <list>
3b07476b 19#include <map>
3b07476b 20#include <string>
620bc212
AR
21#if HAVE_STRINGS_H
22#include <strings.h>
23#endif
3b07476b 24
f4698e0b 25class HeaderWithAcl;
001d55dc 26class HttpHeader;
001d55dc
FC
27class HttpRequest;
28class StoreEntry;
001d55dc 29
f4698e0b
CT
30typedef std::list<HeaderWithAcl> HeaderWithAclList;
31
cde8f31b
NH
32/* Distinguish between Request and Reply (for header mangling) */
33typedef enum {
34 ROR_REQUEST,
35 ROR_REPLY
36} req_or_rep_t;
37
001d55dc
FC
38// Currently a POD
39class headerMangler
1b2f0924 40{
09bbee4c 41public:
3b07476b
CT
42 acl_access *access_list;
43 char *replacement;
44};
3b07476b 45
001d55dc 46/// A collection of headerMangler objects for a given message kind.
d3abcb0d
A
47class HeaderManglers
48{
3b07476b
CT
49public:
50 HeaderManglers();
51 ~HeaderManglers();
52
53 /// returns a header mangler for field e or nil if none was specified
001d55dc 54 const headerMangler *find(const HttpHeaderEntry &e) const;
3b07476b
CT
55
56 /// returns a mangler for the named header (known or custom)
001d55dc 57 headerMangler *track(const char *name);
3b07476b
CT
58
59 /// updates mangler for the named header with a replacement value
60 void setReplacement(const char *name, const char *replacementValue);
61
62 /// report the *_header_access part of the configuration
63 void dumpAccess(StoreEntry *entry, const char *optionName) const;
64 /// report the *_header_replace part of the configuration
65 void dumpReplacement(StoreEntry *entry, const char *optionName) const;
66
67private:
620bc212
AR
68 /// Case-insensitive std::string "less than" comparison functor.
69 /// Fast version recommended by Meyers' "Effective STL" for ASCII c-strings.
6e422d23
A
70 class NoCaseLessThan: public std::binary_function<std::string, std::string, bool>
71 {
620bc212
AR
72 public:
73 bool operator()(const std::string &lhs, const std::string &rhs) const {
74 return strcasecmp(lhs.c_str(), rhs.c_str()) < 0;
75 }
76 };
77
3b07476b 78 /// a name:mangler map; optimize: use unordered map or some such
620bc212 79 typedef std::map<std::string, headerMangler, NoCaseLessThan> ManglersByName;
3b07476b
CT
80
81 /// one mangler for each known header
81ab22b6 82 headerMangler known[static_cast<int>(Http::HdrType::enumEnd_)];
3b07476b
CT
83
84 /// one mangler for each custom header
85 ManglersByName custom;
86
87 /// configured if some mangling ACL applies to all header names
001d55dc 88 headerMangler all;
3b07476b
CT
89
90private:
91 /* not implemented */
92 HeaderManglers(const HeaderManglers &);
93 HeaderManglers &operator =(const HeaderManglers &);
94};
f4698e0b 95
f4698e0b
CT
96class HeaderWithAcl
97{
98public:
789217a2 99 HeaderWithAcl() : aclList(NULL), valueFormat(NULL), fieldId(Http::HdrType::BAD_HDR), quoted(false) {}
f4698e0b
CT
100
101 /// HTTP header field name
102 std::string fieldName;
103
104 /// HTTP header field value, possibly with macros
105 std::string fieldValue;
106
107 /// when the header field should be added (always if nil)
108 ACLList *aclList;
109
110 /// compiled HTTP header field value (no macros)
111 Format::Format *valueFormat;
112
113 /// internal ID for "known" headers or HDR_OTHER
789217a2 114 Http::HdrType fieldId;
f4698e0b
CT
115
116 /// whether fieldValue may contain macros
117 bool quoted;
118};
582c2af2 119
a1b9ec20
AR
120/// A strtoll(10) wrapper that checks for strtoll() failures and other problems.
121/// XXX: This function is not fully compatible with some HTTP syntax rules.
122/// Just like strtoll(), allows whitespace prefix, a sign, and _any_ suffix.
123/// Requires at least one digit to be present.
124/// Sets "off" and "end" arguments if and only if no problems were found.
125/// \return true if and only if no problems were found.
126bool httpHeaderParseOffset(const char *start, int64_t *offPtr, char **endPtr = nullptr);
fc54b8d2 127
d5f18517 128bool httpHeaderHasConnDir(const HttpHeader * hdr, const SBuf &directive);
001d55dc 129int httpHeaderParseInt(const char *start, int *val);
789217a2 130void httpHeaderPutStrf(HttpHeader * hdr, Http::HdrType id, const char *fmt,...) PRINTF_FORMAT_ARG3;
fc54b8d2 131
81858ebc 132const char *getStringPrefix(const char *str, size_t len);
1dff8715 133
cde8f31b 134void httpHdrMangleList(HttpHeader *, HttpRequest *, const AccessLogEntryPointer &al, req_or_rep_t req_or_rep);
f01162cf 135
3b07476b 136#endif
f53969cc 137