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 _session.socket().shutdownSend();
47 }
48}
49
50
51int HTTPStreamBuf::readFromDevice(char* buffer, std::streamsize length)
52{
53 return _session.read(buffer, length);
54}
55
56
57int HTTPStreamBuf::writeToDevice(const char* buffer, std::streamsize length)
58{
59
60 return _session.write(buffer, length);
61}
62
63
64//
65// HTTPIOS
66//
67
68
69HTTPIOS::HTTPIOS(HTTPSession& session, HTTPStreamBuf::openmode mode):
70 _buf(session, mode)
71{
72 poco_ios_init(&_buf);
73}
74
75
76HTTPIOS::~HTTPIOS()
77{
78 try
79 {
80 _buf.close();
81 }
82 catch (...)
83 {
84 }
85}
86
87
88HTTPStreamBuf* HTTPIOS::rdbuf()
89{
90 return &_buf;
91}
92
93
94//
95// HTTPInputStream
96//
97
98
99Poco::MemoryPool HTTPInputStream::_pool(sizeof(HTTPInputStream));
100
101
102HTTPInputStream::HTTPInputStream(HTTPSession& session):
103 HTTPIOS(session, std::ios::in),
104 std::istream(&_buf)
105{
106}
107
108
109HTTPInputStream::~HTTPInputStream()
110{
111}
112
113
114void* HTTPInputStream::operator new(std::size_t /*size*/)
115{
116 return _pool.get();
117}
118
119
120void HTTPInputStream::operator delete(void* ptr)
121{
122 try
123 {
124 _pool.release(ptr);
125 }
126 catch (...)
127 {
128 poco_unexpected();
129 }
130}
131
132
133//
134// HTTPOutputStream
135//
136
137
138Poco::MemoryPool HTTPOutputStream::_pool(sizeof(HTTPOutputStream));
139
140
141HTTPOutputStream::HTTPOutputStream(HTTPSession& session):
142 HTTPIOS(session, std::ios::out),
143 std::ostream(&_buf)
144{
145}
146
147
148HTTPOutputStream::~HTTPOutputStream()
149{
150}
151
152
153void* HTTPOutputStream::operator new(std::size_t /*size*/)
154{
155 return _pool.get();
156}
157
158
159void HTTPOutputStream::operator delete(void* ptr)
160{
161 try
162 {
163 _pool.release(ptr);
164 }
165 catch (...)
166 {
167 poco_unexpected();
168 }
169}
170
171
172} } // namespace Poco::Net
173