| 44 | const StringSearch UrlParser::hash_search(&hash); |
| 45 | |
| 46 | bool UrlParser::ParseUrl(const StringValue& url, UrlPart part, StringValue* result) { |
| 47 | result->Clear(); |
| 48 | // Remove leading and trailing spaces. |
| 49 | StringValue trimmed_url = url.Trim(); |
| 50 | |
| 51 | // All parts require checking for the protocol. |
| 52 | int32_t protocol_pos = protocol_search.Search(&trimmed_url); |
| 53 | if (protocol_pos < 0) return false; |
| 54 | // Positioned to first char after '://'. |
| 55 | StringValue protocol_end = trimmed_url.Substring(protocol_pos + protocol.Len()); |
| 56 | |
| 57 | // Find the end of the authority. The authority ends at the first '/' or '?'. |
| 58 | int32_t auth_end_pos = -1; |
| 59 | { |
| 60 | int32_t first_slash = slash_search.Search(&protocol_end); |
| 61 | int32_t first_question = question_search.Search(&protocol_end); |
| 62 | |
| 63 | auth_end_pos = first_slash; |
| 64 | if (first_slash < 0 || (0 <= first_question && first_question < first_slash)) { |
| 65 | // Either we did not find a slash, or there is one and the first question mark is |
| 66 | // left of the first slash (after the protocol), for example: |
| 67 | // http://example.com?dir=/etc |
| 68 | auth_end_pos = first_question; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | switch(part) { |
| 73 | case AUTHORITY: { |
| 74 | *result = protocol_end.Substring(0, auth_end_pos); |
| 75 | break; |
| 76 | } |
| 77 | |
| 78 | case FILE: |
| 79 | case PATH: { |
| 80 | // Find first '/'. |
| 81 | int32_t start_pos = slash_search.Search(&protocol_end); |
| 82 | if (start_pos < 0) { |
| 83 | // Return empty string. This is what Hive does. |
| 84 | return true; |
| 85 | } |
| 86 | StringValue path_start = protocol_end.Substring(start_pos); |
| 87 | int32_t end_pos; |
| 88 | if (part == FILE) { |
| 89 | // End at '#'. |
| 90 | end_pos = hash_search.Search(&path_start); |
| 91 | } else { |
| 92 | // End string at next '?' or '#'. |
| 93 | end_pos = question_search.Search(&path_start); |
| 94 | if (end_pos < 0) { |
| 95 | // No '?' was found, look for '#'. |
| 96 | end_pos = hash_search.Search(&path_start); |
| 97 | } |
| 98 | } |
| 99 | *result = path_start.Substring(0, end_pos); |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | case HOST: { |