Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
url.cpp
Go to the documentation of this file.
1#include <fc/network/url.hpp>
2#include <fc/string.hpp>
3#include <fc/io/sstream.hpp>
5#include <fc/log/logger.hpp>
6#include <sstream>
7
8namespace fc
9{
10 namespace detail
11 {
13 {
14 public:
15 void parse( const fc::string& s )
16 {
17 std::stringstream ss(s);
18 std::string skip,_lpath,_largs,luser,lpass;
19 std::getline( ss, _proto, ':' );
20 std::getline( ss, skip, '/' );
21 std::getline( ss, skip, '/' );
22
23 if( s.find('@') != size_t(fc::string::npos) ) {
24 fc::string user_pass;
25 std::getline( ss, user_pass, '@' );
26 std::stringstream upss(user_pass);
27 if( user_pass.find( ':' ) != size_t(fc::string::npos) ) {
28 std::getline( upss, luser, ':' );
29 std::getline( upss, lpass, ':' );
30 _user = fc::move(luser);
31 _pass = fc::move(lpass);
32 } else {
33 _user = fc::move(user_pass);
34 }
35 }
36 fc::string host_port;
37 std::getline( ss, host_port, '/' );
38 auto pos = host_port.find( ':' );
39 if( pos != fc::string::npos ) {
40 try {
41 _port = static_cast<uint16_t>(to_uint64( host_port.substr( pos+1 ) ));
42 } catch ( ... ) {
43 FC_THROW_EXCEPTION( parse_error_exception, "Unable to parse port field in url",( "url", s ) );
44 }
45 _host = host_port.substr(0,pos);
46 } else {
47 _host = fc::move(host_port);
48 }
49 std::getline( ss, _lpath, '?' );
50#ifdef WIN32
51 // On windows, a URL like file:///c:/autoexec.bat would result in _lpath = c:/autoexec.bat
52 // which is what we really want (it's already an absolute path)
53 if (!stricmp(_proto.c_str(), "file"))
54 _path = _lpath;
55 else
56 _path = fc::path( "/" ) / _lpath; // let other schemes behave like unix
57#else
58 // On unix, a URL like file:///etc/rc.local would result in _lpath = etc/rc.local
59 // but we really want to make it the absolute path /etc/rc.local
60 _path = fc::path( "/" ) / _lpath;
61#endif
62 std::getline( ss, _largs );
63 if( _args && _args->size() )
64 {
65 // TODO: args = fc::move(_args);
66 _query = fc::move(_largs);
67 }
68 }
69
70 string _proto;
77 std::optional<uint16_t> _port;
78 };
79 }
80
81 void to_variant( const url& u, fc::variant& v )
82 {
83 v = fc::string(u);
84 }
85 void from_variant( const fc::variant& v, url& u )
86 {
87 u = url( v.as_string() );
88 }
89
90 url::operator string()const
91 {
92 std::stringstream ss;
93 ss<<my->_proto<<"://";
94 if( my->_user ) {
95 ss << *my->_user;
96 if( my->_pass ) {
97 ss<<":"<<*my->_pass;
98 }
99 ss<<"@";
100 }
101 if( my->_host ) ss<<*my->_host;
102 if( my->_port ) ss<<":"<<*my->_port;
103 if( my->_path ) ss<<my->_path->generic_string();
104 if( my->_query ) ss<<"?"<<*my->_query;
105 // if( my->_args ) ss<<"?"<<*my->_args;
106 return ss.str();
107 }
108
110 :my( std::make_shared<detail::url_impl>() )
111 {
112 my->parse(u);
113 }
114
115 std::shared_ptr<detail::url_impl> get_null_url()
116 {
117 static auto u = std::make_shared<detail::url_impl>();
118 return u;
119 }
120
122 :my( get_null_url() )
123 { }
124
125 url::url( const url& u )
126 :my(u.my){}
127
129 :my( fc::move(u.my) )
130 {
131 u.my = get_null_url();
132 }
133
134 url::url( const string& proto, const ostring& host, const ostring& user, const ostring& pass,
135 const opath& path, const ostring& query, const ovariant_object& args, const std::optional<uint16_t>& port)
136 :my( std::make_shared<detail::url_impl>() )
137 {
138 my->_proto = proto;
139 my->_host = host;
140 my->_user = user;
141 my->_pass = pass;
142 my->_path = path;
143 my->_query = query;
144 my->_args = args;
145 my->_port = port;
146 }
147
149
151 {
152 my = u.my;
153 return *this;
154 }
155
157 {
158 if( this != &u )
159 {
160 my = fc::move(u.my);
161 u.my= get_null_url();
162 }
163 return *this;
164 }
165
166 string url::proto()const
167 {
168 return my->_proto;
169 }
171 {
172 return my->_host;
173 }
175 {
176 return my->_user;
177 }
179 {
180 return my->_pass;
181 }
183 {
184 return my->_path;
185 }
187 {
188 return my->_query;
189 }
191 {
192 return my->_args;
193 }
194 std::optional<uint16_t> url::port()const
195 {
196 return my->_port;
197 }
198
199
200
201}
202
void parse(const fc::string &s)
Definition url.cpp:15
std::optional< uint16_t > _port
Definition url.cpp:77
ovariant_object _args
Definition url.cpp:76
wraps boost::filesystem::path to provide platform independent path manipulation.
opath path() const
Definition url.cpp:182
ostring host() const
Definition url.cpp:170
~url()
Definition url.cpp:148
string proto() const
Definition url.cpp:166
ovariant_object args() const
Definition url.cpp:190
url & operator=(const url &c)
Definition url.cpp:150
ostring query() const
Definition url.cpp:186
ostring user() const
Definition url.cpp:174
std::optional< uint16_t > port() const
Definition url.cpp:194
url()
Definition url.cpp:121
ostring pass() const
Definition url.cpp:178
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
string as_string() const
Definition variant.cpp:469
Defines exception's used by fc.
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
namespace sysio::chain
Definition authority.cpp:3
std::string string
Definition string.hpp:10
std::shared_ptr< detail::url_impl > get_null_url()
Definition url.cpp:115
std::optional< fc::string > ostring
Definition url.hpp:10
std::optional< fc::path > opath
Definition url.hpp:11
std::optional< fc::variant_object > ovariant_object
Definition url.hpp:12
uint64_t to_uint64(const fc::string &)
Definition string.cpp:105
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
Definition authority.cpp:4
Definition name.hpp:106
string url
Definition main.cpp:166
unsigned short uint16_t
Definition stdint.h:125
char * s