From 87d75584e8df95ff53a354438760088e061a6de5 Mon Sep 17 00:00:00 2001 From: woodser Date: Wed, 15 Apr 2020 13:22:46 -0400 Subject: Allow wallet2.h to run in WebAssembly - Add abstract_http_client.h which http_client.h extends. - Replace simple_http_client with abstract_http_client in wallet2, message_store, message_transporter, and node_rpc_proxy. - Import and export wallet data in wallet2. - Use #if defined __EMSCRIPTEN__ directives to skip incompatible code. --- contrib/epee/src/abstract_http_client.cpp | 142 ++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 contrib/epee/src/abstract_http_client.cpp (limited to 'contrib/epee/src/abstract_http_client.cpp') diff --git a/contrib/epee/src/abstract_http_client.cpp b/contrib/epee/src/abstract_http_client.cpp new file mode 100644 index 000000000..98b5b67d9 --- /dev/null +++ b/contrib/epee/src/abstract_http_client.cpp @@ -0,0 +1,142 @@ +#include "net/abstract_http_client.h" +#include "net/http_base.h" +#include "net/net_parse_helpers.h" + +#undef MONERO_DEFAULT_LOG_CATEGORY +#define MONERO_DEFAULT_LOG_CATEGORY "net.http" + +namespace epee +{ +namespace net_utils +{ + //---------------------------------------------------------------------------------------------------- + bool is_unsafe(unsigned char compare_char) + { + if(compare_char <= 32 || compare_char >= 123) + return true; + + const char* punsave = get_unsave_chars(); + + for(int ichar_pos = 0; 0!=punsave[ichar_pos] ;ichar_pos++) + if(compare_char == punsave[ichar_pos]) + return true; + + return false; + } + //---------------------------------------------------------------------------------------------------- + std::string dec_to_hex(char num, int radix) + { + int temp=0; + std::string csTmp; + int num_char; + + num_char = (int) num; + if (num_char < 0) + num_char = 256 + num_char; + + while (num_char >= radix) + { + temp = num_char % radix; + num_char = (int)floor((float)num_char / (float)radix); + csTmp = get_hex_vals()[temp]; + } + + csTmp += get_hex_vals()[num_char]; + + if(csTmp.size() < 2) + { + csTmp += '0'; + } + + std::reverse(csTmp.begin(), csTmp.end()); + //_mbsrev((unsigned char*)csTmp.data()); + + return csTmp; + } + //---------------------------------------------------------------------------------------------------- + int get_index(const char *s, char c) + { + const char *ptr = (const char*)memchr(s, c, 16); + return ptr ? ptr-s : -1; + } + //---------------------------------------------------------------------------------------------------- + std::string hex_to_dec_2bytes(const char *s) + { + const char *hex = get_hex_vals(); + int i0 = get_index(hex, toupper(s[0])); + int i1 = get_index(hex, toupper(s[1])); + if (i0 < 0 || i1 < 0) + return std::string("%") + std::string(1, s[0]) + std::string(1, s[1]); + return std::string(1, i0 * 16 | i1); + } + //---------------------------------------------------------------------------------------------------- + std::string convert(char val) + { + std::string csRet; + csRet += "%"; + csRet += dec_to_hex(val, 16); + return csRet; + } + //---------------------------------------------------------------------------------------------------- + std::string conver_to_url_format(const std::string& uri) + { + + std::string result; + + for(size_t i = 0; i!= uri.size(); i++) + { + if(is_unsafe(uri[i])) + result += convert(uri[i]); + else + result += uri[i]; + + } + + return result; + } + //---------------------------------------------------------------------------------------------------- + std::string convert_from_url_format(const std::string& uri) + { + + std::string result; + + for(size_t i = 0; i!= uri.size(); i++) + { + if(uri[i] == '%' && i + 2 < uri.size()) + { + result += hex_to_dec_2bytes(uri.c_str() + i + 1); + i += 2; + } + else + result += uri[i]; + + } + + return result; + } + //---------------------------------------------------------------------------------------------------- + std::string convert_to_url_format_force_all(const std::string& uri) + { + std::string result; + + for(size_t i = 0; i!= uri.size(); i++) + { + result += convert(uri[i]); + } + return result; + } + +namespace http +{ + //---------------------------------------------------------------------------------------------------- + bool epee::net_utils::http::abstract_http_client::set_server(const std::string& address, boost::optional user, ssl_options_t ssl_options) + { + http::url_content parsed{}; + const bool r = parse_url(address, parsed); + CHECK_AND_ASSERT_MES(r, false, "failed to parse url: " << address); + set_server(std::move(parsed.host), std::to_string(parsed.port), std::move(user), std::move(ssl_options)); + return true; + } +} +} +} -- cgit v1.2.3