summaryrefslogtreecommitdiffstats
path: root/BrowserSource/Proxy/WebServer.h
blob: 7815e890c05c244b87f11601ce8fcf5b01a45cd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#pragma once

#include <stdint.h>

#include <functional>
#include <future>
#include <map>
#include <mutex>
#include <string>
#include <vector>

#include "WebCommon.h"

namespace WebServer {
	class WebServer {
	public:
		WebServer(std::uint16_t port);

		typedef std::function<void(
			int& status_code,
			std::string& payload,
			ContentType& type)> handler_t;

		bool RegisterPathHandler(const std::string& method,
			const std::string& path, handler_t&& handler);
		void RegisterDefaultHandler(handler_t&& handler);

		bool Run(volatile bool* run);

	private:
		// Dispatch requests by mapping from (method, path) to handler.
		// Dispatch key is (method, path) in that order.
		typedef std::tuple<std::string, std::string> dispatch_key_t;
		static inline dispatch_key_t GetDispatchKey(const std::string& method, const std::string& path)
		{
			return dispatch_key_t(method, path);
		}

		typedef std::map<dispatch_key_t, handler_t> dispatch_map_t;
		dispatch_map_t dispatch_map_;
		handler_t default_handler_;

		const uint16_t port_;

		std::vector<std::future<void>> connections_;
	};
}