-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmap_state_http.cpp
More file actions
297 lines (264 loc) · 7.58 KB
/
Copy pathmap_state_http.cpp
File metadata and controls
297 lines (264 loc) · 7.58 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "map_state_http.h"
#include <winsock2.h>
#include <ws2tcpip.h>
#include "map_state_capture.h"
#include "map_state_json.h"
#include "plugin_config.h"
#include "plugin_helpers.h"
#include <atomic>
#include <sstream>
#include <string>
#include <thread>
#pragma comment(lib, "Ws2_32.lib")
namespace MapStateRuntime
{
namespace Detail
{
namespace
{
constexpr int kHttpReadBufferSize = 8192;
constexpr int kHttpSelectTimeoutMs = 250;
constexpr int kDefaultHttpPort = 9000;
constexpr const char* kLoopbackAddress = "127.0.0.1";
std::thread g_httpThread;
std::atomic<bool> g_httpStopRequested = false;
SOCKET g_httpListenSocket = INVALID_SOCKET;
bool g_wsaStarted = false;
int g_httpPort = kDefaultHttpPort;
bool SendAll(SOCKET socketHandle, const std::string& data)
{
const char* buffer = data.data();
int remaining = static_cast<int>(data.size());
while (remaining > 0)
{
const int sent = send(socketHandle, buffer, remaining, 0);
if (sent == SOCKET_ERROR)
{
return false;
}
buffer += sent;
remaining -= sent;
}
return true;
}
void SendHttpResponse(SOCKET socketHandle, int statusCode, const char* statusText, const char* contentType, const std::string& body)
{
std::ostringstream header;
header << "HTTP/1.1 " << statusCode << ' ' << statusText << "\r\n"
<< "Connection: close\r\n"
<< "Content-Type: " << contentType << "\r\n"
<< "Content-Length: " << body.size() << "\r\n"
<< "Cache-Control: no-store\r\n"
<< "Access-Control-Allow-Origin: *\r\n"
<< "Access-Control-Allow-Methods: GET, OPTIONS\r\n"
<< "Access-Control-Allow-Headers: Content-Type\r\n"
<< "\r\n";
std::string payload = header.str();
payload += body;
SendAll(socketHandle, payload);
}
void SendJsonResponse(SOCKET socketHandle, int statusCode, const char* statusText, const std::string& json)
{
SendHttpResponse(socketHandle, statusCode, statusText, "application/json; charset=utf-8", json);
}
void HandleHttpClient(SOCKET clientSocket)
{
char buffer[kHttpReadBufferSize + 1]{};
const int received = recv(clientSocket, buffer, kHttpReadBufferSize, 0);
if (received <= 0)
{
return;
}
buffer[received] = '\0';
const std::string requestText(buffer, received);
std::istringstream request(requestText);
std::string method;
std::string path;
std::string version;
request >> method >> path >> version;
if (method.empty() || path.empty())
{
SendJsonResponse(clientSocket, 400, "Bad Request", "{\"ok\":false,\"error\":\"invalid_request\"}");
return;
}
const size_t queryPos = path.find('?');
if (queryPos != std::string::npos)
{
path = path.substr(0, queryPos);
}
if (method == "OPTIONS")
{
SendHttpResponse(clientSocket, 204, "No Content", "text/plain; charset=utf-8", {});
return;
}
if (method != "GET")
{
SendJsonResponse(clientSocket, 405, "Method Not Allowed", "{\"ok\":false,\"error\":\"method_not_allowed\"}");
return;
}
const CargoSnapshot snapshot = CopySnapshot();
if (path == "/health")
{
SendJsonResponse(clientSocket, 200, "OK", BuildHealthJson(snapshot, g_httpPort));
return;
}
if (path == "/cargo")
{
RequestCargoSnapshotRefresh("HTTP /cargo");
const CargoSnapshot refreshedSnapshot = CopySnapshot();
SendJsonResponse(clientSocket, 200, "OK", BuildCargoJson(refreshedSnapshot));
return;
}
if (path == "/rupture-cycle")
{
RequestCargoSnapshotRefresh("HTTP /rupture-cycle");
const CargoSnapshot refreshedSnapshot = CopySnapshot();
SendJsonResponse(clientSocket, 200, "OK", BuildRuptureCycleJson(refreshedSnapshot));
return;
}
if (path == "/" || path == "/index.html")
{
SendJsonResponse(clientSocket, 200, "OK", "{\"ok\":true,\"endpoints\":[\"/health\",\"/cargo\",\"/rupture-cycle\"]}");
return;
}
SendJsonResponse(clientSocket, 404, "Not Found", "{\"ok\":false,\"error\":\"not_found\"}");
}
void HttpServerMain()
{
while (!g_httpStopRequested.load())
{
fd_set readSet;
FD_ZERO(&readSet);
FD_SET(g_httpListenSocket, &readSet);
timeval timeout{};
timeout.tv_sec = 0;
timeout.tv_usec = kHttpSelectTimeoutMs * 1000;
const int result = select(0, &readSet, nullptr, nullptr, &timeout);
if (g_httpStopRequested.load())
{
break;
}
if (result == SOCKET_ERROR)
{
const int error = WSAGetLastError();
if (!g_httpStopRequested.load())
{
LOG_ERROR("HTTP select() failed on port %d: %d", g_httpPort, error);
}
break;
}
if (result == 0 || !FD_ISSET(g_httpListenSocket, &readSet))
{
continue;
}
SOCKET clientSocket = accept(g_httpListenSocket, nullptr, nullptr);
if (clientSocket == INVALID_SOCKET)
{
const int error = WSAGetLastError();
if (!g_httpStopRequested.load())
{
LOG_WARN("HTTP accept() failed on port %d: %d", g_httpPort, error);
}
continue;
}
HandleHttpClient(clientSocket);
closesocket(clientSocket);
}
}
}
bool StartHttpServer()
{
if (g_httpListenSocket != INVALID_SOCKET)
{
return true;
}
g_httpPort = MapExtensionPluginConfig::Config::HttpPort();
if (g_httpPort <= 0)
{
g_httpPort = kDefaultHttpPort;
}
WSADATA wsaData{};
const int wsaResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (wsaResult != 0)
{
LOG_ERROR("HTTP WSAStartup failed: %d", wsaResult);
return false;
}
g_wsaStarted = true;
g_httpListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (g_httpListenSocket == INVALID_SOCKET)
{
LOG_ERROR("HTTP socket() failed: %d", WSAGetLastError());
WSACleanup();
g_wsaStarted = false;
return false;
}
const char reuse = 1;
setsockopt(g_httpListenSocket, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(static_cast<u_short>(g_httpPort));
inet_pton(AF_INET, kLoopbackAddress, &addr.sin_addr);
if (bind(g_httpListenSocket, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == SOCKET_ERROR)
{
LOG_ERROR("HTTP bind() failed on %s:%d: %d", kLoopbackAddress, g_httpPort, WSAGetLastError());
closesocket(g_httpListenSocket);
g_httpListenSocket = INVALID_SOCKET;
WSACleanup();
g_wsaStarted = false;
return false;
}
if (listen(g_httpListenSocket, SOMAXCONN) == SOCKET_ERROR)
{
LOG_ERROR("HTTP listen() failed on port %d: %d", g_httpPort, WSAGetLastError());
closesocket(g_httpListenSocket);
g_httpListenSocket = INVALID_SOCKET;
WSACleanup();
g_wsaStarted = false;
return false;
}
g_httpStopRequested = false;
g_httpThread = std::thread(HttpServerMain);
LOG_INFO("HTTP cargo endpoint listening on http://%s:%d", kLoopbackAddress, g_httpPort);
return true;
}
void StopHttpServer()
{
g_httpStopRequested = true;
if (g_httpListenSocket != INVALID_SOCKET)
{
closesocket(g_httpListenSocket);
g_httpListenSocket = INVALID_SOCKET;
}
if (g_httpThread.joinable())
{
g_httpThread.join();
}
if (g_wsaStarted)
{
WSACleanup();
g_wsaStarted = false;
}
}
void HandleHttpProcessDetach(bool processTerminating)
{
if (!processTerminating)
{
return;
}
// During process teardown the CRT can destroy joinable std::thread objects
// after plugin shutdown opportunities are gone, which triggers std::terminate.
// Detach the listener thread here so process exit can continue cleanly.
g_httpStopRequested = true;
if (g_httpListenSocket != INVALID_SOCKET)
{
closesocket(g_httpListenSocket);
g_httpListenSocket = INVALID_SOCKET;
}
if (g_httpThread.joinable())
{
g_httpThread.detach();
}
}
}
}