]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHeader.h
Removed httpHeaderIdByNameDef, small cleanups
[thirdparty/squid.git] / src / HttpHeader.h
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9 #ifndef SQUID_HTTPHEADER_H
10 #define SQUID_HTTPHEADER_H
11
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 "SquidString.h"
18
19 #include <vector>
20
21 /* class forward declarations */
22 class HttpHdrCc;
23 class HttpHdrContRange;
24 class HttpHdrRange;
25 class HttpHdrSc;
26 class Packable;
27 class SBuf;
28
29 /** Possible owners of http header */
30 typedef enum {
31 hoNone =0,
32 #if USE_HTCP
33 hoHtcpReply,
34 #endif
35 hoRequest,
36 hoReply,
37 #if USE_OPENSSL
38 hoErrorDetail,
39 #endif
40 hoEnd
41 } http_hdr_owner_type;
42
43 /** Iteration for headers; use HttpHeaderPos as opaque type, do not interpret */
44 typedef ssize_t HttpHeaderPos;
45
46 /* use this and only this to initialize HttpHeaderPos */
47 #define HttpHeaderInitPos (-1)
48
49 class HttpHeaderEntry
50 {
51 MEMPROXY_CLASS(HttpHeaderEntry);
52
53 public:
54 HttpHeaderEntry(http_hdr_type id, const char *name, const char *value);
55 ~HttpHeaderEntry();
56 static HttpHeaderEntry *parse(const char *field_start, const char *field_end);
57 HttpHeaderEntry *clone() const;
58 void packInto(Packable *p) const;
59 int getInt() const;
60 int64_t getInt64() const;
61
62 http_hdr_type id;
63 String name;
64 String value;
65 };
66
67 class ETag;
68 class TimeOrTag;
69
70 class HttpHeader
71 {
72
73 public:
74 HttpHeader();
75 explicit HttpHeader(const http_hdr_owner_type owner);
76 HttpHeader(const HttpHeader &other);
77 ~HttpHeader();
78
79 HttpHeader &operator =(const HttpHeader &other);
80
81 /* Interface functions */
82 void clean();
83 void append(const HttpHeader * src);
84 void update (HttpHeader const *fresh, HttpHeaderMask const *denied_mask);
85 void compact();
86 int reset();
87 int parse(const char *header_start, size_t len);
88 void packInto(Packable * p, bool mask_sensitive_info=false) const;
89 HttpHeaderEntry *getEntry(HttpHeaderPos * pos) const;
90 HttpHeaderEntry *findEntry(http_hdr_type id) const;
91 int delByName(const char *name);
92 int delById(http_hdr_type id);
93 void delAt(HttpHeaderPos pos, int &headers_deleted);
94 void refreshMask();
95 void addEntry(HttpHeaderEntry * e);
96 void insertEntry(HttpHeaderEntry * e);
97 String getList(http_hdr_type id) const;
98 bool getList(http_hdr_type id, String *s) const;
99 String getStrOrList(http_hdr_type id) const;
100 String getByName(const char *name) const;
101 /// sets value and returns true iff a [possibly empty] named field is there
102 bool getByNameIfPresent(const char *name, String &value) const;
103 String getByNameListMember(const char *name, const char *member, const char separator) const;
104 String getListMember(http_hdr_type id, const char *member, const char separator) const;
105 int has(http_hdr_type id) const;
106 void putInt(http_hdr_type id, int number);
107 void putInt64(http_hdr_type id, int64_t number);
108 void putTime(http_hdr_type id, time_t htime);
109 void insertTime(http_hdr_type id, time_t htime);
110 void putStr(http_hdr_type id, const char *str);
111 void putAuth(const char *auth_scheme, const char *realm);
112 void putCc(const HttpHdrCc * cc);
113 void putContRange(const HttpHdrContRange * cr);
114 void putRange(const HttpHdrRange * range);
115 void putSc(HttpHdrSc *sc);
116 void putWarning(const int code, const char *const text); ///< add a Warning header
117 void putExt(const char *name, const char *value);
118 int getInt(http_hdr_type id) const;
119 int64_t getInt64(http_hdr_type id) const;
120 time_t getTime(http_hdr_type id) const;
121 const char *getStr(http_hdr_type id) const;
122 const char *getLastStr(http_hdr_type id) const;
123 HttpHdrCc *getCc() const;
124 HttpHdrRange *getRange() const;
125 HttpHdrSc *getSc() const;
126 HttpHdrContRange *getContRange() const;
127 const char *getAuth(http_hdr_type id, const char *auth_scheme) const;
128 ETag getETag(http_hdr_type id) const;
129 TimeOrTag getTimeOrTag(http_hdr_type id) const;
130 int hasListMember(http_hdr_type id, const char *member, const char separator) const;
131 int hasByNameListMember(const char *name, const char *member, const char separator) const;
132 void removeHopByHopEntries();
133 inline bool chunked() const; ///< whether message uses chunked Transfer-Encoding
134
135 /* protected, do not use these, use interface functions instead */
136 std::vector<HttpHeaderEntry *> entries; /**< parsed fields in raw format */
137 HttpHeaderMask mask; /**< bit set <=> entry present */
138 http_hdr_owner_type owner; /**< request or reply */
139 int len; /**< length when packed, not counting terminating null-byte */
140
141 protected:
142 /** \deprecated Public access replaced by removeHopByHopEntries() */
143 void removeConnectionHeaderEntries();
144
145 private:
146 HttpHeaderEntry *findLastEntry(http_hdr_type id) const;
147 };
148
149 int httpHeaderParseQuotedString(const char *start, const int len, String *val);
150
151 /// quotes string using RFC 7230 quoted-string rules
152 SBuf httpHeaderQuoteString(const char *raw);
153
154 int httpHeaderHasByNameListMember(const HttpHeader * hdr, const char *name, const char *member, const char separator);
155 void httpHeaderUpdate(HttpHeader * old, const HttpHeader * fresh, const HttpHeaderMask * denied_mask);
156 void httpHeaderCalcMask(HttpHeaderMask * mask, http_hdr_type http_hdr_type_enums[], size_t count);
157
158 inline bool
159 HttpHeader::chunked() const
160 {
161 return has(HDR_TRANSFER_ENCODING) &&
162 hasListMember(HDR_TRANSFER_ENCODING, "chunked", ',');
163 }
164
165 void httpHeaderInitModule(void);
166 void httpHeaderCleanModule(void);
167
168 // for header string->id lookup, use headerLookupTable.lookup(hdr-as-sbuf);
169 extern const LookupTable<http_hdr_type, HeaderTableRecord> headerLookupTable;
170
171 #endif /* SQUID_HTTPHEADER_H */
172