4
// $Id: //poco/1.2/Net/src/HTTPSession.cpp#2 $
10
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
13
// Permission is hereby granted, free of charge, to any person or organization
14
// obtaining a copy of the software and accompanying documentation covered by
15
// this license (the "Software") to use, reproduce, display, distribute,
16
// execute, and transmit the Software, and to prepare derivative works of the
17
// Software, and to permit third-parties to whom the Software is furnished to
18
// do so, all subject to the following:
20
// The copyright notices in the Software and this entire statement, including
21
// the above license grant, this restriction and the following disclaimer,
22
// must be included in all copies of the Software, in whole or in part, and
23
// all derivative works of the Software, unless such copies or derivative
24
// works are solely in the form of machine-executable object code generated by
25
// a source language processor.
27
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33
// DEALINGS IN THE SOFTWARE.
37
#include "Poco/Net/HTTPSession.h"
38
#include "Poco/Net/HTTPBufferAllocator.h"
39
#include "Poco/Net/NetException.h"
43
using Poco::TimeoutException;
50
HTTPSession::HTTPSession():
55
_timeout(HTTP_DEFAULT_TIMEOUT),
61
HTTPSession::HTTPSession(const StreamSocket& socket):
67
_timeout(HTTP_DEFAULT_TIMEOUT),
73
HTTPSession::HTTPSession(const StreamSocket& socket, bool keepAlive):
78
_keepAlive(keepAlive),
79
_timeout(HTTP_DEFAULT_TIMEOUT),
85
HTTPSession::~HTTPSession()
87
if (_pBuffer) HTTPBufferAllocator::deallocate(_pBuffer, HTTPBufferAllocator::BUFFER_SIZE);
93
void HTTPSession::setKeepAlive(bool keepAlive)
95
_keepAlive = keepAlive;
99
void HTTPSession::setTimeout(const Poco::Timespan& timeout)
105
int HTTPSession::get()
107
if (_pCurrent == _pEnd)
110
if (_pCurrent < _pEnd)
113
return std::char_traits<char>::eof();
117
int HTTPSession::peek()
119
if (_pCurrent == _pEnd)
122
if (_pCurrent < _pEnd)
125
return std::char_traits<char>::eof();
129
int HTTPSession::read(char* buffer, std::streamsize length)
131
if (_pCurrent < _pEnd)
133
int n = (int) (_pEnd - _pCurrent);
134
if (n > length) n = (int) length;
135
memcpy(buffer, _pCurrent, n);
139
else return receive(buffer, (int) length);
143
int HTTPSession::write(const char* buffer, std::streamsize length)
147
return _socket.sendBytes(buffer, (int) length);
149
catch (Poco::Exception& exc)
157
int HTTPSession::receive(char* buffer, int length)
161
if (_socket.poll(_timeout, Socket::SELECT_READ))
162
return _socket.receiveBytes(buffer, length);
164
throw TimeoutException();
166
catch (Poco::Exception& exc)
174
void HTTPSession::refill()
178
_pBuffer = HTTPBufferAllocator::allocate(HTTPBufferAllocator::BUFFER_SIZE);
180
_pCurrent = _pEnd = _pBuffer;
181
int n = receive(_pBuffer, HTTPBufferAllocator::BUFFER_SIZE);
186
bool HTTPSession::connected() const
188
return _socket.impl()->initialized();
192
void HTTPSession::connect(const SocketAddress& address)
194
_socket.connect(address, _timeout);
195
_socket.setNoDelay(true);
199
void HTTPSession::abort()
206
void HTTPSession::close()
212
void HTTPSession::setException(const Poco::Exception& exc)
215
_pException = exc.clone();
219
} } // namespace Poco::Net