1 | // |
2 | // HTTPChunkedStream.cpp |
3 | // |
4 | // Library: Net |
5 | // Package: HTTP |
6 | // Module: HTTPChunkedStream |
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/HTTPChunkedStream.h" |
16 | #include "Poco/Net/HTTPSession.h" |
17 | #include "Poco/NumberFormatter.h" |
18 | #include "Poco/NumberParser.h" |
19 | #include "Poco/Ascii.h" |
20 | |
21 | |
22 | using Poco::NumberFormatter; |
23 | using Poco::NumberParser; |
24 | |
25 | |
26 | namespace Poco { |
27 | namespace Net { |
28 | |
29 | |
30 | // |
31 | // HTTPChunkedStreamBuf |
32 | // |
33 | |
34 | |
35 | HTTPChunkedStreamBuf::HTTPChunkedStreamBuf(HTTPSession& session, openmode mode): |
36 | HTTPBasicStreamBuf(HTTPBufferAllocator::BUFFER_SIZE, mode), |
37 | _session(session), |
38 | _mode(mode), |
39 | _chunk(0) |
40 | { |
41 | } |
42 | |
43 | |
44 | HTTPChunkedStreamBuf::~HTTPChunkedStreamBuf() |
45 | { |
46 | } |
47 | |
48 | |
49 | void HTTPChunkedStreamBuf::close() |
50 | { |
51 | if (_mode & std::ios::out) |
52 | { |
53 | sync(); |
54 | _session.write("0\r\n\r\n" , 5); |
55 | } |
56 | } |
57 | |
58 | |
59 | int HTTPChunkedStreamBuf::readFromDevice(char* buffer, std::streamsize length) |
60 | { |
61 | static const int eof = std::char_traits<char>::eof(); |
62 | |
63 | if (_chunk == 0) |
64 | { |
65 | int ch = _session.get(); |
66 | while (Poco::Ascii::isSpace(ch)) ch = _session.get(); |
67 | std::string chunkLen; |
68 | while (Poco::Ascii::isHexDigit(ch) && chunkLen.size() < 8) { chunkLen += (char) ch; ch = _session.get(); } |
69 | if (ch != eof && !(Poco::Ascii::isSpace(ch) || ch == ';')) return eof; |
70 | while (ch != eof && ch != '\n') ch = _session.get(); |
71 | unsigned chunk; |
72 | if (NumberParser::tryParseHex(chunkLen, chunk)) |
73 | _chunk = (std::streamsize) chunk; |
74 | else |
75 | return eof; |
76 | } |
77 | if (_chunk > 0) |
78 | { |
79 | if (length > _chunk) length = _chunk; |
80 | int n = _session.read(buffer, length); |
81 | if (n > 0) _chunk -= n; |
82 | return n; |
83 | } |
84 | else |
85 | { |
86 | int ch = _session.get(); |
87 | while (ch != eof && ch != '\n') ch = _session.get(); |
88 | return 0; |
89 | } |
90 | } |
91 | |
92 | |
93 | int HTTPChunkedStreamBuf::writeToDevice(const char* buffer, std::streamsize length) |
94 | { |
95 | _chunkBuffer.clear(); |
96 | NumberFormatter::appendHex(_chunkBuffer, static_cast<Poco::UInt64>(length)); |
97 | _chunkBuffer.append("\r\n" , 2); |
98 | _chunkBuffer.append(buffer, static_cast<std::string::size_type>(length)); |
99 | _chunkBuffer.append("\r\n" , 2); |
100 | _session.write(_chunkBuffer.data(), static_cast<std::streamsize>(_chunkBuffer.size())); |
101 | return static_cast<int>(length); |
102 | } |
103 | |
104 | |
105 | // |
106 | // HTTPChunkedIOS |
107 | // |
108 | |
109 | |
110 | HTTPChunkedIOS::HTTPChunkedIOS(HTTPSession& session, HTTPChunkedStreamBuf::openmode mode): |
111 | _buf(session, mode) |
112 | { |
113 | poco_ios_init(&_buf); |
114 | } |
115 | |
116 | |
117 | HTTPChunkedIOS::~HTTPChunkedIOS() |
118 | { |
119 | try |
120 | { |
121 | _buf.close(); |
122 | } |
123 | catch (...) |
124 | { |
125 | } |
126 | } |
127 | |
128 | |
129 | HTTPChunkedStreamBuf* HTTPChunkedIOS::rdbuf() |
130 | { |
131 | return &_buf; |
132 | } |
133 | |
134 | |
135 | // |
136 | // HTTPChunkedInputStream |
137 | // |
138 | |
139 | |
140 | Poco::MemoryPool HTTPChunkedInputStream::_pool(sizeof(HTTPChunkedInputStream)); |
141 | |
142 | |
143 | HTTPChunkedInputStream::HTTPChunkedInputStream(HTTPSession& session): |
144 | HTTPChunkedIOS(session, std::ios::in), |
145 | std::istream(&_buf) |
146 | { |
147 | } |
148 | |
149 | |
150 | HTTPChunkedInputStream::~HTTPChunkedInputStream() |
151 | { |
152 | } |
153 | |
154 | |
155 | void* HTTPChunkedInputStream::operator new(std::size_t size) |
156 | { |
157 | return _pool.get(); |
158 | } |
159 | |
160 | |
161 | void HTTPChunkedInputStream::operator delete(void* ptr) |
162 | { |
163 | try |
164 | { |
165 | _pool.release(ptr); |
166 | } |
167 | catch (...) |
168 | { |
169 | poco_unexpected(); |
170 | } |
171 | } |
172 | |
173 | |
174 | // |
175 | // HTTPChunkedOutputStream |
176 | // |
177 | |
178 | |
179 | Poco::MemoryPool HTTPChunkedOutputStream::_pool(sizeof(HTTPChunkedOutputStream)); |
180 | |
181 | |
182 | HTTPChunkedOutputStream::HTTPChunkedOutputStream(HTTPSession& session): |
183 | HTTPChunkedIOS(session, std::ios::out), |
184 | std::ostream(&_buf) |
185 | { |
186 | } |
187 | |
188 | |
189 | HTTPChunkedOutputStream::~HTTPChunkedOutputStream() |
190 | { |
191 | } |
192 | |
193 | |
194 | void* HTTPChunkedOutputStream::operator new(std::size_t size) |
195 | { |
196 | return _pool.get(); |
197 | } |
198 | |
199 | |
200 | void HTTPChunkedOutputStream::operator delete(void* ptr) |
201 | { |
202 | try |
203 | { |
204 | _pool.release(ptr); |
205 | } |
206 | catch (...) |
207 | { |
208 | poco_unexpected(); |
209 | } |
210 | } |
211 | |
212 | |
213 | } } // namespace Poco::Net |
214 | |