]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpMsg.h
Removed squid-old.h
[thirdparty/squid.git] / src / HttpMsg.h
1 /*
2 * $Id$
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.
21 *
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.
26 *
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"
38 #include "HttpRequestMethod.h"
39 #include "HttpStatusCode.h"
40 #include "HttpVersion.h"
41 #include "BodyPipe.h"
42
43 // common parts of HttpRequest and HttpReply
44
45 template <class Msg>
46 class HttpMsgPointerT;
47
48
49 class HttpMsg
50 {
51
52 public:
53 typedef HttpMsgPointerT<HttpMsg> Pointer;
54
55 HttpMsg(http_hdr_owner_type owner);
56 virtual ~HttpMsg();
57
58 virtual void reset() = 0; // will have body when http*Clean()s are gone
59
60 void packInto(Packer * p, bool full_uri) const;
61
62 virtual HttpMsg *_lock(); // please use HTTPMSGLOCK()
63 virtual void _unlock(); // please use HTTPMSGUNLOCK()
64
65 ///< produce a message copy, except for a few connection-specific settings
66 virtual HttpMsg *clone() const = 0; ///< \todo rename: not a true copy?
67
68 /// [re]sets Content-Length header and cached value
69 void setContentLength(int64_t clen);
70
71 /**
72 * \retval true the message sender asks to keep the connection open.
73 * \retval false the message sender will close the connection.
74 *
75 * Factors other than the headers may result in connection closure.
76 */
77 bool persistent() const;
78
79 public:
80 HttpVersion http_ver;
81
82 HttpHeader header;
83
84 HttpHdrCc *cache_control;
85
86 /* Unsupported, writable, may disappear/change in the future
87 * For replies, sums _stored_ status-line, headers, and <CRLF>.
88 * Also used to report parsed header size if parse() is successful */
89 int hdr_sz;
90
91 int64_t content_length;
92
93 AnyP::ProtocolType protocol;
94
95 HttpMsgParseState pstate; /* the current parsing state */
96
97 BodyPipe::Pointer body_pipe; // optional pipeline to receive message body
98
99 // returns true and sets hdr_sz on success
100 // returns false and sets *error to zero when needs more data
101 // returns false and sets *error to a positive http_status code on error
102 bool parse(MemBuf *buf, bool eol, http_status *error);
103
104 bool parseCharBuf(const char *buf, ssize_t end);
105
106 int httpMsgParseStep(const char *buf, int len, int atEnd);
107
108 virtual int httpMsgParseError();
109
110 virtual bool expectingBody(const HttpRequestMethod&, int64_t&) const = 0;
111
112 void firstLineBuf(MemBuf&);
113
114 virtual bool inheritProperties(const HttpMsg *aMsg) = 0;
115
116 protected:
117 /**
118 * Validate the message start line is syntactically correct.
119 * Set HTTP error status according to problems found.
120 *
121 * \retval true Status line has no serious problems.
122 * \retval false Status line has a serious problem. Correct response is indicated by error.
123 */
124 virtual bool sanityCheckStartLine(MemBuf *buf, const size_t hdr_len, http_status *error) = 0;
125
126 virtual void packFirstLineInto(Packer * p, bool full_uri) const = 0;
127
128 virtual bool parseFirstLine(const char *blk_start, const char *blk_end) = 0;
129
130 virtual void hdrCacheInit();
131
132 int lock_count;
133
134 };
135
136 SQUIDCEXTERN int httpMsgIsolateHeaders(const char **parse_start, int len, const char **blk_start, const char **blk_end);
137
138 #define HTTPMSGUNLOCK(a) if(a){(a)->_unlock();(a)=NULL;}
139 #define HTTPMSGLOCK(a) (a)->_lock()
140
141 // TODO: replace HTTPMSGLOCK with general RefCounting and delete this class
142 /// safe HttpMsg pointer wrapper that locks and unlocks the message
143 template <class Msg>
144 class HttpMsgPointerT
145 {
146 public:
147 HttpMsgPointerT(): msg(NULL) {}
148 explicit HttpMsgPointerT(Msg *m): msg(m) { lock(); }
149 virtual ~HttpMsgPointerT() { unlock(); }
150
151 HttpMsgPointerT(const HttpMsgPointerT &p): msg(p.msg) { lock(); }
152 HttpMsgPointerT &operator =(const HttpMsgPointerT &p)
153 { if (msg != p.msg) { unlock(); msg = p.msg; lock(); } return *this; }
154 HttpMsgPointerT &operator =(Msg *newM)
155 { if (msg != newM) { unlock(); msg = newM; lock(); } return *this; }
156
157 /// support converting a child msg pointer into a parent msg pointer
158 template <typename Other>
159 HttpMsgPointerT(const HttpMsgPointerT<Other> &o): msg(o.raw()) { lock(); }
160
161 /// support assigning a child msg pointer to a parent msg pointer
162 template <typename Other>
163 HttpMsgPointerT &operator =(const HttpMsgPointerT<Other> &o)
164 { if (msg != o.raw()) { unlock(); msg = o.raw(); lock(); } return *this; }
165
166 Msg &operator *() { return *msg; }
167 const Msg &operator *() const { return *msg; }
168 Msg *operator ->() { return msg; }
169 const Msg *operator ->() const { return msg; }
170 operator Msg *() const { return msg; }
171 // add more as needed
172
173 /// public access for HttpMsgPointerT copying and assignment; avoid
174 Msg *raw() const { return msg; }
175
176 protected:
177 void lock() { if (msg) HTTPMSGLOCK(msg); } ///< prevent msg destruction
178 void unlock() { HTTPMSGUNLOCK(msg); } ///< allows/causes msg destruction
179
180 private:
181 Msg *msg;
182 };
183
184 /// convenience wrapper to create HttpMsgPointerT<> object based on msg type
185 template <class Msg>
186 inline
187 HttpMsgPointerT<Msg> HttpMsgPointer(Msg *msg)
188 {
189 return HttpMsgPointerT<Msg>(msg);
190 }
191
192 #endif /* SQUID_HTTPMSG_H */