2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_HTTPHEADER_H
10 #define SQUID_HTTPHEADER_H
12 #include "anyp/ProtocolVersion.h"
13 #include "base/LookupTable.h"
14 #include "http/RegisteredHeaders.h"
15 /* because we pass a spec by value */
16 #include "HttpHeaderMask.h"
17 #include "mem/PoolingAllocator.h"
18 #include "sbuf/forward.h"
19 #include "SquidString.h"
23 /* class forward declarations */
25 class HttpHdrContRange
;
30 /** Possible owners of http header */
42 } http_hdr_owner_type
;
44 /** Iteration for headers; use HttpHeaderPos as opaque type, do not interpret */
45 typedef ssize_t HttpHeaderPos
;
47 /* use this and only this to initialize HttpHeaderPos */
48 #define HttpHeaderInitPos (-1)
52 MEMPROXY_CLASS(HttpHeaderEntry
);
55 HttpHeaderEntry(Http::HdrType id
, const SBuf
&name
, const char *value
);
57 static HttpHeaderEntry
*parse(const char *field_start
, const char *field_end
, const http_hdr_owner_type msgType
);
58 HttpHeaderEntry
*clone() const;
59 void packInto(Packable
*p
) const;
61 int64_t getInt64() const;
76 explicit HttpHeader(const http_hdr_owner_type owner
);
77 HttpHeader(const HttpHeader
&other
);
80 HttpHeader
&operator =(const HttpHeader
&other
);
82 /* Interface functions */
84 void append(const HttpHeader
* src
);
85 /// replaces fields with matching names and adds fresh fields with new names
86 /// assuming `fresh` is a 304 reply
87 void update(const HttpHeader
*fresh
);
88 /// \returns whether calling update(fresh) would change our set of fields
89 bool needUpdate(const HttpHeader
*fresh
) const;
91 int parse(const char *header_start
, size_t len
, Http::ContentLengthInterpreter
&interpreter
);
92 /// Parses headers stored in a buffer.
93 /// \returns 1 and sets hdr_sz on success
94 /// \returns 0 when needs more data
95 /// \returns -1 on error
96 int parse(const char *buf
, size_t buf_len
, bool atEnd
, size_t &hdr_sz
, Http::ContentLengthInterpreter
&interpreter
);
97 void packInto(Packable
* p
, bool mask_sensitive_info
=false) const;
98 HttpHeaderEntry
*getEntry(HttpHeaderPos
* pos
) const;
99 HttpHeaderEntry
*findEntry(Http::HdrType id
) const;
100 /// deletes all fields with a given name, if any.
101 /// \return #fields deleted
102 int delByName(const SBuf
&name
);
103 /// \deprecated use SBuf method instead. performance regression: reallocates
104 int delByName(const char *name
) { return delByName(SBuf(name
)); }
105 int delById(Http::HdrType id
);
106 void delAt(HttpHeaderPos pos
, int &headers_deleted
);
108 void addEntry(HttpHeaderEntry
* e
);
109 void insertEntry(HttpHeaderEntry
* e
);
110 String
getList(Http::HdrType id
) const;
111 bool getList(Http::HdrType id
, String
*s
) const;
112 bool conflictingContentLength() const { return conflictingContentLength_
; }
113 String
getStrOrList(Http::HdrType id
) const;
114 String
getByName(const SBuf
&name
) const;
115 String
getByName(const char *name
) const;
116 String
getById(Http::HdrType id
) const;
117 /// returns true iff a [possibly empty] field identified by id is there
118 /// when returning true, also sets the `result` parameter (if it is not nil)
119 bool getByIdIfPresent(Http::HdrType id
, String
*result
) const;
120 /// returns true iff a [possibly empty] named field is there
121 /// when returning true, also sets the `value` parameter (if it is not nil)
122 bool hasNamed(const SBuf
&s
, String
*value
= nullptr) const;
123 /// \deprecated use SBuf method instead.
124 bool hasNamed(const char *name
, unsigned int namelen
, String
*value
= nullptr) const;
125 /// searches for the first matching key=value pair within the name-identified field
126 /// \returns the value of the found pair or an empty string
127 SBuf
getByNameListMember(const char *name
, const char *member
, const char separator
) const;
128 /// searches for the first matching key=value pair within the field
129 /// \returns the value of the found pair or an empty string
130 SBuf
getListMember(Http::HdrType id
, const char *member
, const char separator
) const;
131 int has(Http::HdrType id
) const;
132 /// Appends "this cache" information to VIA header field.
133 /// Takes the initial VIA value from "from" parameter, if provided.
134 void addVia(const AnyP::ProtocolVersion
&ver
, const HttpHeader
*from
= nullptr);
135 void putInt(Http::HdrType id
, int number
);
136 void putInt64(Http::HdrType id
, int64_t number
);
137 void putTime(Http::HdrType id
, time_t htime
);
138 void putStr(Http::HdrType id
, const char *str
);
139 void putAuth(const char *auth_scheme
, const char *realm
);
140 void putCc(const HttpHdrCc
* cc
);
141 void putContRange(const HttpHdrContRange
* cr
);
142 void putRange(const HttpHdrRange
* range
);
143 void putSc(HttpHdrSc
*sc
);
144 void putExt(const char *name
, const char *value
);
145 int getInt(Http::HdrType id
) const;
146 int64_t getInt64(Http::HdrType id
) const;
147 time_t getTime(Http::HdrType id
) const;
148 const char *getStr(Http::HdrType id
) const;
149 const char *getLastStr(Http::HdrType id
) const;
150 HttpHdrCc
*getCc() const;
151 HttpHdrRange
*getRange() const;
152 HttpHdrSc
*getSc() const;
153 HttpHdrContRange
*getContRange() const;
154 SBuf
getAuthToken(Http::HdrType id
, const char *auth_scheme
) const;
155 ETag
getETag(Http::HdrType id
) const;
156 TimeOrTag
getTimeOrTag(Http::HdrType id
) const;
157 int hasListMember(Http::HdrType id
, const char *member
, const char separator
) const;
158 int hasByNameListMember(const char *name
, const char *member
, const char separator
) const;
159 void removeHopByHopEntries();
161 /// whether the message uses chunked Transfer-Encoding
162 /// optimized implementation relies on us rejecting/removing other codings
163 bool chunked() const { return has(Http::HdrType::TRANSFER_ENCODING
); }
165 /// whether message used an unsupported and/or invalid Transfer-Encoding
166 bool unsupportedTe() const { return teUnsupported_
; }
168 /* protected, do not use these, use interface functions instead */
169 std::vector
<HttpHeaderEntry
*, PoolingAllocator
<HttpHeaderEntry
*> > entries
; /**< parsed fields in raw format */
170 HttpHeaderMask mask
; /**< bit set <=> entry present */
171 http_hdr_owner_type owner
; /**< request or reply */
172 int len
; /**< length when packed, not counting terminating null-byte */
175 /** \deprecated Public access replaced by removeHopByHopEntries() */
176 void removeConnectionHeaderEntries();
177 /// either finds the end of headers or returns false
178 /// If the end was found:
179 /// *parse_start points to the first character after the header delimiter
180 /// *blk_start points to the first header character (i.e. old parse_start value)
181 /// *blk_end points to the first header delimiter character (CR or LF in CR?LF).
182 /// If block starts where it ends, then there are no fields in the header.
183 static bool Isolate(const char **parse_start
, size_t l
, const char **blk_start
, const char **blk_end
);
184 bool skipUpdateHeader(const Http::HdrType id
) const;
187 HttpHeaderEntry
*findLastEntry(Http::HdrType id
) const;
188 bool conflictingContentLength_
; ///< found different Content-Length fields
189 /// unsupported encoding, unnecessary syntax characters, and/or
190 /// invalid field-value found in Transfer-Encoding header
191 bool teUnsupported_
= false;
194 int httpHeaderParseQuotedString(const char *start
, const int len
, String
*val
);
196 /// quotes string using RFC 7230 quoted-string rules
197 SBuf
httpHeaderQuoteString(const char *raw
);
199 void httpHeaderCalcMask(HttpHeaderMask
* mask
, Http::HdrType http_hdr_type_enums
[], size_t count
);
201 void httpHeaderInitModule(void);
203 #endif /* SQUID_HTTPHEADER_H */