util.cpp
Go to the documentation of this file.
1 #include "cpr/util.h"
2 
3 #include <algorithm>
4 #include <cctype>
5 #include <cstdint>
6 #include <iomanip>
7 #include <sstream>
8 #include <string>
9 #include <vector>
10 
11 namespace cpr {
12 namespace util {
13 
14 Header parseHeader(const std::string& headers) {
15  Header header;
16  std::vector<std::string> lines;
17  std::istringstream stream(headers);
18  {
19  std::string line;
20  while (std::getline(stream, line, '\n')) {
21  lines.push_back(line);
22  }
23  }
24 
25  for (auto& line : lines) {
26  if (line.substr(0, 5) == "HTTP/") {
27  header.clear();
28  }
29 
30  if (line.length() > 0) {
31  auto found = line.find(":");
32  if (found != std::string::npos) {
33  auto value = line.substr(found + 1);
34  value.erase(0, value.find_first_not_of("\t "));
35  value.resize(std::min(value.size(), value.find_last_not_of("\t\n\r ") + 1));
36  header[line.substr(0, found)] = value;
37  }
38  }
39  }
40 
41  return header;
42 }
43 
44 std::vector<std::string> split(const std::string& to_split, char delimiter) {
45  std::vector<std::string> tokens;
46 
47  std::stringstream stream(to_split);
48  std::string item;
49  while (std::getline(stream, item, delimiter)) {
50  tokens.push_back(item);
51  }
52 
53  return tokens;
54 }
55 
56 size_t writeFunction(void* ptr, size_t size, size_t nmemb, std::string* data) {
57  data->append(static_cast<char*>(ptr), size * nmemb);
58  return size * nmemb;
59 }
60 
62  std::ostringstream escaped;
63  escaped.fill('0');
64  escaped << std::hex;
65 
66  for (auto i = value.cbegin(), n = value.cend(); i != n; ++i) {
67  std::string::value_type c = (*i);
68  // Keep alphanumeric and other accepted characters intact
69  if (std::isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
70  escaped << c;
71  continue;
72  }
73  // Any other characters are percent-encoded
74  escaped << '%' << std::setw(2) << std::int32_t(static_cast<unsigned char>(c));
75  }
76 
77  return escaped.str();
78 }
79 
80 } // namespace util
81 } // namespace cpr
std::vector< std::string > split(const std::string &to_split, char delimiter)
Definition: util.cpp:44
::std::string string
Definition: gtest-port.h:1129
UNITTEST_START char * ptr
Definition: unit1330.c:38
std::string urlEncode(const std::string &value)
Definition: util.cpp:61
unsigned int i
Definition: unit1303.c:79
Definition: auth.cpp:3
UNITTEST_START int * value
Definition: unit1602.c:51
size_t size
Definition: unit1302.c:52
size_t writeFunction(void *ptr, size_t size, size_t nmemb, std::string *data)
Definition: util.cpp:56
Header parseHeader(const std::string &headers)
Definition: util.cpp:14
Definition: debug.c:29


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:17