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 | if (!_session.getKeepAlive()) |
47 | _session.socket().shutdownSend(); |
48 | } |
49 | } |
50 | |
51 | |
52 | int HTTPStreamBuf::readFromDevice(char* buffer, std::streamsize length) |
53 | { |
54 | return _session.read(buffer, length); |
55 | } |
56 | |
57 | |
58 | int 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 | |
70 | HTTPIOS::HTTPIOS(HTTPSession& session, HTTPStreamBuf::openmode mode): |
71 | _buf(session, mode) |
72 | { |
73 | poco_ios_init(&_buf); |
74 | } |
75 | |
76 | |
77 | HTTPIOS::~HTTPIOS() |
78 | { |
79 | try |
80 | { |
81 | _buf.close(); |
82 | } |
83 | catch (...) |
84 | { |
85 | } |
86 | } |
87 | |
88 | |
89 | HTTPStreamBuf* HTTPIOS::rdbuf() |
90 | { |
91 | return &_buf; |
92 | } |
93 | |
94 | |
95 | // |
96 | // HTTPInputStream |
97 | // |
98 | |
99 | |
100 | Poco::MemoryPool HTTPInputStream::_pool(sizeof(HTTPInputStream)); |
101 | |
102 | |
103 | HTTPInputStream::HTTPInputStream(HTTPSession& session): |
104 | HTTPIOS(session, std::ios::in), |
105 | std::istream(&_buf) |
106 | { |
107 | } |
108 | |
109 | |
110 | HTTPInputStream::~HTTPInputStream() |
111 | { |
112 | } |
113 | |
114 | |
115 | void* HTTPInputStream::operator new(std::size_t size) |
116 | { |
117 | return _pool.get(); |
118 | } |
119 | |
120 | |
121 | void 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 | |
139 | Poco::MemoryPool HTTPOutputStream::_pool(sizeof(HTTPOutputStream)); |
140 | |
141 | |
142 | HTTPOutputStream::HTTPOutputStream(HTTPSession& session): |
143 | HTTPIOS(session, std::ios::out), |
144 | std::ostream(&_buf) |
145 | { |
146 | } |
147 | |
148 | |
149 | HTTPOutputStream::~HTTPOutputStream() |
150 | { |
151 | } |
152 | |
153 | |
154 | void* HTTPOutputStream::operator new(std::size_t size) |
155 | { |
156 | return _pool.get(); |
157 | } |
158 | |
159 | |
160 | void 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 | |