1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef BACKENDS_NETWORKING_SDL_NET_LOCALWEBSERVER_H
24 #define BACKENDS_NETWORKING_SDL_NET_LOCALWEBSERVER_H
25 
26 #include "backends/networking/sdl_net/client.h"
27 #include "backends/networking/sdl_net/handlers/basehandler.h"
28 #include "backends/networking/sdl_net/handlers/createdirectoryhandler.h"
29 #include "backends/networking/sdl_net/handlers/downloadfilehandler.h"
30 #include "backends/networking/sdl_net/handlers/filesajaxpagehandler.h"
31 #include "backends/networking/sdl_net/handlers/filespagehandler.h"
32 #include "backends/networking/sdl_net/handlers/indexpagehandler.h"
33 #include "backends/networking/sdl_net/handlers/listajaxhandler.h"
34 #include "backends/networking/sdl_net/handlers/resourcehandler.h"
35 #include "backends/networking/sdl_net/handlers/uploadfilehandler.h"
36 #include "common/hash-str.h"
37 #include "common/mutex.h"
38 #include "common/singleton.h"
39 #include "common/scummsys.h"
40 
41 namespace Common {
42 class SeekableReadStream;
43 }
44 
45 typedef struct _SDLNet_SocketSet *SDLNet_SocketSet;
46 typedef struct _TCPsocket *TCPsocket;
47 
48 namespace Networking {
49 
50 #define NETWORKING_LOCALWEBSERVER_ENABLE_PORT_OVERRIDE
51 
52 class LocalWebserver : public Common::Singleton<LocalWebserver> {
53 	static const uint32 FRAMES_PER_SECOND = 20;
54 	static const uint32 TIMER_INTERVAL = 1000000 / FRAMES_PER_SECOND;
55 	static const uint32 MAX_CONNECTIONS = 10;
56 
57 	friend void localWebserverTimer(void *); //calls handle()
58 
59 	SDLNet_SocketSet _set;
60 	TCPsocket _serverSocket;
61 	Client _client[MAX_CONNECTIONS];
62 	uint32 _clients;
63 	bool _timerStarted, _stopOnIdle, _minimalMode;
64 	Common::HashMap<Common::String, BaseHandler*> _pathHandlers;
65 	BaseHandler *_defaultHandler;
66 	IndexPageHandler _indexPageHandler;
67 	FilesPageHandler _filesPageHandler;
68 	CreateDirectoryHandler _createDirectoryHandler;
69 	DownloadFileHandler _downloadFileHandler;
70 	UploadFileHandler _uploadFileHandler;
71 	ListAjaxHandler _listAjaxHandler;
72 	FilesAjaxPageHandler _filesAjaxPageHandler;
73 	ResourceHandler _resourceHandler;
74 	uint32 _idlingFrames;
75 	Common::Mutex _handleMutex;
76 	Common::String _address;
77 	uint32 _serverPort;
78 
79 	void startTimer(int interval = TIMER_INTERVAL);
80 	void stopTimer();
81 	void handle();
82 	void handleClient(uint32 i);
83 	void acceptClient();
84 	void resolveAddress(void *ipAddress);
85 	void addPathHandler(Common::String path, BaseHandler *handler);
86 
87 public:
88 	static const uint32 DEFAULT_SERVER_PORT = 12345;
89 
90 	LocalWebserver();
91 	virtual ~LocalWebserver();
92 
93 	void start(bool useMinimalMode = false);
94 	void stop();
95 	void stopOnIdle();
96 
97 	Common::String getAddress();
98 	IndexPageHandler &indexPageHandler();
99 	bool isRunning();
100 	static uint32 getPort();
101 
102 	static void setClientGetHandler(Client &client, Common::String response, long code = 200, const char *mimeType = nullptr);
103 	static void setClientGetHandler(Client &client, Common::SeekableReadStream *responseStream, long code = 200, const char *mimeType = nullptr);
104 	static void setClientRedirectHandler(Client &client, Common::String response, Common::String location, const char *mimeType = nullptr);
105 	static void setClientRedirectHandler(Client &client, Common::SeekableReadStream *responseStream, Common::String location, const char *mimeType = nullptr);
106 	static Common::String urlDecode(Common::String value);
107 	static Common::String urlEncodeQueryParameterValue(Common::String value);
108 };
109 
110 /** Shortcut for accessing the local webserver. */
111 #define LocalServer     Networking::LocalWebserver::instance()
112 
113 } // End of namespace Networking
114 
115 #endif
116