Initial commit

This commit is contained in:
2026-02-02 04:50:13 +01:00
commit 5b11698731
22592 changed files with 7677434 additions and 0 deletions

View File

@@ -0,0 +1,198 @@
/* 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/>.
*
*/
#define FORBIDDEN_SYMBOL_ALLOW_ALL
#include "backends/networking/sdl_net/client.h"
#include "backends/networking/sdl_net/localwebserver.h"
#include "common/memstream.h"
#include <SDL_net.h>
namespace Networking {
Client::Client():
_state(INVALID), _set(nullptr), _socket(nullptr), _handler(nullptr),
_previousHandler(nullptr), _stream(nullptr), _buffer(new byte[CLIENT_BUFFER_SIZE]) {}
Client::Client(SDLNet_SocketSet set, TCPsocket socket):
_state(INVALID), _set(nullptr), _socket(nullptr), _handler(nullptr),
_previousHandler(nullptr), _stream(nullptr), _buffer(new byte[CLIENT_BUFFER_SIZE]) {
open(set, socket);
}
Client::~Client() {
close();
delete[] _buffer;
}
void Client::open(SDLNet_SocketSet set, TCPsocket socket) {
if (_state != INVALID)
close();
_state = READING_HEADERS;
_socket = socket;
_set = set;
Reader cleanReader;
_reader = cleanReader;
if (_handler)
delete _handler;
_handler = nullptr;
if (_previousHandler)
delete _previousHandler;
_previousHandler = nullptr;
_stream = new Common::MemoryReadWriteStream(DisposeAfterUse::YES);
if (set) {
int numused = SDLNet_TCP_AddSocket(set, socket);
if (numused == -1) {
error("Client: SDLNet_AddSocket: %s\n", SDLNet_GetError());
}
}
}
bool Client::readMoreIfNeeded() {
if (_stream == nullptr)
return false; //nothing to read into
if (_stream->size() - _stream->pos() > 0)
return true; //not needed, some data left in the stream
if (!_socket)
return false;
if (!SDLNet_SocketReady(_socket))
return false;
int bytes = SDLNet_TCP_Recv(_socket, _buffer, CLIENT_BUFFER_SIZE);
if (bytes <= 0) {
warning("Client::readMoreIfNeeded: recv fail");
close();
return false;
}
if (_stream->write(_buffer, bytes) != (uint32)bytes) {
warning("Client::readMoreIfNeeded: failed to write() into MemoryReadWriteStream");
close();
return false;
}
return true;
}
void Client::readHeaders() {
if (!readMoreIfNeeded())
return;
_reader.setContent(_stream);
if (_reader.readFirstHeaders())
_state = (_reader.badRequest() ? BAD_REQUEST : READ_HEADERS);
}
bool Client::readContent(Common::WriteStream *stream) {
if (!readMoreIfNeeded())
return false;
_reader.setMode(RM_HTTP_GENERIC);
_reader.setContent(_stream);
return _reader.readContent(stream);
}
bool Client::readFirstContent(Common::WriteStream *stream) {
if (!readMoreIfNeeded())
return false;
_reader.setMode(RM_POST_FORM_MULTIPART);
_reader.setContent(_stream);
return _reader.readFirstContent(stream);
}
bool Client::readBlockHeaders(Common::WriteStream *stream) {
if (!readMoreIfNeeded())
return false;
_reader.setContent(_stream);
return _reader.readBlockHeaders(stream);
}
bool Client::readBlockContent(Common::WriteStream *stream) {
if (!readMoreIfNeeded())
return false;
_reader.setContent(_stream);
return _reader.readBlockContent(stream);
}
void Client::setHandler(ClientHandler *handler) {
if (_handler) {
if (_previousHandler)
delete _previousHandler;
_previousHandler = _handler; //can't just delete it, as setHandler() could've been called by handler itself
}
_state = BEING_HANDLED;
_handler = handler;
}
void Client::handle() {
if (_state != BEING_HANDLED)
warning("handle() called in a wrong Client's state");
if (!_handler)
warning("Client doesn't have handler to be handled by");
if (_handler)
_handler->handle(this);
}
void Client::close() {
if (_set) {
if (_socket) {
int numused = SDLNet_TCP_DelSocket(_set, _socket);
if (numused == -1)
error("Client: SDLNet_DelSocket: %s\n", SDLNet_GetError());
}
_set = nullptr;
}
if (_socket) {
SDLNet_TCP_Close(_socket);
_socket = nullptr;
}
if (_stream) {
delete _stream;
_stream = nullptr;
}
_state = INVALID;
}
ClientState Client::state() const { return _state; }
Common::String Client::headers() const { return _reader.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(const Common::String &name) const { return _reader.queryParameter(name); }
Common::String Client::anchor() const { return _reader.anchor(); }
bool Client::noMoreContent() const { return _reader.noMoreContent(); }
bool Client::socketIsReady() { return SDLNet_SocketReady(_socket); }
int Client::recv(void *data, int maxlen) { return SDLNet_TCP_Recv(_socket, data, maxlen); }
int Client::send(void *data, int len) { return SDLNet_TCP_Send(_socket, data, len); }
} // End of namespace Networking

View File

@@ -0,0 +1,125 @@
/* 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/>.
*
*/
#ifndef BACKENDS_NETWORKING_SDL_NET_CLIENT_H
#define BACKENDS_NETWORKING_SDL_NET_CLIENT_H
#include "backends/networking/sdl_net/reader.h"
#include "common/str.h"
namespace Common {
class MemoryReadWriteStream;
}
typedef struct _SDLNet_SocketSet *SDLNet_SocketSet;
typedef struct _TCPsocket *TCPsocket;
namespace Networking {
enum ClientState {
INVALID,
READING_HEADERS,
READ_HEADERS,
BAD_REQUEST,
BEING_HANDLED
};
class Client;
#define CLIENT_BUFFER_SIZE 1 * 1024 * 1024
class ClientHandler {
public:
virtual ~ClientHandler() {};
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;
TCPsocket _socket;
Reader _reader;
ClientHandler *_handler, *_previousHandler;
Common::MemoryReadWriteStream *_stream;
byte *_buffer;
bool readMoreIfNeeded();
public:
Client();
Client(SDLNet_SocketSet set, TCPsocket socket);
virtual ~Client();
void open(SDLNet_SocketSet set, TCPsocket socket);
void readHeaders();
bool readContent(Common::WriteStream *stream);
bool readFirstContent(Common::WriteStream *stream);
bool readBlockHeaders(Common::WriteStream *stream);
bool readBlockContent(Common::WriteStream *stream);
void setHandler(ClientHandler *handler);
void handle();
void close();
ClientState state() const;
Common::String headers() const;
Common::String method() const;
Common::String path() const;
Common::String query() const;
Common::String queryParameter(const Common::String &name) const;
Common::String anchor() const;
bool noMoreContent() const;
/**
* Return SDLNet_SocketReady(_socket).
*
* It's "ready" when it has something
* to read (recv()). You can send()
* when this is false.
*/
bool socketIsReady();
int recv(void *data, int maxlen);
int send(void *data, int len);
};
} // End of namespace Networking
#endif

View File

@@ -0,0 +1,161 @@
/* 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/getclienthandler.h"
#include "common/textconsole.h"
namespace Networking {
GetClientHandler::GetClientHandler(Common::SeekableReadStream *stream):
_responseCode(200), _headersPrepared(false),
_stream(stream), _buffer(new byte[CLIENT_HANDLER_BUFFER_SIZE]) {}
GetClientHandler::~GetClientHandler() {
delete _stream;
delete[] _buffer;
}
const char *GetClientHandler::responseMessage(long responseCode) {
switch (responseCode) {
case 100: return "Continue";
case 101: return "Switching Protocols";
case 102: return "Processing";
case 200: return "OK";
case 201: return "Created";
case 202: return "Accepted";
case 203: return "Non-Authoritative Information";
case 204: return "No Content";
case 205: return "Reset Content";
case 206: return "Partial Content";
case 207: return "Multi-Status";
case 226: return "IM Used";
case 300: return "Multiple Choices";
case 301: return "Moved Permanently";
case 302: return "Moved Temporarily"; //case 302: return "Found";
case 303: return "See Other";
case 304: return "Not Modified";
case 305: return "Use Proxy";
case 306: return "RESERVED";
case 307: return "Temporary Redirect";
case 400: return "Bad Request";
case 401: return "Unauthorized";
case 402: return "Payment Required";
case 403: return "Forbidden";
case 404: return "Not Found";
case 405: return "Method Not Allowed";
case 406: return "Not Acceptable";
case 407: return "Proxy Authentication Required";
case 408: return "Request Timeout";
case 409: return "Conflict";
case 410: return "Gone";
case 411: return "Length Required";
case 412: return "Precondition Failed";
case 413: return "Request Entity Too Large";
case 414: return "Request-URI Too Large";
case 415: return "Unsupported Media Type";
case 416: return "Requested Range Not Satisfiable";
case 417: return "Expectation Failed";
case 422: return "Unprocessable Entity";
case 423: return "Locked";
case 424: return "Failed Dependency";
case 425: return "Unordered Collection";
case 426: return "Upgrade Required";
case 428: return "Precondition Required";
case 429: return "Too Many Requests";
case 431: return "Request Header Fields Too Large";
case 434: return "Requested Host Unavailable";
case 449: return "Retry With";
case 451: return "Unavailable For Legal Reasons";
case 500: return "Internal Server Error";
case 501: return "Not Implemented";
case 502: return "Bad Gateway";
case 503: return "Service Unavailable";
case 504: return "Gateway Timeout";
case 505: return "HTTP Version Not Supported";
case 506: return "Variant Also Negotiates";
case 507: return "Insufficient Storage";
case 508: return "Loop Detected";
case 509: return "Bandwidth Limit Exceeded";
case 510: return "Not Extended";
case 511: return "Network Authentication Required";
}
return "Unknown";
}
void GetClientHandler::prepareHeaders() {
if (!_specialHeaders.contains("Content-Type"))
setHeader("Content-Type", "text/html; charset=UTF-8");
if (!_specialHeaders.contains("Content-Length") && _stream)
setHeader("Content-Length", Common::String::format("%u", unsigned(_stream->size())));
_headers = Common::String::format("HTTP/1.1 %ld %s\r\n", _responseCode, responseMessage(_responseCode));
for (auto &specialHeader : _specialHeaders)
_headers += specialHeader._key + ": " + specialHeader._value + "\r\n";
_headers += "\r\n";
_headersPrepared = true;
}
void GetClientHandler::handle(Client *client) {
if (!client)
return;
if (!_headersPrepared)
prepareHeaders();
uint32 readBytes;
// send headers first
if (_headers.size() > 0) {
readBytes = _headers.size();
if (readBytes > CLIENT_HANDLER_BUFFER_SIZE)
readBytes = CLIENT_HANDLER_BUFFER_SIZE;
memcpy(_buffer, _headers.c_str(), readBytes);
_headers.erase(0, readBytes);
} else {
if (!_stream) {
client->close();
return;
}
readBytes = _stream->read(_buffer, CLIENT_HANDLER_BUFFER_SIZE);
}
if (readBytes != 0)
if (client->send(_buffer, readBytes) != (int)readBytes) {
warning("GetClientHandler: unable to send all bytes to the client");
client->close();
return;
}
// we're done here!
if (_stream->eos())
client->close();
}
void GetClientHandler::setHeader(const Common::String &name, const Common::String &value) { _specialHeaders[name] = value; }
void GetClientHandler::setResponseCode(long code) { _responseCode = code; }
} // End of namespace Networking

View File

@@ -0,0 +1,56 @@
/* 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/>.
*
*/
#ifndef BACKENDS_NETWORKING_SDL_NET_GETCLIENTHANDLER_H
#define BACKENDS_NETWORKING_SDL_NET_GETCLIENTHANDLER_H
#include "backends/networking/sdl_net/client.h"
#include "common/hashmap.h"
#include "common/stream.h"
#include "common/hash-str.h"
namespace Networking {
#define CLIENT_HANDLER_BUFFER_SIZE 1 * 1024 * 1024
class GetClientHandler: public ClientHandler {
Common::HashMap<Common::String, Common::String> _specialHeaders;
long _responseCode;
bool _headersPrepared;
Common::String _headers;
Common::SeekableReadStream *_stream;
byte *_buffer;
static const char *responseMessage(long responseCode);
void prepareHeaders();
public:
GetClientHandler(Common::SeekableReadStream *stream);
~GetClientHandler() override;
void handle(Client *client) override;
void setHeader(const Common::String &name, const Common::String &value);
void setResponseCode(long code);
};
} // End of namespace Networking
#endif

View File

@@ -0,0 +1,40 @@
/* 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/>.
*
*/
#ifndef BACKENDS_NETWORKING_SDL_NET_BASEHANDLER_H
#define BACKENDS_NETWORKING_SDL_NET_BASEHANDLER_H
#include "backends/networking/sdl_net/client.h"
namespace Networking {
class BaseHandler {
public:
BaseHandler() {}
virtual ~BaseHandler() {}
virtual void handle(Client &) = 0;
virtual bool minimalModeSupported() { return false; }
};
} // End of namespace Networking
#endif

View File

@@ -0,0 +1,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/http/httpjsonrequest.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::zeroTerminateContents(_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

View File

@@ -0,0 +1,70 @@
/* 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/>.
*
*/
#ifndef BACKENDS_NETWORKING_SDL_NET_CONNECTCLOUDHANDLER_H
#define BACKENDS_NETWORKING_SDL_NET_CONNECTCLOUDHANDLER_H
#include "backends/networking/sdl_net/handlers/basehandler.h"
#include "backends/networking/sdl_net/client.h"
#include "backends/networking/http/request.h"
#include "common/memstream.h"
namespace Networking {
class ConnectCloudHandler: public BaseHandler {
void handleError(Client &client, const Common::String &message) const;
void setJsonResponseHandler(Client &client, const Common::String &type, const Common::String &message) const;
Networking::ErrorCallback _storageConnectionCallback;
public:
ConnectCloudHandler();
~ConnectCloudHandler() override;
void handle(Client &client) override;
bool minimalModeSupported() override { return true; }
void setStorageConnectionCallback(Networking::ErrorCallback cb) { _storageConnectionCallback = cb; }
void storageConnected(const Networking::ErrorResponse& response) const;
};
class ConnectCloudClientHandler : public ClientHandler {
const ConnectCloudHandler *_cloudHandler;
Common::MemoryWriteStreamDynamic _clientContent;
Client *_client;
static const int32 SUSPICIOUS_CONTENT_SIZE = 640 * 1024; // 640K ought to be enough for anybody
void respond(Client &client, const Common::String &response, long responseCode = 200) const;
void respondWithJson(Client &client, bool error, const Common::String &message, long responseCode = 200) const;
void handleError(Client &client, const Common::String &message, long responseCode) const;
void handleSuccess(Client &client, const Common::String &message) const;
void storageConnectionCallback(const Networking::ErrorResponse &response);
public:
ConnectCloudClientHandler(const ConnectCloudHandler* cloudHandler);
~ConnectCloudClientHandler() override;
void handle(Client *client) override;
};
} // End of namespace Networking
#endif

View File

@@ -0,0 +1,126 @@
/* 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/createdirectoryhandler.h"
#include "backends/networking/sdl_net/handlerutils.h"
#include "backends/networking/sdl_net/localwebserver.h"
#include "common/translation.h"
#include "common/callback.h"
namespace Networking {
CreateDirectoryHandler::CreateDirectoryHandler() {}
CreateDirectoryHandler::~CreateDirectoryHandler() {}
void CreateDirectoryHandler::handleError(Client &client, const Common::String &message) const {
if (client.queryParameter("answer_json") == "true")
setJsonResponseHandler(client, "error", message);
else
HandlerUtils::setFilesManagerErrorMessageHandler(client, message);
}
void CreateDirectoryHandler::setJsonResponseHandler(Client &client, const Common::String &type, const Common::String &message) const {
Common::JSONObject response;
response.setVal("type", new Common::JSONValue(type));
response.setVal("message", new Common::JSONValue(message));
Common::JSONValue json = response;
LocalWebserver::setClientGetHandler(client, json.stringify(true));
}
/// public
void CreateDirectoryHandler::handle(Client &client) {
Common::String path = client.queryParameter("path");
Common::String name = client.queryParameter("directory_name");
// check that <path> is not an absolute root
if (path == "" || path == "/") {
handleError(client, Common::convertFromU32String(_("Can't create directory here!")));
return;
}
// check that <path> contains no '../'
if (HandlerUtils::hasForbiddenCombinations(path)) {
handleError(client, Common::convertFromU32String(_("Invalid path!")));
return;
}
// transform virtual path to actual file system one
Common::String basePath;
Common::Path baseFSPath, fsPath;
if (!urlToPath(path, fsPath, basePath, baseFSPath)) {
handleError(client, Common::convertFromU32String(_("Invalid path!")));
return;
}
// check that <path> exists, is directory and isn't forbidden
Common::FSNode node(fsPath);
if (!HandlerUtils::permittedPath(node.getPath())) {
handleError(client, Common::convertFromU32String(_("Invalid path!")));
return;
}
if (!node.exists()) {
handleError(client, Common::convertFromU32String(_("Parent directory doesn't exists!")));
return;
}
if (!node.isDirectory()) {
handleError(client, Common::convertFromU32String(_("Can't create a directory within a file!")));
return;
}
// check that <directory_name> doesn't exist or is directory
node = Common::FSNode(fsPath.appendComponent(name));
if (node.exists()) {
if (!node.isDirectory()) {
handleError(client, Common::convertFromU32String(_("There is a file with that name in the parent directory!")));
return;
}
} else {
// create the <directory_name> in <path>
if (!node.createDirectory()) {
handleError(client, Common::convertFromU32String(_("Failed to create the directory!")));
return;
}
}
// if json requested, respond with it
if (client.queryParameter("answer_json") == "true") {
setJsonResponseHandler(client, "success", Common::convertFromU32String(_("Directory created successfully!")));
return;
}
// set redirect on success
HandlerUtils::setMessageHandler(
client,
Common::String::format(
"%s<br/><a href=\"files?path=%s\">%s</a>",
Common::convertFromU32String(_("Directory created successfully!")).c_str(),
client.queryParameter("path").c_str(),
Common::convertFromU32String(_("Back to parent directory")).c_str()
),
(client.queryParameter("ajax") == "true" ? "/filesAJAX?path=" : "/files?path=") +
LocalWebserver::urlEncodeQueryParameterValue(client.queryParameter("path"))
);
}
} // End of namespace Networking

View File

@@ -0,0 +1,41 @@
/* 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/>.
*
*/
#ifndef BACKENDS_NETWORKING_SDL_NET_CREATEDIRECTORYHANDLER_H
#define BACKENDS_NETWORKING_SDL_NET_CREATEDIRECTORYHANDLER_H
#include "backends/networking/sdl_net/handlers/filesbasehandler.h"
namespace Networking {
class CreateDirectoryHandler: public FilesBaseHandler {
void handleError(Client &client, const Common::String &message) const;
void setJsonResponseHandler(Client &client, const Common::String &type, const Common::String &message) const;
public:
CreateDirectoryHandler();
~CreateDirectoryHandler() override;
void handle(Client &client) override;
};
} // End of namespace Networking
#endif

View File

@@ -0,0 +1,87 @@
/* 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/downloadfilehandler.h"
#include "backends/networking/sdl_net/getclienthandler.h"
#include "backends/networking/sdl_net/handlerutils.h"
#include "backends/networking/sdl_net/localwebserver.h"
#include "common/translation.h"
namespace Networking {
DownloadFileHandler::DownloadFileHandler() {}
DownloadFileHandler::~DownloadFileHandler() {}
/// public
void DownloadFileHandler::handle(Client &client) {
Common::String path = client.queryParameter("path");
// check that <path> is not an absolute root
if (path == "" || path == "/") {
HandlerUtils::setFilesManagerErrorMessageHandler(client, Common::convertFromU32String(_("Invalid path!")));
return;
}
// check that <path> contains no '../'
if (HandlerUtils::hasForbiddenCombinations(path)) {
HandlerUtils::setFilesManagerErrorMessageHandler(client, Common::convertFromU32String(_("Invalid path!")));
return;
}
// transform virtual path to actual file system one
Common::String basePath;
Common::Path baseFSPath, fsPath;
if (!urlToPath(path, fsPath, basePath, baseFSPath, false) || path.empty()) {
HandlerUtils::setFilesManagerErrorMessageHandler(client, Common::convertFromU32String(_("Invalid path!")));
return;
}
// check that <path> exists, is directory and isn't forbidden
Common::FSNode node(fsPath);
if (!HandlerUtils::permittedPath(node.getPath())) {
HandlerUtils::setFilesManagerErrorMessageHandler(client, Common::convertFromU32String(_("Invalid path!")));
return;
}
if (!node.exists()) {
HandlerUtils::setFilesManagerErrorMessageHandler(client, Common::convertFromU32String(_("The file doesn't exist!")));
return;
}
if (node.isDirectory()) {
HandlerUtils::setFilesManagerErrorMessageHandler(client, Common::convertFromU32String(_("Can't download a directory!")));
return;
}
Common::SeekableReadStream *stream = node.createReadStream();
if (stream == nullptr) {
HandlerUtils::setFilesManagerErrorMessageHandler(client, Common::convertFromU32String(_("Failed to read the file!")));
return;
}
GetClientHandler *handler = new GetClientHandler(stream);
handler->setResponseCode(200);
handler->setHeader("Content-Type", "application/force-download");
handler->setHeader("Content-Disposition", "attachment; filename=\"" + node.getName() + "\"");
handler->setHeader("Content-Transfer-Encoding", "binary");
client.setHandler(handler);
}
} // End of namespace Networking

View File

@@ -0,0 +1,39 @@
/* 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/>.
*
*/
#ifndef BACKENDS_NETWORKING_SDL_NET_DOWNLOADFILEHANDLER_H
#define BACKENDS_NETWORKING_SDL_NET_DOWNLOADFILEHANDLER_H
#include "backends/networking/sdl_net/handlers/filesbasehandler.h"
namespace Networking {
class DownloadFileHandler: public FilesBaseHandler {
public:
DownloadFileHandler();
~DownloadFileHandler() override;
void handle(Client &client) override;
};
} // End of namespace Networking
#endif

View File

@@ -0,0 +1,80 @@
/* 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/filesajaxpagehandler.h"
#include "backends/networking/sdl_net/handlerutils.h"
#include "backends/networking/sdl_net/localwebserver.h"
#include "common/translation.h"
namespace Networking {
#define FILES_PAGE_NAME ".filesAJAX.html"
FilesAjaxPageHandler::FilesAjaxPageHandler() {}
FilesAjaxPageHandler::~FilesAjaxPageHandler() {}
namespace {
Common::String encodeDoubleQuotesAndSlashes(const Common::String &s) {
Common::String result = "";
for (uint32 i = 0; i < s.size(); ++i)
if (s[i] == '"') {
result += "\\\"";
} else if (s[i] == '\\') {
result += "\\\\";
} else {
result += s[i];
}
return result;
}
}
/// public
void FilesAjaxPageHandler::handle(Client &client) {
// load stylish response page from the archive
Common::SeekableReadStream *const stream = HandlerUtils::getArchiveFile(FILES_PAGE_NAME);
if (stream == nullptr) {
HandlerUtils::setFilesManagerErrorMessageHandler(client, Common::convertFromU32String(_("The page is not available without the resources. Make sure file wwwroot.zip from ScummVM distribution is available in 'themepath'.")));
return;
}
Common::String response = HandlerUtils::readEverythingFromStream(stream);
Common::String path = client.queryParameter("path");
//these occur twice:
replace(response, "{create_directory_button}", _("Create directory").encode());
replace(response, "{create_directory_button}", _("Create directory").encode());
replace(response, "{upload_files_button}", _("Upload files").encode()); //tab
replace(response, "{upload_file_button}", _("Upload files").encode()); //button in the tab
replace(response, "{create_directory_desc}", _("Type new directory name:").encode());
replace(response, "{upload_file_desc}", _("Select a file to upload:").encode());
replace(response, "{or_upload_directory_desc}", _("Or select a directory (works in Chrome only):").encode());
replace(response, "{index_of}", _("Index of ").encode());
replace(response, "{loading}", ("Loading..."));
replace(response, "{error}", _("Error occurred").encode());
replace(response, "{start_path}", encodeDoubleQuotesAndSlashes(path));
LocalWebserver::setClientGetHandler(client, response);
}
} // End of namespace Networking

View File

@@ -0,0 +1,39 @@
/* 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/>.
*
*/
#ifndef BACKENDS_NETWORKING_SDL_NET_FILESAJAXPAGEHANDLER_H
#define BACKENDS_NETWORKING_SDL_NET_FILESAJAXPAGEHANDLER_H
#include "backends/networking/sdl_net/handlers/filesbasehandler.h"
namespace Networking {
class FilesAjaxPageHandler: public FilesBaseHandler {
public:
FilesAjaxPageHandler();
~FilesAjaxPageHandler() override;
void handle(Client &client) override;
};
} // End of namespace Networking
#endif

View File

@@ -0,0 +1,83 @@
/* 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/filesbasehandler.h"
#include "backends/saves/default/default-saves.h"
#include "common/config-manager.h"
#include "common/system.h"
namespace Networking {
FilesBaseHandler::FilesBaseHandler() {}
FilesBaseHandler::~FilesBaseHandler() {}
Common::String FilesBaseHandler::parentPath(const Common::String &path) {
Common::String result = path;
if (result.size() && (result.lastChar() == '/' || result.lastChar() == '\\')) result.deleteLastChar();
if (!result.empty()) {
for (int i = result.size() - 1; i >= 0; --i)
if (i == 0 || result[i] == '/' || result[i] == '\\') {
result.erase(i);
break;
}
}
if (result.size() && result.lastChar() != '/' && result.lastChar() != '\\')
result += '/';
return result;
}
bool FilesBaseHandler::urlToPath(Common::String &url, Common::Path &path, Common::String &baseUrl, Common::Path &basePath, bool isDirectory) {
// <url> is not empty, but could lack the trailing slash
if (isDirectory && url.lastChar() != '/')
url += '/';
if (url.hasPrefix("/root") && ConfMan.hasKey("rootpath", "cloud")) {
baseUrl = "/root/";
basePath = ConfMan.getPath("rootpath", "cloud");
url.erase(0, 5);
if (url.size() && url[0] == '/')
url.deleteChar(0); // if that was "/root/ab/c", it becomes "/ab/c", but we need "ab/c"
path = basePath.join(url, '/');
return true;
}
if (url.hasPrefix("/saves")) {
baseUrl = "/saves/";
// determine savepath (prefix to remove)
#ifdef USE_CLOUD
DefaultSaveFileManager *manager = dynamic_cast<DefaultSaveFileManager *>(g_system->getSavefileManager());
basePath = (manager ? manager->concatWithSavesPath("") : ConfMan.getPath("savepath"));
#else
basePath = ConfMan.getPath("savepath");
#endif
url.erase(0, 6);
if (url.size() && url[0] == '/')
url.deleteChar(0);
path = basePath.join(url, '/');
return true;
}
return false;
}
} // End of namespace Networking

View File

@@ -0,0 +1,53 @@
/* 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/>.
*
*/
#ifndef BACKENDS_NETWORKING_SDL_NET_FILESBASEHANDLER_H
#define BACKENDS_NETWORKING_SDL_NET_FILESBASEHANDLER_H
#include "backends/networking/sdl_net/handlers/basehandler.h"
namespace Common {
class Path;
}
namespace Networking {
class FilesBaseHandler: public BaseHandler {
protected:
Common::String parentPath(const Common::String &path);
/**
* Transforms virtual <url> into actual file system path.
*
* Fills base path with actual file system prefix
* and base URL with virtual prefix
*
* Returns true on success.
*/
bool urlToPath(Common::String &url, Common::Path &path, Common::String &baseUrl, Common::Path &basePath, bool isDirectory = true);
public:
FilesBaseHandler();
~FilesBaseHandler() override;
};
} // End of namespace Networking
#endif

View File

@@ -0,0 +1,233 @@
/* 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/filespagehandler.h"
#include "backends/networking/sdl_net/handlerutils.h"
#include "backends/networking/sdl_net/localwebserver.h"
#include "common/config-manager.h"
#include "common/translation.h"
namespace Networking {
#define INDEX_PAGE_NAME ".index.html"
#define FILES_PAGE_NAME ".files.html"
FilesPageHandler::FilesPageHandler() {}
FilesPageHandler::~FilesPageHandler() {}
namespace {
Common::String encodeDoubleQuotes(const Common::String &s) {
Common::String result = "";
for (uint32 i = 0; i < s.size(); ++i)
if (s[i] == '"') {
result += "\\\"";
} else {
result += s[i];
}
return result;
}
Common::String encodeHtmlEntities(const Common::String &s) {
Common::String result = "";
for (uint32 i = 0; i < s.size(); ++i)
if (s[i] == '<')
result += "&lt;";
else if (s[i] == '>')
result += "&gt;";
else if (s[i] == '&')
result += "&amp;";
else if ((byte)s[i] > (byte)0x7F)
result += Common::String::format("&#%d;", (int)s[i]);
else result += s[i];
return result;
}
Common::String getDisplayPath(const Common::String &s) {
Common::String result = "";
for (uint32 i = 0; i < s.size(); ++i)
if (s[i] == '\\')
result += '/';
else
result += s[i];
if (result == "")
return "/";
return result;
}
}
bool FilesPageHandler::listDirectory(const Common::String &path_, Common::String &content, const Common::String &itemTemplate) {
if (path_ == "" || path_ == "/") {
if (ConfMan.hasKey("rootpath", "cloud"))
addItem(content, itemTemplate, IT_DIRECTORY, "/root/", Common::convertFromU32String(_("File system root")));
addItem(content, itemTemplate, IT_DIRECTORY, "/saves/", Common::convertFromU32String(_("Saved games")));
return true;
}
if (HandlerUtils::hasForbiddenCombinations(path_))
return false;
Common::String path = path_;
Common::String basePath;
Common::Path baseFSPath, fsPath;
if (!urlToPath(path, fsPath, basePath, baseFSPath))
return false;
Common::FSNode node = Common::FSNode(fsPath);
if (!HandlerUtils::permittedPath(node.getPath()))
return false;
if (!node.isDirectory())
return false;
// list directory
Common::FSList _nodeContent;
if (!node.getChildren(_nodeContent, Common::FSNode::kListAll, false)) // do not show hidden files
_nodeContent.clear();
else
Common::sort(_nodeContent.begin(), _nodeContent.end());
// add parent directory link
{
Common::Path relPath = fsPath.relativeTo(baseFSPath);
relPath = relPath.getParent();
Common::String filePath("/");
if (!relPath.empty())
filePath = basePath + relPath.toString('/');
addItem(content, itemTemplate, IT_PARENT_DIRECTORY, filePath, Common::convertFromU32String(_("Parent directory")));
}
// fill the content
for (auto &i : _nodeContent) {
Common::String name = i.getName();
if (i.isDirectory())
name += "/";
Common::Path relPath = i.getPath().relativeTo(baseFSPath);
Common::String filePath(basePath);
filePath += relPath.toString('/');
addItem(content, itemTemplate, detectType(i.isDirectory(), name), filePath, name);
}
return true;
}
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;
if (name.hasSuffix(".7z"))
return IT_7Z;
return IT_UNKNOWN;
}
void FilesPageHandler::addItem(Common::String &content, const Common::String &itemTemplate, ItemType itemType, const Common::String &path, const Common::String &name, const Common::String &size) const {
Common::String item = itemTemplate, icon;
bool isDirectory = (itemType == IT_DIRECTORY || itemType == IT_PARENT_DIRECTORY);
switch (itemType) {
case IT_DIRECTORY:
icon = "dir.png";
break;
case IT_PARENT_DIRECTORY:
icon = "up.png";
break;
case IT_TXT:
icon = "txt.png";
break;
case IT_ZIP:
icon = "zip.png";
break;
case IT_7Z:
icon = "7z.png";
break;
default:
icon = "unk.png";
}
replace(item, "{icon}", icon);
replace(item, "{link}", (isDirectory ? "files?path=" : "download?path=") + LocalWebserver::urlEncodeQueryParameterValue(path));
replace(item, "{name}", encodeHtmlEntities(name));
replace(item, "{size}", size);
content += item;
}
/// public
void FilesPageHandler::handle(Client &client) {
Common::String response =
"<html>" \
"<head><title>ScummVM</title><meta charset=\"utf-8\"/></head>" \
"<body>" \
"<p>{create_directory_desc}</p>" \
"<form action=\"create\">" \
"<input type=\"hidden\" name=\"path\" value=\"{path}\"/>" \
"<input type=\"text\" name=\"directory_name\" value=\"\"/>" \
"<input type=\"submit\" value=\"{create_directory_button}\"/>" \
"</form>" \
"<hr/>" \
"<p>{upload_file_desc}</p>" \
"<form action=\"upload?path={path}\" method=\"post\" enctype=\"multipart/form-data\">" \
"<input type=\"file\" name=\"upload_file-f\" allowdirs multiple/>" \
"<span>{or_upload_directory_desc}</span>" \
"<input type=\"file\" name=\"upload_file-d\" directory webkitdirectory multiple/>" \
"<input type=\"submit\" value=\"{upload_file_button}\"/>" \
"</form>"
"<hr/>" \
"<h1>{index_of_directory}</h1>" \
"<table>{content}</table>" \
"</body>" \
"</html>";
Common::String itemTemplate = "<tr><td><img src=\"icons/{icon}\"/></td><td><a href=\"{link}\">{name}</a></td><td>{size}</td></tr>\n"; //TODO: load this template too?
// load stylish response page from the archive
Common::SeekableReadStream *const stream = HandlerUtils::getArchiveFile(FILES_PAGE_NAME);
if (stream)
response = HandlerUtils::readEverythingFromStream(stream);
Common::String path = client.queryParameter("path");
Common::String content = "";
// show an error message if failed to list directory
if (!listDirectory(path, content, itemTemplate)) {
HandlerUtils::setFilesManagerErrorMessageHandler(client, Common::convertFromU32String(_("ScummVM couldn't list the directory you specified.")).c_str());
return;
}
//some of these occur twice:
replace(response, "{create_directory_button}", _("Create directory").encode());
replace(response, "{create_directory_button}", _("Create directory").encode());
replace(response, "{path}", encodeDoubleQuotes(client.queryParameter("path")));
replace(response, "{path}", encodeDoubleQuotes(client.queryParameter("path")));
replace(response, "{upload_files_button}", _("Upload files").encode()); //tab
replace(response, "{upload_file_button}", _("Upload files").encode()); //button in the tab
replace(response, "{create_directory_desc}", _("Type new directory name:").encode());
replace(response, "{upload_file_desc}", _("Select a file to upload:").encode());
replace(response, "{or_upload_directory_desc}", _("Or select a directory (works in Chrome only):").encode());
replace(response, "{index_of_directory}", Common::String::format("%s %s", _("Index of").encode().c_str(), encodeHtmlEntities(getDisplayPath(client.queryParameter("path"))).c_str()));
replace(response, "{content}", content);
LocalWebserver::setClientGetHandler(client, response);
}
} // End of namespace Networking

View File

@@ -0,0 +1,61 @@
/* 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/>.
*
*/
#ifndef BACKENDS_NETWORKING_SDL_NET_FILESPAGEHANDLER_H
#define BACKENDS_NETWORKING_SDL_NET_FILESPAGEHANDLER_H
#include "backends/networking/sdl_net/handlers/filesbasehandler.h"
namespace Networking {
class FilesPageHandler: public FilesBaseHandler {
enum ItemType {
IT_DIRECTORY,
IT_PARENT_DIRECTORY,
IT_TXT,
IT_ZIP,
IT_7Z,
IT_UNKNOWN
};
/**
* Lists the directory <path>.
*
* Returns true on success.
*/
bool listDirectory(const Common::String &path, Common::String &content, const Common::String &itemTemplate);
/** Helper method for detecting items' type. */
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, const Common::String &path, const Common::String &name, const Common::String &size = Common::String()) const;
public:
FilesPageHandler();
~FilesPageHandler() override;
void handle(Client &client) override;
};
} // End of namespace Networking
#endif

View File

@@ -0,0 +1,52 @@
/* 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/indexpagehandler.h"
#include "backends/networking/sdl_net/handlerutils.h"
#include "backends/networking/sdl_net/localwebserver.h"
#include "common/translation.h"
namespace Networking {
IndexPageHandler::IndexPageHandler(): CommandSender(nullptr) {}
IndexPageHandler::~IndexPageHandler() {}
/// public
void IndexPageHandler::handle(Client &client) {
// redirect to "/filesAJAX"
HandlerUtils::setMessageHandler(
client,
Common::String::format(
"%s<br/><a href=\"files\">%s</a>",
Common::convertFromU32String(_("This is a local webserver index page.")).c_str(),
Common::convertFromU32String(_("Open Files manager")).c_str()
),
"/filesAJAX"
);
}
bool IndexPageHandler::minimalModeSupported() {
return true;
}
} // End of namespace Networking

View File

@@ -0,0 +1,42 @@
/* 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/>.
*
*/
#ifndef BACKENDS_NETWORKING_SDL_NET_INDEXPAGEHANDLER_H
#define BACKENDS_NETWORKING_SDL_NET_INDEXPAGEHANDLER_H
#include "backends/networking/sdl_net/handlers/basehandler.h"
#include "gui/object.h"
namespace Networking {
class LocalWebserver;
class IndexPageHandler: public BaseHandler, public GUI::CommandSender {
public:
IndexPageHandler();
~IndexPageHandler() override;
void handle(Client &client) override;
bool minimalModeSupported() override;
};
} // End of namespace Networking
#endif

View File

@@ -0,0 +1,155 @@
/* 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/listajaxhandler.h"
#include "backends/networking/sdl_net/handlerutils.h"
#include "backends/networking/sdl_net/localwebserver.h"
#include "common/config-manager.h"
#include "common/formats/json.h"
#include "common/translation.h"
namespace Networking {
ListAjaxHandler::ListAjaxHandler() {}
ListAjaxHandler::~ListAjaxHandler() {}
Common::JSONObject ListAjaxHandler::listDirectory(const Common::String &path_) {
Common::JSONArray itemsList;
Common::JSONObject errorResult;
Common::JSONObject successResult;
successResult.setVal("type", new Common::JSONValue("success"));
errorResult.setVal("type", new Common::JSONValue("error"));
if (path_ == "" || path_ == "/") {
if (ConfMan.hasKey("rootpath", "cloud"))
addItem(itemsList, IT_DIRECTORY, "/root/", Common::convertFromU32String(_("File system root")));
addItem(itemsList, IT_DIRECTORY, "/saves/", Common::convertFromU32String(_("Saved games")));
successResult.setVal("items", new Common::JSONValue(itemsList));
return successResult;
}
if (HandlerUtils::hasForbiddenCombinations(path_))
return errorResult;
Common::String path = path_;
Common::String basePath;
Common::Path baseFSPath, fsPath;
if (!urlToPath(path, fsPath, basePath, baseFSPath))
return errorResult;
Common::FSNode node = Common::FSNode(fsPath);
if (!HandlerUtils::permittedPath(node.getPath()))
return errorResult;
if (!node.isDirectory())
return errorResult;
// list directory
Common::FSList _nodeContent;
if (!node.getChildren(_nodeContent, Common::FSNode::kListAll, false)) // do not show hidden files
_nodeContent.clear();
else
Common::sort(_nodeContent.begin(), _nodeContent.end());
// add parent directory link
{
Common::Path relPath = fsPath.relativeTo(baseFSPath);
relPath = relPath.getParent();
Common::String filePath("/");
if (!relPath.empty())
filePath = basePath + relPath.toString('/');
addItem(itemsList, IT_PARENT_DIRECTORY, filePath, Common::convertFromU32String(_("Parent directory")));
}
// fill the content
for (auto &curNode : _nodeContent) {
Common::String name = curNode.getName();
if (curNode.isDirectory())
name += "/";
Common::Path relPath = curNode.getPath().relativeTo(baseFSPath);
Common::String filePath(basePath);
filePath += relPath.toString('/');
addItem(itemsList, detectType(curNode.isDirectory(), name), filePath, name);
}
successResult.setVal("items", new Common::JSONValue(itemsList));
return successResult;
}
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;
if (name.hasSuffix(".7z"))
return IT_7Z;
return IT_UNKNOWN;
}
void ListAjaxHandler::addItem(Common::JSONArray &responseItemsList, ItemType itemType, const Common::String &path, const Common::String &name, const Common::String &size) {
Common::String icon;
bool isDirectory = (itemType == IT_DIRECTORY || itemType == IT_PARENT_DIRECTORY);
switch (itemType) {
case IT_DIRECTORY:
icon = "dir.png";
break;
case IT_PARENT_DIRECTORY:
icon = "up.png";
break;
case IT_TXT:
icon = "txt.png";
break;
case IT_ZIP:
icon = "zip.png";
break;
case IT_7Z:
icon = "7z.png";
break;
default:
icon = "unk.png";
}
Common::JSONObject item;
item.setVal("name", new Common::JSONValue(name));
item.setVal("path", new Common::JSONValue(path));
item.setVal("isDirectory", new Common::JSONValue(isDirectory));
item.setVal("size", new Common::JSONValue(size));
item.setVal("icon", new Common::JSONValue(icon));
responseItemsList.push_back(new Common::JSONValue(item));
}
/// public
void ListAjaxHandler::handle(Client &client) {
Common::String path = client.queryParameter("path");
Common::JSONValue jsonResponse = listDirectory(path);
Common::String response = jsonResponse.stringify(true);
LocalWebserver::setClientGetHandler(client, response);
}
} // End of namespace Networking

View File

@@ -0,0 +1,62 @@
/* 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/>.
*
*/
#ifndef BACKENDS_NETWORKING_SDL_NET_LISTAJAXHANDLER_H
#define BACKENDS_NETWORKING_SDL_NET_LISTAJAXHANDLER_H
#include "backends/networking/sdl_net/handlers/filesbasehandler.h"
#include "common/formats/json.h"
namespace Networking {
class ListAjaxHandler: public FilesBaseHandler {
enum ItemType {
IT_DIRECTORY,
IT_PARENT_DIRECTORY,
IT_TXT,
IT_ZIP,
IT_7Z,
IT_UNKNOWN
};
/**
* Lists the directory <path>.
*
* Returns JSON with either listed directory or error response.
*/
Common::JSONObject listDirectory(const Common::String &path);
/** Helper method for detecting items' type. */
static ItemType detectType(bool isDirectory, const Common::String &name);
/** Helper method for adding items into the files list. */
static void addItem(Common::JSONArray &responseItemsList, ItemType itemType, const Common::String &path, const Common::String &name, const Common::String &size = "");
public:
ListAjaxHandler();
~ListAjaxHandler() override;
void handle(Client &client) override;
};
} // End of namespace Networking
#endif

View File

@@ -0,0 +1,77 @@
/* 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/resourcehandler.h"
#include "backends/networking/sdl_net/handlerutils.h"
#include "backends/networking/sdl_net/localwebserver.h"
namespace Networking {
ResourceHandler::ResourceHandler() {}
ResourceHandler::~ResourceHandler() {}
const char *ResourceHandler::determineMimeType(const Common::String &filename) {
// text
if (filename.hasSuffix(".html")) return "text/html";
if (filename.hasSuffix(".css")) return "text/css";
if (filename.hasSuffix(".txt")) return "text/plain";
if (filename.hasSuffix(".js")) return "application/javascript";
// images
if (filename.hasSuffix(".jpeg") || filename.hasSuffix(".jpg") || filename.hasSuffix(".jpe")) return "image/jpeg";
if (filename.hasSuffix(".gif")) return "image/gif";
if (filename.hasSuffix(".png")) return "image/png";
if (filename.hasSuffix(".svg")) return "image/svg+xml";
if (filename.hasSuffix(".tiff")) return "image/tiff";
if (filename.hasSuffix(".ico")) return "image/vnd.microsoft.icon";
if (filename.hasSuffix(".wbmp")) return "image/vnd.wap.wbmp";
if (filename.hasSuffix(".zip")) return "application/zip";
return "application/octet-stream";
}
/// public
void ResourceHandler::handle(Client &client) {
Common::String filename = client.path();
// remove leading slash
if (filename.size() > 0 && filename[0] == '/')
filename.deleteChar(0);
// if archive hidden file is requested, ignore
if (filename.size() > 0 && filename[0] == '.')
return;
// if file not found, don't set handler either
Common::SeekableReadStream *file = HandlerUtils::getArchiveFile(filename);
if (file == nullptr)
return;
LocalWebserver::setClientGetHandler(client, file, 200, determineMimeType(filename));
}
bool ResourceHandler::minimalModeSupported() {
return true;
}
} // End of namespace Networking

View File

@@ -0,0 +1,41 @@
/* 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/>.
*
*/
#ifndef BACKENDS_NETWORKING_SDL_NET_RESOURCEHANDLER_H
#define BACKENDS_NETWORKING_SDL_NET_RESOURCEHANDLER_H
#include "backends/networking/sdl_net/handlers/basehandler.h"
namespace Networking {
class ResourceHandler: public BaseHandler {
static const char *determineMimeType(const Common::String &filename);
public:
ResourceHandler();
~ResourceHandler() override;
void handle(Client &client) override;
bool minimalModeSupported() override;
};
} // End of namespace Networking
#endif

View File

@@ -0,0 +1,78 @@
/* 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/uploadfilehandler.h"
#include "backends/networking/sdl_net/handlerutils.h"
#include "backends/networking/sdl_net/uploadfileclienthandler.h"
#include "common/system.h"
#include "common/translation.h"
namespace Networking {
UploadFileHandler::UploadFileHandler() {}
UploadFileHandler::~UploadFileHandler() {}
/// public
void UploadFileHandler::handle(Client &client) {
Common::String path = client.queryParameter("path");
// check that <path> is not an absolute root
if (path == "" || path == "/" || path == "\\") {
HandlerUtils::setFilesManagerErrorMessageHandler(client, Common::convertFromU32String(_("Invalid path!")));
return;
}
// check that <path> contains no '../'
if (HandlerUtils::hasForbiddenCombinations(path)) {
HandlerUtils::setFilesManagerErrorMessageHandler(client, Common::convertFromU32String(_("Invalid path!")));
return;
}
// transform virtual path to actual file system one
Common::String basePath;
Common::Path baseFSPath, fsPath;
if (!urlToPath(path, fsPath, basePath, baseFSPath)) {
HandlerUtils::setFilesManagerErrorMessageHandler(client, Common::convertFromU32String(_("Invalid path!")));
return;
}
// check that <path> exists, is directory and isn't forbidden
Common::FSNode node(fsPath);
if (!HandlerUtils::permittedPath(node.getPath())) {
HandlerUtils::setFilesManagerErrorMessageHandler(client, Common::convertFromU32String(_("Invalid path!")));
return;
}
if (!node.exists()) {
HandlerUtils::setFilesManagerErrorMessageHandler(client, Common::convertFromU32String(_("The parent directory doesn't exist!")));
return;
}
if (!node.isDirectory()) {
HandlerUtils::setFilesManagerErrorMessageHandler(client, Common::convertFromU32String(_("Can't upload into a file!")));
return;
}
// if all OK, set special handler
client.setHandler(new UploadFileClientHandler(fsPath));
}
} // End of namespace Networking

View File

@@ -0,0 +1,39 @@
/* 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/>.
*
*/
#ifndef BACKENDS_NETWORKING_SDL_NET_UPLOADFILEHANDLER_H
#define BACKENDS_NETWORKING_SDL_NET_UPLOADFILEHANDLER_H
#include "backends/networking/sdl_net/handlers/filesbasehandler.h"
namespace Networking {
class UploadFileHandler: public FilesBaseHandler {
public:
UploadFileHandler();
~UploadFileHandler() override;
void handle(Client &client) override;
};
} // End of namespace Networking
#endif

View File

@@ -0,0 +1,210 @@
/* 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/handlerutils.h"
#include "backends/networking/sdl_net/localwebserver.h"
#include "backends/saves/default/default-saves.h"
#include "common/archive.h"
#include "common/config-manager.h"
#include "common/file.h"
#include "common/memstream.h"
#include "common/translation.h"
#include "common/compression/unzip.h"
namespace Networking {
#define ARCHIVE_NAME "wwwroot.zip"
#define INDEX_PAGE_NAME ".index.html"
Common::Archive *HandlerUtils::getZipArchive() {
// first search in themepath
if (ConfMan.hasKey("themepath")) {
const Common::FSNode &node = Common::FSNode(ConfMan.getPath("themepath"));
if (node.exists() && node.isReadable() && node.isDirectory()) {
Common::FSNode fileNode = node.getChild(ARCHIVE_NAME);
if (fileNode.exists() && fileNode.isReadable() && !fileNode.isDirectory()) {
Common::SeekableReadStream *const stream = fileNode.createReadStream();
Common::Archive *zipArchive = Common::makeZipArchive(stream);
if (zipArchive)
return zipArchive;
}
}
}
// then use SearchMan to find it
Common::ArchiveMemberList fileList;
SearchMan.listMatchingMembers(fileList, ARCHIVE_NAME);
for (auto &m : fileList) {
Common::SeekableReadStream *const stream = m->createReadStream();
Common::Archive *zipArchive = Common::makeZipArchive(stream);
if (zipArchive)
return zipArchive;
}
return nullptr;
}
Common::ArchiveMemberList HandlerUtils::listArchive() {
Common::ArchiveMemberList resultList;
Common::Archive *zipArchive = getZipArchive();
if (zipArchive) {
zipArchive->listMembers(resultList);
delete zipArchive;
}
return resultList;
}
Common::SeekableReadStream *HandlerUtils::getArchiveFile(const Common::String &name) {
Common::SeekableReadStream *result = nullptr;
Common::Archive *zipArchive = getZipArchive();
if (zipArchive) {
const Common::ArchiveMemberPtr ptr = zipArchive->getMember(Common::Path(name, '/'));
if (ptr.get() == nullptr)
return nullptr;
result = ptr->createReadStream();
delete zipArchive;
}
return result;
}
Common::String HandlerUtils::readEverythingFromStream(Common::SeekableReadStream *const stream) {
Common::String result;
char buf[1024];
uint32 readBytes;
while (!stream->eos()) {
readBytes = stream->read(buf, 1024);
result += Common::String(buf, readBytes);
}
return result;
}
Common::SeekableReadStream *HandlerUtils::makeResponseStreamFromString(const Common::String &response) {
byte *data = (byte *)malloc(response.size());
assert(data);
memcpy(data, response.c_str(), response.size());
return new Common::MemoryReadStream(data, response.size(), DisposeAfterUse::YES);
}
Common::String HandlerUtils::normalizePath(const Common::String &path) {
Common::String normalized;
bool slash = false;
for (uint32 i = 0; i < path.size(); ++i) {
char c = path[i];
if (c == '\\' || c == '/') {
slash = true;
continue;
}
if (slash) {
normalized += '/';
slash = false;
}
if ('A' <= c && c <= 'Z') {
normalized += c - 'A' + 'a';
} else {
normalized += c;
}
}
if (slash) normalized += '/';
return normalized;
}
bool HandlerUtils::hasForbiddenCombinations(const Common::String &path) {
return (path.contains("/../") || path.contains("\\..\\") || path.contains("\\../") || path.contains("/..\\"));
}
bool HandlerUtils::isBlacklisted(const Common::Path &path) {
const char *blacklist[] = {
"/etc",
"/bin",
"c:/windows" // just saying: I know guys who install windows on another drives
};
// normalize path
Common::Path normalized = path.normalize();
uint32 size = sizeof(blacklist) / sizeof(const char *);
for (uint32 i = 0; i < size; ++i)
if (normalized.isRelativeTo(Common::Path(blacklist[i], '/')))
return true;
return false;
}
bool HandlerUtils::hasPermittedPrefix(const Common::Path &path) {
// normalize path
Common::Path normalized = path.normalize();
// prefix for /root/
Common::Path prefix;
if (ConfMan.hasKey("rootpath", "cloud")) {
prefix = ConfMan.getPath("rootpath", "cloud").normalize();
if (normalized.isRelativeTo(prefix))
return true;
}
// prefix for /saves/
#ifdef USE_CLOUD
DefaultSaveFileManager *manager = dynamic_cast<DefaultSaveFileManager *>(g_system->getSavefileManager());
prefix = (manager ? manager->concatWithSavesPath("") : ConfMan.getPath("savepath"));
#else
prefix = ConfMan.getPath("savepath");
#endif
prefix = prefix.normalize();
return normalized.isRelativeTo(prefix);
}
bool HandlerUtils::permittedPath(const Common::Path &path) {
return hasPermittedPrefix(path) && !isBlacklisted(path);
}
void HandlerUtils::setMessageHandler(Client &client, const Common::String &message, const Common::String &redirectTo) {
Common::String response = "<html><head><title>ScummVM</title><meta charset=\"utf-8\"/></head><body>{message}</body></html>";
// load stylish response page from the archive
Common::SeekableReadStream *const stream = getArchiveFile(INDEX_PAGE_NAME);
if (stream)
response = readEverythingFromStream(stream);
replace(response, "{message}", message);
if (redirectTo.empty())
LocalWebserver::setClientGetHandler(client, response);
else
LocalWebserver::setClientRedirectHandler(client, response, redirectTo);
}
void HandlerUtils::setFilesManagerErrorMessageHandler(Client &client, const Common::String &message, const Common::String &redirectTo) {
setMessageHandler(
client,
Common::String::format(
"%s<br/><a href=\"files%s?path=%s\">%s</a>",
message.c_str(),
client.queryParameter("ajax") == "true" ? "AJAX" : "",
"%2F", //that's encoded "/"
Common::convertFromU32String(_("Back to the files manager")).c_str()
),
redirectTo
);
}
} // End of namespace Networking

View File

@@ -0,0 +1,50 @@
/* 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/>.
*
*/
#ifndef BACKENDS_NETWORKING_SDL_NET_HANDLERUTILS_H
#define BACKENDS_NETWORKING_SDL_NET_HANDLERUTILS_H
#include "backends/networking/sdl_net/client.h"
#include "common/archive.h"
namespace Networking {
class HandlerUtils {
public:
static Common::Archive *getZipArchive();
static Common::ArchiveMemberList listArchive();
static Common::SeekableReadStream *getArchiveFile(const Common::String &name);
static Common::String readEverythingFromStream(Common::SeekableReadStream *const stream);
static Common::SeekableReadStream *makeResponseStreamFromString(const Common::String &response);
static Common::String normalizePath(const Common::String &path);
static bool hasForbiddenCombinations(const Common::String &path);
static bool isBlacklisted(const Common::Path &path);
static bool hasPermittedPrefix(const Common::Path &path);
static bool permittedPath(const Common::Path &path);
static void setMessageHandler(Client &client, const Common::String &message, const Common::String &redirectTo = "");
static void setFilesManagerErrorMessageHandler(Client &client, const Common::String &message, const Common::String &redirectTo = "");
};
} // End of namespace Networking
#endif

View File

@@ -0,0 +1,502 @@
/* 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/>.
*
*/
#define FORBIDDEN_SYMBOL_ALLOW_ALL
#include "backends/networking/sdl_net/localwebserver.h"
#include "backends/networking/sdl_net/getclienthandler.h"
#include "common/memstream.h"
#include "common/str.h"
#include "common/system.h"
#include "common/timer.h"
#include "common/translation.h"
#include <SDL_net.h>
#include "common/config-manager.h"
#ifdef POSIX
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#ifndef SIOCGIFCONF
#include <sys/sockio.h>
#endif
#ifndef _SIZEOF_ADDR_IFREQ
#define _SIZEOF_ADDR_IFREQ sizeof
#endif
#define LSSDP_BUFFER_LEN 2048
#endif // POSIX
#ifdef PLAYSTATION3
#include <net/netctl.h>
#endif
namespace Common {
class MemoryReadWriteStream;
DECLARE_SINGLETON(Networking::LocalWebserver);
}
namespace Networking {
LocalWebserver::LocalWebserver(): _set(nullptr), _serverSocket(nullptr), _timerStarted(false),
_stopOnIdle(false), _minimalMode(false), _clients(0), _idlingFrames(0), _serverPort(DEFAULT_SERVER_PORT) {
addPathHandler("/", &_indexPageHandler);
addPathHandler("/files", &_filesPageHandler);
addPathHandler("/create", &_createDirectoryHandler);
addPathHandler("/download", &_downloadFileHandler);
addPathHandler("/upload", &_uploadFileHandler);
addPathHandler("/list", &_listAjaxHandler);
addPathHandler("/filesAJAX", &_filesAjaxPageHandler);
#ifdef USE_CLOUD
addPathHandler("/connect_cloud", &_connectCloudHandler);
#endif // USE_CLOUD
_defaultHandler = &_resourceHandler;
}
LocalWebserver::~LocalWebserver() {
stop();
}
void localWebserverTimer(void *ignored) {
LocalServer.handle();
}
void LocalWebserver::startTimer(int interval) {
Common::TimerManager *manager = g_system->getTimerManager();
if (manager->installTimerProc(localWebserverTimer, interval, nullptr, "Networking::LocalWebserver's Timer")) {
_timerStarted = true;
} else {
warning("Failed to install Networking::LocalWebserver's timer");
}
}
void LocalWebserver::stopTimer() {
Common::TimerManager *manager = g_system->getTimerManager();
manager->removeTimerProc(localWebserverTimer);
_timerStarted = false;
}
void LocalWebserver::start(bool useMinimalMode) {
_handleMutex.lock();
_serverPort = getPort();
_stopOnIdle = false;
if (_timerStarted) {
_handleMutex.unlock();
return;
}
_minimalMode = useMinimalMode;
startTimer();
// Create a listening TCP socket
IPaddress ip;
if (SDLNet_ResolveHost(&ip, nullptr, _serverPort) == -1) {
error("LocalWebserver: SDLNet_ResolveHost: %s\n", SDLNet_GetError());
}
resolveAddress(&ip);
_serverSocket = SDLNet_TCP_Open(&ip);
if (!_serverSocket) {
warning("LocalWebserver: SDLNet_TCP_Open: %s", SDLNet_GetError());
stopTimer();
g_system->displayMessageOnOSD(_("Failed to start local webserver.\nCheck whether selected port is not used by another application and try again."));
_handleMutex.unlock();
return;
}
// Create a socket set
_set = SDLNet_AllocSocketSet(MAX_CONNECTIONS + 1); //one more for our server socket
if (!_set) {
error("LocalWebserver: SDLNet_AllocSocketSet: %s\n", SDLNet_GetError());
}
int numused = SDLNet_TCP_AddSocket(_set, _serverSocket);
if (numused == -1) {
error("LocalWebserver: SDLNet_AddSocket: %s\n", SDLNet_GetError());
}
if (_timerStarted)
g_system->taskStarted(OSystem::kLocalServer);
_handleMutex.unlock();
}
void LocalWebserver::stop() {
_handleMutex.lock();
if (_timerStarted) {
stopTimer();
g_system->taskFinished(OSystem::kLocalServer);
}
if (_serverSocket) {
SDLNet_TCP_Close(_serverSocket);
_serverSocket = nullptr;
}
for (uint32 i = 0; i < MAX_CONNECTIONS; ++i)
_client[i].close();
_clients = 0;
if (_set) {
SDLNet_FreeSocketSet(_set);
_set = nullptr;
}
_handleMutex.unlock();
}
void LocalWebserver::stopOnIdle() { _stopOnIdle = true; }
void LocalWebserver::addPathHandler(const Common::String &path, BaseHandler *handler) {
if (_pathHandlers.contains(path))
warning("LocalWebserver::addPathHandler: path already had a handler");
_pathHandlers[path] = handler;
}
Common::String LocalWebserver::getAddress() { return _address; }
IndexPageHandler &LocalWebserver::indexPageHandler() { return _indexPageHandler; }
bool LocalWebserver::isRunning() {
bool result = false;
_handleMutex.lock();
result = _timerStarted;
_handleMutex.unlock();
return result;
}
uint32 LocalWebserver::getPort() {
#ifdef NETWORKING_LOCALWEBSERVER_ENABLE_PORT_OVERRIDE
if (ConfMan.hasKey("local_server_port"))
return ConfMan.getInt("local_server_port");
#endif
return DEFAULT_SERVER_PORT;
}
void LocalWebserver::handle() {
_handleMutex.lock();
int numready = SDLNet_CheckSockets(_set, 0);
if (numready == -1) {
error("LocalWebserver: SDLNet_CheckSockets: %s\n", SDLNet_GetError());
} else if (numready) {
acceptClient();
}
for (uint32 i = 0; i < MAX_CONNECTIONS; ++i)
handleClient(i);
_clients = 0;
for (uint32 i = 0; i < MAX_CONNECTIONS; ++i)
if (_client[i].state() != INVALID)
++_clients;
if (_clients == 0)
++_idlingFrames;
else
_idlingFrames = 0;
if (_idlingFrames > FRAMES_PER_SECOND && _stopOnIdle) {
_handleMutex.unlock();
stop();
return;
}
_handleMutex.unlock();
}
void LocalWebserver::handleClient(uint32 i) {
switch (_client[i].state()) {
case INVALID:
return;
case READING_HEADERS:
_client[i].readHeaders();
break;
case READ_HEADERS: {
// decide what to do next with that client
// check whether we know a handler for such URL
BaseHandler *handler = nullptr;
if (_pathHandlers.contains(_client[i].path())) {
handler = _pathHandlers[_client[i].path()];
} else {
// try default handler
handler = _defaultHandler;
}
// if server's in "minimal mode", only handlers which support it are used
if (handler && (!_minimalMode || handler->minimalModeSupported()))
handler->handle(_client[i]);
if (_client[i].state() == BEING_HANDLED || _client[i].state() == INVALID)
break;
// if no handler, answer with default BAD REQUEST
}
// fall through
case BAD_REQUEST:
setClientGetHandler(_client[i], "<html><head><title>ScummVM - Bad Request</title></head><body>BAD REQUEST</body></html>", 400);
break;
case BEING_HANDLED:
_client[i].handle();
break;
}
}
void LocalWebserver::acceptClient() {
if (!SDLNet_SocketReady(_serverSocket))
return;
TCPsocket client = SDLNet_TCP_Accept(_serverSocket);
if (!client)
return;
if (_clients == MAX_CONNECTIONS) { //drop the connection
SDLNet_TCP_Close(client);
return;
}
++_clients;
for (uint32 i = 0; i < MAX_CONNECTIONS; ++i)
if (_client[i].state() == INVALID) {
_client[i].open(_set, client);
break;
}
}
void LocalWebserver::resolveAddress(void *ipAddress) {
IPaddress *ip = (IPaddress *)ipAddress;
// not resolved
_address = Common::String::format("http://127.0.0.1:%u/ (unresolved)", _serverPort);
// default way (might work everywhere, surely works on Windows)
const char *name = SDLNet_ResolveIP(ip);
if (name == nullptr) {
warning("LocalWebserver: SDLNet_ResolveIP: %s", SDLNet_GetError());
} else {
IPaddress localIp;
if (SDLNet_ResolveHost(&localIp, name, _serverPort) == -1) {
warning("LocalWebserver: SDLNet_ResolveHost: %s", SDLNet_GetError());
} else {
_address = Common::String::format(
"http://%u.%u.%u.%u:%u/",
localIp.host & 0xFF, (localIp.host >> 8) & 0xFF, (localIp.host >> 16) & 0xFF, (localIp.host >> 24) & 0xFF,
_serverPort
);
}
}
// check that our trick worked
if (_address.contains("/127.0.0.1:") || _address.contains("localhost") || _address.contains("/0.0.0.0:"))
warning("LocalWebserver: Failed to resolve IP with the default way");
else
return;
// if not - try platform-specific
#ifdef POSIX
void *tmpAddrPtr = NULL;
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
warning("LocalWebserver: failed to create socket: %s (%d)", strerror(errno), errno);
} else {
// get ifconfig
char buffer[LSSDP_BUFFER_LEN] = {};
struct ifconf ifc;
ifc.ifc_len = sizeof(buffer);
ifc.ifc_buf = (caddr_t) buffer;
if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
warning("LocalWebserver: ioctl SIOCGIFCONF failed: %s (%d)", strerror(errno), errno);
} else {
struct ifreq *i;
for (size_t index = 0; index < (size_t)ifc.ifc_len; index += _SIZEOF_ADDR_IFREQ(*i)) {
i = (struct ifreq *)(buffer + index);
Common::String addr;
// IPv4
if (i->ifr_addr.sa_family == AF_INET) {
tmpAddrPtr = &((struct sockaddr_in *)&i->ifr_addr)->sin_addr;
char addressBuffer[INET_ADDRSTRLEN];
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
debug(9, "%s IP Address %s", i->ifr_name, addressBuffer);
addr = addressBuffer;
}
// IPv6
/*
if (i->ifr_addr.sa_family == AF_INET6) {
tmpAddrPtr = &((struct sockaddr_in6 *)&i->ifr_addr)->sin6_addr;
char addressBuffer[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
debug(9, "%s IP Address %s", i->ifr_name, addressBuffer);
addr = addressBuffer;
}
*/
if (addr.empty())
continue;
// ignored IPv4 addresses
if (addr.equals("127.0.0.1") || addr.equals("0.0.0.0") || addr.equals("localhost"))
continue;
// ignored IPv6 addresses
/*
if (addr.equals("::1"))
continue;
*/
// use the address found
_address = "http://" + addr + Common::String::format(":%u/", _serverPort);
}
}
// close socket
if (close(fd) != 0) {
warning("LocalWebserver: failed to close socket [fd %d]: %s (%d)", fd, strerror(errno), errno);
}
}
#elif defined(PLAYSTATION3)
netCtlInit();
s32 connectionState;
netCtlGetState(&connectionState);
if (connectionState == NET_CTL_STATE_IPObtained) {
union net_ctl_info info;
if (netCtlGetInfo(NET_CTL_INFO_IP_ADDRESS, &info) == 0) {
debug(9, "LocalWebserver: IP Address %s", info.ip_address);
_address = "http://" + Common::String(info.ip_address) + Common::String::format(":%u/", _serverPort);
} else {
warning("LocalWebserver: failed to get IP address for network connection");
}
} else {
warning("LocalWebserver: no active network connection was detected");
}
netCtlTerm();
#endif
}
void LocalWebserver::setClientGetHandler(Client &client, const Common::String &response, long code, const char *mimeType) {
byte *data = new byte[response.size()];
memcpy(data, response.c_str(), response.size());
Common::MemoryReadStream *stream = new Common::MemoryReadStream(data, response.size(), DisposeAfterUse::YES);
setClientGetHandler(client, stream, code, mimeType);
}
void LocalWebserver::setClientGetHandler(Client &client, Common::SeekableReadStream *responseStream, long code, const char *mimeType) {
GetClientHandler *handler = new GetClientHandler(responseStream);
handler->setResponseCode(code);
if (mimeType)
handler->setHeader("Content-Type", mimeType);
client.setHandler(handler);
}
void LocalWebserver::setClientRedirectHandler(Client &client, const Common::String &response, const Common::String &location, const char *mimeType) {
byte *data = new byte[response.size()];
memcpy(data, response.c_str(), response.size());
Common::MemoryReadStream *stream = new Common::MemoryReadStream(data, response.size(), DisposeAfterUse::YES);
setClientRedirectHandler(client, stream, location, mimeType);
}
void LocalWebserver::setClientRedirectHandler(Client &client, Common::SeekableReadStream *responseStream, const Common::String &location, const char *mimeType) {
GetClientHandler *handler = new GetClientHandler(responseStream);
handler->setResponseCode(302); //redirect
handler->setHeader("Location", location);
if (mimeType)
handler->setHeader("Content-Type", mimeType);
client.setHandler(handler);
}
namespace {
int hexDigit(char c) {
if ('0' <= c && c <= '9') return c - '0';
if ('A' <= c && c <= 'F') return c - 'A' + 10;
if ('a' <= c && c <= 'f') return c - 'a' + 10;
return -1;
}
}
Common::String LocalWebserver::urlDecode(const Common::String &value) {
Common::String result = "";
uint32 size = value.size();
for (uint32 i = 0; i < size; ++i) {
if (value[i] == '+') {
result += ' ';
continue;
}
if (value[i] == '%' && i + 2 < size) {
int d1 = hexDigit(value[i + 1]);
int d2 = hexDigit(value[i + 2]);
if (0 <= d1 && d1 < 16 && 0 <= d2 && d2 < 16) {
result += (char)(d1 * 16 + d2);
i = i + 2;
continue;
}
}
result += value[i];
}
return result;
}
namespace {
bool isQueryUnreserved(char c) {
return (
('0' <= c && c <= '9') ||
('A' <= c && c <= 'Z') ||
('a' <= c && c <= 'z') ||
c == '-' || c == '_' || c == '.' || c == '!' ||
c == '~' || c == '*' || c == '\'' || c == '(' || c == ')'
);
}
}
Common::String LocalWebserver::urlEncodeQueryParameterValue(const Common::String &value) {
//OK chars = alphanum | "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
//reserved for query are ";", "/", "?", ":", "@", "&", "=", "+", ","
//that means these must be encoded too or otherwise they could malform the query
Common::String result = "";
char hexChar[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
for (uint32 i = 0; i < value.size(); ++i) {
char c = value[i];
if (isQueryUnreserved(c))
result += c;
else {
result += '%';
result += hexChar[(c >> 4) & 0xF];
result += hexChar[c & 0xF];
}
}
return result;
}
} // End of namespace Networking

