aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/vfs/vfsservice.cpp
blob: d302a10ec4d33ef4694b8a5a49df41df0ab99dc3 (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
// Copyright Epic Games, Inc. All Rights Reserved.

#include "vfsservice.h"
#include "vfsimpl.h"

#include <zencore/compactbinarybuilder.h>

namespace zen {

using namespace std::literals;

#if ZEN_WITH_VFS

//////////////////////////////////////////////////////////////////////////

bool
GetContentAsCbObject(HttpServerRequest& HttpReq, CbObject& Cb)
{
	IoBuffer		Payload			   = HttpReq.ReadPayload();
	HttpContentType PayloadContentType = HttpReq.RequestContentType();

	switch (PayloadContentType)
	{
		case HttpContentType::kJSON:
		case HttpContentType::kUnknownContentType:
		case HttpContentType::kText:
			{
				std::string JsonText(reinterpret_cast<const char*>(Payload.GetData()), Payload.GetSize());
				Cb = LoadCompactBinaryFromJson(JsonText).AsObject();
				if (!Cb)
				{
					HttpReq.WriteResponse(HttpResponseCode::BadRequest,
										  HttpContentType::kText,
										  "Content format not supported, expected JSON format");
					return false;
				}
			}
			break;
		case HttpContentType::kCbObject:
			Cb = LoadCompactBinaryObject(Payload);
			if (!Cb)
			{
				HttpReq.WriteResponse(HttpResponseCode::BadRequest,
									  HttpContentType::kText,
									  "Content format not supported, expected compact binary format");
				return false;
			}
			break;
		default:
			HttpReq.WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, "Invalid request content type");
			return false;
	}

	return true;
}

//////////////////////////////////////////////////////////////////////////
//
// to test:
//
// echo {"method": "mount", "params": {"path": "d:\\VFS_ROOT"}} | curl.exe http://localhost:8558/vfs --data-binary @-
// echo {"method": "unmount"} | curl.exe http://localhost:8558/vfs --data-binary @-

VfsService::VfsService()
{
	m_Impl = new Impl;

	m_Router.RegisterRoute(
		"info",
		[&](HttpRouterRequest& Request) {
			CbObjectWriter Cbo;
			Cbo << "running" << m_Impl->IsVfsRunning();
			Cbo << "rootpath" << m_Impl->GetMountpointPath();

			Request.ServerRequest().WriteResponse(HttpResponseCode::OK, Cbo.Save());
		},
		HttpVerb::kGet | HttpVerb::kHead);

	m_Router.RegisterRoute(
		"",
		[&](HttpRouterRequest& Req) {
			CbObject Payload;

			if (!GetContentAsCbObject(Req.ServerRequest(), Payload))
				return;

			std::string_view RpcName = Payload["method"sv].AsString();

			if (RpcName == "mount"sv)
			{
				CbObjectView	 Params	   = Payload["params"sv].AsObjectView();
				std::string_view Mountpath = Params["path"sv].AsString();

				if (Mountpath.empty())
				{
					return Req.ServerRequest().WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, "no path specified");
				}

				if (m_Impl->IsVfsRunning())
				{
					return Req.ServerRequest().WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, "VFS already mounted");
				}

				try
				{
					m_Impl->Mount(Mountpath);
				}
				catch (const std::exception& Ex)
				{
					return Req.ServerRequest().WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, Ex.what());
				}

				Req.ServerRequest().WriteResponse(HttpResponseCode::OK);
			}
			else if (RpcName == "unmount"sv)
			{
				if (!m_Impl->IsVfsRunning())
				{
					return Req.ServerRequest().WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, "VFS not active");
				}

				try
				{
					m_Impl->Unmount();
				}
				catch (const std::exception& Ex)
				{
					return Req.ServerRequest().WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, Ex.what());
				}

				Req.ServerRequest().WriteResponse(HttpResponseCode::OK);
			}
			else
			{
				Req.ServerRequest().WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, "unknown RPC"sv);
			}
		},
		HttpVerb::kPost);
}

VfsService::~VfsService()
{
	delete m_Impl;
}

void
VfsService::Mount(std::string_view MountPoint)
{
	m_Impl->Mount(MountPoint);
}

void
VfsService::Unmount()
{
	m_Impl->Unmount();
}

void
VfsService::AddService(Ref<ProjectStore>&& Ps)
{
	m_Impl->AddService(std::move(Ps));
}

void
VfsService::AddService(Ref<ZenCacheStore>&& Z$)
{
	m_Impl->AddService(std::move(Z$));
}

#else

VfsService::VfsService()
{
}

VfsService::~VfsService()
{
}

void
VfsService::Mount(std::string_view MountPoint)
{
	ZEN_UNUSED(MountPoint);
}

void
VfsService::Unmount()
{
}

void
VfsService::AddService(Ref<ProjectStore>&& Ps)
{
	ZEN_UNUSED(Ps);
}

void
VfsService::AddService(Ref<ZenCacheStore>&& Z$)
{
	ZEN_UNUSED(Z$);
}

#endif

const char*
VfsService::BaseUri() const
{
	return "/vfs/";
}

void
VfsService::HandleRequest(HttpServerRequest& HttpServiceRequest)
{
	m_Router.HandleRequest(HttpServiceRequest);
}

}  // namespace zen