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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include <auth/authmgr.h>
#include <upstream/jupiter.h>
#include <upstream/upstreamcache.h>
#include <upstream/upstreamservice.h>
#include <upstream/zen.h>
#include <zencore/compactbinarybuilder.h>
#include <zencore/string.h>
#include <json11.hpp>
namespace zen {
using namespace std::literals;
namespace {
json11::Json TryGetJson(IoBuffer Body, HttpContentType ContentType, std::string& OutError)
{
if (!Body)
{
OutError = "No data"sv;
return json11::Json();
}
if ((ContentType == HttpContentType::kJSON || ContentType == HttpContentType::kCbObject) == false)
{
OutError = "Invalid content type"sv;
return json11::Json();
}
if (ContentType == ZenContentType::kJSON)
{
std::string JsonText(reinterpret_cast<const char*>(Body.GetData()), Body.GetSize());
return json11::Json::parse(JsonText, OutError);
}
if (CbObject Obj = LoadCompactBinaryObject(Body))
{
ExtendableStringBuilder<512> Sb;
return json11::Json::parse(Obj.ToJson(Sb).ToString(), OutError);
}
OutError = "Invalid compact binary"sv;
return json11::Json();
}
void WriteErrorResponse(HttpServerRequest& Request, std::string_view Property, std::string_view Reason)
{
CbObjectWriter Response;
Response << "Result"sv << false;
Response.BeginObject("Error"sv);
Response << "Property"sv << Property << "Reason"sv << Reason;
Response.EndObject();
Request.WriteResponse(HttpResponseCode::BadRequest, Response.Save());
}
void WriteSuccessResponse(HttpServerRequest& Request)
{
CbObjectWriter Response;
Response << "Result"sv << true;
Request.WriteResponse(HttpResponseCode::OK, Response.Save());
}
} // namespace
HttpUpstreamService::HttpUpstreamService(UpstreamCache& Upstream, AuthMgr& Mgr) : m_Upstream(Upstream), m_AuthMgr(Mgr)
{
m_Router.RegisterRoute(
"endpoints",
[this](HttpRouterRequest& Req) {
CbObjectWriter Writer;
Writer.BeginArray("Endpoints"sv);
m_Upstream.IterateEndpoints([&Writer](UpstreamEndpoint& Ep) {
UpstreamEndpointInfo Info = Ep.GetEndpointInfo();
UpstreamEndpointStatus Status = Ep.GetStatus();
Writer.BeginObject();
Writer << "Name"sv << Info.Name;
Writer << "Url"sv << Info.Url;
Writer << "State"sv << ToString(Status.State);
Writer << "Reason"sv << Status.Reason;
Writer.EndObject();
return true;
});
Writer.EndArray();
Req.ServerRequest().WriteResponse(HttpResponseCode::OK, Writer.Save());
},
HttpVerb::kGet);
m_Router.RegisterRoute(
"endpoints",
[this](HttpRouterRequest& RouterRequest) {
HttpServerRequest& ServerRequest = RouterRequest.ServerRequest();
std::string JsonError;
json11::Json Json = TryGetJson(ServerRequest.ReadPayload(), ServerRequest.RequestContentType(), JsonError);
if (!JsonError.empty())
{
return ServerRequest.WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, JsonError);
}
const auto Type = Json["Type"].string_value();
const auto Name = Json["Name"].string_value();
const auto Url = Json["Url"].string_value();
const auto Namespace = Json["Namespace"].string_value();
const auto OAuthProvider = Json["OAuthProvider"].string_value();
const auto OAuthClientId = Json["OAuthClientId"].string_value();
const auto OAuthSecret = Json["OAuthSecret"].string_value();
const auto OAuthToken = Json["OAuthToken"].string_value();
if ((Type == "Horde"sv || Type == "Zen"sv) == false)
{
return WriteErrorResponse(ServerRequest, "Type"sv, "Invalid endpoint type, must be Zen or Horde"sv);
}
if (Name.empty())
{
return WriteErrorResponse(ServerRequest, "Name"sv, "Invalid endpoint name"sv);
}
if (Url.empty())
{
return WriteErrorResponse(ServerRequest, "Url"sv, "Invalid endpoint URL"sv);
}
bool IsNameUnique = true;
m_Upstream.IterateEndpoints([&Name, &IsNameUnique](UpstreamEndpoint& Ep) {
IsNameUnique = IsNameUnique && Ep.GetEndpointInfo().Name != Name;
return IsNameUnique;
});
if (IsNameUnique == false)
{
return WriteErrorResponse(ServerRequest, "Url"sv, "Endpoint name is not unique"sv);
}
std::unique_ptr<zen::UpstreamEndpoint> Endpoint;
if (Type == "Zen"sv)
{
std::vector<std::string> Urls;
Urls.push_back(Json["Url"].string_value());
Endpoint = zen::MakeZenUpstreamEndpoint({.Name = Name, .Urls = Urls});
}
else
{
if (Namespace.empty())
{
return WriteErrorResponse(ServerRequest, "Namespace"sv, "Invalid Horde namespace"sv);
}
if (OAuthProvider.empty())
{
return WriteErrorResponse(ServerRequest, "OAuthProvider"sv, "Invalid Horde OAuth provider URL"sv);
}
if (OAuthToken.empty())
{
if (OAuthClientId.empty())
{
return WriteErrorResponse(ServerRequest, "OAuthClientId"sv, "Invalid Horde OAuth client ID"sv);
}
if (OAuthSecret.empty())
{
return WriteErrorResponse(ServerRequest, "OAuthSecret"sv, "Invalid Horde OAuth secret"sv);
}
}
const zen::CloudCacheClientOptions Options = {.Name = Name,
.ServiceUrl = Url,
.DdcNamespace = Namespace,
.BlobStoreNamespace = Namespace,
.OAuthProvider = OAuthProvider,
.OAuthClientId = OAuthClientId,
.OAuthSecret = OAuthSecret,
.AccessToken = OAuthToken};
Endpoint = zen::MakeJupiterUpstreamEndpoint(Options, m_AuthMgr);
}
m_Upstream.RegisterEndpoint(std::move(Endpoint));
WriteSuccessResponse(ServerRequest);
},
HttpVerb::kPost);
}
HttpUpstreamService::~HttpUpstreamService()
{
}
const char*
HttpUpstreamService::BaseUri() const
{
return "/upstream/";
}
void
HttpUpstreamService::HandleRequest(zen::HttpServerRequest& Request)
{
m_Router.HandleRequest(Request);
}
} // namespace zen
|