1 | // |
---|
2 | // HTTPSession.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Net/include/Poco/Net/HTTPSession.h#5 $ |
---|
5 | // |
---|
6 | // Library: Net |
---|
7 | // Package: HTTP |
---|
8 | // Module: HTTPSession |
---|
9 | // |
---|
10 | // Definition of the HTTPSession class. |
---|
11 | // |
---|
12 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Net_HTTPSession_INCLUDED |
---|
40 | #define Net_HTTPSession_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Net/Net.h" |
---|
44 | #include "Poco/Net/StreamSocket.h" |
---|
45 | #include "Poco/Timespan.h" |
---|
46 | #include "Poco/Exception.h" |
---|
47 | #include <ios> |
---|
48 | |
---|
49 | |
---|
50 | namespace Poco { |
---|
51 | namespace Net { |
---|
52 | |
---|
53 | |
---|
54 | class Net_API HTTPSession |
---|
55 | /// HTTPSession implements basic HTTP session management |
---|
56 | /// for both HTTP clients and HTTP servers. |
---|
57 | /// |
---|
58 | /// HTTPSession implements buffering for HTTP connections, as well |
---|
59 | /// as specific support for the various HTTP stream classes. |
---|
60 | /// |
---|
61 | /// This class can not be instantiated. HTTPClientSession or |
---|
62 | /// HTTPServerSession must be used instead. |
---|
63 | { |
---|
64 | public: |
---|
65 | void setKeepAlive(bool keepAlive); |
---|
66 | /// Sets the keep-alive flag for this session. |
---|
67 | /// |
---|
68 | /// If the keep-alive flag is enabled, persistent |
---|
69 | /// HTTP/1.1 connections are supported. |
---|
70 | |
---|
71 | bool getKeepAlive() const; |
---|
72 | /// Returns the value of the keep-alive flag for |
---|
73 | /// this session. |
---|
74 | |
---|
75 | void setTimeout(const Poco::Timespan& timeout); |
---|
76 | /// Sets the timeout for the HTTP session. |
---|
77 | |
---|
78 | Poco::Timespan getTimeout() const; |
---|
79 | /// Returns the timeout for the HTTP session. |
---|
80 | |
---|
81 | bool connected() const; |
---|
82 | /// Returns true if the underlying socket is connected. |
---|
83 | |
---|
84 | void abort(); |
---|
85 | /// Aborts a session in progress by shutting down |
---|
86 | /// and closing the underlying socket. |
---|
87 | |
---|
88 | const Poco::Exception* networkException() const; |
---|
89 | /// If sending or receiving data over the underlying |
---|
90 | /// socket connection resulted in an exception, a |
---|
91 | /// pointer to this exception is returned. |
---|
92 | /// |
---|
93 | /// Otherwise, NULL is returned. |
---|
94 | |
---|
95 | enum |
---|
96 | { |
---|
97 | HTTP_PORT = 80 |
---|
98 | }; |
---|
99 | |
---|
100 | StreamSocket detachSocket(); |
---|
101 | /// Detaches the socket from the session. |
---|
102 | /// |
---|
103 | /// The socket is returned, and a new, uninitialized socket is |
---|
104 | /// attached to the session. |
---|
105 | |
---|
106 | protected: |
---|
107 | HTTPSession(); |
---|
108 | /// Creates a HTTP session using an |
---|
109 | /// unconnected stream socket. |
---|
110 | |
---|
111 | HTTPSession(const StreamSocket& socket); |
---|
112 | /// Creates a HTTP session using the |
---|
113 | /// given socket. The session takes ownership |
---|
114 | /// of the socket and closes it when it's no |
---|
115 | /// longer used. |
---|
116 | |
---|
117 | HTTPSession(const StreamSocket& socket, bool keepAlive); |
---|
118 | /// Creates a HTTP session using the |
---|
119 | /// given socket. The session takes ownership |
---|
120 | /// of the socket and closes it when it's no |
---|
121 | /// longer used. |
---|
122 | |
---|
123 | virtual ~HTTPSession(); |
---|
124 | /// Destroys the HTTPSession and closes the |
---|
125 | /// underlying socket. |
---|
126 | |
---|
127 | int get(); |
---|
128 | /// Returns the next byte in the buffer. |
---|
129 | /// Reads more data from the socket if there are |
---|
130 | /// no bytes left in the buffer. |
---|
131 | |
---|
132 | int peek(); |
---|
133 | /// Peeks at the next character in the buffer. |
---|
134 | /// Reads more data from the socket if there are |
---|
135 | /// no bytes left in the buffer. |
---|
136 | |
---|
137 | virtual int read(char* buffer, std::streamsize length); |
---|
138 | /// Reads up to length bytes. |
---|
139 | /// |
---|
140 | /// If there is data in the buffer, this data |
---|
141 | /// is returned. Otherwise, data is read from |
---|
142 | /// the socket to avoid unnecessary buffering. |
---|
143 | |
---|
144 | virtual int write(const char* buffer, std::streamsize length); |
---|
145 | /// Writes data to the socket. |
---|
146 | |
---|
147 | int receive(char* buffer, int length); |
---|
148 | /// Reads up to length bytes. |
---|
149 | |
---|
150 | int buffered() const; |
---|
151 | /// Returns the number of bytes in the buffer. |
---|
152 | |
---|
153 | StreamSocket& socket(); |
---|
154 | /// Returns a reference to the underlying socket. |
---|
155 | |
---|
156 | void refill(); |
---|
157 | /// Refills the internal buffer. |
---|
158 | |
---|
159 | virtual void connect(const SocketAddress& address); |
---|
160 | /// Connects the underlying socket to the given address |
---|
161 | /// and sets the socket's receive timeout. |
---|
162 | |
---|
163 | void attachSocket(const StreamSocket& socket); |
---|
164 | /// Attaches a socket to the session, replacing the |
---|
165 | /// previously attached socket. |
---|
166 | |
---|
167 | void close(); |
---|
168 | /// Closes the underlying socket. |
---|
169 | |
---|
170 | void setException(const Poco::Exception& exc); |
---|
171 | /// Stores a clone of the exception. |
---|
172 | |
---|
173 | private: |
---|
174 | enum |
---|
175 | { |
---|
176 | HTTP_DEFAULT_TIMEOUT = 60000000 |
---|
177 | }; |
---|
178 | |
---|
179 | HTTPSession(const HTTPSession&); |
---|
180 | HTTPSession& operator = (const HTTPSession&); |
---|
181 | |
---|
182 | StreamSocket _socket; |
---|
183 | char* _pBuffer; |
---|
184 | char* _pCurrent; |
---|
185 | char* _pEnd; |
---|
186 | bool _keepAlive; |
---|
187 | Poco::Timespan _timeout; |
---|
188 | Poco::Exception* _pException; |
---|
189 | |
---|
190 | friend class HTTPStreamBuf; |
---|
191 | friend class HTTPHeaderStreamBuf; |
---|
192 | friend class HTTPFixedLengthStreamBuf; |
---|
193 | friend class HTTPChunkedStreamBuf; |
---|
194 | }; |
---|
195 | |
---|
196 | |
---|
197 | // |
---|
198 | // inlines |
---|
199 | // |
---|
200 | inline bool HTTPSession::getKeepAlive() const |
---|
201 | { |
---|
202 | return _keepAlive; |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | inline Poco::Timespan HTTPSession::getTimeout() const |
---|
207 | { |
---|
208 | return _timeout; |
---|
209 | } |
---|
210 | |
---|
211 | |
---|
212 | inline StreamSocket& HTTPSession::socket() |
---|
213 | { |
---|
214 | return _socket; |
---|
215 | } |
---|
216 | |
---|
217 | |
---|
218 | inline const Poco::Exception* HTTPSession::networkException() const |
---|
219 | { |
---|
220 | return _pException; |
---|
221 | } |
---|
222 | |
---|
223 | |
---|
224 | inline int HTTPSession::buffered() const |
---|
225 | { |
---|
226 | return static_cast<int>(_pEnd - _pCurrent); |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | } } // namespace Poco::Net |
---|
231 | |
---|
232 | |
---|
233 | #endif // Net_HTTPSession_INCLUDED |
---|