~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Net/src/HTTPSession.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// HTTPSession.cpp
 
3
//
 
4
// $Id: //poco/1.2/Net/src/HTTPSession.cpp#2 $
 
5
//
 
6
// Library: Net
 
7
// Package: HTTP
 
8
// Module:  HTTPSession
 
9
//
 
10
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
 
11
// and Contributors.
 
12
//
 
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:
 
19
// 
 
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.
 
26
// 
 
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.
 
34
//
 
35
 
 
36
 
 
37
#include "Poco/Net/HTTPSession.h"
 
38
#include "Poco/Net/HTTPBufferAllocator.h"
 
39
#include "Poco/Net/NetException.h"
 
40
#include <string.h>
 
41
 
 
42
 
 
43
using Poco::TimeoutException;
 
44
 
 
45
 
 
46
namespace Poco {
 
47
namespace Net {
 
48
 
 
49
 
 
50
HTTPSession::HTTPSession():
 
51
        _pBuffer(0),
 
52
        _pCurrent(0),
 
53
        _pEnd(0),
 
54
        _keepAlive(false),
 
55
        _timeout(HTTP_DEFAULT_TIMEOUT),
 
56
        _pException(0)
 
57
{
 
58
}
 
59
 
 
60
 
 
61
HTTPSession::HTTPSession(const StreamSocket& socket):
 
62
        _socket(socket),
 
63
        _pBuffer(0),
 
64
        _pCurrent(0),
 
65
        _pEnd(0),
 
66
        _keepAlive(false),
 
67
        _timeout(HTTP_DEFAULT_TIMEOUT),
 
68
        _pException(0)
 
69
{
 
70
}
 
71
 
 
72
 
 
73
HTTPSession::HTTPSession(const StreamSocket& socket, bool keepAlive):
 
74
        _socket(socket),
 
75
        _pBuffer(0),
 
76
        _pCurrent(0),
 
77
        _pEnd(0),
 
78
        _keepAlive(keepAlive),
 
79
        _timeout(HTTP_DEFAULT_TIMEOUT),
 
80
        _pException(0)
 
81
{
 
82
}
 
83
 
 
84
 
 
85
HTTPSession::~HTTPSession()
 
86
{
 
87
        if (_pBuffer) HTTPBufferAllocator::deallocate(_pBuffer, HTTPBufferAllocator::BUFFER_SIZE);
 
88
        close();
 
89
        delete _pException;
 
90
}
 
91
 
 
92
 
 
93
void HTTPSession::setKeepAlive(bool keepAlive)
 
94
{
 
95
        _keepAlive = keepAlive;
 
96
}
 
97
 
 
98
 
 
99
void HTTPSession::setTimeout(const Poco::Timespan& timeout)
 
100
{
 
101
        _timeout = timeout;
 
102
}
 
103
 
 
104
 
 
105
int HTTPSession::get()
 
106
{
 
107
        if (_pCurrent == _pEnd)
 
108
                refill();
 
109
        
 
110
        if (_pCurrent < _pEnd)
 
111
                return *_pCurrent++;
 
112
        else
 
113
                return std::char_traits<char>::eof();
 
114
}
 
115
 
 
116
        
 
117
int HTTPSession::peek()
 
118
{
 
119
        if (_pCurrent == _pEnd)
 
120
                refill();
 
121
 
 
122
        if (_pCurrent < _pEnd)
 
123
                return *_pCurrent;
 
124
        else
 
125
                return std::char_traits<char>::eof();
 
126
}
 
127
 
 
128
        
 
129
int HTTPSession::read(char* buffer, std::streamsize length)
 
130
{
 
131
        if (_pCurrent < _pEnd)
 
132
        {
 
133
                int n = (int) (_pEnd - _pCurrent);
 
134
                if (n > length) n = (int) length;
 
135
                memcpy(buffer, _pCurrent, n);
 
136
                _pCurrent += n;
 
137
                return n;
 
138
        }
 
139
        else return receive(buffer, (int) length);
 
140
}
 
141
 
 
142
 
 
143
int HTTPSession::write(const char* buffer, std::streamsize length)
 
144
{
 
145
        try
 
146
        {
 
147
                return _socket.sendBytes(buffer, (int) length);
 
148
        }
 
149
        catch (Poco::Exception& exc)
 
150
        {
 
151
                setException(exc);
 
152
                throw;
 
153
        }
 
154
}
 
155
 
 
156
 
 
157
int HTTPSession::receive(char* buffer, int length)
 
158
{
 
159
        try
 
160
        {
 
161
                if (_socket.poll(_timeout, Socket::SELECT_READ))
 
162
                        return _socket.receiveBytes(buffer, length);
 
163
                else
 
164
                        throw TimeoutException();
 
165
        }
 
166
        catch (Poco::Exception& exc)
 
167
        {
 
168
                setException(exc);
 
169
                throw;
 
170
        }
 
171
}
 
172
 
 
173
 
 
174
void HTTPSession::refill()
 
175
{
 
176
        if (!_pBuffer)
 
177
        {
 
178
                _pBuffer = HTTPBufferAllocator::allocate(HTTPBufferAllocator::BUFFER_SIZE);
 
179
        }
 
180
        _pCurrent = _pEnd = _pBuffer;
 
181
        int n = receive(_pBuffer, HTTPBufferAllocator::BUFFER_SIZE);
 
182
        _pEnd += n;
 
183
}
 
184
 
 
185
 
 
186
bool HTTPSession::connected() const
 
187
{
 
188
        return _socket.impl()->initialized();
 
189
}
 
190
 
 
191
 
 
192
void HTTPSession::connect(const SocketAddress& address)
 
193
{
 
194
        _socket.connect(address, _timeout);
 
195
        _socket.setNoDelay(true);
 
196
}
 
197
 
 
198
 
 
199
void HTTPSession::abort()
 
200
{
 
201
        _socket.shutdown();
 
202
        close();
 
203
}
 
204
 
 
205
 
 
206
void HTTPSession::close()
 
207
{
 
208
        _socket.close();
 
209
}
 
210
 
 
211
 
 
212
void HTTPSession::setException(const Poco::Exception& exc)
 
213
{
 
214
        delete _pException;
 
215
        _pException = exc.clone();
 
216
}
 
217
 
 
218
 
 
219
} } // namespace Poco::Net