1//
2// HTTPStream.cpp
3//
4// Library: Net
5// Package: HTTP
6// Module: HTTPStream
7//
8// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/Net/HTTPStream.h"
16#include "Poco/Net/HTTPSession.h"
17
18
19namespace Poco {
20namespace Net {
21
22
23//
24// HTTPStreamBuf
25//
26
27
28HTTPStreamBuf::HTTPStreamBuf(HTTPSession& session, openmode mode):
29 HTTPBasicStreamBuf(HTTPBufferAllocator::BUFFER_SIZE, mode),
30 _session(session),
31 _mode(mode)
32{
33}
34
35
36HTTPStreamBuf::~HTTPStreamBuf()
37{
38}
39
40
41void HTTPStreamBuf::close()
42{
43 if (_mode & std::ios::out)
44 {
45 sync();
46 if (!_session.getKeepAlive())
47 _session.socket().shutdownSend();
48 }
49}
50
51
52int HTTPStreamBuf::readFromDevice(char* buffer, std::streamsize length)
53{
54 return _session.read(buffer, length);
55}
56
57
58int HTTPStreamBuf::writeToDevice(const char* buffer, std::streamsize length)
59{
60
61 return _session.write(buffer, length);
62}
63
64
65//
66// HTTPIOS
67//
68
69
70HTTPIOS::HTTPIOS(HTTPSession& session, HTTPStreamBuf::openmode mode):
71 _buf(session, mode)
72{
73 poco_ios_init(&_buf);
74}
75
76
77HTTPIOS::~HTTPIOS()
78{
79 try
80 {
81 _buf.close();
82 }
83 catch (...)
84 {
85 }
86}
87
88
89HTTPStreamBuf* HTTPIOS::rdbuf()
90{
91 return &_buf;
92}
93
94
95//
96// HTTPInputStream
97//
98
99
100Poco::MemoryPool HTTPInputStream::_pool(sizeof(HTTPInputStream));
101
102
103HTTPInputStream::HTTPInputStream(HTTPSession& session):
104 HTTPIOS(session, std::ios::in),
105 std::istream(&_buf)
106{
107}
108
109
110HTTPInputStream::~HTTPInputStream()
111{
112}
113
114
115void* HTTPInputStream::operator new(std::size_t size)
116{
117 return _pool.get();
118}
119
120
121void HTTPInputStream::operator delete(void* ptr)
122{
123 try
124 {
125 _pool.release(ptr);
126 }
127 catch (...)
128 {
129 poco_unexpected();
130 }
131}
132
133
134//
135// HTTPOutputStream
136//
137
138
139Poco::MemoryPool HTTPOutputStream::_pool(sizeof(HTTPOutputStream));
140
141
142HTTPOutputStream::HTTPOutputStream(HTTPSession& session):
143 HTTPIOS(session, std::ios::out),
144 std::ostream(&_buf)
145{
146}
147
148
149HTTPOutputStream::~HTTPOutputStream()
150{
151}
152
153
154void* HTTPOutputStream::operator new(std::size_t size)
155{
156 return _pool.get();
157}
158
159
160void HTTPOutputStream::operator delete(void* ptr)
161{
162 try
163 {
164 _pool.release(ptr);
165 }
166 catch (...)
167 {
168 poco_unexpected();
169 }
170}
171
172
173} } // namespace Poco::Net
174