blob: 5de347a17b23f1ab0d2d25ff73d154afe6226217 (
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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "cidstore.h"
#include <zencore/compress.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <zenstore/cidstore.h>
#include <gsl/gsl-lite.hpp>
namespace zen {
HttpCidService::HttpCidService(CidStore& Store) : m_CidStore(Store)
{
m_Router.AddPattern("cid", "([0-9A-Fa-f]{40})");
m_Router.RegisterRoute(
"{cid}",
[this](HttpRouterRequest& Req) {
IoHash Hash = IoHash::FromHexString(Req.GetCapture(1));
ZEN_DEBUG("CID request for {}", Hash);
HttpServerRequest& ServerRequest = Req.ServerRequest();
switch (ServerRequest.RequestVerb())
{
case HttpVerb::kGet:
case HttpVerb::kHead:
{
if (IoBuffer Value = m_CidStore.FindChunkByCid(Hash))
{
return ServerRequest.WriteResponse(HttpResponseCode::OK, HttpContentType::kBinary, Value);
}
return ServerRequest.WriteResponse(HttpResponseCode::NotFound);
}
break;
case HttpVerb::kPut:
{
IoBuffer Payload = ServerRequest.ReadPayload();
CompressedBuffer Compressed = CompressedBuffer::FromCompressed(SharedBuffer(Payload));
if (!Compressed)
{
return ServerRequest.WriteResponse(HttpResponseCode::UnsupportedMediaType);
}
IoHash PayloadHash = IoHash::FromBLAKE3(Compressed.GetRawHash());
// URI hash must match content hash
if (PayloadHash != Hash)
{
return ServerRequest.WriteResponse(HttpResponseCode::BadRequest);
}
m_CidStore.AddChunk(Compressed);
return ServerRequest.WriteResponse(HttpResponseCode::OK);
}
break;
default:
break;
}
},
HttpVerb::kGet | HttpVerb::kPut | HttpVerb::kHead);
}
const char*
HttpCidService::BaseUri() const
{
return "/cid/";
}
void
HttpCidService::HandleRequest(zen::HttpServerRequest& Request)
{
if (Request.RelativeUri().empty())
{
// Root URI request
switch (Request.RequestVerb())
{
case HttpVerb::kPut:
case HttpVerb::kPost:
{
IoBuffer Payload = Request.ReadPayload();
CompressedBuffer Compressed = CompressedBuffer::FromCompressed(SharedBuffer(Payload));
if (!Compressed)
{
return Request.WriteResponse(HttpResponseCode::UnsupportedMediaType);
}
IoHash PayloadHash = IoHash::FromBLAKE3(Compressed.GetRawHash());
ZEN_DEBUG("CID POST request for {} ({} bytes)", PayloadHash, Payload.Size());
auto InsertResult = m_CidStore.AddChunk(Compressed);
if (InsertResult.New)
{
return Request.WriteResponse(HttpResponseCode::Created);
}
else
{
return Request.WriteResponse(HttpResponseCode::OK);
}
}
break;
case HttpVerb::kGet:
case HttpVerb::kHead:
break;
default:
break;
}
}
else
{
m_Router.HandleRequest(Request);
}
}
} // namespace zen
|