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
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "backends/networking/sdl_net/handlers/connectcloudhandler.h"
#include "backends/fs/fs-factory.h"
#include "backends/cloud/cloudmanager.h"
#include "backends/networking/curl/curljsonrequest.h"
#include "backends/networking/sdl_net/getclienthandler.h"
#include "backends/networking/sdl_net/handlerutils.h"
#include "backends/networking/sdl_net/localwebserver.h"
#include "backends/networking/sdl_net/reader.h"
#include "common/formats/json.h"
#include "common/memstream.h"
#include "common/callback.h"
namespace Networking {
ConnectCloudHandler::ConnectCloudHandler() : _storageConnectionCallback(nullptr) {}
ConnectCloudHandler::~ConnectCloudHandler() {}
void ConnectCloudHandler::handle(Client &client) {
client.setHandler(new ConnectCloudClientHandler(this));
}
void ConnectCloudHandler::storageConnected(const Networking::ErrorResponse &response) const {
if (_storageConnectionCallback)
(*_storageConnectionCallback)(response);
}
//
ConnectCloudClientHandler::ConnectCloudClientHandler(const ConnectCloudHandler *cloudHandler):
_cloudHandler(cloudHandler), _clientContent(DisposeAfterUse::YES), _client(nullptr) {}
ConnectCloudClientHandler::~ConnectCloudClientHandler() {}
void ConnectCloudClientHandler::respond(Client &client, const Common::String &response, long responseCode) const {
Common::SeekableReadStream *responseStream = HandlerUtils::makeResponseStreamFromString(response);
GetClientHandler *responseHandler = new GetClientHandler(responseStream);
responseHandler->setResponseCode(responseCode);
responseHandler->setHeader("Access-Control-Allow-Origin", "https://cloud.scummvm.org");
responseHandler->setHeader("Access-Control-Allow-Methods", "POST");
responseHandler->setHeader("Access-Control-Allow-Headers", "Content-Type");
client.setHandler(responseHandler);
}
void ConnectCloudClientHandler::respondWithJson(Client &client, bool error, const Common::String &message, long responseCode) const {
Common::JSONObject response;
response.setVal("error", new Common::JSONValue(error));
response.setVal("message", new Common::JSONValue(message));
Common::JSONValue json = response;
respond(client, json.stringify(true), responseCode);
}
void ConnectCloudClientHandler::handleError(Client &client, const Common::String &message, long responseCode) const {
respondWithJson(client, true, message, responseCode);
}
void ConnectCloudClientHandler::handleSuccess(Client &client, const Common::String &message) const {
respondWithJson(client, false, message);
}
/// public
void ConnectCloudClientHandler::handle(Client *client) {
if (client == nullptr) {
warning("ConnectCloudClientHandler::handle(): empty client pointer");
return;
}
_client = client;
if (client->method() == "OPTIONS") {
respond(*client, "", 200);
return;
}
if (client->method() != "POST") {
handleError(*client, "Method Not Allowed", 405);
return;
}
if (_clientContent.size() > SUSPICIOUS_CONTENT_SIZE) {
handleError(*client, "Bad Request", 400);
return;
}
if (!client->readContent(&_clientContent))
return;
char *contents = Common::JSON::untaintContents(_clientContent);
Common::JSONValue *json = Common::JSON::parse(contents);
if (json == nullptr) {
handleError(*client, "Not Acceptable", 406);
return;
}
Networking::ErrorCallback callback = new Common::Callback<ConnectCloudClientHandler, const Networking::ErrorResponse &>(this, &ConnectCloudClientHandler::storageConnectionCallback);
Networking::JsonResponse jsonResponse(nullptr, json);
if (!CloudMan.connectStorage(jsonResponse, callback)) { // JSON doesn't have "storage" in it or it was invalid
delete json;
delete callback;
handleError(*client, "Not Acceptable", 406);
}
}
void ConnectCloudClientHandler::storageConnectionCallback(const Networking::ErrorResponse &response) {
if (response.failed || response.interrupted) {
Common::String message = "Failed to connect storage.";
if (response.failed) {
message += " Context: ";
message += response.response.c_str();
}
handleError(*_client, message, 200);
} else {
handleSuccess(*_client, "Storage connected.");
}
_cloudHandler->storageConnected(response);
}
} // End of namespace Networking
|