| 156 | {} |
| 157 | |
| 158 | bool GeoParser::Parse(std::string const & raw, GeoURLInfo & info) const |
| 159 | { |
| 160 | info.Reset(); |
| 161 | |
| 162 | /* |
| 163 | * References: |
| 164 | * - https://datatracker.ietf.org/doc/html/rfc5870 |
| 165 | * - https://developer.android.com/guide/components/intents-common#Maps |
| 166 | * - https://developers.google.com/maps/documentation/urls/android-intents |
| 167 | */ |
| 168 | |
| 169 | /* |
| 170 | * Check that URI starts with geo: |
| 171 | */ |
| 172 | if (!raw.starts_with("geo:")) |
| 173 | return false; |
| 174 | |
| 175 | /* |
| 176 | * Check for trailing `(label)` which is not RFC3986-compliant (thanks, Google). |
| 177 | */ |
| 178 | size_t end = string::npos; |
| 179 | if (raw.size() > 2 && raw.back() == ')' && string::npos != (end = raw.rfind('('))) |
| 180 | { |
| 181 | // head (label) |
| 182 | // ^end |
| 183 | info.m_label = url::UrlDecode(raw.substr(end + 1, raw.size() - end - 2)); |
| 184 | // Remove any whitespace between `head` and `(`. |
| 185 | end--; |
| 186 | while (end > 0 && (raw[end] == ' ' || raw[end] == '+')) |
| 187 | end--; |
| 188 | } |
| 189 | |
| 190 | url::Url url(end == string::npos ? raw : raw.substr(0, end + 1)); |
| 191 | if (!url.IsValid()) |
| 192 | return false; |
| 193 | ASSERT_EQUAL(url.GetScheme(), "geo", ()); |
| 194 | |
| 195 | // Fix non-RFC url/hostname, reported by an Android user, with & instead of ? |
| 196 | std::string_view constexpr kWrongZoomInHost = "&z="; |
| 197 | if (std::string::npos != url.GetHost().find(kWrongZoomInHost)) |
| 198 | { |
| 199 | auto fixedUrl = raw; |
| 200 | fixedUrl.replace(raw.find(kWrongZoomInHost), 1, 1, std::string::value_type{'?'}); |
| 201 | url = url::Url{fixedUrl}; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Parse coordinates before ';' character |
| 206 | */ |
| 207 | std::string coordinates = url.GetHost().substr(0, url.GetHost().find(';')); |
| 208 | if (!coordinates.empty()) |
| 209 | { |
| 210 | boost::smatch m; |
| 211 | if (!boost::regex_match(coordinates, m, m_latlonRe) || m.size() < 3) |
| 212 | { |
| 213 | // no match? try URL decoding before giving up |
| 214 | coordinates = url::UrlDecode(coordinates); |
| 215 | if (!boost::regex_match(coordinates, m, m_latlonRe) || m.size() < 3) |
nothing calls this directly
no test coverage detected