]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHeader.h
Convert packing methods from Packer* to Packable* objects
[thirdparty/squid.git] / src / HttpHeader.h
1 /*
2 * Copyright (C) 1996-2015 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_HTTPHEADER_H
10 #define SQUID_HTTPHEADER_H
11
12 #include "http/RegisteredHeaders.h"
13 /* because we pass a spec by value */
14 #include "HttpHeaderMask.h"
15 #include "mem/forward.h"
16 #include "SquidString.h"
17
18 #include <vector>
19
20 /* class forward declarations */
21 class HttpHdrCc;
22 class HttpHdrContRange;
23 class HttpHdrRange;
24 class HttpHdrSc;
25 class Packable;
26 class StoreEntry;
27 class SBuf;
28
29 /** possible types for http header fields */
30 typedef enum {
31 ftInvalid = HDR_ENUM_END, /**< to catch nasty errors with hdr_id<->fld_type clashes */
32 ftInt,
33 ftInt64,
34 ftStr,
35 ftDate_1123,
36 ftETag,
37 ftPCc,
38 ftPContRange,
39 ftPRange,
40 ftPSc,
41 ftDate_1123_or_ETag
42 } field_type;
43
44 /** Possible owners of http header */
45 typedef enum {
46 hoNone =0,
47 #if USE_HTCP
48 hoHtcpReply,
49 #endif
50 hoRequest,
51 hoReply,
52 #if USE_OPENSSL
53 hoErrorDetail,
54 #endif
55 hoEnd
56 } http_hdr_owner_type;
57
58 class HttpHeaderFieldAttrs
59 {
60 public:
61 HttpHeaderFieldAttrs() : name(NULL), id(HDR_BAD_HDR), type(ftInvalid) {}
62 HttpHeaderFieldAttrs(const char *aName, http_hdr_type anId, field_type aType = ftInvalid) : name(aName), id(anId), type(aType) {}
63 #if __cplusplus >= 201103L
64 HttpHeaderFieldAttrs(const HttpHeaderFieldAttrs &) = default;
65 HttpHeaderFieldAttrs(HttpHeaderFieldAttrs &&) = default;
66 #endif
67 // nothing to do as name is a pointer to global const string
68 ~HttpHeaderFieldAttrs() {}
69
70 const char *name;
71 http_hdr_type id;
72 field_type type;
73 };
74
75 /** Iteration for headers; use HttpHeaderPos as opaque type, do not interpret */
76 typedef ssize_t HttpHeaderPos;
77
78 /* use this and only this to initialize HttpHeaderPos */
79 #define HttpHeaderInitPos (-1)
80
81 class HttpHeaderEntry
82 {
83 MEMPROXY_CLASS(HttpHeaderEntry);
84
85 public:
86 HttpHeaderEntry(http_hdr_type id, const char *name, const char *value);
87 ~HttpHeaderEntry();
88 static HttpHeaderEntry *parse(const char *field_start, const char *field_end);
89 HttpHeaderEntry *clone() const;
90 void packInto(Packable *p) const;
91 int getInt() const;
92 int64_t getInt64() const;
93
94 http_hdr_type id;
95 String name;
96 String value;
97 };
98
99 class ETag;
100 class TimeOrTag;
101
102 class HttpHeader
103 {
104
105 public:
106 HttpHeader();
107 explicit HttpHeader(const http_hdr_owner_type owner);
108 HttpHeader(const HttpHeader &other);
109 ~HttpHeader();
110
111 HttpHeader &operator =(const HttpHeader &other);
112
113 /* Interface functions */
114 void clean();
115 void append(const HttpHeader * src);
116 void update (HttpHeader const *fresh, HttpHeaderMask const *denied_mask);
117 void compact();
118 int reset();
119 int parse(const char *header_start, size_t len);
120 void packInto(Packable * p, bool mask_sensitive_info=false) const;
121 HttpHeaderEntry *getEntry(HttpHeaderPos * pos) const;
122 HttpHeaderEntry *findEntry(http_hdr_type id) const;
123 int delByName(const char *name);
124 int delById(http_hdr_type id);
125 void delAt(HttpHeaderPos pos, int &headers_deleted);
126 void refreshMask();
127 void addEntry(HttpHeaderEntry * e);
128 void insertEntry(HttpHeaderEntry * e);
129 String getList(http_hdr_type id) const;
130 bool getList(http_hdr_type id, String *s) const;
131 String getStrOrList(http_hdr_type id) const;
132 String getByName(const char *name) const;
133 /// sets value and returns true iff a [possibly empty] named field is there
134 bool getByNameIfPresent(const char *name, String &value) const;
135 String getByNameListMember(const char *name, const char *member, const char separator) const;
136 String getListMember(http_hdr_type id, const char *member, const char separator) const;
137 int has(http_hdr_type id) const;
138 void putInt(http_hdr_type id, int number);
139 void putInt64(http_hdr_type id, int64_t number);
140 void putTime(http_hdr_type id, time_t htime);
141 void insertTime(http_hdr_type id, time_t htime);
142 void putStr(http_hdr_type id, const char *str);
143 void putAuth(const char *auth_scheme, const char *realm);
144 void putCc(const HttpHdrCc * cc);
145 void putContRange(const HttpHdrContRange * cr);
146 void putRange(const HttpHdrRange * range);
147 void putSc(HttpHdrSc *sc);
148 void putWarning(const int code, const char *const text); ///< add a Warning header
149 void putExt(const char *name, const char *value);
150 int getInt(http_hdr_type id) const;
151 int64_t getInt64(http_hdr_type id) const;
152 time_t getTime(http_hdr_type id) const;
153 const char *getStr(http_hdr_type id) const;
154 const char *getLastStr(http_hdr_type id) const;
155 HttpHdrCc *getCc() const;
156 HttpHdrRange *getRange() const;
157 HttpHdrSc *getSc() const;
158 HttpHdrContRange *getContRange() const;
159 const char *getAuth(http_hdr_type id, const char *auth_scheme) const;
160 ETag getETag(http_hdr_type id) const;
161 TimeOrTag getTimeOrTag(http_hdr_type id) const;
162 int hasListMember(http_hdr_type id, const char *member, const char separator) const;
163 int hasByNameListMember(const char *name, const char *member, const char separator) const;
164 void removeHopByHopEntries();
165 inline bool chunked() const; ///< whether message uses chunked Transfer-Encoding
166
167 /* protected, do not use these, use interface functions instead */
168 std::vector<HttpHeaderEntry *> entries; /**< parsed fields in raw format */
169 HttpHeaderMask mask; /**< bit set <=> entry present */
170 http_hdr_owner_type owner; /**< request or reply */
171 int len; /**< length when packed, not counting terminating null-byte */
172
173 protected:
174 /** \deprecated Public access replaced by removeHopByHopEntries() */
175 void removeConnectionHeaderEntries();
176
177 private:
178 HttpHeaderEntry *findLastEntry(http_hdr_type id) const;
179 };
180
181 int httpHeaderParseQuotedString(const char *start, const int len, String *val);
182
183 /// quotes string using RFC 7230 quoted-string rules
184 SBuf httpHeaderQuoteString(const char *raw);
185
186 int httpHeaderHasByNameListMember(const HttpHeader * hdr, const char *name, const char *member, const char separator);
187 void httpHeaderUpdate(HttpHeader * old, const HttpHeader * fresh, const HttpHeaderMask * denied_mask);
188 void httpHeaderCalcMask(HttpHeaderMask * mask, http_hdr_type http_hdr_type_enums[], size_t count);
189
190 inline bool
191 HttpHeader::chunked() const
192 {
193 return has(HDR_TRANSFER_ENCODING) &&
194 hasListMember(HDR_TRANSFER_ENCODING, "chunked", ',');
195 }
196
197 void httpHeaderInitModule(void);
198 void httpHeaderCleanModule(void);
199
200 #endif /* SQUID_HTTPHEADER_H */
201