aboutsummaryrefslogtreecommitdiff
path: root/contrib/epee/src/abstract_http_client.cpp
blob: 98b5b67d97f9b5d92deb441d154c617c88314923 (plain) (blame)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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<login> 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;
  }
}
}
}