| 20 | } |
| 21 | |
| 22 | bool Url::Parse(std::string const & url) |
| 23 | { |
| 24 | // Get url scheme. |
| 25 | size_t start = url.find(':'); |
| 26 | if (start == string::npos || start == 0) |
| 27 | return false; |
| 28 | m_scheme = url.substr(0, start); |
| 29 | |
| 30 | // Skip slashes. |
| 31 | start = url.find_first_not_of('/', start + 1); |
| 32 | if (start == std::string::npos) |
| 33 | return true; |
| 34 | |
| 35 | // Get host. |
| 36 | size_t end = url.find_first_of("/?#", start); |
| 37 | if (end == string::npos) |
| 38 | { |
| 39 | m_host = url.substr(start); |
| 40 | return true; |
| 41 | } |
| 42 | else |
| 43 | m_host = url.substr(start, end - start); |
| 44 | |
| 45 | // Get path. |
| 46 | if (url[end] == '/') |
| 47 | { |
| 48 | // Skip slashes. |
| 49 | start = url.find_first_not_of('/', end); |
| 50 | if (start == std::string::npos) |
| 51 | return true; |
| 52 | |
| 53 | end = url.find_first_of("?#", start); |
| 54 | if (end == string::npos) |
| 55 | { |
| 56 | m_path = url.substr(start); |
| 57 | return true; |
| 58 | } |
| 59 | else |
| 60 | m_path = url.substr(start, end - start); |
| 61 | } |
| 62 | |
| 63 | // Parse query/fragment for keys and values. |
| 64 | for (start = end + 1; start < url.size();) |
| 65 | { |
| 66 | end = url.find_first_of("&#", start); |
| 67 | if (end == string::npos) |
| 68 | end = url.size(); |
| 69 | |
| 70 | // Skip empty keys. |
| 71 | if (end != start) |
| 72 | { |
| 73 | size_t const eq = url.find('=', start); |
| 74 | |
| 75 | string key; |
| 76 | string value; |
| 77 | if (eq != string::npos && eq < end) |
| 78 | { |
| 79 | key = UrlDecode(url.substr(start, eq - start)); |
no test coverage detected