| 272 | // http://example.com/index.html#pos |
| 273 | |
| 274 | bool SplitURI |
| 275 | ( |
| 276 | const std::string& uri, |
| 277 | std::string& protocol, |
| 278 | std::string& host, |
| 279 | std::string& file, |
| 280 | int& port, |
| 281 | bool& useSSL |
| 282 | ) |
| 283 | { |
| 284 | const char *p = uri.c_str(); |
| 285 | const char *sl = strstr(p, "//"); |
| 286 | unsigned int offs = 0; |
| 287 | if(sl) |
| 288 | { |
| 289 | size_t colon = uri.find(':'); |
| 290 | size_t firstslash = uri.find('/'); |
| 291 | if(colon < firstslash) |
| 292 | protocol = uri.substr(0, colon); |
| 293 | if(strncmp(p, "http://", 7) == 0) |
| 294 | { |
| 295 | useSSL = false; |
| 296 | offs = 7; |
| 297 | } |
| 298 | else if(strncmp(p, "https://", 8) == 0) |
| 299 | { |
| 300 | useSSL = true; |
| 301 | offs = 8; |
| 302 | } |
| 303 | else |
| 304 | return false; |
| 305 | |
| 306 | p = sl + 2; |
| 307 | } |
| 308 | |
| 309 | sl = strchr(p, '/'); |
| 310 | if(!sl) |
| 311 | { |
| 312 | host = p; |
| 313 | file = "/"; |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | host = uri.substr(offs, sl - p); |
| 318 | file = sl; |
| 319 | } |
| 320 | |
| 321 | port = -1; |
| 322 | size_t colon = host.find(':'); |
| 323 | if(colon != std::string::npos) |
| 324 | { |
| 325 | port = atoi(host.c_str() + colon); |
| 326 | host.erase(port); |
| 327 | } |
| 328 | |
| 329 | return true; |
| 330 | } |
| 331 | |