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