1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
/*
* Copyright (C) 2015-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "WsgiInputStream.h"
#include "network/httprequesthandler/python/HTTPPythonRequest.h"
#include "utils/StringUtils.h"
namespace XBMCAddon
{
namespace xbmcwsgi
{
WsgiInputStreamIterator::WsgiInputStreamIterator()
: m_data(),
m_line()
{ }
#ifndef SWIG
WsgiInputStreamIterator::WsgiInputStreamIterator(const String& data, bool end /* = false */)
: m_data(data),
m_remaining(end ? 0 : data.size()),
m_line()
{ }
#endif
WsgiInputStreamIterator::~WsgiInputStreamIterator() = default;
String WsgiInputStreamIterator::read(unsigned long size /* = 0 */) const
{
// make sure we don't try to read more data than we have
if (size <= 0 || size > m_remaining)
size = m_remaining;
// remember the current read offset
size_t offset = static_cast<size_t>(m_offset);
// adjust the read offset and the remaining data length
m_offset += size;
m_remaining -= size;
// return the data being requested
return m_data.substr(offset, size);
}
String WsgiInputStreamIterator::readline(unsigned long size /* = 0 */) const
{
// make sure we don't try to read more data than we have
if (size <= 0 || size > m_remaining)
size = m_remaining;
size_t offset = static_cast<size_t>(m_offset);
size_t pos = m_data.find('\n', offset);
// make sure pos has a valid value and includes the \n character
if (pos == std::string::npos)
pos = m_data.size();
else
pos += 1;
if (pos - offset < size)
size = pos - offset;
// read the next line
String line = read(size);
// remove any trailing \r\n
StringUtils::TrimRight(line, "\r\n");
return line;
}
std::vector<String> WsgiInputStreamIterator::readlines(unsigned long sizehint /* = 0 */) const
{
std::vector<String> lines;
// make sure we don't try to read more data than we have
if (sizehint <= 0 || sizehint > m_remaining)
sizehint = m_remaining;
do
{
// read a full line
String line = readline();
// adjust the sizehint by the number of bytes just read
sizehint -= line.length();
// add it to the list of read lines
lines.push_back(line);
} while (sizehint > 0);
return lines;
}
#ifndef SWIG
WsgiInputStreamIterator& WsgiInputStreamIterator::operator++()
{
m_line.clear();
if (!end())
{
// read the next line
m_line = readline();
}
return *this;
}
bool WsgiInputStreamIterator::operator==(const WsgiInputStreamIterator& rhs)
{
return m_data == rhs.m_data &&
m_offset == rhs.m_offset &&
m_remaining == rhs.m_remaining;
}
bool WsgiInputStreamIterator::operator!=(const WsgiInputStreamIterator& rhs)
{
return !(*this == rhs);
}
String& WsgiInputStreamIterator::operator*()
{
return m_line;
}
#endif
WsgiInputStream::WsgiInputStream()
: m_request(NULL)
{ }
WsgiInputStream::~WsgiInputStream()
{
m_request = NULL;
}
#ifndef SWIG
WsgiInputStreamIterator* WsgiInputStream::begin()
{
return new WsgiInputStreamIterator(m_data, false);
}
WsgiInputStreamIterator* WsgiInputStream::end()
{
return new WsgiInputStreamIterator(m_data, true);
}
void WsgiInputStream::SetRequest(HTTPPythonRequest* request)
{
if (m_request != NULL)
return;
m_request = request;
// set the remaining bytes to be read
m_data = m_request->requestContent;
m_offset = 0;
m_remaining = m_data.size();
}
#endif
}
}
|