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