2016-06-15 16:38:37 +06:00
|
|
|
/* 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 2
|
|
|
|
* 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define FORBIDDEN_SYMBOL_ALLOW_ALL
|
|
|
|
|
|
|
|
#include "backends/networking/sdl_net/localwebserver.h"
|
2016-06-15 23:54:53 +06:00
|
|
|
#include "backends/networking/sdl_net/getclienthandler.h"
|
|
|
|
#include "common/memstream.h"
|
2016-06-15 16:38:37 +06:00
|
|
|
#include "common/str.h"
|
|
|
|
#include "common/system.h"
|
|
|
|
#include "common/timer.h"
|
|
|
|
#include "common/textconsole.h"
|
|
|
|
#include <SDL/SDL_net.h>
|
|
|
|
|
|
|
|
namespace Common {
|
2016-06-15 23:54:53 +06:00
|
|
|
class MemoryReadWriteStream;
|
2016-06-15 16:38:37 +06:00
|
|
|
|
|
|
|
DECLARE_SINGLETON(Networking::LocalWebserver);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Networking {
|
|
|
|
|
2016-06-16 19:34:57 +06:00
|
|
|
LocalWebserver::LocalWebserver(): _set(nullptr), _serverSocket(nullptr), _timerStarted(false),
|
|
|
|
_stopOnIdle(false), _clients(0), _idlingFrames(0) {
|
2016-06-16 15:19:13 +06:00
|
|
|
_indexPageHandler.addPathHandler(*this);
|
|
|
|
}
|
2016-06-15 16:38:37 +06:00
|
|
|
|
|
|
|
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, 0, "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() {
|
2016-06-18 14:15:25 +06:00
|
|
|
_handleMutex.lock();
|
2016-06-16 21:04:10 +06:00
|
|
|
_stopOnIdle = false;
|
2016-07-05 16:31:52 +06:00
|
|
|
if (_timerStarted) {
|
|
|
|
_handleMutex.unlock();
|
|
|
|
return;
|
|
|
|
}
|
2016-06-15 16:38:37 +06:00
|
|
|
startTimer();
|
|
|
|
|
|
|
|
// Create a listening TCP socket
|
|
|
|
IPaddress ip;
|
|
|
|
if (SDLNet_ResolveHost(&ip, NULL, SERVER_PORT) == -1) {
|
|
|
|
error("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
|
|
|
|
}
|
2016-07-05 16:31:52 +06:00
|
|
|
_address = Common::String::format(
|
|
|
|
"http://%u.%u.%u.%u:%u/",
|
|
|
|
(ip.host>>24)&0xFF, (ip.host >> 16) & 0xFF, (ip.host >> 8) & 0xFF, ip.host & 0xFF,
|
|
|
|
SERVER_PORT
|
|
|
|
);
|
2016-06-15 16:38:37 +06:00
|
|
|
_serverSocket = SDLNet_TCP_Open(&ip);
|
|
|
|
if (!_serverSocket) {
|
|
|
|
error("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a socket set
|
|
|
|
_set = SDLNet_AllocSocketSet(MAX_CONNECTIONS + 1); //one more for our server socket
|
|
|
|
if (!_set) {
|
|
|
|
error("SDLNet_AllocSocketSet: %s\n", SDLNet_GetError());
|
|
|
|
}
|
|
|
|
|
|
|
|
int numused = SDLNet_TCP_AddSocket(_set, _serverSocket);
|
|
|
|
if (numused == -1) {
|
|
|
|
error("SDLNet_AddSocket: %s\n", SDLNet_GetError());
|
|
|
|
}
|
2016-06-18 14:15:25 +06:00
|
|
|
_handleMutex.unlock();
|
2016-06-15 16:38:37 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocalWebserver::stop() {
|
2016-06-18 14:15:25 +06:00
|
|
|
_handleMutex.lock();
|
2016-06-15 16:38:37 +06:00
|
|
|
if (_timerStarted) stopTimer();
|
|
|
|
|
|
|
|
if (_serverSocket) {
|
|
|
|
SDLNet_TCP_Close(_serverSocket);
|
|
|
|
_serverSocket = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32 i = 0; i < MAX_CONNECTIONS; ++i)
|
2016-06-15 21:36:20 +06:00
|
|
|
_client[i].close();
|
2016-06-15 16:38:37 +06:00
|
|
|
|
|
|
|
_clients = 0;
|
2016-06-15 21:36:20 +06:00
|
|
|
|
|
|
|
if (_set) {
|
|
|
|
SDLNet_FreeSocketSet(_set);
|
|
|
|
_set = nullptr;
|
|
|
|
}
|
2016-06-18 14:15:25 +06:00
|
|
|
_handleMutex.unlock();
|
2016-06-15 16:38:37 +06:00
|
|
|
}
|
|
|
|
|
2016-06-16 15:19:13 +06:00
|
|
|
void LocalWebserver::stopOnIdle() { _stopOnIdle = true; }
|
|
|
|
|
2016-06-16 14:19:18 +06:00
|
|
|
void LocalWebserver::addPathHandler(Common::String path, ClientHandler handler) {
|
|
|
|
if (_pathHandlers.contains(path)) warning("LocalWebserver::addPathHandler: path already had a handler");
|
|
|
|
_pathHandlers[path] = handler;
|
|
|
|
}
|
|
|
|
|
2016-06-16 15:19:13 +06:00
|
|
|
void LocalWebserver::removePathHandler(Common::String path) {
|
|
|
|
if (!_pathHandlers.contains(path)) warning("LocalWebserver::removePathHandler: no handler known for this path");
|
|
|
|
_pathHandlers.erase(path);
|
|
|
|
}
|
|
|
|
|
2016-07-05 16:31:52 +06:00
|
|
|
Common::String LocalWebserver::getAddress() { return _address; }
|
|
|
|
|
2016-06-16 15:19:13 +06:00
|
|
|
IndexPageHandler &LocalWebserver::indexPageHandler() { return _indexPageHandler; }
|
|
|
|
|
2016-07-05 16:31:52 +06:00
|
|
|
bool LocalWebserver::isRunning() {
|
|
|
|
bool result = false;
|
|
|
|
_handleMutex.lock();
|
|
|
|
result = _timerStarted;
|
|
|
|
_handleMutex.unlock();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-06-15 16:38:37 +06:00
|
|
|
void LocalWebserver::handle() {
|
2016-06-18 14:15:25 +06:00
|
|
|
_handleMutex.lock();
|
2016-06-15 16:38:37 +06:00
|
|
|
int numready = SDLNet_CheckSockets(_set, 0);
|
|
|
|
if (numready == -1) {
|
|
|
|
error("SDLNet_CheckSockets: %s\n", SDLNet_GetError());
|
2016-06-15 21:36:20 +06:00
|
|
|
} else if (numready) acceptClient();
|
|
|
|
|
|
|
|
for (uint32 i = 0; i < MAX_CONNECTIONS; ++i)
|
|
|
|
handleClient(i);
|
2016-06-16 15:19:13 +06:00
|
|
|
|
2016-06-16 19:34:57 +06:00
|
|
|
_clients = 0;
|
|
|
|
for (uint32 i = 0; i < MAX_CONNECTIONS; ++i)
|
|
|
|
if (_client[i].state() != INVALID)
|
|
|
|
++_clients;
|
|
|
|
|
|
|
|
if (_clients == 0) ++_idlingFrames;
|
|
|
|
else _idlingFrames = 0;
|
|
|
|
|
2016-06-18 14:15:25 +06:00
|
|
|
if (_idlingFrames > FRAMES_PER_SECOND && _stopOnIdle) {
|
|
|
|
_handleMutex.unlock();
|
|
|
|
stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleMutex.unlock();
|
2016-06-15 21:36:20 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
//if GET, check whether we know a handler for such URL
|
|
|
|
//if PUT, check whether we know a handler for that URL
|
2016-06-16 14:19:18 +06:00
|
|
|
if (_pathHandlers.contains(_client[i].path()))
|
|
|
|
(*_pathHandlers[_client[i].path()])(_client[i]);
|
|
|
|
|
|
|
|
if (_client[i].state() == BEING_HANDLED || _client[i].state() == INVALID) break;
|
|
|
|
|
2016-06-15 21:36:20 +06:00
|
|
|
//if no handler, answer with default BAD REQUEST
|
2016-06-16 14:19:18 +06:00
|
|
|
//fallthrough
|
2016-06-15 21:59:03 +06:00
|
|
|
case BAD_REQUEST:
|
2016-06-15 23:54:53 +06:00
|
|
|
setClientGetHandler(_client[i], "<html><head><title>ScummVM - Bad Request</title></head><body>BAD REQUEST</body></html>", 400);
|
|
|
|
break;
|
|
|
|
case BEING_HANDLED:
|
|
|
|
_client[i].handle();
|
2016-06-15 21:59:03 +06:00
|
|
|
break;
|
2016-06-15 16:38:37 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-15 21:36:20 +06:00
|
|
|
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;
|
|
|
|
}
|
2016-06-16 19:34:57 +06:00
|
|
|
|
|
|
|
++_clients;
|
|
|
|
for (uint32 i = 0; i < MAX_CONNECTIONS; ++i)
|
|
|
|
if (_client[i].state() == INVALID) {
|
|
|
|
_client[i].open(_set, client);
|
|
|
|
break;
|
|
|
|
}
|
2016-06-15 21:36:20 +06:00
|
|
|
}
|
|
|
|
|
2016-06-16 19:34:57 +06:00
|
|
|
void LocalWebserver::setClientGetHandler(Client &client, Common::String response, long code, const char *mimeType) {
|
2016-06-15 23:54:53 +06:00
|
|
|
byte *data = new byte[response.size()];
|
|
|
|
memcpy(data, response.c_str(), response.size());
|
|
|
|
Common::MemoryReadStream *stream = new Common::MemoryReadStream(data, response.size(), DisposeAfterUse::YES);
|
2016-06-16 19:34:57 +06:00
|
|
|
setClientGetHandler(client, stream, code, mimeType);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocalWebserver::setClientGetHandler(Client &client, Common::SeekableReadStream *responseStream, long code, const char *mimeType) {
|
|
|
|
GetClientHandler *handler = new GetClientHandler(responseStream);
|
2016-06-15 23:54:53 +06:00
|
|
|
handler->setResponseCode(code);
|
2016-06-16 19:34:57 +06:00
|
|
|
if (mimeType) handler->setHeader("Content-Type", mimeType);
|
2016-06-15 23:54:53 +06:00
|
|
|
client.setHandler(handler);
|
|
|
|
}
|
|
|
|
|
2016-06-16 19:34:57 +06:00
|
|
|
const char *LocalWebserver::determineMimeType(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";
|
|
|
|
}
|
|
|
|
|
2016-06-15 16:38:37 +06:00
|
|
|
} // End of namespace Networking
|