]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHeaderTools.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / HttpHeaderTools.h
1 /*
2 * Copyright (C) 1996-2014 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 #ifndef SQUID_HTTPHEADERTOOLS_H
10 #define SQUID_HTTPHEADERTOOLS_H
11
12 #include "acl/forward.h"
13 #include "format/Format.h"
14 #include "HttpHeader.h"
15 #include "typedefs.h"
16
17 #include <functional>
18 #include <list>
19 #include <map>
20 #include <string>
21 #if HAVE_STRINGS_H
22 #include <strings.h>
23 #endif
24
25 class HeaderWithAcl;
26 class HttpHeader;
27 class HttpHeaderFieldInfo;
28 class HttpRequest;
29 class StoreEntry;
30 class String;
31
32 typedef std::list<HeaderWithAcl> HeaderWithAclList;
33
34 // Currently a POD
35 class headerMangler
36 {
37 public:
38 acl_access *access_list;
39 char *replacement;
40 };
41
42 /// A collection of headerMangler objects for a given message kind.
43 class HeaderManglers
44 {
45 public:
46 HeaderManglers();
47 ~HeaderManglers();
48
49 /// returns a header mangler for field e or nil if none was specified
50 const headerMangler *find(const HttpHeaderEntry &e) const;
51
52 /// returns a mangler for the named header (known or custom)
53 headerMangler *track(const char *name);
54
55 /// updates mangler for the named header with a replacement value
56 void setReplacement(const char *name, const char *replacementValue);
57
58 /// report the *_header_access part of the configuration
59 void dumpAccess(StoreEntry *entry, const char *optionName) const;
60 /// report the *_header_replace part of the configuration
61 void dumpReplacement(StoreEntry *entry, const char *optionName) const;
62
63 private:
64 /// Case-insensitive std::string "less than" comparison functor.
65 /// Fast version recommended by Meyers' "Effective STL" for ASCII c-strings.
66 class NoCaseLessThan: public std::binary_function<std::string, std::string, bool>
67 {
68 public:
69 bool operator()(const std::string &lhs, const std::string &rhs) const {
70 return strcasecmp(lhs.c_str(), rhs.c_str()) < 0;
71 }
72 };
73
74 /// a name:mangler map; optimize: use unordered map or some such
75 typedef std::map<std::string, headerMangler, NoCaseLessThan> ManglersByName;
76
77 /// one mangler for each known header
78 headerMangler known[HDR_ENUM_END];
79
80 /// one mangler for each custom header
81 ManglersByName custom;
82
83 /// configured if some mangling ACL applies to all header names
84 headerMangler all;
85
86 private:
87 /* not implemented */
88 HeaderManglers(const HeaderManglers &);
89 HeaderManglers &operator =(const HeaderManglers &);
90 };
91
92 class HeaderWithAcl
93 {
94 public:
95 HeaderWithAcl() : aclList(NULL), valueFormat(NULL), fieldId(HDR_BAD_HDR), quoted(false) {}
96
97 /// HTTP header field name
98 std::string fieldName;
99
100 /// HTTP header field value, possibly with macros
101 std::string fieldValue;
102
103 /// when the header field should be added (always if nil)
104 ACLList *aclList;
105
106 /// compiled HTTP header field value (no macros)
107 Format::Format *valueFormat;
108
109 /// internal ID for "known" headers or HDR_OTHER
110 http_hdr_type fieldId;
111
112 /// whether fieldValue may contain macros
113 bool quoted;
114 };
115
116 int httpHeaderParseOffset(const char *start, int64_t * off);
117
118 HttpHeaderFieldInfo *httpHeaderBuildFieldsInfo(const HttpHeaderFieldAttrs * attrs, int count);
119 void httpHeaderDestroyFieldsInfo(HttpHeaderFieldInfo * info, int count);
120 http_hdr_type httpHeaderIdByName(const char *name, size_t name_len, const HttpHeaderFieldInfo * attrs, int end);
121 http_hdr_type httpHeaderIdByNameDef(const char *name, int name_len);
122 const char *httpHeaderNameById(int id);
123 int httpHeaderHasConnDir(const HttpHeader * hdr, const char *directive);
124 int httpHeaderParseInt(const char *start, int *val);
125 void httpHeaderPutStrf(HttpHeader * hdr, http_hdr_type id, const char *fmt,...) PRINTF_FORMAT_ARG3;
126
127 const char *getStringPrefix(const char *str, size_t len);
128
129 void httpHdrMangleList(HttpHeader *, HttpRequest *, int req_or_rep);
130
131 #endif
132