1 /*
2  * Kiss - A refined core library for D programming language.
3  *
4  * Copyright (C) 2015-2018  Shanghai Putao Technology Co., Ltd
5  *
6  * Developer: HuntLabs.cn
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module kiss.util.uri;
13 
14 import std.regex;
15 import std.conv : to;
16 
17 /**
18 */
19 class Uri
20 {
21     private
22     {
23         string _uriString;
24         string _scheme;
25         string _host;
26         ushort _port;
27         string _path;
28         string _username;
29         string _password;
30         string _query;
31         string _fragment;
32 
33         bool _valid = false;
34     }
35 
36     this(string uri)
37     {
38         this._uriString = uri;
39     }
40 
41     bool parse()
42     {
43         if (this._uriString)
44         {
45             return false;
46         }
47 
48         auto uriReg = regex(r"/^([a-z0-9+.-]+):(?://(?:((?:[a-z0-9-._~!$&'()*+,;=:]|%[0-9A-F]{2})*)@)?((?:[a-z0-9-._~!$&'()*+,;=]|%[0-9A-F]{2})*)(?::(\d*))?(/(?:[a-z0-9-._~!$&'()*+,;=:@/]|%[0-9A-F]{2})*)?|(/?(?:[a-z0-9-._~!$&'()*+,;=:@]|%[0-9A-F]{2})+(?:[a-z0-9-._~!$&'()*+,;=:@/]|%[0-9A-F]{2})*)?)(?:\?((?:[a-z0-9-._~!$&'()*+,;=:/?@]|%[0-9A-F]{2})*))?(?:#((?:[a-z0-9-._~!$&'()*+,;=:/?@]|%[0-9A-F]{2})*))?$/i");
49         auto m = match(this._uriString, uriReg);
50         if (!m)
51         {
52             this._valid = false;
53             return false;
54         }
55 
56         this._scheme = m.captures[1];
57         this._username = m.captures[2];
58         this._password = m.captures[3];
59         this._host = m.captures[4];
60         this._port = (m.captures[5]).to!ushort;
61         this._path = m.captures[6];
62         this._query = m.captures[7];
63         this._fragment = m.captures[8];
64 
65         this._valid = true;
66 
67         return true;
68     }
69 
70     bool valid()
71     {
72         return this._valid;
73     }
74 
75     @property Uri scheme(string scheme)
76     {
77         this._scheme = scheme;
78 
79         return this;
80     }
81 
82     @property string scheme()
83     {
84         return this._scheme;
85     }
86 
87     @property Uri username(string username)
88     {
89         this._username = username;
90 
91         return this;
92     }
93 
94     @property string username()
95     {
96         return this._username;
97     }
98 
99     @property Uri password(string password)
100     {
101         this._password = password;
102 
103         return this;
104     }
105 
106     @property string password()
107     {
108         return this._password;
109     }
110 
111     @property Uri host(string host)
112     {
113         this._host = host;
114 
115         return this;
116     }
117 
118     @property string host()
119     {
120         return this._host;
121     }
122 
123     @property Uri path(string path)
124     {
125         this._path = path;
126 
127         return this;
128     }
129 
130     @property string path()
131     {
132         return this._path;
133     }
134 
135     @property Uri query(string query)
136     {
137         this._query = query;
138 
139         return this;
140     }
141 
142     @property string query()
143     {
144         return this._query;
145     }
146 
147     @property Uri fragment(string fragment)
148     {
149         this._fragment = fragment;
150 
151         return this;
152     }
153 
154     @property string fragment()
155     {
156         return this._fragment;
157     }
158 
159     override string toString()
160     {
161         return this._uriString;
162     }
163 }