-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.hpp
91 lines (76 loc) · 2.36 KB
/
url.hpp
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
#ifndef URL_HPP
#define URL_HPP
#include <exception>
#include <map>
#include <string>
#include <vector>
class Url {
public:
Url( ) = default;
Url( const Url & ) = default;
Url( Url && ) = default;
Url( const std::string &str ) {
std::exception_ptr errPtr;
try {
parse( str.cbegin( ), str.cend( ) );
} catch( ... ) {
errPtr = std::current_exception( );
}
if( errPtr ) {
clear( );
std::rethrow_exception( errPtr );
}
}
Url &operator=( const Url & ) = default;
Url &operator=( Url && ) = default;
Url &operator=( const std::string &str ) {
std::exception_ptr errPtr;
clear( );
try {
parse( str.cbegin( ), str.cend( ) );
} catch( ... ) {
errPtr = std::current_exception( );
}
if( errPtr ) {
clear( );
std::rethrow_exception( errPtr );
}
return *this;
}
bool empty( ) const { return m_path.empty( ); }
bool operator!( ) const { return m_path.empty( ); }
operator bool( ) const { return !m_path.empty( ); }
const std::string raw( ) const { return m_raw; }
const std::string &schema( ) const { return m_schema; }
const std::string &auth( ) const { return m_auth; }
const std::string &host( ) const { return m_host; }
int port( ) const { return m_port; }
const std::vector< std::string > &path( ) const { return m_path; }
const std::map< std::string, std::string > &query( ) const { return m_query; }
const std::string §ion( ) const { return m_section; }
void clear( ) {
m_raw.clear( );
m_schema.clear( );
m_auth.clear( );
m_host.clear( );
m_port = -1;
m_path.clear( );
m_query.clear( );
m_section.clear( );
}
private:
using Iterator = std::string::const_iterator;
void parse( Iterator begin, Iterator end );
Iterator setSchema( Iterator begin, Iterator end, Iterator final );
Iterator setPort( Iterator begin, Iterator end );
Iterator appendPath( Iterator begin, Iterator end );
std::string m_raw;
int m_port = -1;
std::string m_schema;
std::string m_auth;
std::string m_host;
std::vector< std::string > m_path;
std::map< std::string, std::string > m_query;
std::string m_section;
};
#endif