| 1 | // |
| 2 | // HTTPFixedLengthStream.cpp |
| 3 | // |
| 4 | // Library: Net |
| 5 | // Package: HTTP |
| 6 | // Module: HTTPFixedLengthStream |
| 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/HTTPFixedLengthStream.h" |
| 16 | #include "Poco/Net/HTTPSession.h" |
| 17 | |
| 18 | |
| 19 | using Poco::BufferedStreamBuf; |
| 20 | |
| 21 | |
| 22 | namespace Poco { |
| 23 | namespace Net { |
| 24 | |
| 25 | |
| 26 | // |
| 27 | // HTTPFixedLengthStreamBuf |
| 28 | // |
| 29 | |
| 30 | |
| 31 | HTTPFixedLengthStreamBuf::HTTPFixedLengthStreamBuf(HTTPSession& session, ContentLength length, openmode mode): |
| 32 | HTTPBasicStreamBuf(HTTPBufferAllocator::BUFFER_SIZE, mode), |
| 33 | _session(session), |
| 34 | _length(length), |
| 35 | _count(0) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | |
| 40 | HTTPFixedLengthStreamBuf::~HTTPFixedLengthStreamBuf() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | |
| 45 | int HTTPFixedLengthStreamBuf::readFromDevice(char* buffer, std::streamsize length) |
| 46 | { |
| 47 | int n = 0; |
| 48 | if (_count < _length) |
| 49 | { |
| 50 | if (_count + length > _length) |
| 51 | length = static_cast<std::streamsize>(_length - _count); |
| 52 | n = _session.read(buffer, length); |
| 53 | if (n > 0) _count += n; |
| 54 | } |
| 55 | return n; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | int HTTPFixedLengthStreamBuf::writeToDevice(const char* buffer, std::streamsize length) |
| 60 | { |
| 61 | int n = 0; |
| 62 | if (_count < _length) |
| 63 | { |
| 64 | if (_count + length > _length) |
| 65 | length = static_cast<std::streamsize>(_length - _count); |
| 66 | n = _session.write(buffer, length); |
| 67 | if (n > 0) _count += n; |
| 68 | } |
| 69 | return n; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | // |
| 74 | // HTTPFixedLengthIOS |
| 75 | // |
| 76 | |
| 77 | |
| 78 | HTTPFixedLengthIOS::HTTPFixedLengthIOS(HTTPSession& session, HTTPFixedLengthStreamBuf::ContentLength length, HTTPFixedLengthStreamBuf::openmode mode): |
| 79 | _buf(session, length, mode) |
| 80 | { |
| 81 | poco_ios_init(&_buf); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | HTTPFixedLengthIOS::~HTTPFixedLengthIOS() |
| 86 | { |
| 87 | try |
| 88 | { |
| 89 | _buf.sync(); |
| 90 | } |
| 91 | catch (...) |
| 92 | { |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | |
| 97 | HTTPFixedLengthStreamBuf* HTTPFixedLengthIOS::rdbuf() |
| 98 | { |
| 99 | return &_buf; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | // |
| 104 | // HTTPFixedLengthInputStream |
| 105 | // |
| 106 | |
| 107 | |
| 108 | Poco::MemoryPool HTTPFixedLengthInputStream::_pool(sizeof(HTTPFixedLengthInputStream)); |
| 109 | |
| 110 | |
| 111 | HTTPFixedLengthInputStream::HTTPFixedLengthInputStream(HTTPSession& session, HTTPFixedLengthStreamBuf::ContentLength length): |
| 112 | HTTPFixedLengthIOS(session, length, std::ios::in), |
| 113 | std::istream(&_buf) |
| 114 | { |
| 115 | } |
| 116 | |
| 117 | |
| 118 | HTTPFixedLengthInputStream::~HTTPFixedLengthInputStream() |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | |
| 123 | void* HTTPFixedLengthInputStream::operator new(std::size_t size) |
| 124 | { |
| 125 | return _pool.get(); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | void HTTPFixedLengthInputStream::operator delete(void* ptr) |
| 130 | { |
| 131 | try |
| 132 | { |
| 133 | _pool.release(ptr); |
| 134 | } |
| 135 | catch (...) |
| 136 | { |
| 137 | poco_unexpected(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | |
| 142 | // |
| 143 | // HTTPFixedLengthOutputStream |
| 144 | // |
| 145 | |
| 146 | |
| 147 | Poco::MemoryPool HTTPFixedLengthOutputStream::_pool(sizeof(HTTPFixedLengthOutputStream)); |
| 148 | |
| 149 | |
| 150 | HTTPFixedLengthOutputStream::HTTPFixedLengthOutputStream(HTTPSession& session, HTTPFixedLengthStreamBuf::ContentLength length): |
| 151 | HTTPFixedLengthIOS(session, length, std::ios::out), |
| 152 | std::ostream(&_buf) |
| 153 | { |
| 154 | } |
| 155 | |
| 156 | |
| 157 | HTTPFixedLengthOutputStream::~HTTPFixedLengthOutputStream() |
| 158 | { |
| 159 | } |
| 160 | |
| 161 | |
| 162 | void* HTTPFixedLengthOutputStream::operator new(std::size_t size) |
| 163 | { |
| 164 | return _pool.get(); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | void HTTPFixedLengthOutputStream::operator delete(void* ptr) |
| 169 | { |
| 170 | try |
| 171 | { |
| 172 | _pool.release(ptr); |
| 173 | } |
| 174 | catch (...) |
| 175 | { |
| 176 | poco_unexpected(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | |
| 181 | } } // namespace Poco::Net |
| 182 | |