View File

@@ -0,0 +1,125 @@
/* 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/>.
*
*/
#ifndef BACKENDS_NETWORKING_SDL_NET_LOCALWEBSERVER_H
#define BACKENDS_NETWORKING_SDL_NET_LOCALWEBSERVER_H
#include "backends/networking/sdl_net/client.h"
#include "backends/networking/sdl_net/handlers/basehandler.h"
#include "backends/networking/sdl_net/handlers/createdirectoryhandler.h"
#include "backends/networking/sdl_net/handlers/downloadfilehandler.h"
#include "backends/networking/sdl_net/handlers/filesajaxpagehandler.h"
#include "backends/networking/sdl_net/handlers/filespagehandler.h"
#include "backends/networking/sdl_net/handlers/indexpagehandler.h"
#include "backends/networking/sdl_net/handlers/listajaxhandler.h"
#include "backends/networking/sdl_net/handlers/resourcehandler.h"
#include "backends/networking/sdl_net/handlers/uploadfilehandler.h"
#include "common/hash-str.h"
#include "common/mutex.h"
#include "common/singleton.h"
#include "common/scummsys.h"
#ifdef USE_CLOUD
#include "backends/networking/sdl_net/handlers/connectcloudhandler.h"
#endif // USE_CLOUD
namespace Common {
class SeekableReadStream;
}
typedef struct _SDLNet_SocketSet *SDLNet_SocketSet;
typedef struct _TCPsocket *TCPsocket;
namespace Networking {
#define NETWORKING_LOCALWEBSERVER_ENABLE_PORT_OVERRIDE
class LocalWebserver : public Common::Singleton<LocalWebserver> {
static const uint32 FRAMES_PER_SECOND = 20;
static const uint32 TIMER_INTERVAL = 1000000 / FRAMES_PER_SECOND;
static const uint32 MAX_CONNECTIONS = 10;
friend void localWebserverTimer(void *); //calls handle()
SDLNet_SocketSet _set;
TCPsocket _serverSocket;
Client _client[MAX_CONNECTIONS];
uint32 _clients;
bool _timerStarted, _stopOnIdle, _minimalMode;
Common::HashMap<Common::String, BaseHandler*> _pathHandlers;
BaseHandler *_defaultHandler;
IndexPageHandler _indexPageHandler;
FilesPageHandler _filesPageHandler;
CreateDirectoryHandler _createDirectoryHandler;
DownloadFileHandler _downloadFileHandler;
UploadFileHandler _uploadFileHandler;
ListAjaxHandler _listAjaxHandler;
FilesAjaxPageHandler _filesAjaxPageHandler;
#ifdef USE_CLOUD
ConnectCloudHandler _connectCloudHandler;
#endif // USE_CLOUD
ResourceHandler _resourceHandler;
uint32 _idlingFrames;
Common::Mutex _handleMutex;
Common::String _address;
uint32 _serverPort;
void startTimer(int interval = TIMER_INTERVAL);
void stopTimer();
void handle();
void handleClient(uint32 i);
void acceptClient();
void resolveAddress(void *ipAddress);
void addPathHandler(const Common::String &path, BaseHandler *handler);
public:
static const uint32 DEFAULT_SERVER_PORT = 12345;
LocalWebserver();
~LocalWebserver() override;
void start(bool useMinimalMode = false);
void stop();
void stopOnIdle();
Common::String getAddress();
IndexPageHandler &indexPageHandler();
bool isRunning();
static uint32 getPort();
#ifdef USE_CLOUD
void setStorageConnectionCallback(Networking::ErrorCallback cb) { _connectCloudHandler.setStorageConnectionCallback(cb); }
#endif // USE_CLOUD
static void setClientGetHandler(Client &client, const Common::String &response, long code = 200, const char *mimeType = nullptr);
static void setClientGetHandler(Client &client, Common::SeekableReadStream *responseStream, long code = 200, const char *mimeType = nullptr);
static void setClientRedirectHandler(Client &client, const Common::String &response, const Common::String &location, const char *mimeType = nullptr);
static void setClientRedirectHandler(Client &client, Common::SeekableReadStream *responseStream, const Common::String &location, const char *mimeType = nullptr);
static Common::String urlDecode(const Common::String &value);
static Common::String urlEncodeQueryParameterValue(const Common::String &value);
};
/** Shortcut for accessing the local webserver. */
#define LocalServer Networking::LocalWebserver::instance()
} // End of namespace Networking
#endif

