CLOUD: Update Reader to support pausing

That means that if current buffer is over, reader will stop reading and
it's safe to call readResponse() again, so it would continue from the
place it left.
This commit is contained in:
Alexander Tkachev 2016-07-07 17:21:40 +06:00
parent 79b39bf0d0
commit bb67b81d04
3 changed files with 78 additions and 19 deletions

View File

@ -50,7 +50,8 @@ LocalWebserver::LocalWebserver(): _set(nullptr), _serverSocket(nullptr), _timerS
_defaultHandler = _resourceHandler.getHandler();
Reader reader;
reader.readResponse();
reader.setBytesLeft(128);
while (!reader.readResponse()) reader.setBytesLeft(20);
}
LocalWebserver::~LocalWebserver() {

View File

@ -28,6 +28,9 @@
namespace Networking {
Reader::Reader() {
_state = RS_NONE;
_bytesLeft = 0;
_contentLength = 0;
_availableBytes = 0;
@ -35,6 +38,9 @@ Reader::Reader() {
_windowUsed = 0;
_windowSize = 0;
_headers = "";
_buffer = "";
_content =
"POST /upload HTTP/1.1\r\n" \
"Host: 127.0.0.1:12345\r\n" \
@ -61,10 +67,20 @@ Reader::Reader() {
Reader::~Reader() {}
void Reader::readResponse() {
bool Reader::readResponse() {
if (_state == RS_NONE) _state = RS_READING_HEADERS;
while (true) {
readHeaders(); //til "\r\n\r\n"
readContent(); //til "--" + _boundary
if (!bytesLeft()) return false;
if (_state == RS_READING_HEADERS)
if (!readHeaders())
return false;
if (_state == RS_READING_CONTENT)
if (!readContent())
return false;
if (_availableBytes >= 2) {
Common::String bts;
bts += readOne();
@ -79,15 +95,25 @@ void Reader::readResponse() {
}
if (_availableBytes > 0) debug("STRANGE END: %llu bytes left", _availableBytes);
else debug("END");
return true;
}
void Reader::readHeaders() {
bool Reader::readHeaders() {
Common::String boundary = "\r\n\r\n";
makeWindow(boundary.size());
if (_window == nullptr) {
makeWindow(boundary.size());
_headers = "";
}
Common::String headers = "";
while (readOneByteInString(headers, boundary));
handleHeaders(headers);
while (readOneByteInString(_headers, boundary)) {
if (!bytesLeft()) return false;
}
handleHeaders(_headers);
freeWindow();
_state = RS_READING_CONTENT;
return true;
}
namespace {
@ -136,9 +162,12 @@ void Reader::handleHeaders(Common::String headers) {
}
}
void Reader::readContent() {
bool Reader::readContent() {
Common::String boundary = "--" + _boundary;
makeWindow(boundary.size());
if (_window == nullptr) {
makeWindow(boundary.size());
_buffer = "";
}
/*
if (_fieldIsFile) {
@ -150,10 +179,15 @@ void Reader::readContent() {
handleFileContent(tempFileName);
} else {
*/
Common::String buffer = "";
while (readOneByteInString(buffer, boundary)) ;
handleValueContent(buffer);
while (readOneByteInString(_buffer, boundary)) {
if (!bytesLeft()) return false;
}
handleValueContent(_buffer);
//}
freeWindow();
_state = RS_READING_HEADERS;
return true;
}
void Reader::handleFileContent(Common::String filename) {
@ -166,14 +200,19 @@ void Reader::handleValueContent(Common::String value) {
}
void Reader::makeWindow(uint32 size) {
delete[] _window;
_window = nullptr;
freeWindow();
_window = new byte[size];
_windowUsed = 0;
_windowSize = size;
}
void Reader::freeWindow() {
delete[] _window;
_window = nullptr;
_windowUsed = _windowSize = 0;
}
/*
bool Reader::readOneByteInStream(stream) {
b = read(1);
@ -214,7 +253,12 @@ byte Reader::readOne() {
byte b = _content[0];
_content.deleteChar(0);
--_availableBytes;
--_bytesLeft;
return b;
}
uint32 Reader::bytesLeft() { return _bytesLeft; }
void Reader::setBytesLeft(uint32 b) { _bytesLeft = b; }
} // End of namespace Networking

View File

@ -30,15 +30,26 @@
namespace Networking {
enum ReaderState {
RS_NONE,
RS_READING_HEADERS,
RS_READING_CONTENT
};
class Reader {
///Common::String _headers;
///Common::String _method, _path, _query, _anchor;
ReaderState _state;
Common::String _content;
uint32 _bytesLeft;
Common::String _boundary;
uint32 _contentLength;
uint32 _availableBytes;
Common::String _headers;
Common::String _buffer;
Common::String _currentFieldName;
Common::HashMap<Common::String, Common::String> _fields;
Common::HashMap<Common::String, Common::String> _attachedFiles;
@ -46,23 +57,26 @@ class Reader {
byte *_window;
uint32 _windowUsed, _windowSize;
void readHeaders();
void readContent();
bool readHeaders(); //true when ended reading
bool readContent(); //true when ended reading
void handleHeaders(Common::String headers);
void handleFileContent(Common::String filename);
void handleValueContent(Common::String value);
void makeWindow(uint32 size);
void freeWindow();
///bool Reader::readOneByteInStream(stream);
bool Reader::readOneByteInString(Common::String &buffer, const Common::String &boundary);
byte readOne();
uint32 bytesLeft();
public:
Reader();
~Reader();
void readResponse();
bool readResponse(); //true when ended reading
void setBytesLeft(uint32 b);
};
} // End of namespace Networking