Debian Codegraph @

contrib/epee/src/abstract_http_client.cpp

raw symbols SCIP
1 #include <math.h>
2 #include "net/abstract_http_client.h"
3 #include "net/http_base.h"
4 #include "net/net_parse_helpers.h"
5 #include "misc_log_ex.h"
6
7 #undef MONERO_DEFAULT_LOG_CATEGORY
8 #define MONERO_DEFAULT_LOG_CATEGORY "net.http"
9
10 namespace epee
11 {
12 namespace net_utils
13 {
14 //----------------------------------------------------------------------------------------------------
15 bool is_unsafe(unsigned char compare_char)
16 {
17 if(compare_char <= 32 || compare_char >= 123)
18 return true;
19
20 const char* punsave = get_unsave_chars();
21
22 for(int ichar_pos = 0; 0!=punsave[ichar_pos] ;ichar_pos++)
23 if(compare_char == punsave[ichar_pos])
24 return true;
25
26 return false;
27 }
28 //----------------------------------------------------------------------------------------------------
29 std::string dec_to_hex(char num, int radix)
30 {
31 int temp=0;
32 std::string csTmp;
33 int num_char;
34
35 num_char = (int) num;
36 if (num_char < 0)
37 num_char = 256 + num_char;
38
39 while (num_char >= radix)
40 {
41 temp = num_char % radix;
42 num_char = (int)floor((float)num_char / (float)radix);
43 csTmp = get_hex_vals()[temp];
44 }
45
46 csTmp += get_hex_vals()[num_char];
47
48 if(csTmp.size() < 2)
49 {
50 csTmp += '0';
51 }
52
53 std::reverse(csTmp.begin(), csTmp.end());
54 //_mbsrev((unsigned char*)csTmp.data());
55
56 return csTmp;
57 }
58 //----------------------------------------------------------------------------------------------------
59 int get_index(const char *s, char c)
60 {
61 const char *ptr = (const char*)memchr(s, c, 16);
62 return ptr ? ptr-s : -1;
63 }
64 //----------------------------------------------------------------------------------------------------
65 std::string hex_to_dec_2bytes(const char *s)
66 {
67 const char *hex = get_hex_vals();
68 int i0 = get_index(hex, toupper(s[0]));
69 int i1 = get_index(hex, toupper(s[1]));
70 if (i0 < 0 || i1 < 0)
71 return std::string("%") + std::string(1, s[0]) + std::string(1, s[1]);
72 return std::string(1, i0 * 16 | i1);
73 }
74 //----------------------------------------------------------------------------------------------------
75 std::string convert(char val)
76 {
77 std::string csRet;
78 csRet += "%";
79 csRet += dec_to_hex(val, 16);
80 return csRet;
81 }
82 //----------------------------------------------------------------------------------------------------
83 std::string conver_to_url_format(const std::string& uri)
84 {
85
86 std::string result;
87
88 for(size_t i = 0; i!= uri.size(); i++)
89 {
90 if(is_unsafe(uri[i]))
91 result += convert(uri[i]);
92 else
93 result += uri[i];
94
95 }
96
97 return result;
98 }
99 //----------------------------------------------------------------------------------------------------
100 std::string convert_from_url_format(const std::string& uri)
101 {
102
103 std::string result;
104
105 for(size_t i = 0; i!= uri.size(); i++)
106 {
107 if(uri[i] == '%' && i + 2 < uri.size())
108 {
109 result += hex_to_dec_2bytes(uri.c_str() + i + 1);
110 i += 2;
111 }
112 else
113 result += uri[i];
114
115 }
116
117 return result;
118 }
119 //----------------------------------------------------------------------------------------------------
120 std::string convert_to_url_format_force_all(const std::string& uri)
121 {
122 std::string result;
123
124 for(size_t i = 0; i!= uri.size(); i++)
125 {
126 result += convert(uri[i]);
127 }
128 return result;
129 }
130
131 namespace http
132 {
133 //----------------------------------------------------------------------------------------------------
134 bool epee::net_utils::http::abstract_http_client::set_server(const std::string& address, boost::optional<login> user, ssl_options_t ssl_options)
135 {
136 http::url_content parsed{};
137 const bool r = parse_url(address, parsed);
138 CHECK_AND_ASSERT_MES(r, false, "failed to parse url: " << address);
139 if (parsed.port == 0)
140 {
141 if (parsed.schema == "http")
142 parsed.port = 80;
143 else if (parsed.schema == "https")
144 parsed.port = 443;
145 }
146 set_server(std::move(parsed.host), std::to_string(parsed.port), std::move(user), std::move(ssl_options));
147 return true;
148 }
149
150 bool epee::net_utils::http::abstract_http_client::set_proxy(const std::string& address)
151 {
152 return false;
153 }
154 }
155 }
156 }