2 * Copyright (C) 1996-2015 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 "http/RegisteredHeaders.h"
13 /* because we pass a spec by value */
14 #include "HttpHeaderMask.h"
15 #include "mem/forward.h"
16 #include "SquidString.h"
20 /* class forward declarations */
22 class HttpHdrContRange
;
29 /** possible types for http header fields */
31 ftInvalid
= HDR_ENUM_END
, /**< to catch nasty errors with hdr_id<->fld_type clashes */
44 /** Possible owners of http header */
56 } http_hdr_owner_type
;
58 class HttpHeaderFieldAttrs
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;
67 // nothing to do as name is a pointer to global const string
68 ~HttpHeaderFieldAttrs() {}
75 /** Iteration for headers; use HttpHeaderPos as opaque type, do not interpret */
76 typedef ssize_t HttpHeaderPos
;
78 /* use this and only this to initialize HttpHeaderPos */
79 #define HttpHeaderInitPos (-1)
83 MEMPROXY_CLASS(HttpHeaderEntry
);
86 HttpHeaderEntry(http_hdr_type id
, const char *name
, const char *value
);
88 static HttpHeaderEntry
*parse(const char *field_start
, const char *field_end
);
89 HttpHeaderEntry
*clone() const;
90 void packInto(Packable
*p
) const;
92 int64_t getInt64() const;
107 explicit HttpHeader(const http_hdr_owner_type owner
);
108 HttpHeader(const HttpHeader
&other
);
111 HttpHeader
&operator =(const HttpHeader
&other
);
113 /* Interface functions */
115 void append(const HttpHeader
* src
);
116 void update (HttpHeader
const *fresh
, HttpHeaderMask
const *denied_mask
);
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
);
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
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 */
174 /** \deprecated Public access replaced by removeHopByHopEntries() */
175 void removeConnectionHeaderEntries();
178 HttpHeaderEntry
*findLastEntry(http_hdr_type id
) const;
181 int httpHeaderParseQuotedString(const char *start
, const int len
, String
*val
);
183 /// quotes string using RFC 7230 quoted-string rules
184 SBuf
httpHeaderQuoteString(const char *raw
);
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
);
191 HttpHeader::chunked() const
193 return has(HDR_TRANSFER_ENCODING
) &&
194 hasListMember(HDR_TRANSFER_ENCODING
, "chunked", ',');
197 void httpHeaderInitModule(void);
198 void httpHeaderCleanModule(void);
200 #endif /* SQUID_HTTPHEADER_H */