aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/sessions/httpsessions.cpp
blob: 05be3c814e1dff6e5ccbfdc23274a4fb574ccfac (plain) (blame)
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
// Copyright Epic Games, Inc. All Rights Reserved.

#include "httpsessions.h"

#include <zencore/compactbinarybuilder.h>
#include <zencore/compactbinaryvalidation.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <zencore/trace.h>
#include "sessions.h"

namespace zen {
using namespace std::literals;

HttpSessionsService::HttpSessionsService(HttpStatusService& StatusService, HttpStatsService& StatsService, SessionsService& Sessions)
: m_Log(logging::Get("sessions"))
, m_StatusService(StatusService)
, m_StatsService(StatsService)
, m_Sessions(Sessions)
{
	Initialize();
}

HttpSessionsService::~HttpSessionsService()
{
	m_StatsService.UnregisterHandler("sessions", *this);
	m_StatusService.UnregisterHandler("sessions", *this);
}

const char*
HttpSessionsService::BaseUri() const
{
	return "/sessions/";
}

void
HttpSessionsService::HandleRequest(HttpServerRequest& Request)
{
	metrics::OperationTiming::Scope $(m_HttpRequests);

	if (m_Router.HandleRequest(Request) == false)
	{
		ZEN_WARN("No route found for {0}", Request.RelativeUri());
		return Request.WriteResponse(HttpResponseCode::NotFound, HttpContentType::kText, "Not found"sv);
	}
}

CbObject
HttpSessionsService::CollectStats()
{
	ZEN_TRACE_CPU("SessionsService::Stats");
	CbObjectWriter Cbo;

	EmitSnapshot("requests", m_HttpRequests, Cbo);

	Cbo.BeginObject("sessions");
	{
		Cbo << "readcount" << m_SessionsStats.SessionReadCount;
		Cbo << "writecount" << m_SessionsStats.SessionWriteCount;
		Cbo << "deletecount" << m_SessionsStats.SessionDeleteCount;
		Cbo << "listcount" << m_SessionsStats.SessionListCount;
		Cbo << "requestcount" << m_SessionsStats.RequestCount;
		Cbo << "badrequestcount" << m_SessionsStats.BadRequestCount;
		Cbo << "count" << m_Sessions.GetSessionCount();
	}
	Cbo.EndObject();

	return Cbo.Save();
}

void
HttpSessionsService::HandleStatsRequest(HttpServerRequest& HttpReq)
{
	HttpReq.WriteResponse(HttpResponseCode::OK, CollectStats());
}

void
HttpSessionsService::HandleStatusRequest(HttpServerRequest& Request)
{
	ZEN_TRACE_CPU("HttpSessionsService::Status");
	CbObjectWriter Cbo;
	Cbo << "ok" << true;
	Request.WriteResponse(HttpResponseCode::OK, Cbo.Save());
}

void
HttpSessionsService::Initialize()
{
	using namespace std::literals;

	ZEN_INFO("Initializing Sessions Service");

	static constexpr AsciiSet ValidHexCharactersSet{"0123456789abcdefABCDEF"};

	m_Router.AddMatcher("session_id", [](std::string_view Str) -> bool {
		return Str.length() == Oid::StringLength && AsciiSet::HasOnly(Str, ValidHexCharactersSet);
	});

	m_Router.RegisterRoute(
		"list",
		[this](HttpRouterRequest& Req) { ListSessionsRequest(Req); },
		HttpVerb::kGet);

	m_Router.RegisterRoute(
		"{session_id}",
		[this](HttpRouterRequest& Req) { SessionRequest(Req); },
		HttpVerb::kGet | HttpVerb::kPost | HttpVerb::kPut | HttpVerb::kDelete);

	m_Router.RegisterRoute(
		"",
		[this](HttpRouterRequest& Req) { ListSessionsRequest(Req); },
		HttpVerb::kGet);

	m_StatsService.RegisterHandler("sessions", *this);
	m_StatusService.RegisterHandler("sessions", *this);
}

static void
WriteSessionInfo(CbWriter& Writer, const SessionsService::SessionInfo& Info)
{
	Writer << "id" << Info.Id;
	if (!Info.AppName.empty())
	{
		Writer << "appname" << Info.AppName;
	}
	if (Info.JobId != Oid::Zero)
	{
		Writer << "jobid" << Info.JobId;
	}
	Writer << "created_at" << Info.CreatedAt;
	Writer << "updated_at" << Info.UpdatedAt;

	if (Info.Metadata.GetSize() > 0)
	{
		Writer.BeginObject("metadata");
		for (const CbField& Field : Info.Metadata)
		{
			Writer.AddField(Field);
		}
		Writer.EndObject();
	}
}

void
HttpSessionsService::ListSessionsRequest(HttpRouterRequest& Req)
{
	HttpServerRequest& ServerRequest = Req.ServerRequest();

	m_SessionsStats.SessionListCount++;
	m_SessionsStats.RequestCount++;

	std::vector<Ref<SessionsService::Session>> Sessions = m_Sessions.GetSessions();

	CbObjectWriter Response;
	Response.BeginArray("sessions");
	for (const Ref<SessionsService::Session>& Session : Sessions)
	{
		Response.BeginObject();
		{
			WriteSessionInfo(Response, Session->Info());
		}
		Response.EndObject();
	}
	Response.EndArray();

	return ServerRequest.WriteResponse(HttpResponseCode::OK, Response.Save());
}

void
HttpSessionsService::SessionRequest(HttpRouterRequest& Req)
{
	HttpServerRequest& ServerRequest = Req.ServerRequest();

	const Oid SessionId = Oid::TryFromHexString(Req.GetCapture(1));
	if (SessionId == Oid::Zero)
	{
		m_SessionsStats.BadRequestCount++;
		return ServerRequest.WriteResponse(HttpResponseCode::BadRequest,
										   HttpContentType::kText,
										   fmt::format("Invalid session id '{}'", Req.GetCapture(1)));
	}

	m_SessionsStats.RequestCount++;

	switch (ServerRequest.RequestVerb())
	{
		case HttpVerb::kPost:
		case HttpVerb::kPut:
			{
				IoBuffer Payload = ServerRequest.ReadPayload();
				CbObject RequestObject;

				if (Payload.GetSize() > 0)
				{
					if (CbValidateError ValidationResult = ValidateCompactBinary(Payload.GetView(), CbValidateMode::All);
						ValidationResult != CbValidateError::None)
					{
						m_SessionsStats.BadRequestCount++;
						return ServerRequest.WriteResponse(HttpResponseCode::BadRequest,
														   HttpContentType::kText,
														   fmt::format("Invalid payload: {}", zen::ToString(ValidationResult)));
					}
					RequestObject = LoadCompactBinaryObject(Payload);
				}

				if (ServerRequest.RequestVerb() == HttpVerb::kPost)
				{
					std::string	 AppName(RequestObject["appname"sv].AsString());
					Oid			 JobId		  = RequestObject["jobid"sv].AsObjectId();
					CbObjectView MetadataView = RequestObject["metadata"sv].AsObjectView();

					m_SessionsStats.SessionWriteCount++;
					if (m_Sessions.RegisterSession(SessionId, std::move(AppName), JobId, MetadataView))
					{
						return ServerRequest.WriteResponse(HttpResponseCode::Created, HttpContentType::kText, fmt::format("{}", SessionId));
					}
					else
					{
						// Already exists - try update instead
						if (m_Sessions.UpdateSession(SessionId, MetadataView))
						{
							return ServerRequest.WriteResponse(HttpResponseCode::OK, HttpContentType::kText, fmt::format("{}", SessionId));
						}
						return ServerRequest.WriteResponse(HttpResponseCode::InternalServerError);
					}
				}
				else
				{
					// PUT - update only
					m_SessionsStats.SessionWriteCount++;
					if (m_Sessions.UpdateSession(SessionId, RequestObject["metadata"sv].AsObjectView()))
					{
						return ServerRequest.WriteResponse(HttpResponseCode::OK, HttpContentType::kText, fmt::format("{}", SessionId));
					}
					return ServerRequest.WriteResponse(HttpResponseCode::NotFound,
													   HttpContentType::kText,
													   fmt::format("Session '{}' not found", SessionId));
				}
			}
		case HttpVerb::kGet:
			{
				m_SessionsStats.SessionReadCount++;
				Ref<SessionsService::Session> Session = m_Sessions.GetSession(SessionId);
				if (Session)
				{
					CbObjectWriter Response;
					WriteSessionInfo(Response, Session->Info());
					return ServerRequest.WriteResponse(HttpResponseCode::OK, Response.Save());
				}
				return ServerRequest.WriteResponse(HttpResponseCode::NotFound);
			}
		case HttpVerb::kDelete:
			{
				m_SessionsStats.SessionDeleteCount++;
				if (m_Sessions.RemoveSession(SessionId))
				{
					return ServerRequest.WriteResponse(HttpResponseCode::OK);
				}
				return ServerRequest.WriteResponse(HttpResponseCode::NotFound);
			}
	}
}

}  // namespace zen