]> git.ipfire.org Git - thirdparty/squid.git/blame - src/HttpMsg.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / HttpMsg.h
CommitLineData
f1974911 1/*
262a0e14 2 * $Id$
f1974911 3 *
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
26ac0430 21 *
f1974911 22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26ac0430 26 *
f1974911 27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 */
32
33#ifndef SQUID_HTTPMSG_H
34#define SQUID_HTTPMSG_H
35
36#include "typedefs.h"
37#include "HttpHeader.h"
582c2af2 38#include "HttpRequestMethod.h"
6feb0e7c 39#include "HttpStatusCode.h"
f1974911 40#include "HttpVersion.h"
5f8252d2 41#include "BodyPipe.h"
f1974911 42
43// common parts of HttpRequest and HttpReply
44
db260341
AR
45template <class Msg>
46class HttpMsgPointerT;
47
f1974911 48class HttpMsg
49{
50
51public:
db260341
AR
52 typedef HttpMsgPointerT<HttpMsg> Pointer;
53
f1974911 54 HttpMsg(http_hdr_owner_type owner);
4a56ee8d 55 virtual ~HttpMsg();
f1974911 56
57 virtual void reset() = 0; // will have body when http*Clean()s are gone
58
f1974911 59 void packInto(Packer * p, bool full_uri) const;
60
6dd9f4bd 61 virtual HttpMsg *_lock(); // please use HTTPMSGLOCK()
62 virtual void _unlock(); // please use HTTPMSGUNLOCK()
4a56ee8d 63
26ac0430
AJ
64 ///< produce a message copy, except for a few connection-specific settings
65 virtual HttpMsg *clone() const = 0; ///< \todo rename: not a true copy?
fa0e6114 66
3ff65596
AR
67 /// [re]sets Content-Length header and cached value
68 void setContentLength(int64_t clen);
69
cb1f4d34 70 /**
4a1acc56
AJ
71 * \retval true the message sender asks to keep the connection open.
72 * \retval false the message sender will close the connection.
73 *
74 * Factors other than the headers may result in connection closure.
75 */
76 bool persistent() const;
77
f1974911 78public:
79 HttpVersion http_ver;
4a56ee8d 80
f1974911 81 HttpHeader header;
82
83 HttpHdrCc *cache_control;
84
85 /* Unsupported, writable, may disappear/change in the future
86 * For replies, sums _stored_ status-line, headers, and <CRLF>.
87 * Also used to report parsed header size if parse() is successful */
88 int hdr_sz;
89
47f6e231 90 int64_t content_length;
4a56ee8d 91
0c3d3f65 92 AnyP::ProtocolType protocol;
f1974911 93
e3ab4be3 94 HttpMsgParseState pstate; /* the current parsing state */
4a56ee8d 95
5f8252d2 96 BodyPipe::Pointer body_pipe; // optional pipeline to receive message body
97
e3ab4be3 98 // returns true and sets hdr_sz on success
99 // returns false and sets *error to zero when needs more data
100 // returns false and sets *error to a positive http_status code on error
f1974911 101 bool parse(MemBuf *buf, bool eol, http_status *error);
4a56ee8d 102
59eed7dc 103 bool parseCharBuf(const char *buf, ssize_t end);
4a56ee8d 104
666f514b 105 int httpMsgParseStep(const char *buf, int len, int atEnd);
4a56ee8d 106
fb525683 107 virtual int httpMsgParseError();
4a56ee8d 108
60745f24 109 virtual bool expectingBody(const HttpRequestMethod&, int64_t&) const = 0;
e3ab4be3 110
4a56ee8d 111 void firstLineBuf(MemBuf&);
26ac0430 112
d67acb4e 113 virtual bool inheritProperties(const HttpMsg *aMsg) = 0;
f1974911 114
115protected:
e1381638
AJ
116 /**
117 * Validate the message start line is syntactically correct.
118 * Set HTTP error status according to problems found.
119 *
120 * \retval true Status line has no serious problems.
121 * \retval false Status line has a serious problem. Correct response is indicated by error.
122 */
96ee497f 123 virtual bool sanityCheckStartLine(MemBuf *buf, const size_t hdr_len, http_status *error) = 0;
4a56ee8d 124
f1974911 125 virtual void packFirstLineInto(Packer * p, bool full_uri) const = 0;
4a56ee8d 126
429f7150 127 virtual bool parseFirstLine(const char *blk_start, const char *blk_end) = 0;
4a56ee8d 128
07947ad8 129 virtual void hdrCacheInit();
3cfc19b3 130
4a56ee8d 131 int lock_count;
3cfc19b3 132
f1974911 133};
134
666f514b 135SQUIDCEXTERN int httpMsgIsolateHeaders(const char **parse_start, int len, const char **blk_start, const char **blk_end);
f1974911 136
6dd9f4bd 137#define HTTPMSGUNLOCK(a) if(a){(a)->_unlock();(a)=NULL;}
138#define HTTPMSGLOCK(a) (a)->_lock()
139
66c38d9a
AR
140// TODO: replace HTTPMSGLOCK with general RefCounting and delete this class
141/// safe HttpMsg pointer wrapper that locks and unlocks the message
142template <class Msg>
143class HttpMsgPointerT
144{
145public:
146 HttpMsgPointerT(): msg(NULL) {}
147 explicit HttpMsgPointerT(Msg *m): msg(m) { lock(); }
148 virtual ~HttpMsgPointerT() { unlock(); }
149
150 HttpMsgPointerT(const HttpMsgPointerT &p): msg(p.msg) { lock(); }
151 HttpMsgPointerT &operator =(const HttpMsgPointerT &p)
a9d98b6f 152 { if (msg != p.msg) { unlock(); msg = p.msg; lock(); } return *this; }
db260341
AR
153 HttpMsgPointerT &operator =(Msg *newM)
154 { if (msg != newM) { unlock(); msg = newM; lock(); } return *this; }
155
156 /// support converting a child msg pointer into a parent msg pointer
157 template <typename Other>
158 HttpMsgPointerT(const HttpMsgPointerT<Other> &o): msg(o.raw()) { lock(); }
159
160 /// support assigning a child msg pointer to a parent msg pointer
161 template <typename Other>
162 HttpMsgPointerT &operator =(const HttpMsgPointerT<Other> &o)
163 { if (msg != o.raw()) { unlock(); msg = o.raw(); lock(); } return *this; }
66c38d9a
AR
164
165 Msg &operator *() { return *msg; }
166 const Msg &operator *() const { return *msg; }
167 Msg *operator ->() { return msg; }
168 const Msg *operator ->() const { return msg; }
44ab1ba2 169 operator Msg *() const { return msg; }
66c38d9a
AR
170 // add more as needed
171
db260341
AR
172 /// public access for HttpMsgPointerT copying and assignment; avoid
173 Msg *raw() const { return msg; }
174
a1083e70 175protected:
66c38d9a
AR
176 void lock() { if (msg) HTTPMSGLOCK(msg); } ///< prevent msg destruction
177 void unlock() { HTTPMSGUNLOCK(msg); } ///< allows/causes msg destruction
178
179private:
180 Msg *msg;
181};
182
183/// convenience wrapper to create HttpMsgPointerT<> object based on msg type
184template <class Msg>
185inline
186HttpMsgPointerT<Msg> HttpMsgPointer(Msg *msg)
187{
188 return HttpMsgPointerT<Msg>(msg);
189}
190
f1974911 191#endif /* SQUID_HTTPMSG_H */