diff options
Diffstat (limited to '')
-rw-r--r-- | src/unstable/url.cpp | 378 |
1 files changed, 189 insertions, 189 deletions
diff --git a/src/unstable/url.cpp b/src/unstable/url.cpp index 3c9da4b..75131af 100644 --- a/src/unstable/url.cpp +++ b/src/unstable/url.cpp | |||
@@ -13,8 +13,8 @@ | |||
13 | #include <stdlib.h> | 13 | #include <stdlib.h> |
14 | 14 | ||
15 | char Bu::Url::hexcode[] = { | 15 | char Bu::Url::hexcode[] = { |
16 | '0', '1', '2', '3', '4', '5', '6', '7', | 16 | '0', '1', '2', '3', '4', '5', '6', '7', |
17 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' | 17 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' |
18 | }; | 18 | }; |
19 | 19 | ||
20 | Bu::Url::Url() | 20 | Bu::Url::Url() |
@@ -23,7 +23,7 @@ Bu::Url::Url() | |||
23 | 23 | ||
24 | Bu::Url::Url( const Bu::String &sUrl ) | 24 | Bu::Url::Url( const Bu::String &sUrl ) |
25 | { | 25 | { |
26 | parseUrl( sUrl ); | 26 | parseUrl( sUrl ); |
27 | } | 27 | } |
28 | 28 | ||
29 | Bu::Url::~Url() | 29 | Bu::Url::~Url() |
@@ -32,254 +32,254 @@ Bu::Url::~Url() | |||
32 | 32 | ||
33 | void Bu::Url::parseUrl( const Bu::String &sUrl ) | 33 | void Bu::Url::parseUrl( const Bu::String &sUrl ) |
34 | { | 34 | { |
35 | clear(); | 35 | clear(); |
36 | 36 | ||
37 | Bu::String::const_iterator i = sUrl.begin(); | 37 | Bu::String::const_iterator i = sUrl.begin(); |
38 | parseProtocol( i ); | 38 | parseProtocol( i ); |
39 | parseUserPass( i ); | 39 | parseUserPass( i ); |
40 | parseHost( i ); | 40 | parseHost( i ); |
41 | parsePath( i ); | 41 | parsePath( i ); |
42 | } | 42 | } |
43 | 43 | ||
44 | Bu::String Bu::Url::decode( const Bu::String &sStr ) | 44 | Bu::String Bu::Url::decode( const Bu::String &sStr ) |
45 | { | 45 | { |
46 | Bu::String sRet; | 46 | Bu::String sRet; |
47 | char buf[3] = {0, 0, 0}; | 47 | char buf[3] = {0, 0, 0}; |
48 | for( Bu::String::const_iterator i = sStr.begin(); i; i++ ) | 48 | for( Bu::String::const_iterator i = sStr.begin(); i; i++ ) |
49 | { | 49 | { |
50 | if( *i == '+' ) | 50 | if( *i == '+' ) |
51 | { | 51 | { |
52 | sRet += ' '; | 52 | sRet += ' '; |
53 | } | 53 | } |
54 | else if( *i == '%' ) | 54 | else if( *i == '%' ) |
55 | { | 55 | { |
56 | i++; | 56 | i++; |
57 | buf[0] = *i; | 57 | buf[0] = *i; |
58 | i++; | 58 | i++; |
59 | buf[1] = *i; | 59 | buf[1] = *i; |
60 | sRet += (char)((unsigned char)strtol( buf, NULL, 16 )); | 60 | sRet += (char)((unsigned char)strtol( buf, NULL, 16 )); |
61 | } | 61 | } |
62 | else | 62 | else |
63 | { | 63 | { |
64 | sRet += *i; | 64 | sRet += *i; |
65 | } | 65 | } |
66 | } | 66 | } |
67 | return sRet; | 67 | return sRet; |
68 | } | 68 | } |
69 | 69 | ||
70 | Bu::String Bu::Url::encode( const Bu::String &sStr ) | 70 | Bu::String Bu::Url::encode( const Bu::String &sStr ) |
71 | { | 71 | { |
72 | Bu::String sRet; | 72 | Bu::String sRet; |
73 | for( Bu::String::const_iterator i = sStr.begin(); i; i++ ) | 73 | for( Bu::String::const_iterator i = sStr.begin(); i; i++ ) |
74 | { | 74 | { |
75 | if( *i == ' ' ) | 75 | if( *i == ' ' ) |
76 | { | 76 | { |
77 | sRet += '+'; | 77 | sRet += '+'; |
78 | } | 78 | } |
79 | else if( | 79 | else if( |
80 | (*i >= 'A' && *i <= 'Z') || | 80 | (*i >= 'A' && *i <= 'Z') || |
81 | (*i >= 'a' && *i <= 'z') || | 81 | (*i >= 'a' && *i <= 'z') || |
82 | (*i >= '0' && *i <= '9') || | 82 | (*i >= '0' && *i <= '9') || |
83 | (*i == '-' || *i == '_' || *i == '.' || *i == '~') | 83 | (*i == '-' || *i == '_' || *i == '.' || *i == '~') |
84 | ) | 84 | ) |
85 | { | 85 | { |
86 | sRet += *i; | 86 | sRet += *i; |
87 | } | 87 | } |
88 | else | 88 | else |
89 | { | 89 | { |
90 | unsigned char b = *i; | 90 | unsigned char b = *i; |
91 | sRet += '%'; | 91 | sRet += '%'; |
92 | sRet += hexcode[(b>>4)&0xF]; | 92 | sRet += hexcode[(b>>4)&0xF]; |
93 | sRet += hexcode[b&0xF]; | 93 | sRet += hexcode[b&0xF]; |
94 | } | 94 | } |
95 | } | 95 | } |
96 | return sRet; | 96 | return sRet; |
97 | } | 97 | } |
98 | 98 | ||
99 | void Bu::Url::parseProtocol( Bu::String::const_iterator &i ) | 99 | void Bu::Url::parseProtocol( Bu::String::const_iterator &i ) |
100 | { | 100 | { |
101 | Bu::String::const_iterator s = i.find("://", 3); | 101 | Bu::String::const_iterator s = i.find("://", 3); |
102 | if( !s ) | 102 | if( !s ) |
103 | throw Bu::ExceptionBase("No :// in url"); | 103 | throw Bu::ExceptionBase("No :// in url"); |
104 | Bu::String sTmp( i, s ); | 104 | Bu::String sTmp( i, s ); |
105 | setProtocol( sTmp ); | 105 | setProtocol( sTmp ); |
106 | i = s + 3; | 106 | i = s + 3; |
107 | } | 107 | } |
108 | 108 | ||
109 | void Bu::Url::setProtocol( const Bu::String &sNewProto, bool bAutoSetPort ) | 109 | void Bu::Url::setProtocol( const Bu::String &sNewProto, bool bAutoSetPort ) |
110 | { | 110 | { |
111 | sProtocol = sNewProto; | 111 | sProtocol = sNewProto; |
112 | #ifndef WIN32 | 112 | #ifndef WIN32 |
113 | if( bAutoSetPort ) | 113 | if( bAutoSetPort ) |
114 | { | 114 | { |
115 | struct servent *se = getservbyname( sProtocol.getStr(), "tcp" ); | 115 | struct servent *se = getservbyname( sProtocol.getStr(), "tcp" ); |
116 | if( se ) | 116 | if( se ) |
117 | { | 117 | { |
118 | iPort = ntohs( se->s_port ); | 118 | iPort = ntohs( se->s_port ); |
119 | } | 119 | } |
120 | } | 120 | } |
121 | #endif | 121 | #endif |
122 | } | 122 | } |
123 | 123 | ||
124 | void Bu::Url::parseUserPass( Bu::String::const_iterator &i ) | 124 | void Bu::Url::parseUserPass( Bu::String::const_iterator &i ) |
125 | { | 125 | { |
126 | Bu::String::const_iterator s = i.find('@'); | 126 | Bu::String::const_iterator s = i.find('@'); |
127 | if( !s ) | 127 | if( !s ) |
128 | return; | 128 | return; |
129 | 129 | ||
130 | Bu::String::const_iterator p = i.find(':'); | 130 | Bu::String::const_iterator p = i.find(':'); |
131 | if( p ) | 131 | if( p ) |
132 | { | 132 | { |
133 | sUser.set( i, p ); | 133 | sUser.set( i, p ); |
134 | sPass.set( p+1, s ); | 134 | sPass.set( p+1, s ); |
135 | } | 135 | } |
136 | else | 136 | else |
137 | { | 137 | { |
138 | sUser.set( i, s ); | 138 | sUser.set( i, s ); |
139 | } | 139 | } |
140 | 140 | ||
141 | i = s + 1; | 141 | i = s + 1; |
142 | } | 142 | } |
143 | 143 | ||
144 | void Bu::Url::parseHost( Bu::String::const_iterator &i ) | 144 | void Bu::Url::parseHost( Bu::String::const_iterator &i ) |
145 | { | 145 | { |
146 | Bu::String::const_iterator s = i; | 146 | Bu::String::const_iterator s = i; |
147 | for( ; s && *s != '/'; s++ ) | 147 | for( ; s && *s != '/'; s++ ) |
148 | { | 148 | { |
149 | if( *s == ':' ) | 149 | if( *s == ':' ) |
150 | { | 150 | { |
151 | sHost.set( i, s ); | 151 | sHost.set( i, s ); |
152 | i = s + 1; | 152 | i = s + 1; |
153 | s = i.find('/'); | 153 | s = i.find('/'); |
154 | Bu::String sPort( i, s ); | 154 | Bu::String sPort( i, s ); |
155 | iPort = strtol( sPort.getStr(), NULL, 10 ); | 155 | iPort = strtol( sPort.getStr(), NULL, 10 ); |
156 | i = s; | 156 | i = s; |
157 | return; | 157 | return; |
158 | } | 158 | } |
159 | } | 159 | } |
160 | sHost.set( i, s ); | 160 | sHost.set( i, s ); |
161 | i = s; | 161 | i = s; |
162 | } | 162 | } |
163 | 163 | ||
164 | void Bu::Url::parsePath( const Bu::String &sPath ) | 164 | void Bu::Url::parsePath( const Bu::String &sPath ) |
165 | { | 165 | { |
166 | Bu::String::const_iterator i = sPath.begin(); | 166 | Bu::String::const_iterator i = sPath.begin(); |
167 | parsePath( i ); | 167 | parsePath( i ); |
168 | } | 168 | } |
169 | 169 | ||
170 | void Bu::Url::parsePath( Bu::String::const_iterator &i ) | 170 | void Bu::Url::parsePath( Bu::String::const_iterator &i ) |
171 | { | 171 | { |
172 | if( i ) | 172 | if( i ) |
173 | { | 173 | { |
174 | Bu::String::const_iterator s = i.find('?'); | 174 | Bu::String::const_iterator s = i.find('?'); |
175 | sPath.set( i, s ); | 175 | sPath.set( i, s ); |
176 | i = s + 1; | 176 | i = s + 1; |
177 | if( s ) | 177 | if( s ) |
178 | { | 178 | { |
179 | parseParams( i ); | 179 | parseParams( i ); |
180 | } | 180 | } |
181 | } | 181 | } |
182 | else | 182 | else |
183 | { | 183 | { |
184 | sPath = "/"; | 184 | sPath = "/"; |
185 | } | 185 | } |
186 | } | 186 | } |
187 | 187 | ||
188 | void Bu::Url::parseParams( const Bu::String &sQuery ) | 188 | void Bu::Url::parseParams( const Bu::String &sQuery ) |
189 | { | 189 | { |
190 | Bu::String::const_iterator i = sQuery.begin(); | 190 | Bu::String::const_iterator i = sQuery.begin(); |
191 | parseParams( i ); | 191 | parseParams( i ); |
192 | } | 192 | } |
193 | 193 | ||
194 | void Bu::Url::parseParams( Bu::String::const_iterator &i ) | 194 | void Bu::Url::parseParams( Bu::String::const_iterator &i ) |
195 | { | 195 | { |
196 | bool bName = true; | 196 | bool bName = true; |
197 | Bu::String sName, sValue; | 197 | Bu::String sName, sValue; |
198 | for( Bu::String::const_iterator s = i; s; s++ ) | 198 | for( Bu::String::const_iterator s = i; s; s++ ) |
199 | { | 199 | { |
200 | if( bName ) | 200 | if( bName ) |
201 | { | 201 | { |
202 | if( *s == '&' ) | 202 | if( *s == '&' ) |
203 | { | 203 | { |
204 | sName.set( i, s ); | 204 | sName.set( i, s ); |
205 | sValue.clear(); | 205 | sValue.clear(); |
206 | i = s + 1; | 206 | i = s + 1; |
207 | addParam( decode( sName ), decode( sValue ) ); | 207 | addParam( decode( sName ), decode( sValue ) ); |
208 | } | 208 | } |
209 | else if( *s == '=' ) | 209 | else if( *s == '=' ) |
210 | { | 210 | { |
211 | sName.set( i, s ); | 211 | sName.set( i, s ); |
212 | i = s + 1; | 212 | i = s + 1; |
213 | bName = false; | 213 | bName = false; |
214 | } | 214 | } |
215 | } | 215 | } |
216 | else | 216 | else |
217 | { | 217 | { |
218 | if( *s == '&' ) | 218 | if( *s == '&' ) |
219 | { | 219 | { |
220 | sValue.set( i, s ); | 220 | sValue.set( i, s ); |
221 | i = s + 1; | 221 | i = s + 1; |
222 | bName = true; | 222 | bName = true; |
223 | addParam( decode( sName ), decode( sValue ) ); | 223 | addParam( decode( sName ), decode( sValue ) ); |
224 | } | 224 | } |
225 | } | 225 | } |
226 | } | 226 | } |
227 | if( i ) | 227 | if( i ) |
228 | { | 228 | { |
229 | if( bName ) | 229 | if( bName ) |
230 | { | 230 | { |
231 | sName.set( i ); | 231 | sName.set( i ); |
232 | sValue.clear(); | 232 | sValue.clear(); |
233 | } | 233 | } |
234 | else | 234 | else |
235 | { | 235 | { |
236 | sValue.set( i ); | 236 | sValue.set( i ); |
237 | } | 237 | } |
238 | addParam( decode( sName ), decode( sValue ) ); | 238 | addParam( decode( sName ), decode( sValue ) ); |
239 | } | 239 | } |
240 | } | 240 | } |
241 | 241 | ||
242 | void Bu::Url::addParam( const Bu::String &n, const Bu::String &v ) | 242 | void Bu::Url::addParam( const Bu::String &n, const Bu::String &v ) |
243 | { | 243 | { |
244 | lParam.append( Param( n, v ) ); | 244 | lParam.append( Param( n, v ) ); |
245 | } | 245 | } |
246 | 246 | ||
247 | void Bu::Url::clear() | 247 | void Bu::Url::clear() |
248 | { | 248 | { |
249 | sProtocol.clear(); | 249 | sProtocol.clear(); |
250 | sUser.clear(); | 250 | sUser.clear(); |
251 | sPass.clear(); | 251 | sPass.clear(); |
252 | sHost.clear(); | 252 | sHost.clear(); |
253 | sPath.clear(); | 253 | sPath.clear(); |
254 | iPort.clear(); | 254 | iPort.clear(); |
255 | } | 255 | } |
256 | 256 | ||
257 | Bu::String Bu::Url::getFullPath() const | 257 | Bu::String Bu::Url::getFullPath() const |
258 | { | 258 | { |
259 | Bu::String sBuf = sPath; | 259 | Bu::String sBuf = sPath; |
260 | if( !lParam.isEmpty() ) | 260 | if( !lParam.isEmpty() ) |
261 | { | 261 | { |
262 | for( ParamList::const_iterator i = lParam.begin(); i; i++ ) | 262 | for( ParamList::const_iterator i = lParam.begin(); i; i++ ) |
263 | { | 263 | { |
264 | if( i == lParam.begin() ) | 264 | if( i == lParam.begin() ) |
265 | sBuf += "?"; | 265 | sBuf += "?"; |
266 | else | 266 | else |
267 | sBuf += "&"; | 267 | sBuf += "&"; |
268 | 268 | ||
269 | sBuf += encode( (*i).sName ); | 269 | sBuf += encode( (*i).sName ); |
270 | if( !(*i).sValue.isEmpty() ) | 270 | if( !(*i).sValue.isEmpty() ) |
271 | { | 271 | { |
272 | sBuf += "=" + encode( (*i).sValue ); | 272 | sBuf += "=" + encode( (*i).sValue ); |
273 | } | 273 | } |
274 | } | 274 | } |
275 | } | 275 | } |
276 | 276 | ||
277 | return sBuf; | 277 | return sBuf; |
278 | } | 278 | } |
279 | 279 | ||
280 | Bu::String Bu::Url::getUrl() const | 280 | Bu::String Bu::Url::getUrl() const |
281 | { | 281 | { |
282 | Bu::String sBuf = sProtocol + "://" + sHost + getFullPath(); | 282 | Bu::String sBuf = sProtocol + "://" + sHost + getFullPath(); |
283 | return sBuf; | 283 | return sBuf; |
284 | } | 284 | } |
285 | 285 | ||