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 | |
19 | namespace Poco { |
20 | namespace Net { |
21 | |
22 | |
23 | // |
24 | // HTTPStreamBuf |
25 | // |
26 | |
27 | |
28 | HTTPStreamBuf::HTTPStreamBuf(HTTPSession& session, openmode mode): |
29 | HTTPBasicStreamBuf(HTTPBufferAllocator::BUFFER_SIZE, mode), |
30 | _session(session), |
31 | _mode(mode) |
32 | { |
33 | } |
34 | |
35 | |
36 | HTTPStreamBuf::~HTTPStreamBuf() |
37 | { |
38 | } |
39 | |
40 | |
41 | void HTTPStreamBuf::close() |
42 | { |
43 | if (_mode & std::ios::out) |
44 | { |
45 | sync(); |
46 | _session.socket().shutdownSend(); |
47 | } |
48 | } |
49 | |
50 | |
51 | int HTTPStreamBuf::readFromDevice(char* buffer, std::streamsize length) |
52 | { |
53 | return _session.read(buffer, length); |
54 | } |
55 | |
56 | |
57 | int 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 | |
69 | HTTPIOS::HTTPIOS(HTTPSession& session, HTTPStreamBuf::openmode mode): |
70 | _buf(session, mode) |
71 | { |
72 | poco_ios_init(&_buf); |
73 | } |
74 | |
75 | |
76 | HTTPIOS::~HTTPIOS() |
77 | { |
78 | try |
79 | { |
80 | _buf.close(); |
81 | } |
82 | catch (...) |
83 | { |
84 | } |
85 | } |
86 | |
87 | |
88 | HTTPStreamBuf* HTTPIOS::rdbuf() |
89 | { |
90 | return &_buf; |
91 | } |
92 | |
93 | |
94 | // |
95 | // HTTPInputStream |
96 | // |
97 | |
98 | |
99 | Poco::MemoryPool HTTPInputStream::_pool(sizeof(HTTPInputStream)); |
100 | |
101 | |
102 | HTTPInputStream::HTTPInputStream(HTTPSession& session): |
103 | HTTPIOS(session, std::ios::in), |
104 | std::istream(&_buf) |
105 | { |
106 | } |
107 | |
108 | |
109 | HTTPInputStream::~HTTPInputStream() |
110 | { |
111 | } |
112 | |
113 | |
114 | void* HTTPInputStream::operator new(std::size_t /*size*/) |
115 | { |
116 | return _pool.get(); |
117 | } |
118 | |
119 | |
120 | void 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 | |
138 | Poco::MemoryPool HTTPOutputStream::_pool(sizeof(HTTPOutputStream)); |
139 | |
140 | |
141 | HTTPOutputStream::HTTPOutputStream(HTTPSession& session): |
142 | HTTPIOS(session, std::ios::out), |
143 | std::ostream(&_buf) |
144 | { |
145 | } |
146 | |
147 | |
148 | HTTPOutputStream::~HTTPOutputStream() |
149 | { |
150 | } |
151 | |
152 | |
153 | void* HTTPOutputStream::operator new(std::size_t /*size*/) |
154 | { |
155 | return _pool.get(); |
156 | } |
157 | |
158 | |
159 | void 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 | |