forked from microsoft/react-native-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpServer.h
More file actions
142 lines (104 loc) · 3.77 KB
/
Copy pathHttpServer.h
File metadata and controls
142 lines (104 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// clang-format off
#pragma once
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/strand.hpp>
#include <boost/beast/core/flat_buffer.hpp>
#include <boost/beast/core/tcp_stream.hpp>
#include <boost/beast/http.hpp>
#include <thread>
namespace Microsoft::React::Test
{
using DynamicRequest = boost::beast::http::request<boost::beast::http::dynamic_body>;
using DynamicResponse = boost::beast::http::response<boost::beast::http::dynamic_body>;
using EmptyResponse = boost::beast::http::response<boost::beast::http::empty_body>;
using FileResponse = boost::beast::http::response<boost::beast::http::file_body>;
using StringResponse = boost::beast::http::response<boost::beast::http::string_body>;
class ResponseWrapper
{
public:
enum class ResponseType : size_t { Empty, Dynamic, File, String };
private:
std::shared_ptr<void> m_response;
ResponseType m_type;
public:
ResponseWrapper(DynamicResponse&& response);
ResponseWrapper(EmptyResponse&& response);
ResponseWrapper(FileResponse&& response);
ResponseWrapper(StringResponse&& response);
ResponseWrapper(DynamicResponse const& response);
ResponseWrapper(EmptyResponse const& response);
ResponseWrapper(StringResponse const& response);
ResponseWrapper(ResponseWrapper&&) = default;
std::shared_ptr<void> Response();
ResponseType Type();
};
#pragma region Utility functions
boost::beast::multi_buffer CreateStringResponseBody(std::string&& content);
#pragma endregion Utility functions
struct HttpCallbacks
{
std::function<void()> OnResponseSent;
std::function<void()> OnRequest;
std::function<ResponseWrapper(const DynamicRequest &)> OnGet;
std::function<ResponseWrapper(const DynamicRequest &)> OnPost;
std::function<ResponseWrapper(const DynamicRequest &)> OnPatch;
std::function<ResponseWrapper(const DynamicRequest &)> OnOptions;
std::function<ResponseWrapper(const DynamicRequest &)> OnConnect;
std::function<ResponseWrapper(const DynamicRequest &)> OnTrace;
//Not supported by Boost/Beast
#if 0
std::function<ResponseWrapper(const DynamicRequest &)> OnTrack;
#endif // 0
};
///
// Represents a client session in within an owning server instance.
// Generates and submits the appropriate HTTP response.
///
class HttpSession : public std::enable_shared_from_this<HttpSession>
{
boost::beast::tcp_stream m_stream;
boost::beast::flat_buffer m_buffer;
DynamicRequest m_request;
std::shared_ptr<ResponseWrapper> m_response; // Generic response
HttpCallbacks& m_callbacks;
std::function<void(Microsoft::React::Test::ResponseWrapper&&)> m_sendLambda;
void Read();
void Respond();
void Close();
void OnRead(boost::system::error_code ec, std::size_t transferred);
void OnWrite(bool close, boost::system::error_code ec, std::size_t transferred);
public:
HttpSession(boost::asio::ip::tcp::socket&& socket, HttpCallbacks &callbacks,
boost::asio::io_context& ioContext
);
~HttpSession();
void Start();
};
///
// Represents an HTTP server endpoint (IP:PORT).
// Accepts client connections and dispatches sessions for each incoming
// connection.
///
class HttpServer : public std::enable_shared_from_this<HttpServer>
{
size_t m_ioThreadCount;
std::vector<std::thread> m_ioThreads;
boost::asio::io_context m_ioContext;
boost::asio::ip::tcp::acceptor m_acceptor;
HttpCallbacks m_callbacks;
void OnAccept(boost::system::error_code ec, boost::asio::ip::tcp::socket socket);
public:
///
// address - Valid IP address string (i.e. "127.0.0.1).
// port - TCP port number (i.e. 80).
///
HttpServer(std::string &&address, uint16_t port, int concurrency = 1);
HttpServer(uint16_t port, int concurrency = 1);
~HttpServer();
void Accept();
void Start();
void Stop();
void Abort();//TODO: Remove?
HttpCallbacks& Callbacks();
};
} // namespace Microsoft::React::Test