]> git.ipfire.org Git - thirdparty/squid.git/blame - src/HttpHeader.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / HttpHeader.h
CommitLineData
e6ccf245 1/*
4ac4a490 2 * Copyright (C) 1996-2017 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
9#ifndef SQUID_HTTPHEADER_H
10#define SQUID_HTTPHEADER_H
11
383154d7 12#include "base/LookupTable.h"
1139d406 13#include "http/RegisteredHeaders.h"
528b2c61 14/* because we pass a spec by value */
25b6a907 15#include "HttpHeaderMask.h"
ed6e9fb9 16#include "mem/forward.h"
92e8f3ad 17#include "sbuf/forward.h"
71b673d4 18#include "SquidString.h"
43ca19e0 19
b7347197
FC
20#include <vector>
21
696a257c 22/* class forward declarations */
696a257c 23class HttpHdrCc;
71b673d4 24class HttpHdrContRange;
696a257c 25class HttpHdrRange;
71b673d4 26class HttpHdrSc;
17802cf1 27class Packable;
696a257c 28
63be0a78 29/** Possible owners of http header */
25b6a907 30typedef enum {
0b57cb3d 31 hoNone =0,
25b6a907 32#if USE_HTCP
33 hoHtcpReply,
34#endif
35 hoRequest,
02259ff8 36 hoReply,
cb4f4424 37#if USE_OPENSSL
02259ff8
CT
38 hoErrorDetail,
39#endif
40 hoEnd
25b6a907 41} http_hdr_owner_type;
42
63be0a78 43/** Iteration for headers; use HttpHeaderPos as opaque type, do not interpret */
985c86bc 44typedef ssize_t HttpHeaderPos;
45
46/* use this and only this to initialize HttpHeaderPos */
47#define HttpHeaderInitPos (-1)
48
eede25e7 49class HttpHeaderEntry
50{
741c2986 51 MEMPROXY_CLASS(HttpHeaderEntry);
eede25e7 52
53public:
789217a2 54 HttpHeaderEntry(Http::HdrType id, const char *name, const char *value);
eede25e7 55 ~HttpHeaderEntry();
cdce6c61 56 static HttpHeaderEntry *parse(const char *field_start, const char *field_end);
eede25e7 57 HttpHeaderEntry *clone() const;
17802cf1 58 void packInto(Packable *p) const;
eede25e7 59 int getInt() const;
47f6e231 60 int64_t getInt64() const;
741c2986 61
789217a2 62 Http::HdrType id;
30abd221 63 String name;
64 String value;
eede25e7 65};
66
81a94152
AJ
67class ETag;
68class TimeOrTag;
69
924f73bc 70class HttpHeader
71{
72
73public:
75faaa7a 74 HttpHeader();
5e5fa5b1
AR
75 explicit HttpHeader(const http_hdr_owner_type owner);
76 HttpHeader(const HttpHeader &other);
75faaa7a 77 ~HttpHeader();
5e5fa5b1
AR
78
79 HttpHeader &operator =(const HttpHeader &other);
80
924f73bc 81 /* Interface functions */
519e0948 82 void clean();
a9925b40 83 void append(const HttpHeader * src);
1a210de4 84 bool update(HttpHeader const *fresh);
394499bd 85 void compact();
784619e6 86 int parse(const char *header_start, size_t len);
69c698a3
EB
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);
17802cf1 92 void packInto(Packable * p, bool mask_sensitive_info=false) const;
a9925b40 93 HttpHeaderEntry *getEntry(HttpHeaderPos * pos) const;
789217a2 94 HttpHeaderEntry *findEntry(Http::HdrType id) const;
a9925b40 95 int delByName(const char *name);
789217a2 96 int delById(Http::HdrType id);
ba9fb01d 97 void delAt(HttpHeaderPos pos, int &headers_deleted);
98 void refreshMask();
a9925b40 99 void addEntry(HttpHeaderEntry * e);
100 void insertEntry(HttpHeaderEntry * e);
789217a2
FC
101 String getList(Http::HdrType id) const;
102 bool getList(Http::HdrType id, String *s) const;
3e42b356 103 bool conflictingContentLength() const { return conflictingContentLength_; }
789217a2 104 String getStrOrList(Http::HdrType id) const;
81ab22b6 105 String getByName(const SBuf &name) const;
30abd221 106 String getByName(const char *name) const;
81ab22b6
FC
107 String getById(Http::HdrType id) const;
108 /// sets value and returns true iff a [possibly empty] field identified by id is there
109 bool getByIdIfPresent(Http::HdrType id, String &result) const;
b2c44718 110 /// sets value and returns true iff a [possibly empty] named field is there
81ab22b6
FC
111 bool getByNameIfPresent(const SBuf &s, String &value) const;
112 bool getByNameIfPresent(const char *name, int namelen, String &value) const;
30abd221 113 String getByNameListMember(const char *name, const char *member, const char separator) const;
789217a2
FC
114 String getListMember(Http::HdrType id, const char *member, const char separator) const;
115 int has(Http::HdrType id) const;
116 void putInt(Http::HdrType id, int number);
117 void putInt64(Http::HdrType id, int64_t number);
118 void putTime(Http::HdrType id, time_t htime);
789217a2 119 void putStr(Http::HdrType id, const char *str);
a9925b40 120 void putAuth(const char *auth_scheme, const char *realm);
121 void putCc(const HttpHdrCc * cc);
122 void putContRange(const HttpHdrContRange * cr);
123 void putRange(const HttpHdrRange * range);
124 void putSc(HttpHdrSc *sc);
bcfba8bd 125 void putWarning(const int code, const char *const text); ///< add a Warning header
a9925b40 126 void putExt(const char *name, const char *value);
789217a2
FC
127 int getInt(Http::HdrType id) const;
128 int64_t getInt64(Http::HdrType id) const;
129 time_t getTime(Http::HdrType id) const;
130 const char *getStr(Http::HdrType id) const;
131 const char *getLastStr(Http::HdrType id) const;
a9925b40 132 HttpHdrCc *getCc() const;
133 HttpHdrRange *getRange() const;
134 HttpHdrSc *getSc() const;
135 HttpHdrContRange *getContRange() const;
789217a2
FC
136 const char *getAuth(Http::HdrType id, const char *auth_scheme) const;
137 ETag getETag(Http::HdrType id) const;
138 TimeOrTag getTimeOrTag(Http::HdrType id) const;
139 int hasListMember(Http::HdrType id, const char *member, const char separator) const;
a9925b40 140 int hasByNameListMember(const char *name, const char *member, const char separator) const;
2cdeea82 141 void removeHopByHopEntries();
c3d0ba0c 142 inline bool chunked() const; ///< whether message uses chunked Transfer-Encoding
63be0a78 143
924f73bc 144 /* protected, do not use these, use interface functions instead */
f53969cc
SM
145 std::vector<HttpHeaderEntry *> entries; /**< parsed fields in raw format */
146 HttpHeaderMask mask; /**< bit set <=> entry present */
147 http_hdr_owner_type owner; /**< request or reply */
148 int len; /**< length when packed, not counting terminating null-byte */
a9925b40 149
2cdeea82 150protected:
63be0a78 151 /** \deprecated Public access replaced by removeHopByHopEntries() */
2cdeea82 152 void removeConnectionHeaderEntries();
69c698a3
EB
153 /// either finds the end of headers or returns false
154 /// If the end was found:
155 /// *parse_start points to the first character after the header delimiter
156 /// *blk_start points to the first header character (i.e. old parse_start value)
157 /// *blk_end points to the first header delimiter character (CR or LF in CR?LF).
158 /// If block starts where it ends, then there are no fields in the header.
159 static bool Isolate(const char **parse_start, size_t l, const char **blk_start, const char **blk_end);
1a210de4 160 bool needUpdate(const HttpHeader *fresh) const;
2d4f252d
EB
161 bool skipUpdateHeader(const Http::HdrType id) const;
162 void updateWarnings();
63be0a78 163
a9925b40 164private:
789217a2 165 HttpHeaderEntry *findLastEntry(Http::HdrType id) const;
3e42b356 166 bool conflictingContentLength_; ///< found different Content-Length fields
924f73bc 167};
528b2c61 168
8a648e8d 169int httpHeaderParseQuotedString(const char *start, const int len, String *val);
a2c7f09a 170
e7ce227f
AR
171/// quotes string using RFC 7230 quoted-string rules
172SBuf httpHeaderQuoteString(const char *raw);
a2c7f09a 173
789217a2 174void httpHeaderCalcMask(HttpHeaderMask * mask, Http::HdrType http_hdr_type_enums[], size_t count);
25b6a907 175
c3d0ba0c
AR
176inline bool
177HttpHeader::chunked() const
178{
789217a2
FC
179 return has(Http::HdrType::TRANSFER_ENCODING) &&
180 hasListMember(Http::HdrType::TRANSFER_ENCODING, "chunked", ',');
c3d0ba0c
AR
181}
182
8a648e8d 183void httpHeaderInitModule(void);
ec6f82c1 184
e6ccf245 185#endif /* SQUID_HTTPHEADER_H */
f53969cc 186