8000 url-encode and -decode in place (#10301) · arangodb/arangodb@17acc94 · GitHub
[go: up one dir, main page]

Skip to content

Commit 17acc94

Browse files
authored
url-encode and -decode in place (#10301)
1 parent 2f62f62 commit 17acc94

File tree

3 files changed

+56
-58
lines changed

3 files changed

+56
-58
lines changed

3rdParty/fuerte/src/HttpConnection.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,23 +276,23 @@ std::string HttpConnection<ST>::buildRequestBody(Request const& req) {
276276
// construct request path ("/_db/<name>/" prefix)
277277
if (!req.header.database.empty()) {
278278
header.append("/_db/");
279-
header.append(http::urlEncode(req.header.database));
279+
http::urlEncode(header, req.header.database);
280280
}
281281
// must start with /, also turns /_db/abc into /_db/abc/
282282
if (req.header.path.empty() || req.header.path[0] != '/') {
283283
header.push_back('/');
284284
}
285285

286-
if (req.header.parameters.empty()) {
287-
header.append(req.header.path);
288-
} else {
289-
header.append(req.header.path);
286+
header.append(req.header.path);
287+
if (!req.header.parameters.empty()) {
290288
header.push_back('?');
291289
for (auto const& p : req.header.parameters) {
292290
if (header.back() != '?') {
293291
header.push_back('&');
294292
}
295-
header.append(http::urlEncode(p.first) + "=" + http::urlEncode(p.second));
293+
http::urlEncode(header, p.first);
294+
header.push_back('=');
295+
http::urlEncode(header, p.second);
296296
}
297297
}
298298
header.append(" HTTP/1.1\r\n")

3rdParty/fuerte/src/http.cpp

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -35,71 +35,49 @@ static inline int hex2int(char ch, int errorValue = 0) {
3535
return errorValue;
3636
}
3737

38-
std::string urlEncode(char const* src, size_t const len) {
38+
void urlEncode(std::string& out, char const* src, size_t len) {
3939
static char hexChars[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
4040
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
4141

42-
char const* end = src + len;
43-
4442
if (len >= (SIZE_MAX - 1) / 3) {
4543
throw std::overflow_error("out of memory");
4644
}
4745

48-
std::string result;
49-
result.reserve(3 * len);
46+
out.reserve(out.size() + 3 * len);
5047

48+
char const* end = src + len;
5149
for (; src < end; ++src) {
5250
if ('0' <= *src && *src <= '9') {
53-
result.push_back(*src);
54-
}
55-
56-
else if ('a' <= *src && *src <= 'z') {
57-
result.push_back(*src);
58-
}
59-
60-
else if ('A' <= *src && *src <= 'Z') {
61-
result.push_back(*src);
62-
}
63-
64-
else if (*src == '-' || *src == '_' || *src == '~') {
65-
result.push_back(*src);
66-
}
67-
68-
else {
51+
out.push_back(*src);
52+
} else if ('a' <= *src && *src <= 'z') {
53+
out.push_back(*src);
54+
} else if ('A' <= *src && *src <= 'Z') {
55+
out.push_back(*src);
56+
} else if (*src == '-' || *src == '_' || *src == '~') {
57+
out.push_back(*src);
58+
} else {
6959
uint8_t n = (uint8_t)(*src);
7060
uint8_t n1 = n >> 4;
7161
uint8_t n2 = n & 0x0F;
7262

73-
result.push_back('%');
74-
result.push_back(hexChars[n1]);
75-
result.push_back(hexChars[n2]);
63+
out.push_back('%');
64+
out.push_back(hexChars[n1]);
65+
out.push_back(hexChars[n2]);
7666
}
7767
}
78-
79-
return result;
8068
}
8169

82-
std::string urlEncode(char const* src) {
83-
if (src != nullptr) {
84-
size_t len = strlen(src);
85-
return urlEncode(src, len);
86-
}
87-
return "";
88-
}
89-
90-
std::string urlDecode(std::string const& str) {
91-
std::string result;
70+
void urlDecode(std::string& out, char const* src, size_t len) {
9271
// reserve enough room so we do not need to re-alloc
93-
result.reserve(str.size() + 16);
72+
out.reserve(out.size() + len + 16);
9473

95-
char const* src = str.c_str();
96-
char const* end = src + str.size();
74+
char const* end = src + len;
9775

9876
for (; src < end && *src != '%'; ++src) {
9977
if (*src == '+') {
100-
result.push_back(' ');
78+
out.push_back(' ');
10179
} else {
102-
result.push_back(*src);
80+
out.push_back(*src);
10381
}
10482
}
10583

@@ -112,10 +90,10 @@ std::string urlDecode(std::string const& str) {
11290
src += 1;
11391
} else {
11492
if (h2 == -1) {
115-
result.push_back(h1);
93+
out.push_back(h1);
11694
src += 2;
11795
} else {
118-
result.push_back(h1 << 4 | h2);
96+
out.push_back(h1 << 4 | h2);
11997
src += 3;
12098
}
12199
}
@@ -125,7 +103,7 @@ std::string urlDecode(std::string const& str) {
125103
if (h1 == -1) {
126104
src += 1;
127105
} else {
128-
result.push_back(h1);
106+
out.push_back(h1);
129107
src += 2;
130108
}
131109
} else {
@@ -134,13 +112,11 @@ std::string urlDecode(std::string const& str) {
134112

135113
for (; src < end && *src != '%'; ++src) {
136114
if (*src == '+') {
137-
result.push_back(' ');
115+
out.push_back(' ');
138116
} else {
139-
result.push_back(*src);
117+
out.push_back(*src);
140118
}
141119
}
142120
}
143-
144-
return result;
145121
}
146122
}}}} // namespace arangodb::fuerte::v1::http

3rdParty/fuerte/src/http.h

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,34 @@ struct RequestItem {
4646
}
4747
};
4848

49-
std::string urlDecode(std::string const& str);
50-
std::string urlEncode(char const* src, size_t const len);
51-
std::string urlEncode(char const* src);
49+
/// url-decodes [src, src+len) into out
50+
void urlDecode(std::string& out, char const* src, size_t len);
5251

52+
/// url-decodes str into out - convenience function
53+
inline void urlDecode(std::string& out, std::string const& str) {
54+
return urlDecode(out, str.data(), str.size());
55+
}
56+
57+
/// url-decodes str and returns it - convenience function
58+
inline std::string urlDecode(std::string const& str) {
59+
std::string result;
60+
urlDecode(result, str.c_str(), str.size());
61+
return result;
62+
}
63+
64+
/// url-encodes [src, src+len) into out
65+
void urlEncode(std::string& out, char const* src, size_t len);
66+
67+
/// url-encodes str into out - convenience function
68+
inline void urlEncode(std::string& out, std::string const& str) {
69+
return urlEncode(out, str.data(), str.size());
70+
}
71+
72+
/// url-encodes str and returns it - convenience function
5373
inline std::string urlEncode(std::string const& str) {
54-
return urlEncode(str.c_str(), str.size());
74+
std::string result;
75+
urlEncode(result, str.c_str(), str.size());
76+
return result;
5577
}
5678
}}}} // namespace arangodb::fuerte::v1::http
5779
#endif

0 commit comments

Comments
 (0)
0