forked from libcpr/cpr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
81 lines (67 loc) · 2.08 KB
/
Copy pathutil.cpp
File metadata and controls
81 lines (67 loc) · 2.08 KB
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
#include "cpr/util.h"
#include <algorithm>
#include <cctype>
#include <cstdint>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
namespace cpr {
namespace util {
Header parseHeader(const std::string& headers) {
Header header;
std::vector<std::string> lines;
std::istringstream stream(headers);
{
std::string line;
while (std::getline(stream, line, '\n')) {
lines.push_back(line);
}
}
for (auto& line : lines) {
if (line.substr(0, 5) == "HTTP/") {
header.clear();
}
if (line.length() > 0) {
auto found = line.find(":");
if (found != std::string::npos) {
auto value = line.substr(found + 1);
value.erase(0, value.find_first_not_of("\t "));
value.resize(std::min(value.size(), value.find_last_not_of("\t\n\r ") + 1));
header[line.substr(0, found)] = value;
}
}
}
return header;
}
std::vector<std::string> split(const std::string& to_split, char delimiter) {
std::vector<std::string> tokens;
std::stringstream stream(to_split);
std::string item;
while (std::getline(stream, item, delimiter)) {
tokens.push_back(item);
}
return tokens;
}
size_t writeFunction(void* ptr, size_t size, size_t nmemb, std::string* data) {
data->append(static_cast<char*>(ptr), size * nmemb);
return size * nmemb;
}
std::string urlEncode(const std::string& value) {
std::ostringstream escaped;
escaped.fill('0');
escaped << std::hex;
for (auto i = value.cbegin(), n = value.cend(); i != n; ++i) {
std::string::value_type c = (*i);
// Keep alphanumeric and other accepted characters intact
if (std::isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
escaped << c;
continue;
}
// Any other characters are percent-encoded
escaped << '%' << std::setw(2) << std::int32_t(static_cast<unsigned char>(c));
}
return escaped.str();
}
} // namespace util
} // namespace cpr