diff options
Diffstat (limited to 'backends/networking')
-rw-r--r-- | backends/networking/sdl_net/client.cpp | 59 | ||||
-rw-r--r-- | backends/networking/sdl_net/client.h | 4 | ||||
-rw-r--r-- | backends/networking/sdl_net/reader.cpp | 37 | ||||
-rw-r--r-- | backends/networking/sdl_net/reader.h | 6 |
4 files changed, 51 insertions, 55 deletions
diff --git a/backends/networking/sdl_net/client.cpp b/backends/networking/sdl_net/client.cpp index adcbafdb12..3edf9c32f5 100644 --- a/backends/networking/sdl_net/client.cpp +++ b/backends/networking/sdl_net/client.cpp @@ -44,12 +44,7 @@ void Client::open(SDLNet_SocketSet set, TCPsocket socket) { _state = READING_HEADERS; _socket = socket; _set = set; - _headers = ""; _reader = Reader(); - _method = ""; - _path = ""; - _query = ""; - _anchor = ""; _handler = nullptr; if (set) { int numused = SDLNet_TCP_AddSocket(set, socket); @@ -73,16 +68,8 @@ void Client::readHeaders() { } _reader.setContent(buffer, bytes); - if (_reader.readResponse()) { - if (_reader.badRequest()) _state = BAD_REQUEST; - else { - _state = READ_HEADERS; - _method = _reader.method(); - _path = _reader.path(); - _query = _reader.query(); - _anchor = _reader.anchor(); - } - } + if (_reader.readRequest()) + _state = (_reader.badRequest() ? BAD_REQUEST : READ_HEADERS); } void Client::setHandler(ClientHandler *handler) { @@ -118,39 +105,17 @@ void Client::close() { ClientState Client::state() const { return _state; } -Common::String Client::headers() const { return _headers; } - -Common::String Client::method() const { return _method; } - -Common::String Client::path() const { return _path; } - -Common::String Client::query() const { return _query; } - -Common::String Client::queryParameter(Common::String name) const { - // this approach is a bit slower than searching for the <name> - // yet I believe it to be the right one, because we probably can have "<name>=" in the value of other key - Common::String key = ""; - Common::String value = ""; - bool readingKey = true; - for (uint32 i = 0; i < _query.size(); ++i) { - if (readingKey) { - if (_query[i] == '=') { - readingKey = false; - value = ""; - } else key += _query[i]; - } else { - if (_query[i] == '&') { - if (key == name) return LocalWebserver::urlDecode(value); - readingKey = true; - key = ""; - } else value += _query[i]; - } - } - if (key == name) return LocalWebserver::urlDecode(value); //the last key doesn't have an '&' in the end of the query - return ""; -} +//Common::String Client::headers() const { return _headers; } + +Common::String Client::method() const { return _reader.method(); } + +Common::String Client::path() const { return _reader.path(); } + +Common::String Client::query() const { return _reader.query(); } + +Common::String Client::queryParameter(Common::String name) const { return _reader.queryParameter(name); } -Common::String Client::anchor() const { return _anchor; } +Common::String Client::anchor() const { return _reader.anchor(); } bool Client::socketIsReady() { return SDLNet_SocketReady(_socket); } diff --git a/backends/networking/sdl_net/client.h b/backends/networking/sdl_net/client.h index a54f72ae2b..9764dacb51 100644 --- a/backends/networking/sdl_net/client.h +++ b/backends/networking/sdl_net/client.h @@ -53,8 +53,6 @@ class Client { SDLNet_SocketSet _set; TCPsocket _socket; Reader _reader; - Common::String _headers; - Common::String _method, _path, _query, _anchor; ClientHandler *_handler; public: @@ -69,7 +67,7 @@ public: void close(); ClientState state() const; - Common::String headers() const; + //Common::String headers() const; Common::String method() const; Common::String path() const; Common::String query() const; diff --git a/backends/networking/sdl_net/reader.cpp b/backends/networking/sdl_net/reader.cpp index 3f7983506a..452520abfe 100644 --- a/backends/networking/sdl_net/reader.cpp +++ b/backends/networking/sdl_net/reader.cpp @@ -23,7 +23,8 @@ #define FORBIDDEN_SYMBOL_ALLOW_ALL #include "backends/networking/sdl_net/reader.h" -#include <common/debug.h> +#include "backends/networking/sdl_net/localwebserver.h" +#include "common/debug.h" namespace Networking { @@ -46,7 +47,7 @@ Reader::Reader() { Reader::~Reader() {} -bool Reader::readResponse() { +bool Reader::readRequest() { if (_state == RS_NONE) _state = RS_READING_HEADERS; while (true) { @@ -210,6 +211,34 @@ void Reader::parsePathQueryAndAnchor(Common::String path) { } else _query += path[i]; } else _anchor += path[i]; } + + parseQueryParameters(); +} + +void Reader::parseQueryParameters() { + Common::String key = ""; + Common::String value = ""; + bool readingKey = true; + for (uint32 i = 0; i < _query.size(); ++i) { + if (readingKey) { + if (_query[i] == '=') { + readingKey = false; + value = ""; + } else key += _query[i]; + } else { + if (_query[i] == '&') { + if (_queryParameters.contains(key)) warning("Query parameter \"%s\" is already set!"); + else _queryParameters[key] = LocalWebserver::urlDecode(value); + readingKey = true; + key = ""; + } else value += _query[i]; + } + } + + if (!key.empty()) { + if (_queryParameters.contains(key)) warning("Query parameter \"%s\" is already set!"); + else _queryParameters[key] = LocalWebserver::urlDecode(value); + } } bool Reader::readContent() { @@ -246,7 +275,7 @@ void Reader::handleFileContent(Common::String filename) { void Reader::handleValueContent(Common::String value) { debug("\nHANDLE CONTENT:\n>>%s<<", value.c_str()); - _fields[_currentFieldName] = value; + _queryParameters[_currentFieldName] = value; } void Reader::makeWindow(uint32 size) { @@ -322,6 +351,8 @@ Common::String Reader::path() const { return _path; } Common::String Reader::query() const { return _query; } +Common::String Reader::queryParameter(Common::String name) const { return _queryParameters[name]; } + Common::String Reader::anchor() const { return _anchor; } } // End of namespace Networking diff --git a/backends/networking/sdl_net/reader.h b/backends/networking/sdl_net/reader.h index 245f63abd1..e5ef523ee9 100644 --- a/backends/networking/sdl_net/reader.h +++ b/backends/networking/sdl_net/reader.h @@ -49,7 +49,7 @@ class Reader { ///Common::String _headers; Common::String _method, _path, _query, _anchor; - Common::HashMap<Common::String, Common::String> _fields; + Common::HashMap<Common::String, Common::String> _queryParameters; Common::HashMap<Common::String, Common::String> _attachedFiles; uint32 _contentLength; Common::String _boundary; @@ -65,6 +65,7 @@ class Reader { void parseFirstLine(const Common::String &headers); void parsePathQueryAndAnchor(Common::String path); + void parseQueryParameters(); void makeWindow(uint32 size); void freeWindow(); @@ -78,13 +79,14 @@ public: Reader(); ~Reader(); - bool readResponse(); //true when ended reading + bool readRequest(); //true when ended reading void setContent(byte *buffer, uint32 size); bool badRequest(); Common::String method() const; Common::String path() const; Common::String query() const; + Common::String queryParameter(Common::String name) const; Common::String anchor() const; }; |