2 * Copyright (C) 1996-2016 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 "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"
22 /* class forward declarations */
24 class HttpHdrContRange
;
29 /** Possible owners of http header */
41 } http_hdr_owner_type
;
43 /** Iteration for headers; use HttpHeaderPos as opaque type, do not interpret */
44 typedef ssize_t HttpHeaderPos
;
46 /* use this and only this to initialize HttpHeaderPos */
47 #define HttpHeaderInitPos (-1)
51 MEMPROXY_CLASS(HttpHeaderEntry
);
54 HttpHeaderEntry(Http::HdrType id
, const char *name
, const char *value
);
56 static HttpHeaderEntry
*parse(const char *field_start
, const char *field_end
);
57 HttpHeaderEntry
*clone() const;
58 void packInto(Packable
*p
) const;
60 int64_t getInt64() const;
75 explicit HttpHeader(const http_hdr_owner_type owner
);
76 HttpHeader(const HttpHeader
&other
);
79 HttpHeader
&operator =(const HttpHeader
&other
);
81 /* Interface functions */
83 void append(const HttpHeader
* src
);
84 void update (HttpHeader
const *fresh
, HttpHeaderMask
const *denied_mask
);
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
);
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
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 */
146 /** \deprecated Public access replaced by removeHopByHopEntries() */
147 void removeConnectionHeaderEntries();
150 HttpHeaderEntry
*findLastEntry(Http::HdrType id
) const;
151 bool conflictingContentLength_
; ///< found different Content-Length fields
154 int httpHeaderParseQuotedString(const char *start
, const int len
, String
*val
);
156 /// quotes string using RFC 7230 quoted-string rules
157 SBuf
httpHeaderQuoteString(const char *raw
);
159 void httpHeaderCalcMask(HttpHeaderMask
* mask
, Http::HdrType http_hdr_type_enums
[], size_t count
);
162 HttpHeader::chunked() const
164 return has(Http::HdrType::TRANSFER_ENCODING
) &&
165 hasListMember(Http::HdrType::TRANSFER_ENCODING
, "chunked", ',');
168 void httpHeaderInitModule(void);
170 #endif /* SQUID_HTTPHEADER_H */