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