]> git.ipfire.org Git - thirdparty/squid.git/blame - src/HttpHeader.h
Use RegisteredRunners for WCCP (de)activation (#2104)
[thirdparty/squid.git] / src / HttpHeader.h
CommitLineData
e6ccf245 1/*
1f7b830e 2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
e6ccf245 3 *
bbc27441
AJ
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.
e6ccf245 7 */
8
ff9d9458
FC
9#ifndef SQUID_SRC_HTTPHEADER_H
10#define SQUID_SRC_HTTPHEADER_H
e6ccf245 11
90be6ff5 12#include "anyp/ProtocolVersion.h"
383154d7 13#include "base/LookupTable.h"
1139d406 14#include "http/RegisteredHeaders.h"
528b2c61 15/* because we pass a spec by value */
25b6a907 16#include "HttpHeaderMask.h"
c3b51d64 17#include "mem/PoolingAllocator.h"
92e8f3ad 18#include "sbuf/forward.h"
71b673d4 19#include "SquidString.h"
43ca19e0 20
b7347197
FC
21#include <vector>
22
696a257c 23/* class forward declarations */
696a257c 24class HttpHdrCc;
71b673d4 25class HttpHdrContRange;
696a257c 26class HttpHdrRange;
71b673d4 27class HttpHdrSc;
17802cf1 28class Packable;
696a257c 29
63be0a78 30/** Possible owners of http header */
25b6a907 31typedef enum {
0b57cb3d 32 hoNone =0,
25b6a907 33#if USE_HTCP
34 hoHtcpReply,
35#endif
36 hoRequest,
02259ff8 37 hoReply,
cb4f4424 38#if USE_OPENSSL
02259ff8
CT
39 hoErrorDetail,
40#endif
41 hoEnd
25b6a907 42} http_hdr_owner_type;
43
63be0a78 44/** Iteration for headers; use HttpHeaderPos as opaque type, do not interpret */
985c86bc 45typedef ssize_t HttpHeaderPos;
46
47/* use this and only this to initialize HttpHeaderPos */
48#define HttpHeaderInitPos (-1)
49
eede25e7 50class HttpHeaderEntry
51{
741c2986 52 MEMPROXY_CLASS(HttpHeaderEntry);
eede25e7 53
54public:
d5f18517 55 HttpHeaderEntry(Http::HdrType id, const SBuf &name, const char *value);
eede25e7 56 ~HttpHeaderEntry();
2358b975 57 static HttpHeaderEntry *parse(const char *field_start, const char *field_end, const http_hdr_owner_type msgType);
eede25e7 58 HttpHeaderEntry *clone() const;
17802cf1 59 void packInto(Packable *p) const;
eede25e7 60 int getInt() const;
47f6e231 61 int64_t getInt64() const;
741c2986 62
fbc23dd6
EB
63 /// expected number of bytes written by packInto(), including ": " and CRLF
64 size_t length() const { return name.length() + 2 + value.size() + 2; }
65
789217a2 66 Http::HdrType id;
d5f18517 67 SBuf name;
30abd221 68 String value;
eede25e7 69};
70
81a94152
AJ
71class ETag;
72class TimeOrTag;
73
924f73bc 74class HttpHeader
75{
76
77public:
5e5fa5b1
AR
78 explicit HttpHeader(const http_hdr_owner_type owner);
79 HttpHeader(const HttpHeader &other);
75faaa7a 80 ~HttpHeader();
5e5fa5b1
AR
81
82 HttpHeader &operator =(const HttpHeader &other);
83
924f73bc 84 /* Interface functions */
519e0948 85 void clean();
a9925b40 86 void append(const HttpHeader * src);
66d51f4f 87 /// replaces fields with matching names and adds fresh fields with new names
0ac163be 88 /// assuming `fresh` is a 304 reply
66d51f4f
AR
89 void update(const HttpHeader *fresh);
90 /// \returns whether calling update(fresh) would change our set of fields
91 bool needUpdate(const HttpHeader *fresh) const;
394499bd 92 void compact();
4f1c93a7 93 int parse(const char *header_start, size_t len, Http::ContentLengthInterpreter &interpreter);
69c698a3
EB
94 /// Parses headers stored in a buffer.
95 /// \returns 1 and sets hdr_sz on success
96 /// \returns 0 when needs more data
97 /// \returns -1 on error
4f1c93a7 98 int parse(const char *buf, size_t buf_len, bool atEnd, size_t &hdr_sz, Http::ContentLengthInterpreter &interpreter);
17802cf1 99 void packInto(Packable * p, bool mask_sensitive_info=false) const;
a9925b40 100 HttpHeaderEntry *getEntry(HttpHeaderPos * pos) const;
789217a2 101 HttpHeaderEntry *findEntry(Http::HdrType id) const;
d5f18517
AJ
102 /// deletes all fields with a given name, if any.
103 /// \return #fields deleted
104 int delByName(const SBuf &name);
105 /// \deprecated use SBuf method instead. performance regression: reallocates
106 int delByName(const char *name) { return delByName(SBuf(name)); }
789217a2 107 int delById(Http::HdrType id);
ba9fb01d 108 void delAt(HttpHeaderPos pos, int &headers_deleted);
109 void refreshMask();
a9925b40 110 void addEntry(HttpHeaderEntry * e);
789217a2
FC
111 String getList(Http::HdrType id) const;
112 bool getList(Http::HdrType id, String *s) const;
3e42b356 113 bool conflictingContentLength() const { return conflictingContentLength_; }
789217a2 114 String getStrOrList(Http::HdrType id) const;
81ab22b6 115 String getByName(const SBuf &name) const;
30abd221 116 String getByName(const char *name) const;
81ab22b6 117 String getById(Http::HdrType id) const;
f29d429e
EB
118 /// returns true iff a [possibly empty] field identified by id is there
119 /// when returning true, also sets the `result` parameter (if it is not nil)
120 bool getByIdIfPresent(Http::HdrType id, String *result) const;
121 /// returns true iff a [possibly empty] named field is there
122 /// when returning true, also sets the `value` parameter (if it is not nil)
aee3523a 123 bool hasNamed(const SBuf &s, String *value = nullptr) const;
d5f18517 124 /// \deprecated use SBuf method instead.
aee3523a 125 bool hasNamed(const char *name, unsigned int namelen, String *value = nullptr) const;
36c774f7
EB
126 /// searches for the first matching key=value pair within the name-identified field
127 /// \returns the value of the found pair or an empty string
128 SBuf getByNameListMember(const char *name, const char *member, const char separator) const;
129 /// searches for the first matching key=value pair within the field
130 /// \returns the value of the found pair or an empty string
131 SBuf getListMember(Http::HdrType id, const char *member, const char separator) const;
789217a2 132 int has(Http::HdrType id) const;
90be6ff5
EB
133 /// Appends "this cache" information to VIA header field.
134 /// Takes the initial VIA value from "from" parameter, if provided.
aee3523a 135 void addVia(const AnyP::ProtocolVersion &ver, const HttpHeader *from = nullptr);
789217a2
FC
136 void putInt(Http::HdrType id, int number);
137 void putInt64(Http::HdrType id, int64_t number);
138 void putTime(Http::HdrType id, time_t htime);
789217a2 139 void putStr(Http::HdrType id, const char *str);
a9925b40 140 void putAuth(const char *auth_scheme, const char *realm);
182faab8 141 void putCc(const HttpHdrCc &cc);
a9925b40 142 void putContRange(const HttpHdrContRange * cr);
143 void putRange(const HttpHdrRange * range);
144 void putSc(HttpHdrSc *sc);
145 void putExt(const char *name, const char *value);
05ba40fc
EW
146
147 /// Ensures that the header has the given field, removing or replacing any
148 /// same-name fields with conflicting values as needed.
149 void updateOrAddStr(Http::HdrType, const SBuf &);
150
789217a2
FC
151 int getInt(Http::HdrType id) const;
152 int64_t getInt64(Http::HdrType id) const;
153 time_t getTime(Http::HdrType id) const;
154 const char *getStr(Http::HdrType id) const;
155 const char *getLastStr(Http::HdrType id) const;
a9925b40 156 HttpHdrCc *getCc() const;
157 HttpHdrRange *getRange() const;
158 HttpHdrSc *getSc() const;
159 HttpHdrContRange *getContRange() const;
2582f64a 160 SBuf getAuthToken(Http::HdrType id, const char *auth_scheme) const;
789217a2
FC
161 ETag getETag(Http::HdrType id) const;
162 TimeOrTag getTimeOrTag(Http::HdrType id) const;
163 int hasListMember(Http::HdrType id, const char *member, const char separator) const;
a9925b40 164 int hasByNameListMember(const char *name, const char *member, const char separator) const;
2cdeea82 165 void removeHopByHopEntries();
f6dd87e9
AJ
166
167 /// whether the message uses chunked Transfer-Encoding
168 /// optimized implementation relies on us rejecting/removing other codings
169 bool chunked() const { return has(Http::HdrType::TRANSFER_ENCODING); }
170
171 /// whether message used an unsupported and/or invalid Transfer-Encoding
172 bool unsupportedTe() const { return teUnsupported_; }
63be0a78 173
924f73bc 174 /* protected, do not use these, use interface functions instead */
c3b51d64 175 std::vector<HttpHeaderEntry*, PoolingAllocator<HttpHeaderEntry*> > entries; /**< parsed fields in raw format */
f53969cc
SM
176 HttpHeaderMask mask; /**< bit set <=> entry present */
177 http_hdr_owner_type owner; /**< request or reply */
178 int len; /**< length when packed, not counting terminating null-byte */
a9925b40 179
2cdeea82 180protected:
63be0a78 181 /** \deprecated Public access replaced by removeHopByHopEntries() */
2cdeea82 182 void removeConnectionHeaderEntries();
69c698a3
EB
183 /// either finds the end of headers or returns false
184 /// If the end was found:
185 /// *parse_start points to the first character after the header delimiter
186 /// *blk_start points to the first header character (i.e. old parse_start value)
187 /// *blk_end points to the first header delimiter character (CR or LF in CR?LF).
188 /// If block starts where it ends, then there are no fields in the header.
189 static bool Isolate(const char **parse_start, size_t l, const char **blk_start, const char **blk_end);
2d4f252d 190 bool skipUpdateHeader(const Http::HdrType id) const;
63be0a78 191
a9925b40 192private:
789217a2 193 HttpHeaderEntry *findLastEntry(Http::HdrType id) const;
3e42b356 194 bool conflictingContentLength_; ///< found different Content-Length fields
f6dd87e9
AJ
195 /// unsupported encoding, unnecessary syntax characters, and/or
196 /// invalid field-value found in Transfer-Encoding header
197 bool teUnsupported_ = false;
924f73bc 198};
528b2c61 199
8a648e8d 200int httpHeaderParseQuotedString(const char *start, const int len, String *val);
a2c7f09a 201
4e143970
FC
202namespace Http {
203
204/**
205 * Parses an HTTP quoted-string sequence (RFC 9110, Section 5.6.4).
206 *
207 * \param a brief human-friendly description of the string being parsed
208 * \param start input buffer (an opening double-quote is expected at *start)
209 * \param length is the number of characters in the given buffer
210 *
211 * \returns string contents with open/closing quotes stripped and any quoted-pairs decoded
212 *
213 * Avoid this slow function on performance-sensitive code paths.
214 * TODO: Replace with an efficient, SBuf-friendly implementation.
215 *
216 * \sa httpHeaderParseQuotedString() for a String-friendly function.
217 */
218SBuf SlowlyParseQuotedString(const char *description, const char *start, size_t length);
219
220}
221
e7ce227f
AR
222/// quotes string using RFC 7230 quoted-string rules
223SBuf httpHeaderQuoteString(const char *raw);
a2c7f09a 224
789217a2 225void httpHeaderCalcMask(HttpHeaderMask * mask, Http::HdrType http_hdr_type_enums[], size_t count);
25b6a907 226
8a648e8d 227void httpHeaderInitModule(void);
ec6f82c1 228
ff9d9458 229#endif /* SQUID_SRC_HTTPHEADER_H */
f53969cc 230