Skip to content

Commit 5320a92

Browse files
authored
Sector index (#205)
* Added interface of SectorIndex Added StorageInfo and FsStat structures Signed-off-by: artyom-yurin <[email protected]> * Added GetStorageInfo and StorageAttach functions Signed-off-by: artyom-yurin <[email protected]> * Added BestAlloc method Added sealSpaceUse util function Added heartbeats Signed-off-by: artyom-yurin <[email protected]> * Added storageReportHealth Signed-off-by: artyom-yurin <[email protected]> * Added functions for working with sectors Signed-off-by: artyom-yurin <[email protected]> * Added uri handler Added validation of urls Added url formatting Signed-off-by: artyom-yurin <[email protected]>
1 parent 4584e80 commit 5320a92

21 files changed

+1629
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ cmake-build*
3333
deps/our-filecoin-ffi/include
3434
deps/our-filecoin-ffi/lib
3535
deps/our-filecoin-ffi/filecoin_ffi_commit_installed
36+
core/markets/storage/example/resources.hpp
3637

3738
cmake-build*
3839
.gtm

core/common/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ target_link_libraries(logger
4343
)
4444

4545
add_subdirectory(libp2p)
46+
add_subdirectory(uri_parser)
4647

4748
add_library(todo_error
4849
todo_error.hpp

core/common/uri_parser/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
add_library(uri_parser
5+
uri_parser.cpp
6+
percent_encoding.cpp
7+
)
+255
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
// Copyright © 2017-2019 Dmitriy Khaustov
7+
//
8+
// Licensed under the Apache License, Version 2.0 (the "License");
9+
// you may not use this file except in compliance with the License.
10+
// You may obtain a copy of the License at
11+
//
12+
// http://www.apache.org/licenses/LICENSE-2.0
13+
//
14+
// Unless required by applicable law or agreed to in writing, software
15+
// distributed under the License is distributed on an "AS IS" BASIS,
16+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
// See the License for the specific language governing permissions and
18+
// limitations under the License.
19+
//
20+
// Author: Dmitriy Khaustov aka xDimon
21+
// Contacts: [email protected]
22+
// File created on: 2017.05.30
23+
24+
// PercentEncoding.cpp
25+
26+
#include "common/uri_parser/percent_encoding.hpp"
27+
28+
#include <iomanip>
29+
#include <sstream>
30+
#include <stdexcept>
31+
32+
// unreserved
33+
static const std::string unreserved(
34+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
35+
"abcdefghijklmnopqrstuvwxyz"
36+
"0123456789"
37+
"-_.~");
38+
39+
// reserved
40+
static const std::string reserved("!*'();:@&=+$,/?#[]");
41+
42+
inline static bool need_encode(const char c) {
43+
if (unreserved.find(c) != std::string::npos) {
44+
return false;
45+
}
46+
if (reserved.find(c) != std::string::npos) {
47+
return true;
48+
}
49+
if (c == '%') {
50+
return true;
51+
}
52+
return true;
53+
}
54+
55+
static int decodePercentEscapedByte(std::istringstream &iss) {
56+
auto c = iss.get();
57+
if (c != '%') {
58+
throw std::runtime_error("Wrong token for start percent-encoded sequence");
59+
}
60+
61+
int result = 0;
62+
63+
if (iss.eof()) {
64+
throw std::runtime_error(
65+
"Unxpected end of data during try parse percent-encoded symbol");
66+
}
67+
c = iss.get();
68+
if (c >= '0' && c <= '9') {
69+
result = c - '0';
70+
} else if (c >= 'a' && c <= 'f') {
71+
result = 10 + c - 'a';
72+
} else if (c >= 'A' && c <= 'F') {
73+
result = 10 + c - 'A';
74+
} else if (c == -1) {
75+
throw std::runtime_error(
76+
"Unxpected end of data during try parse percent-encoded symbol");
77+
} else {
78+
throw std::runtime_error("Wrong percent-encoded symbol");
79+
}
80+
81+
if (iss.eof()) {
82+
throw std::runtime_error(
83+
"Unxpected end of data during try parse percent-encoded symbol");
84+
}
85+
c = iss.get();
86+
if (c >= '0' && c <= '9') {
87+
result = (result << 4) | (c - '0');
88+
} else if (c >= 'a' && c <= 'f') {
89+
result = (result << 4) | (10 + c - 'a');
90+
} else if (c >= 'A' && c <= 'F') {
91+
result = (result << 4) | (10 + c - 'A');
92+
} else if (c == -1) {
93+
throw std::runtime_error(
94+
"Unxpected end of data during try parse percent-encoded symbol");
95+
} else {
96+
throw std::runtime_error("Wrong percent-encoded symbol");
97+
}
98+
99+
return result;
100+
}
101+
102+
static uint32_t decodePercentEscaped(std::istringstream &iss, bool &isUtf8) {
103+
auto c = decodePercentEscapedByte(iss);
104+
105+
if (!isUtf8) {
106+
return static_cast<uint32_t>(c);
107+
}
108+
109+
int bytes;
110+
uint32_t symbol = 0;
111+
if ((c & 0b11111100) == 0b11111100) {
112+
bytes = 6;
113+
symbol = static_cast<uint8_t>(c) & static_cast<uint8_t>(0b1);
114+
} else if ((c & 0b11111000) == 0b11111000) {
115+
bytes = 5;
116+
symbol = static_cast<uint8_t>(c) & static_cast<uint8_t>(0b11);
117+
} else if ((c & 0b11110000) == 0b11110000) {
118+
bytes = 4;
119+
symbol = static_cast<uint8_t>(c) & static_cast<uint8_t>(0b111);
120+
} else if ((c & 0b11100000) == 0b11100000) {
121+
bytes = 3;
122+
symbol = static_cast<uint8_t>(c) & static_cast<uint8_t>(0b1111);
123+
} else if ((c & 0b11000000) == 0b11000000) {
124+
bytes = 2;
125+
symbol = static_cast<uint8_t>(c) & static_cast<uint8_t>(0b11111);
126+
} else if ((c & 0b10000000) == 0b00000000) {
127+
return static_cast<uint8_t>(c) & static_cast<uint8_t>(0b1111111);
128+
} else {
129+
isUtf8 = false;
130+
return static_cast<uint32_t>(c);
131+
}
132+
133+
auto fc = c;
134+
auto p = iss.tellg();
135+
136+
while (--bytes > 0) {
137+
if (iss.eof()) {
138+
isUtf8 = false;
139+
return static_cast<uint32_t>(fc);
140+
}
141+
try {
142+
c = decodePercentEscapedByte(iss);
143+
} catch (...) {
144+
iss.clear(iss.goodbit);
145+
iss.seekg(p);
146+
return static_cast<uint32_t>(fc);
147+
}
148+
if ((c & 0b11000000) != 0b10000000) {
149+
iss.clear(iss.goodbit);
150+
iss.seekg(p);
151+
return static_cast<uint32_t>(fc);
152+
}
153+
symbol = (symbol << 6) | (c & 0b0011'1111);
154+
}
155+
156+
return symbol;
157+
}
158+
159+
std::string PercentEncoding::decode(const std::string &input) {
160+
std::istringstream iss(input);
161+
std::ostringstream oss;
162+
bool isUtf8 = true;
163+
164+
while (!iss.eof()) {
165+
int c = iss.peek();
166+
if (c == '%') {
167+
auto symbol = decodePercentEscaped(iss, isUtf8);
168+
169+
if (symbol <= 0b0111'1111) // 7bit -> 1byte
170+
{
171+
oss.put(static_cast<uint8_t>(symbol));
172+
} else if (symbol <= 0b0111'1111'1111) // 11bit -> 2byte
173+
{
174+
oss.put(
175+
static_cast<uint8_t>(0b1100'0000 | (0b0001'1111 & (symbol >> 6))));
176+
oss.put(
177+
static_cast<uint8_t>(0b1000'0000 | (0b0011'1111 & (symbol >> 0))));
178+
} else if (symbol <= 0b1111'1111'1111'1111) // 16bit -> 3byte
179+
{
180+
oss.put(
181+
static_cast<uint8_t>(0b1110'0000 | (0b0000'1111 & (symbol >> 12))));
182+
oss.put(
183+
static_cast<uint8_t>(0b1000'0000 | (0b0011'1111 & (symbol >> 6))));
184+
oss.put(
185+
static_cast<uint8_t>(0b1000'0000 | (0b0011'1111 & (symbol >> 0))));
186+
} else if (symbol <= 0b0001'1111'1111'1111'1111'1111) // 21bit -> 4byte
187+
{
188+
oss.put(
189+
static_cast<uint8_t>(0b1111'0000 | (0b0000'0111 & (symbol >> 18))));
190+
oss.put(
191+
static_cast<uint8_t>(0b1000'0000 | (0b0011'1111 & (symbol >> 12))));
192+
oss.put(
193+
static_cast<uint8_t>(0b1000'0000 | (0b0011'1111 & (symbol >> 6))));
194+
oss.put(
195+
static_cast<uint8_t>(0b1000'0000 | (0b0011'1111 & (symbol >> 0))));
196+
} else if (symbol
197+
<= 0b0011'1111'1111'1111'1111'1111'1111) // 26bit -> 5byte
198+
{
199+
oss.put(
200+
static_cast<uint8_t>(0b1111'1000 | (0b0000'0011 & (symbol >> 24))));
201+
oss.put(
202+
static_cast<uint8_t>(0b1000'0000 | (0b0011'1111 & (symbol >> 18))));
203+
oss.put(
204+
static_cast<uint8_t>(0b1000'0000 | (0b0011'1111 & (symbol >> 12))));
205+
oss.put(
206+
static_cast<uint8_t>(0b1000'0000 | (0b0011'1111 & (symbol >> 6))));
207+
oss.put(
208+
static_cast<uint8_t>(0b1000'0000 | (0b0011'1111 & (symbol >> 0))));
209+
} else if (symbol
210+
<= 0b0111'1111'1111'1111'1111'1111'1111'1111) // 31bit ->
211+
// 6byte
212+
{
213+
oss.put(
214+
static_cast<uint8_t>(0b1111'1100 | (0b0000'0001 & (symbol >> 30))));
215+
oss.put(
216+
static_cast<uint8_t>(0b1000'0000 | (0b0011'1111 & (symbol >> 24))));
217+
oss.put(
218+
static_cast<uint8_t>(0b1000'0000 | (0b0011'1111 & (symbol >> 18))));
219+
oss.put(
220+
static_cast<uint8_t>(0b1000'0000 | (0b0011'1111 & (symbol >> 12))));
221+
oss.put(
222+
static_cast<uint8_t>(0b1000'0000 | (0b0011'1111 & (symbol >> 6))));
223+
oss.put(
224+
static_cast<uint8_t>(0b1000'0000 | (0b0011'1111 & (symbol >> 0))));
225+
}
226+
} else if (c == -1) {
227+
break;
228+
} else {
229+
iss.ignore();
230+
oss.put(static_cast<uint8_t>(c));
231+
}
232+
}
233+
234+
return oss.str();
235+
}
236+
237+
std::string PercentEncoding::encode(const std::string &input) {
238+
std::istringstream iss(input);
239+
std::ostringstream oss;
240+
241+
while (!iss.eof()) {
242+
auto c = iss.get();
243+
if (c == -1) {
244+
break;
245+
}
246+
if (need_encode(static_cast<uint8_t>(c))) {
247+
oss << '%' << std::uppercase << std::setfill('0') << std::setw(2)
248+
<< std::hex << c;
249+
} else {
250+
oss.put(static_cast<uint8_t>(c));
251+
}
252+
}
253+
254+
return oss.str();
255+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
// Copyright © 2017-2019 Dmitriy Khaustov
7+
//
8+
// Licensed under the Apache License, Version 2.0 (the "License");
9+
// you may not use this file except in compliance with the License.
10+
// You may obtain a copy of the License at
11+
//
12+
// http://www.apache.org/licenses/LICENSE-2.0
13+
//
14+
// Unless required by applicable law or agreed to in writing, software
15+
// distributed under the License is distributed on an "AS IS" BASIS,
16+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
// See the License for the specific language governing permissions and
18+
// limitations under the License.
19+
//
20+
// Author: Dmitriy Khaustov aka xDimon
21+
// Contacts: [email protected]
22+
// File created on: 2017.05.30
23+
24+
// PercentEncoding.hpp
25+
26+
#pragma once
27+
28+
#include <string>
29+
30+
class PercentEncoding final {
31+
public:
32+
static std::string encode(const std::string &input);
33+
34+
static std::string decode(std::string const &input);
35+
};

0 commit comments

Comments
 (0)