View File

@@ -0,0 +1,544 @@
/* 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/reader.h"
#include "backends/fs/fs-factory.h"
#include "backends/networking/sdl_net/localwebserver.h"
#include "common/memstream.h"
#include "common/stream.h"
namespace Networking {
Reader::Reader() {
_state = RS_NONE;
_mode = RM_HTTP_GENERIC;
_content = nullptr;
_bytesLeft = 0;
_window = nullptr;
_windowUsed = 0;
_windowSize = 0;
_windowReadPosition = 0;
_windowWritePosition = 0;
_windowHash = 0;
_headersStream = nullptr;
_firstBlock = true;
_contentLength = 0;
_availableBytes = 0;
_isBadRequest = false;
_allContentRead = false;
}
Reader::~Reader() {
cleanup();
}
Reader &Reader::operator=(Reader &r) {
if (this == &r)
return *this;
cleanup();
_state = r._state;
_content = r._content;
_bytesLeft = r._bytesLeft;
r._state = RS_NONE;
_window = r._window;
_windowUsed = r._windowUsed;
_windowSize = r._windowSize;
_windowReadPosition = r._windowReadPosition;
_windowWritePosition = r._windowWritePosition;
_windowHash = r._windowHash;
r._window = nullptr;
_headersStream = r._headersStream;
r._headersStream = nullptr;
_headers = r._headers;
_method = r._method;
_path = r._path;
_query = r._query;
_anchor = r._anchor;
_queryParameters = r._queryParameters;
_contentLength = r._contentLength;
_boundary = r._boundary;
_availableBytes = r._availableBytes;
_firstBlock = r._firstBlock;
_isBadRequest = r._isBadRequest;
_allContentRead = r._allContentRead;
return *this;
}
void Reader::cleanup() {
//_content is not to be freed, it's not owned by Reader
if (_headersStream != nullptr)
delete _headersStream;
if (_window != nullptr)
freeWindow();
}
namespace {
uint32 calculateHash(const Common::String& boundary) {
uint32 result = 0;
for (uint32 i = 0; i < boundary.size(); ++i)
result ^= boundary[i];
return result;
}
}
bool Reader::readAndHandleFirstHeaders() {
Common::String boundary = "\r\n\r\n";
uint32 boundaryHash = calculateHash(boundary);
if (_window == nullptr) {
makeWindow(boundary.size());
}
if (_headersStream == nullptr) {
_headersStream = new Common::MemoryReadWriteStream(DisposeAfterUse::YES);
}
while (readOneByteInStream(_headersStream, boundary, boundaryHash)) {
if (_headersStream->size() > SUSPICIOUS_HEADERS_SIZE) {
_isBadRequest = true;
return true;
}
if (!bytesLeft())
return false;
}
handleFirstHeaders(_headersStream);
freeWindow();
_state = RS_READING_CONTENT;
return true;
}
bool Reader::readBlockHeadersIntoStream(Common::WriteStream *stream) {
Common::String boundary = "\r\n\r\n";
uint32 boundaryHash = calculateHash(boundary);
if (_window == nullptr) makeWindow(boundary.size());
while (readOneByteInStream(stream, boundary, boundaryHash)) {
if (!bytesLeft())
return false;
}
if (stream) stream->flush();
freeWindow();
_state = RS_READING_CONTENT;
return true;
}
namespace {
void readFromThatUntilLineEnd(const char *cstr, const Common::String &needle, Common::String &result) {
const char *position = strstr(cstr, needle.c_str());
if (position) {
char c;
for (const char *i = position + needle.size(); c = *i, c != 0; ++i) {
if (c == '\n' || c == '\r')
break;
result += c;
}
}
}
}
void Reader::handleFirstHeaders(Common::MemoryReadWriteStream *headersStream) {
if (!_boundary.empty()) {
warning("Reader: handleFirstHeaders() called when first headers were already handled");
return;
}
//parse method, path, query, fragment
_headers = readEverythingFromMemoryStream(headersStream);
parseFirstLine(_headers);
//find boundary
_boundary = "";
readFromThatUntilLineEnd(_headers.c_str(), "boundary=", _boundary);
//find content length
Common::String contentLength = "";
readFromThatUntilLineEnd(_headers.c_str(), "Content-Length: ", contentLength);
_contentLength = contentLength.asUint64();
_availableBytes = _contentLength;
}
void Reader::parseFirstLine(const Common::String &headersToParse) {
uint32 headersSize = headersToParse.size();
bool bad = false;
if (headersSize > 0) {
const char *cstr = headersToParse.c_str();
const char *position = strstr(cstr, "\r\n");
if (position) { //we have at least one line - and we want the first one
//"<METHOD> <path> HTTP/<VERSION>\r\n"
Common::String methodParsed, pathParsed, http, buf;
uint32 length = position - cstr;
if (headersSize > length)
headersSize = length;
for (uint32 i = 0; i < headersSize; ++i) {
if (headersToParse[i] != ' ')
buf += headersToParse[i];
if (headersToParse[i] == ' ' || i == headersSize - 1) {
if (methodParsed == "") {
methodParsed = buf;
} else if (pathParsed == "") {
pathParsed = buf;
} else if (http == "") {
http = buf;
} else {
bad = true;
break;
}
buf = "";
}
}
//check that method is supported
if (methodParsed != "GET" && methodParsed != "PUT" && methodParsed != "POST" && methodParsed != "OPTIONS")
bad = true;
//check that HTTP/<VERSION> is OK
if (!http.hasPrefix("HTTP/"))
bad = true;
_method = methodParsed;
parsePathQueryAndAnchor(pathParsed);
}
}
if (bad) _isBadRequest = true;
}
void Reader::parsePathQueryAndAnchor(const Common::String &pathToParse) {
//<path>[?query][#anchor]
bool readingPath = true;
bool readingQuery = false;
_path = "";
_query = "";
_anchor = "";
for (uint32 i = 0; i < pathToParse.size(); ++i) {
if (readingPath) {
if (pathToParse[i] == '?') {
readingPath = false;
readingQuery = true;
} else {
_path += pathToParse[i];
}
} else if (readingQuery) {
if (pathToParse[i] == '#') {
readingQuery = false;
} else {
_query += pathToParse[i];
}
} else {
_anchor += pathToParse[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("Reader: query parameter \"%s\" is already set!", key.c_str());
else
_queryParameters[key] = LocalWebserver::urlDecode(value);
readingKey = true;
key = "";
} else {
value += _query[i];
}
}
}
if (!key.empty()) {
if (_queryParameters.contains(key))
warning("Reader: query parameter \"%s\" is already set!", key.c_str());
else
_queryParameters[key] = LocalWebserver::urlDecode(value);
}
}
bool Reader::readContentIntoStream(Common::WriteStream *stream) {
Common::String boundary = "--" + _boundary;
if (!_firstBlock)
boundary = "\r\n" + boundary;
if (_boundary.empty())
boundary = "\r\n";
if (_window == nullptr)
makeWindow(boundary.size());
uint32 boundaryHash = calculateHash(boundary);
while (readOneByteInStream(stream, boundary, boundaryHash)) {
if (!bytesLeft())
return false;
}
_firstBlock = false;
if (stream)
stream->flush();
freeWindow();
_state = RS_READING_HEADERS;
return true;
}
void Reader::makeWindow(uint32 size) {
freeWindow();
_window = new byte[size];
_windowUsed = 0;
_windowSize = size;
_windowReadPosition = 0;
_windowWritePosition = 0;
_windowHash = 0;
}
void Reader::freeWindow() {
delete[] _window;
_window = nullptr;
_windowUsed = _windowSize = 0;
_windowReadPosition = _windowWritePosition = 0;
_windowHash = 0;
}
namespace {
bool windowEqualsString(const byte *window, uint32 windowStart, uint32 windowSize, const Common::String &boundary) {
if (boundary.size() != windowSize)
return false;
for (uint32 i = 0; i < windowSize; ++i) {
if (window[(windowStart + i) % windowSize] != boundary[i])
return false;
}
return true;
}
}
bool Reader::readOneByteInStream(Common::WriteStream *stream, const Common::String &boundary, const uint32 boundaryHash) {
byte b = readOne();
++_windowUsed;
_window[_windowWritePosition] = b;
_windowWritePosition = (_windowWritePosition + 1) % _windowSize;
_windowHash ^= b;
if (_windowUsed < _windowSize)
return true;
//when window is filled, check whether that's the boundary
if (_windowHash == boundaryHash && windowEqualsString(_window, _windowReadPosition, _windowSize, boundary))
return false;
//if not, add the first byte of the window to the string
if (stream)
stream->writeByte(_window[_windowReadPosition]);
_windowHash ^= _window[_windowReadPosition];
_windowReadPosition = (_windowReadPosition + 1) % _windowSize;
--_windowUsed;
return true;
}
byte Reader::readOne() {
byte b = 0;
_content->read(&b, 1);
--_availableBytes;
--_bytesLeft;
return b;
}
/// public
bool Reader::readFirstHeaders() {
if (_state == RS_NONE)
_state = RS_READING_HEADERS;
if (!bytesLeft())
return false;
if (_state == RS_READING_HEADERS)
return readAndHandleFirstHeaders();
warning("Reader::readFirstHeaders(): bad state");
return false;
}
bool Reader::readContent(Common::WriteStream* stream) {
if (_mode != RM_HTTP_GENERIC) {
warning("Reader::readContent(): bad mode");
return false;
}
if (_state != RS_READING_CONTENT) {
warning("Reader::readContent(): bad state");
return false;
}
if (!bytesLeft())
return false;
byte buffer[1024];
while (_availableBytes > 0) {
uint32 bytesRead = _content->read(&buffer, 1024);
_availableBytes -= bytesRead;
_bytesLeft -= bytesRead;
if (stream)
if (stream->write(buffer, bytesRead) != bytesRead) {
warning("Reader::readContent(): failed to write buffer to stream");
return false;
}
if (_availableBytes == 0)
_allContentRead = true;
if (!bytesLeft())
break;
}
if (stream)
stream->flush();
return _allContentRead;
}
bool Reader::readFirstContent(Common::WriteStream *stream) {
if (_mode != RM_POST_FORM_MULTIPART) {
warning("Reader::readFirstContent(): bad mode");
return false;
}
if (_state != RS_READING_CONTENT) {
warning("Reader::readFirstContent(): bad state");
return false;
}
// no difference, actually
return readBlockContent(stream);
}
bool Reader::readBlockHeaders(Common::WriteStream *stream) {
if (_mode != RM_POST_FORM_MULTIPART) {
warning("Reader::readBlockHeaders(): bad mode");
return false;
}
if (_state != RS_READING_HEADERS) {
warning("Reader::readBlockHeaders(): bad state");
return false;
}
if (!bytesLeft())
return false;
return readBlockHeadersIntoStream(stream);
}
bool Reader::readBlockContent(Common::WriteStream *stream) {
if (_mode != RM_POST_FORM_MULTIPART) {
warning("Reader::readBlockContent(): bad mode");
return false;
}
if (_state != RS_READING_CONTENT) {
warning("Reader::readBlockContent(): bad state");
return false;
}
if (!bytesLeft())
return false;
if (!readContentIntoStream(stream))
return false;
if (_availableBytes >= 2) {
Common::String bts;
bts += readOne();
bts += readOne();
if (bts == "--")
_allContentRead = true;
else if (bts != "\r\n")
warning("Reader: strange bytes: \"%s\"", bts.c_str());
} else {
warning("Reader: strange ending");
_allContentRead = true;
}
return true;
}
uint32 Reader::bytesLeft() const { return _bytesLeft; }
void Reader::setMode(ReaderMode mode) { _mode = mode; }
void Reader::setContent(Common::MemoryReadWriteStream *stream) {
_content = stream;
_bytesLeft = stream->size() - stream->pos();
}
bool Reader::badRequest() const { return _isBadRequest; }
bool Reader::noMoreContent() const { return _allContentRead; }
Common::String Reader::headers() const { return _headers; }
Common::String Reader::method() const { return _method; }
Common::String Reader::path() const { return _path; }
Common::String Reader::query() const { return _query; }
Common::String Reader::queryParameter(const Common::String &name) const { return _queryParameters.getValOrDefault(name); }
Common::String Reader::anchor() const { return _anchor; }
Common::String Reader::readEverythingFromMemoryStream(Common::MemoryReadWriteStream *stream) {
Common::String result;
char buf[1024];
uint32 readBytes;
while (true) {
readBytes = stream->read(buf, 1024);
if (readBytes == 0)
break;
result += Common::String(buf, readBytes);
}
return result;
}
} // End of namespace Networking

View File

@@ -0,0 +1,151 @@
/* 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/>.
*
*/
#ifndef BACKENDS_NETWORKING_SDL_NET_READER_H
#define BACKENDS_NETWORKING_SDL_NET_READER_H
#include "common/str.h"
#include "common/hashmap.h"
#include "common/hash-str.h"
namespace Common {
class MemoryReadWriteStream;
class WriteStream;
}
namespace Networking {
enum ReaderState {
RS_NONE,
RS_READING_HEADERS,
RS_READING_CONTENT
};
enum ReaderMode {
RM_HTTP_GENERIC,
RM_POST_FORM_MULTIPART
};
/**
* 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;
ReaderMode _mode;
Common::MemoryReadWriteStream *_content;
uint32 _bytesLeft;
byte *_window;
uint32 _windowUsed, _windowSize, _windowReadPosition, _windowWritePosition;
uint32 _windowHash;
Common::MemoryReadWriteStream *_headersStream;
Common::String _headers;
Common::String _method, _path, _query, _anchor;
Common::HashMap<Common::String, Common::String> _queryParameters;
uint32 _contentLength;
Common::String _boundary;
uint32 _availableBytes;
bool _firstBlock;
bool _isBadRequest;
bool _allContentRead;
void cleanup();
bool readAndHandleFirstHeaders(); //true when ended reading
bool readBlockHeadersIntoStream(Common::WriteStream *stream); //true when ended reading
bool readContentIntoStream(Common::WriteStream *stream); //true when ended reading
void handleFirstHeaders(Common::MemoryReadWriteStream *headers);
void parseFirstLine(const Common::String &headers);
void parsePathQueryAndAnchor(const Common::String &pathToParse);
void parseQueryParameters();
void makeWindow(uint32 size);
void freeWindow();
bool readOneByteInStream(Common::WriteStream *stream, const Common::String &boundary, const uint32 boundaryHash);
byte readOne();
uint32 bytesLeft() const;
public:
static const int32 SUSPICIOUS_HEADERS_SIZE = 1024 * 1024; // 1 MB is really a lot
Reader();
~Reader();
Reader &operator=(Reader &r);
bool readFirstHeaders(); //true when ended reading
bool readContent(Common::WriteStream *stream); //true when ended reading
bool readFirstContent(Common::WriteStream *stream); //true when ended reading
bool readBlockHeaders(Common::WriteStream *stream); //true when ended reading
bool readBlockContent(Common::WriteStream *stream); //true when ended reading
void setMode(ReaderMode mode);
void setContent(Common::MemoryReadWriteStream *stream);
bool badRequest() const;
bool noMoreContent() const;
Common::String headers() const;
Common::String method() const;
Common::String path() const;
Common::String query() const;
Common::String queryParameter(const Common::String &name) const;
Common::String anchor() const;
static Common::String readEverythingFromMemoryStream(Common::MemoryReadWriteStream *stream);
};
} // End of namespace Networking
#endif

View File

@@ -0,0 +1,208 @@
/* 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/uploadfileclienthandler.h"
#include "backends/networking/sdl_net/handlerutils.h"
#include "backends/networking/sdl_net/localwebserver.h"
#include "backends/networking/sdl_net/reader.h"
#include "common/file.h"
#include "common/memstream.h"
#include "common/translation.h"
namespace Networking {
UploadFileClientHandler::UploadFileClientHandler(const Common::Path &parentDirectoryPath):
_state(UFH_READING_CONTENT), _headersStream(nullptr), _contentStream(nullptr),
_parentDirectoryPath(parentDirectoryPath), _uploadedFiles(0) {}
UploadFileClientHandler::~UploadFileClientHandler() {
delete _headersStream;
delete _contentStream;
}
void UploadFileClientHandler::handle(Client *client) {
if (client == nullptr) {
warning("UploadFileClientHandler::handle(): empty client pointer");
return;
}
while (true) {
switch (_state) {
case UFH_READING_CONTENT:
if (client->readFirstContent(nullptr)) {
_state = UFH_READING_BLOCK_HEADERS;
continue;
}
break;
case UFH_READING_BLOCK_HEADERS:
if (_headersStream == nullptr)
_headersStream = new Common::MemoryReadWriteStream(DisposeAfterUse::YES);
if (client->readBlockHeaders(_headersStream)) {
handleBlockHeaders(client);
continue;
}
// fail on suspicious headers
if (_headersStream->size() > Reader::SUSPICIOUS_HEADERS_SIZE) {
setErrorMessageHandler(*client, Common::convertFromU32String(_("Invalid request: headers are too long!")));
}
break;
case UFH_READING_BLOCK_CONTENT:
// _contentStream is created by handleBlockHeaders() if needed
if (client->readBlockContent(_contentStream)) {
handleBlockContent(client);
continue;
}
break;
case UFH_ERROR:
case UFH_STOP:
return;
}
break;
}
}
namespace {
void readFromThatUntilDoubleQuote(const char *cstr, const Common::String &needle, Common::String &result) {
const char *position = strstr(cstr, needle.c_str());
if (position) {
char c;
for (const char *i = position + needle.size(); c = *i, c != 0; ++i) {
if (c == '"')
break;
result += c;
}
}
}
}
void UploadFileClientHandler::handleBlockHeaders(Client *client) {
_state = UFH_READING_BLOCK_CONTENT;
// fail on suspicious headers
if (_headersStream->size() > Reader::SUSPICIOUS_HEADERS_SIZE) {
setErrorMessageHandler(*client, Common::convertFromU32String(_("Invalid request: headers are too long!")));
}
// search for "upload_file" field
Common::String headers = Reader::readEverythingFromMemoryStream(_headersStream);
Common::String fieldName = "";
readFromThatUntilDoubleQuote(headers.c_str(), "name=\"", fieldName);
if (!fieldName.hasPrefix("upload_file"))
return;
Common::String filename = "";
readFromThatUntilDoubleQuote(headers.c_str(), "filename=\"", filename);
// skip block if <filename> is empty
if (filename.empty())
return;
if (HandlerUtils::hasForbiddenCombinations(filename))
return;
// check that <path>/<filename> doesn't exist
Common::Path path = _parentDirectoryPath.appendComponent(filename);
Common::FSNode originalNode(path);
if (!HandlerUtils::permittedPath(originalNode.getPath())) {
setErrorMessageHandler(*client, Common::convertFromU32String(_("Invalid path!")));
return;
}
if (originalNode.exists()) {
setErrorMessageHandler(*client, Common::convertFromU32String(_("There is a file with that name in the parent directory!")));
return;
}
// remove previous stream (if there is one)
if (_contentStream) {
delete _contentStream;
_contentStream = nullptr;
}
// create file stream (and necessary subdirectories)
Common::DumpFile *f = new Common::DumpFile();
if (!f->open(originalNode.getPath(), true)) {
delete f;
setErrorMessageHandler(*client, Common::convertFromU32String(_("Failed to upload the file!")));
return;
}
_contentStream = f;
}
void UploadFileClientHandler::handleBlockContent(Client *client) {
_state = UFH_READING_BLOCK_HEADERS;
// if previous block headers were file-related and created a stream
if (_contentStream) {
_contentStream->flush();
++_uploadedFiles;
delete _contentStream;
_contentStream = nullptr;
if (client->noMoreContent()) {
// success - redirect back to directory listing
setSuccessHandler(*client);
return;
}
}
// no more content avaiable
if (client->noMoreContent()) {
// if no file field was found - failure
if (_uploadedFiles == 0) {
setErrorMessageHandler(*client, Common::convertFromU32String(_("No file was passed!")));
} else {
setSuccessHandler(*client);
}
}
}
void UploadFileClientHandler::setErrorMessageHandler(Client &client, const Common::String &message) {
HandlerUtils::setFilesManagerErrorMessageHandler(client, message);
_state = UFH_ERROR;
}
void UploadFileClientHandler::setSuccessHandler(Client &client) {
// success - redirect back to directory listing
HandlerUtils::setMessageHandler(
client,
Common::String::format(
"%s<br/><a href=\"files?path=%s\">%s</a>",
Common::convertFromU32String(_("Uploaded successfully!")).c_str(),
client.queryParameter("path").c_str(),
Common::convertFromU32String(_("Back to parent directory")).c_str()
),
(client.queryParameter("ajax") == "true" ? "/filesAJAX?path=" : "/files?path=") +
LocalWebserver::urlEncodeQueryParameterValue(client.queryParameter("path"))
);
_state = UFH_STOP;
}
} // End of namespace Networking

View File

@@ -0,0 +1,70 @@
/* 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/>.
*
*/
#ifndef BACKENDS_NETWORKING_SDL_NET_UPLOADFILECLIENTHANDLER_H
#define BACKENDS_NETWORKING_SDL_NET_UPLOADFILECLIENTHANDLER_H
#include "backends/networking/sdl_net/client.h"
#include "common/path.h"
#include "common/stream.h"
namespace Networking {
enum UploadFileHandlerState {
UFH_READING_CONTENT,
UFH_READING_BLOCK_HEADERS,
UFH_READING_BLOCK_CONTENT,
UFH_ERROR,
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;
Common::WriteStream *_contentStream;
Common::Path _parentDirectoryPath;
uint32 _uploadedFiles;
void handleBlockHeaders(Client *client);
void handleBlockContent(Client *client);
void setErrorMessageHandler(Client &client, const Common::String &message);
void setSuccessHandler(Client &client);
public:
UploadFileClientHandler(const Common::Path &parentDirectoryPath);
~UploadFileClientHandler() override;
void handle(Client *client) override;
};
} // End of namespace Networking
#endif