diff options
author | Alexander Tkachev | 2016-07-20 19:51:39 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | 01161ae7ddbc5f147dd9e71991eb2f1a1c9a7b06 (patch) | |
tree | 510e5c6e81976262498835066cd94a21b42b4f69 /backends/networking | |
parent | b180c73675846f45abab2190b39e0b9d0d6addbf (diff) | |
download | scummvm-rg350-01161ae7ddbc5f147dd9e71991eb2f1a1c9a7b06.tar.gz scummvm-rg350-01161ae7ddbc5f147dd9e71991eb2f1a1c9a7b06.tar.bz2 scummvm-rg350-01161ae7ddbc5f147dd9e71991eb2f1a1c9a7b06.zip |
CLOUD: Do some refactoring/cleanup in Networking
Diffstat (limited to 'backends/networking')
-rw-r--r-- | backends/networking/browser/openurl.h | 5 | ||||
-rw-r--r-- | backends/networking/connection/islimited.h | 5 | ||||
-rw-r--r-- | backends/networking/curl/connectionmanager.cpp | 10 | ||||
-rw-r--r-- | backends/networking/curl/connectionmanager.h | 4 | ||||
-rw-r--r-- | backends/networking/sdl_net/client.h | 20 | ||||
-rw-r--r-- | backends/networking/sdl_net/handlers/filespagehandler.cpp | 4 | ||||
-rw-r--r-- | backends/networking/sdl_net/handlers/filespagehandler.h | 4 | ||||
-rw-r--r-- | backends/networking/sdl_net/handlers/indexpagehandler.cpp | 4 | ||||
-rw-r--r-- | backends/networking/sdl_net/handlers/listajaxhandler.cpp | 2 | ||||
-rw-r--r-- | backends/networking/sdl_net/handlers/listajaxhandler.h | 4 | ||||
-rw-r--r-- | backends/networking/sdl_net/handlerutils.cpp | 3 | ||||
-rw-r--r-- | backends/networking/sdl_net/localwebserver.cpp | 2 | ||||
-rw-r--r-- | backends/networking/sdl_net/localwebserver.h | 3 | ||||
-rw-r--r-- | backends/networking/sdl_net/reader.h | 32 | ||||
-rw-r--r-- | backends/networking/sdl_net/uploadfileclienthandler.h | 10 |
15 files changed, 92 insertions, 20 deletions
diff --git a/backends/networking/browser/openurl.h b/backends/networking/browser/openurl.h index fe2f452f62..15b4bf385b 100644 --- a/backends/networking/browser/openurl.h +++ b/backends/networking/browser/openurl.h @@ -28,6 +28,11 @@ namespace Networking { namespace Browser { +/** + * Opens URL in default browser (if available on the target system). + * + * Returns true on success. + */ bool openUrl(const Common::String &url); } // End of namespace Browser diff --git a/backends/networking/connection/islimited.h b/backends/networking/connection/islimited.h index 69be26701c..b23d31d157 100644 --- a/backends/networking/connection/islimited.h +++ b/backends/networking/connection/islimited.h @@ -26,6 +26,11 @@ namespace Networking { namespace Connection { +/** +* Returns whether connection's limited (if available on the target system). +* +* Returns true if connection seems limited. +*/ bool isLimited(); } // End of namespace Connection diff --git a/backends/networking/curl/connectionmanager.cpp b/backends/networking/curl/connectionmanager.cpp index b553e8f1ac..6affad7c1d 100644 --- a/backends/networking/curl/connectionmanager.cpp +++ b/backends/networking/curl/connectionmanager.cpp @@ -63,7 +63,7 @@ ConnectionManager::~ConnectionManager() { _handleMutex.unlock(); } -void ConnectionManager::registerEasyHandle(CURL *easy) { +void ConnectionManager::registerEasyHandle(CURL *easy) const { curl_multi_add_handle(_multi, easy); } @@ -80,7 +80,7 @@ void ConnectionManager::showCloudDisabledIcon() { startTimer(); } -Common::String ConnectionManager::urlEncode(Common::String s) { +Common::String ConnectionManager::urlEncode(Common::String s) const { if (!_multi) return ""; char *output = curl_easy_escape(_multi, s.c_str(), s.size()); if (output) { @@ -111,7 +111,7 @@ void ConnectionManager::startTimer(int interval) { } void ConnectionManager::stopTimer() { - debug("timer stopped"); + debug(9, "timer stopped"); Common::TimerManager *manager = g_system->getTimerManager(); manager->removeTimerProc(connectionsThread); _timerStarted = false; @@ -146,7 +146,7 @@ void ConnectionManager::interateRequests() { _addedRequestsMutex.unlock(); //call handle() of all running requests (so they can do their work) - debug("handling %d request(s)", _requests.size()); + debug(9, "handling %d request(s)", _requests.size()); for (Common::Array<RequestWithCallback>::iterator i = _requests.begin(); i != _requests.end();) { Request *request = i->request; if (request) { @@ -182,7 +182,7 @@ void ConnectionManager::processTransfers() { if (stream) stream->finished(); if (curlMsg->msg == CURLMSG_DONE) { - debug("ConnectionManager: SUCCESS (%d - %s)", curlMsg->data.result, curl_easy_strerror(curlMsg->data.result)); + debug(9, "ConnectionManager: SUCCESS (%d - %s)", curlMsg->data.result, curl_easy_strerror(curlMsg->data.result)); } else { debug("ConnectionManager: FAILURE (CURLMsg (%d))", curlMsg->msg); } diff --git a/backends/networking/curl/connectionmanager.h b/backends/networking/curl/connectionmanager.h index 0da5d5ad84..b4ecbc9d73 100644 --- a/backends/networking/curl/connectionmanager.h +++ b/backends/networking/curl/connectionmanager.h @@ -98,7 +98,7 @@ public: * So, if you want to start any libcurl transfer, you must create * an easy handle and register it using this method. */ - void registerEasyHandle(CURL *easy); + void registerEasyHandle(CURL *easy) const; /** * Use this method to add new Request into manager's queue. @@ -119,7 +119,7 @@ public: void showCloudDisabledIcon(); /** Return URL-encoded version of given string. */ - Common::String urlEncode(Common::String s); + Common::String urlEncode(Common::String s) const; static uint32 getCloudRequestsPeriodInMicroseconds(); }; diff --git a/backends/networking/sdl_net/client.h b/backends/networking/sdl_net/client.h index f37d04658c..134c1be05d 100644 --- a/backends/networking/sdl_net/client.h +++ b/backends/networking/sdl_net/client.h @@ -53,6 +53,26 @@ public: virtual void handle(Client *client) = 0; }; +/** + * Client class represents one client's HTTP request + * to the LocalWebserver. + * + * While in READING_HEADERS state, it's kept in LocalWebserver. + * Client must read the headers and decide whether it's + * READ_HEADERS (could be handled) or BAD_REQUEST (failed). + * + * If it's READ_HEADERS, LocalWebserver searches for a corresponding + * BaseHandler. These classes use the information from headers - + * like method, path, GET parameters - to build the response + * for this client's request. When they do, they call setHandler() + * and pass a special ClientHandler. Client becomes BEING_HANDLED. + * + * While in that state, LocalWebserver calls Client's handle() and + * it's passed to ClientHandler. The latter does the job: it commands + * Client to read or write bytes with its socket or calls + * readContent() methods, so Client reads the request through Reader. + */ + class Client { ClientState _state; SDLNet_SocketSet _set; diff --git a/backends/networking/sdl_net/handlers/filespagehandler.cpp b/backends/networking/sdl_net/handlers/filespagehandler.cpp index 5ead4f31d9..2e934a31c6 100644 --- a/backends/networking/sdl_net/handlers/filespagehandler.cpp +++ b/backends/networking/sdl_net/handlers/filespagehandler.cpp @@ -168,7 +168,7 @@ bool FilesPageHandler::listDirectory(Common::String path, Common::String &conten return true; } -FilesPageHandler::ItemType FilesPageHandler::detectType(bool isDirectory, const Common::String &name) const { +FilesPageHandler::ItemType FilesPageHandler::detectType(bool isDirectory, const Common::String &name) { if (isDirectory) return IT_DIRECTORY; if (name.hasSuffix(".txt")) return IT_TXT; if (name.hasSuffix(".zip")) return IT_ZIP; @@ -176,7 +176,7 @@ FilesPageHandler::ItemType FilesPageHandler::detectType(bool isDirectory, const return IT_UNKNOWN; } -void FilesPageHandler::addItem(Common::String &content, const Common::String &itemTemplate, ItemType itemType, Common::String path, Common::String name, Common::String size) { +void FilesPageHandler::addItem(Common::String &content, const Common::String &itemTemplate, ItemType itemType, Common::String path, Common::String name, Common::String size) const { Common::String item = itemTemplate, icon; bool isDirectory = (itemType == IT_DIRECTORY || itemType == IT_PARENT_DIRECTORY); switch (itemType) { diff --git a/backends/networking/sdl_net/handlers/filespagehandler.h b/backends/networking/sdl_net/handlers/filespagehandler.h index f102acd9fc..c66b79e093 100644 --- a/backends/networking/sdl_net/handlers/filespagehandler.h +++ b/backends/networking/sdl_net/handlers/filespagehandler.h @@ -47,10 +47,10 @@ class FilesPageHandler: public FilesBaseHandler { bool listDirectory(Common::String path, Common::String &content, const Common::String &itemTemplate); /** Helper method for detecting items' type. */ - ItemType detectType(bool isDirectory, const Common::String &name) const; + static ItemType detectType(bool isDirectory, const Common::String &name); /** Helper method for adding items into the files list. */ - void addItem(Common::String &content, const Common::String &itemTemplate, ItemType itemType, Common::String path, Common::String name, Common::String size = ""); + void addItem(Common::String &content, const Common::String &itemTemplate, ItemType itemType, Common::String path, Common::String name, Common::String size = "") const; public: FilesPageHandler(); diff --git a/backends/networking/sdl_net/handlers/indexpagehandler.cpp b/backends/networking/sdl_net/handlers/indexpagehandler.cpp index 9208cd0b15..e12016acba 100644 --- a/backends/networking/sdl_net/handlers/indexpagehandler.cpp +++ b/backends/networking/sdl_net/handlers/indexpagehandler.cpp @@ -36,7 +36,7 @@ void IndexPageHandler::handle(Client &client) { Common::String code = client.queryParameter("code"); if (code == "") { - // redirect to "/files" + // redirect to "/filesAJAX" HandlerUtils::setMessageHandler( client, Common::String::format( @@ -45,7 +45,7 @@ void IndexPageHandler::handle(Client &client) { client.queryParameter("path").c_str(), _("Open Files manager") ), - "/files" + "/filesAJAX" ); return; } diff --git a/backends/networking/sdl_net/handlers/listajaxhandler.cpp b/backends/networking/sdl_net/handlers/listajaxhandler.cpp index 8657ab52ed..6319485e3d 100644 --- a/backends/networking/sdl_net/handlers/listajaxhandler.cpp +++ b/backends/networking/sdl_net/handlers/listajaxhandler.cpp @@ -96,7 +96,7 @@ Common::JSONObject ListAjaxHandler::listDirectory(Common::String path) { return successResult; } -ListAjaxHandler::ItemType ListAjaxHandler::detectType(bool isDirectory, const Common::String &name) const { +ListAjaxHandler::ItemType ListAjaxHandler::detectType(bool isDirectory, const Common::String &name) { if (isDirectory) return IT_DIRECTORY; if (name.hasSuffix(".txt")) return IT_TXT; if (name.hasSuffix(".zip")) return IT_ZIP; diff --git a/backends/networking/sdl_net/handlers/listajaxhandler.h b/backends/networking/sdl_net/handlers/listajaxhandler.h index cef6a071a7..c157e1b0ba 100644 --- a/backends/networking/sdl_net/handlers/listajaxhandler.h +++ b/backends/networking/sdl_net/handlers/listajaxhandler.h @@ -48,10 +48,10 @@ class ListAjaxHandler: public FilesBaseHandler { Common::JSONObject listDirectory(Common::String path); /** Helper method for detecting items' type. */ - ItemType detectType(bool isDirectory, const Common::String &name) const; + static ItemType detectType(bool isDirectory, const Common::String &name); /** Helper method for adding items into the files list. */ - void addItem(Common::JSONArray &responseItemsList, ItemType itemType, Common::String path, Common::String name, Common::String size = ""); + static void addItem(Common::JSONArray &responseItemsList, ItemType itemType, Common::String path, Common::String name, Common::String size = ""); public: ListAjaxHandler(); diff --git a/backends/networking/sdl_net/handlerutils.cpp b/backends/networking/sdl_net/handlerutils.cpp index f249ee1e29..cdc505bfa9 100644 --- a/backends/networking/sdl_net/handlerutils.cpp +++ b/backends/networking/sdl_net/handlerutils.cpp @@ -110,8 +110,9 @@ void HandlerUtils::setMessageHandler(Client &client, Common::String message, Com void HandlerUtils::setFilesManagerErrorMessageHandler(Client &client, Common::String message, Common::String redirectTo) { setMessageHandler(client, Common::String::format( - "%s<br/><a href=\"files?path=%s\">%s</a>", + "%s<br/><a href=\"files%s?path=%s\">%s</a>", message.c_str(), + client.queryParameter("ajax") == "true" ? "AJAX": "", "%2F", //that's encoded "/" _("Back to the files manager") ), redirectTo diff --git a/backends/networking/sdl_net/localwebserver.cpp b/backends/networking/sdl_net/localwebserver.cpp index 7c3edfc17f..baa2974aee 100644 --- a/backends/networking/sdl_net/localwebserver.cpp +++ b/backends/networking/sdl_net/localwebserver.cpp @@ -323,7 +323,7 @@ void LocalWebserver::resolveAddress(void *ipAddress) { */ // use the address found - _address = "http://" + addr + Common::String::format(":%u/", SERVER_PORT); + _address = "http://" + addr + Common::String::format(":%u/", _serverPort); } if (ifAddrStruct != NULL) freeifaddrs(ifAddrStruct); diff --git a/backends/networking/sdl_net/localwebserver.h b/backends/networking/sdl_net/localwebserver.h index 107c21e341..9c677f36b0 100644 --- a/backends/networking/sdl_net/localwebserver.h +++ b/backends/networking/sdl_net/localwebserver.h @@ -81,8 +81,7 @@ class LocalWebserver : public Common::Singleton<LocalWebserver> { void handle(); void handleClient(uint32 i); void acceptClient(); - - void resolveAddress(void *ipAddress); + void resolveAddress(void *ipAddress); public: static const uint32 DEFAULT_SERVER_PORT = 12345; diff --git a/backends/networking/sdl_net/reader.h b/backends/networking/sdl_net/reader.h index d234a82782..d4a0ba4e88 100644 --- a/backends/networking/sdl_net/reader.h +++ b/backends/networking/sdl_net/reader.h @@ -40,6 +40,38 @@ enum ReaderState { RS_READING_CONTENT }; +/** + * This is a helper class for Client. + * + * It parses HTTP request and finds headers + * and content. It also supports POST form/multipart. + * + * One might pass the request even byte by byte, + * Reader will always be able to continue from the + * state it stopped on. + * + * Main headers/content must be read with + * readFirstHeaders() and readFirstContent() methods. + * Further headers/content blocks (POST form/multipart) + * must be read with readBlockHeaders() and readBlockContent(). + * + * Main headers and parsed URL components could be accessed + * with special methods after reading. + * + * To use the object, call setContent() and then one of those + * reading methods. It would return whether reading is over + * or not. If reading is over, content stream still could + * contain bytes to read with other methods. + * + * If reading is not over, Reader awaits you to call the + * same reading method when you'd get more content. + * + * If it's over, you should check whether Reader awaits + * more content with noMoreContent() and call the other + * reading method, if it is. When headers are read, one + * must read contents, and vice versa. + */ + class Reader { ReaderState _state; Common::MemoryReadWriteStream *_content; diff --git a/backends/networking/sdl_net/uploadfileclienthandler.h b/backends/networking/sdl_net/uploadfileclienthandler.h index f61a2fab5e..4aa6929888 100644 --- a/backends/networking/sdl_net/uploadfileclienthandler.h +++ b/backends/networking/sdl_net/uploadfileclienthandler.h @@ -36,6 +36,16 @@ enum UploadFileHandlerState { UFH_STOP }; +/** + * This class handles POST form/multipart upload. + * + * handleBlockHeaders() looks for filename and, if it's found, + * handleBlockContent() saves content into the file with such name. + * + * If no file found or other error occurs, it sets + * default error message handler. + */ + class UploadFileClientHandler: public ClientHandler { UploadFileHandlerState _state; Common::MemoryReadWriteStream *_headersStream; |