2 * Copyright (C) 1996-2018 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
);
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 bool update(HttpHeader
const *fresh
);
87 int parse(const char *header_start
, size_t len
, Http::ContentLengthInterpreter
&interpreter
);
88 /// Parses headers stored in a buffer.
89 /// \returns 1 and sets hdr_sz on success
90 /// \returns 0 when needs more data
91 /// \returns -1 on error
92 int parse(const char *buf
, size_t buf_len
, bool atEnd
, size_t &hdr_sz
, Http::ContentLengthInterpreter
&interpreter
);
93 void packInto(Packable
* p
, bool mask_sensitive_info
=false) const;
94 HttpHeaderEntry
*getEntry(HttpHeaderPos
* pos
) const;
95 HttpHeaderEntry
*findEntry(Http::HdrType id
) const;
96 /// deletes all fields with a given name, if any.
97 /// \return #fields deleted
98 int delByName(const SBuf
&name
);
99 /// \deprecated use SBuf method instead. performance regression: reallocates
100 int delByName(const char *name
) { return delByName(SBuf(name
)); }
101 int delById(Http::HdrType id
);
102 void delAt(HttpHeaderPos pos
, int &headers_deleted
);
104 void addEntry(HttpHeaderEntry
* e
);
105 void insertEntry(HttpHeaderEntry
* e
);
106 String
getList(Http::HdrType id
) const;
107 bool getList(Http::HdrType id
, String
*s
) const;
108 bool conflictingContentLength() const { return conflictingContentLength_
; }
109 String
getStrOrList(Http::HdrType id
) const;
110 String
getByName(const SBuf
&name
) const;
111 String
getByName(const char *name
) const;
112 String
getById(Http::HdrType id
) const;
113 /// returns true iff a [possibly empty] field identified by id is there
114 /// when returning true, also sets the `result` parameter (if it is not nil)
115 bool getByIdIfPresent(Http::HdrType id
, String
*result
) const;
116 /// returns true iff a [possibly empty] named field is there
117 /// when returning true, also sets the `value` parameter (if it is not nil)
118 bool hasNamed(const SBuf
&s
, String
*value
= 0) const;
119 /// \deprecated use SBuf method instead.
120 bool hasNamed(const char *name
, unsigned int namelen
, String
*value
= 0) const;
121 String
getByNameListMember(const char *name
, const char *member
, const char separator
) const;
122 String
getListMember(Http::HdrType id
, const char *member
, const char separator
) const;
123 int has(Http::HdrType id
) const;
124 /// Appends "this cache" information to VIA header field.
125 /// Takes the initial VIA value from "from" parameter, if provided.
126 void addVia(const AnyP::ProtocolVersion
&ver
, const HttpHeader
*from
= 0);
127 void putInt(Http::HdrType id
, int number
);
128 void putInt64(Http::HdrType id
, int64_t number
);
129 void putTime(Http::HdrType id
, time_t htime
);
130 void putStr(Http::HdrType id
, const char *str
);
131 void putAuth(const char *auth_scheme
, const char *realm
);
132 void putCc(const HttpHdrCc
* cc
);
133 void putContRange(const HttpHdrContRange
* cr
);
134 void putRange(const HttpHdrRange
* range
);
135 void putSc(HttpHdrSc
*sc
);
136 void putWarning(const int code
, const char *const text
); ///< add a Warning header
137 void putExt(const char *name
, const char *value
);
138 int getInt(Http::HdrType id
) const;
139 int64_t getInt64(Http::HdrType id
) const;
140 time_t getTime(Http::HdrType id
) const;
141 const char *getStr(Http::HdrType id
) const;
142 const char *getLastStr(Http::HdrType id
) const;
143 HttpHdrCc
*getCc() const;
144 HttpHdrRange
*getRange() const;
145 HttpHdrSc
*getSc() const;
146 HttpHdrContRange
*getContRange() const;
147 const char *getAuth(Http::HdrType id
, const char *auth_scheme
) const;
148 ETag
getETag(Http::HdrType id
) const;
149 TimeOrTag
getTimeOrTag(Http::HdrType id
) const;
150 int hasListMember(Http::HdrType id
, const char *member
, const char separator
) const;
151 int hasByNameListMember(const char *name
, const char *member
, const char separator
) const;
152 void removeHopByHopEntries();
153 inline bool chunked() const; ///< whether message uses chunked Transfer-Encoding
155 /* protected, do not use these, use interface functions instead */
156 std::vector
<HttpHeaderEntry
*, PoolingAllocator
<HttpHeaderEntry
*> > entries
; /**< parsed fields in raw format */
157 HttpHeaderMask mask
; /**< bit set <=> entry present */
158 http_hdr_owner_type owner
; /**< request or reply */
159 int len
; /**< length when packed, not counting terminating null-byte */
162 /** \deprecated Public access replaced by removeHopByHopEntries() */
163 void removeConnectionHeaderEntries();
164 /// either finds the end of headers or returns false
165 /// If the end was found:
166 /// *parse_start points to the first character after the header delimiter
167 /// *blk_start points to the first header character (i.e. old parse_start value)
168 /// *blk_end points to the first header delimiter character (CR or LF in CR?LF).
169 /// If block starts where it ends, then there are no fields in the header.
170 static bool Isolate(const char **parse_start
, size_t l
, const char **blk_start
, const char **blk_end
);
171 bool needUpdate(const HttpHeader
*fresh
) const;
172 bool skipUpdateHeader(const Http::HdrType id
) const;
173 void updateWarnings();
176 HttpHeaderEntry
*findLastEntry(Http::HdrType id
) const;
177 bool conflictingContentLength_
; ///< found different Content-Length fields
180 int httpHeaderParseQuotedString(const char *start
, const int len
, String
*val
);
182 /// quotes string using RFC 7230 quoted-string rules
183 SBuf
httpHeaderQuoteString(const char *raw
);
185 void httpHeaderCalcMask(HttpHeaderMask
* mask
, Http::HdrType http_hdr_type_enums
[], size_t count
);
188 HttpHeader::chunked() const
190 return has(Http::HdrType::TRANSFER_ENCODING
) &&
191 hasListMember(Http::HdrType::TRANSFER_ENCODING
, "chunked", ',');
194 void httpHeaderInitModule(void);
196 #endif /* SQUID_HTTPHEADER_H */