Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 1 | //===---- URI.h - File URIs with schemes -------------------------*- C++-*-===// |
| 2 | // |
Chandler Carruth | b1ace23 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "URI.h" |
Eric Liu | 550f2ef | 2018-09-25 11:47:14 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/StringExtras.h" |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/Twine.h" |
| 12 | #include "llvm/Support/Error.h" |
| 13 | #include "llvm/Support/Format.h" |
Eric Liu | 42e82af | 2018-09-25 10:47:46 +0000 | [diff] [blame] | 14 | #include "llvm/Support/FormatVariadic.h" |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Path.h" |
Eric Liu | 42e82af | 2018-09-25 10:47:46 +0000 | [diff] [blame] | 16 | #include <algorithm> |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 17 | |
| 18 | LLVM_INSTANTIATE_REGISTRY(clang::clangd::URISchemeRegistry) |
| 19 | |
| 20 | namespace clang { |
| 21 | namespace clangd { |
| 22 | namespace { |
| 23 | |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 24 | inline llvm::Error make_string_error(const llvm::Twine &Message) { |
| 25 | return llvm::make_error<llvm::StringError>(Message, |
| 26 | llvm::inconvertibleErrorCode()); |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Dmitri Gribenko | 409457d | 2019-08-22 11:32:57 +0000 | [diff] [blame] | 29 | /// This manages file paths in the file system. All paths in the scheme |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 30 | /// are absolute (with leading '/'). |
Eric Liu | 124fa5d | 2018-11-28 10:30:42 +0000 | [diff] [blame] | 31 | /// Note that this scheme is hardcoded into the library and not registered in |
| 32 | /// registry. |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 33 | class FileSystemScheme : public URIScheme { |
| 34 | public: |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 35 | llvm::Expected<std::string> |
| 36 | getAbsolutePath(llvm::StringRef /*Authority*/, llvm::StringRef Body, |
| 37 | llvm::StringRef /*HintPath*/) const override { |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 38 | if (!Body.startswith("/")) |
| 39 | return make_string_error("File scheme: expect body to be an absolute " |
| 40 | "path starting with '/': " + |
| 41 | Body); |
| 42 | // For Windows paths e.g. /X: |
| 43 | if (Body.size() > 2 && Body[0] == '/' && Body[2] == ':') |
| 44 | Body.consume_front("/"); |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 45 | llvm::SmallVector<char, 16> Path(Body.begin(), Body.end()); |
| 46 | llvm::sys::path::native(Path); |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 47 | return std::string(Path.begin(), Path.end()); |
| 48 | } |
| 49 | |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 50 | llvm::Expected<URI> |
| 51 | uriFromAbsolutePath(llvm::StringRef AbsolutePath) const override { |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 52 | std::string Body; |
| 53 | // For Windows paths e.g. X: |
| 54 | if (AbsolutePath.size() > 1 && AbsolutePath[1] == ':') |
| 55 | Body = "/"; |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 56 | Body += llvm::sys::path::convert_to_slash(AbsolutePath); |
Eric Liu | 124fa5d | 2018-11-28 10:30:42 +0000 | [diff] [blame] | 57 | return URI("file", /*Authority=*/"", Body); |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 58 | } |
| 59 | }; |
| 60 | |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 61 | llvm::Expected<std::unique_ptr<URIScheme>> |
| 62 | findSchemeByName(llvm::StringRef Scheme) { |
Eric Liu | 124fa5d | 2018-11-28 10:30:42 +0000 | [diff] [blame] | 63 | if (Scheme == "file") |
Jonas Devlieghere | aaed81d | 2019-08-14 23:52:23 +0000 | [diff] [blame] | 64 | return std::make_unique<FileSystemScheme>(); |
Eric Liu | 124fa5d | 2018-11-28 10:30:42 +0000 | [diff] [blame] | 65 | |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 66 | for (auto I = URISchemeRegistry::begin(), E = URISchemeRegistry::end(); |
| 67 | I != E; ++I) { |
| 68 | if (I->getName() != Scheme) |
| 69 | continue; |
| 70 | return I->instantiate(); |
| 71 | } |
| 72 | return make_string_error("Can't find scheme: " + Scheme); |
| 73 | } |
| 74 | |
| 75 | bool shouldEscape(unsigned char C) { |
| 76 | // Unreserved characters. |
Eric Liu | 8ce01a9 | 2018-02-07 12:12:06 +0000 | [diff] [blame] | 77 | if ((C >= 'a' && C <= 'z') || (C >= 'A' && C <= 'Z') || |
| 78 | (C >= '0' && C <= '9')) |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 79 | return false; |
| 80 | switch (C) { |
| 81 | case '-': |
| 82 | case '_': |
| 83 | case '.': |
| 84 | case '~': |
| 85 | case '/': // '/' is only reserved when parsing. |
Kadir Cetinkaya | 8f25351 | 2018-10-09 10:29:54 +0000 | [diff] [blame] | 86 | // ':' is only reserved for relative URI paths, which clangd doesn't produce. |
| 87 | case ':': |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 88 | return false; |
| 89 | } |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | /// Encodes a string according to percent-encoding. |
| 94 | /// - Unreserved characters are not escaped. |
| 95 | /// - Reserved characters always escaped with exceptions like '/'. |
| 96 | /// - All other characters are escaped. |
Sam McCall | f65bad2 | 2019-07-08 02:46:21 +0000 | [diff] [blame] | 97 | void percentEncode(llvm::StringRef Content, std::string &Out) { |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 98 | std::string Result; |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 99 | for (unsigned char C : Content) |
| 100 | if (shouldEscape(C)) |
Sam McCall | f65bad2 | 2019-07-08 02:46:21 +0000 | [diff] [blame] | 101 | { |
| 102 | Out.push_back('%'); |
| 103 | Out.push_back(llvm::hexdigit(C / 16)); |
| 104 | Out.push_back(llvm::hexdigit(C % 16)); |
| 105 | } else |
| 106 | { Out.push_back(C); } |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /// Decodes a string according to percent-encoding. |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 110 | std::string percentDecode(llvm::StringRef Content) { |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 111 | std::string Result; |
| 112 | for (auto I = Content.begin(), E = Content.end(); I != E; ++I) { |
| 113 | if (*I != '%') { |
| 114 | Result += *I; |
| 115 | continue; |
| 116 | } |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 117 | if (*I == '%' && I + 2 < Content.end() && llvm::isHexDigit(*(I + 1)) && |
| 118 | llvm::isHexDigit(*(I + 2))) { |
| 119 | Result.push_back(llvm::hexFromNibbles(*(I + 1), *(I + 2))); |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 120 | I += 2; |
| 121 | } else |
| 122 | Result.push_back(*I); |
| 123 | } |
| 124 | return Result; |
| 125 | } |
| 126 | |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 127 | bool isValidScheme(llvm::StringRef Scheme) { |
Eric Liu | 42e82af | 2018-09-25 10:47:46 +0000 | [diff] [blame] | 128 | if (Scheme.empty()) |
| 129 | return false; |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 130 | if (!llvm::isAlpha(Scheme[0])) |
Eric Liu | 42e82af | 2018-09-25 10:47:46 +0000 | [diff] [blame] | 131 | return false; |
| 132 | return std::all_of(Scheme.begin() + 1, Scheme.end(), [](char C) { |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 133 | return llvm::isAlnum(C) || C == '+' || C == '.' || C == '-'; |
Eric Liu | 42e82af | 2018-09-25 10:47:46 +0000 | [diff] [blame] | 134 | }); |
| 135 | } |
| 136 | |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 137 | } // namespace |
| 138 | |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 139 | URI::URI(llvm::StringRef Scheme, llvm::StringRef Authority, |
| 140 | llvm::StringRef Body) |
Eric Liu | c94f5a9 | 2018-01-29 15:37:46 +0000 | [diff] [blame] | 141 | : Scheme(Scheme), Authority(Authority), Body(Body) { |
| 142 | assert(!Scheme.empty()); |
| 143 | assert((Authority.empty() || Body.startswith("/")) && |
| 144 | "URI body must start with '/' when authority is present."); |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Eric Liu | c94f5a9 | 2018-01-29 15:37:46 +0000 | [diff] [blame] | 147 | std::string URI::toString() const { |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 148 | std::string Result; |
Sam McCall | f65bad2 | 2019-07-08 02:46:21 +0000 | [diff] [blame] | 149 | percentEncode(Scheme, Result); |
| 150 | Result.push_back(':'); |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 151 | if (Authority.empty() && Body.empty()) |
Sam McCall | f65bad2 | 2019-07-08 02:46:21 +0000 | [diff] [blame] | 152 | return Result; |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 153 | // If authority if empty, we only print body if it starts with "/"; otherwise, |
| 154 | // the URI is invalid. |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 155 | if (!Authority.empty() || llvm::StringRef(Body).startswith("/")) |
Sam McCall | f65bad2 | 2019-07-08 02:46:21 +0000 | [diff] [blame] | 156 | { |
| 157 | Result.append("//"); |
| 158 | percentEncode(Authority, Result); |
| 159 | } |
| 160 | percentEncode(Body, Result); |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 161 | return Result; |
| 162 | } |
| 163 | |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 164 | llvm::Expected<URI> URI::parse(llvm::StringRef OrigUri) { |
Eric Liu | c94f5a9 | 2018-01-29 15:37:46 +0000 | [diff] [blame] | 165 | URI U; |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 166 | llvm::StringRef Uri = OrigUri; |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 167 | |
| 168 | auto Pos = Uri.find(':'); |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 169 | if (Pos == llvm::StringRef::npos) |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 170 | return make_string_error("Scheme must be provided in URI: " + OrigUri); |
Eric Liu | 42e82af | 2018-09-25 10:47:46 +0000 | [diff] [blame] | 171 | auto SchemeStr = Uri.substr(0, Pos); |
| 172 | U.Scheme = percentDecode(SchemeStr); |
| 173 | if (!isValidScheme(U.Scheme)) |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 174 | return make_string_error(llvm::formatv("Invalid scheme: {0} (decoded: {1})", |
| 175 | SchemeStr, U.Scheme)); |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 176 | Uri = Uri.substr(Pos + 1); |
| 177 | if (Uri.consume_front("//")) { |
| 178 | Pos = Uri.find('/'); |
| 179 | U.Authority = percentDecode(Uri.substr(0, Pos)); |
| 180 | Uri = Uri.substr(Pos); |
| 181 | } |
| 182 | U.Body = percentDecode(Uri); |
| 183 | return U; |
| 184 | } |
| 185 | |
Haojian Wu | f1f4b3f | 2019-09-23 14:39:37 +0000 | [diff] [blame] | 186 | llvm::Expected<std::string> URI::resolve(llvm::StringRef FileURI, |
| 187 | llvm::StringRef HintPath) { |
| 188 | auto Uri = URI::parse(FileURI); |
| 189 | if (!Uri) |
| 190 | return Uri.takeError(); |
| 191 | auto Path = URI::resolve(*Uri, HintPath); |
| 192 | if (!Path) |
| 193 | return Path.takeError(); |
| 194 | return *Path; |
| 195 | } |
| 196 | |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 197 | llvm::Expected<URI> URI::create(llvm::StringRef AbsolutePath, |
| 198 | llvm::StringRef Scheme) { |
| 199 | if (!llvm::sys::path::is_absolute(AbsolutePath)) |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 200 | return make_string_error("Not a valid absolute path: " + AbsolutePath); |
| 201 | auto S = findSchemeByName(Scheme); |
| 202 | if (!S) |
| 203 | return S.takeError(); |
| 204 | return S->get()->uriFromAbsolutePath(AbsolutePath); |
| 205 | } |
| 206 | |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 207 | URI URI::create(llvm::StringRef AbsolutePath) { |
| 208 | if (!llvm::sys::path::is_absolute(AbsolutePath)) |
Eric Liu | 94caf6c | 2018-11-22 15:02:05 +0000 | [diff] [blame] | 209 | llvm_unreachable( |
| 210 | ("Not a valid absolute path: " + AbsolutePath).str().c_str()); |
| 211 | for (auto &Entry : URISchemeRegistry::entries()) { |
Eric Liu | 94caf6c | 2018-11-22 15:02:05 +0000 | [diff] [blame] | 212 | auto URI = Entry.instantiate()->uriFromAbsolutePath(AbsolutePath); |
Kirill Bobyrev | 8ee3adc | 2018-09-06 12:54:43 +0000 | [diff] [blame] | 213 | // For some paths, conversion to different URI schemes is impossible. These |
| 214 | // should be just skipped. |
| 215 | if (!URI) { |
| 216 | // Ignore the error. |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 217 | llvm::consumeError(URI.takeError()); |
Kirill Bobyrev | 8ee3adc | 2018-09-06 12:54:43 +0000 | [diff] [blame] | 218 | continue; |
| 219 | } |
Eric Liu | 94caf6c | 2018-11-22 15:02:05 +0000 | [diff] [blame] | 220 | return std::move(*URI); |
Kirill Bobyrev | 8ee3adc | 2018-09-06 12:54:43 +0000 | [diff] [blame] | 221 | } |
Eric Liu | 94caf6c | 2018-11-22 15:02:05 +0000 | [diff] [blame] | 222 | // Fallback to file: scheme which should work for any paths. |
| 223 | return URI::createFile(AbsolutePath); |
Kirill Bobyrev | 8ee3adc | 2018-09-06 12:54:43 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 226 | URI URI::createFile(llvm::StringRef AbsolutePath) { |
Eric Liu | 124fa5d | 2018-11-28 10:30:42 +0000 | [diff] [blame] | 227 | auto U = FileSystemScheme().uriFromAbsolutePath(AbsolutePath); |
Eric Liu | c94f5a9 | 2018-01-29 15:37:46 +0000 | [diff] [blame] | 228 | if (!U) |
| 229 | llvm_unreachable(llvm::toString(U.takeError()).c_str()); |
| 230 | return std::move(*U); |
| 231 | } |
| 232 | |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 233 | llvm::Expected<std::string> URI::resolve(const URI &Uri, |
| 234 | llvm::StringRef HintPath) { |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 235 | auto S = findSchemeByName(Uri.Scheme); |
| 236 | if (!S) |
| 237 | return S.takeError(); |
| 238 | return S->get()->getAbsolutePath(Uri.Authority, Uri.Body, HintPath); |
| 239 | } |
| 240 | |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 241 | llvm::Expected<std::string> URI::resolvePath(llvm::StringRef AbsPath, |
| 242 | llvm::StringRef HintPath) { |
| 243 | if (!llvm::sys::path::is_absolute(AbsPath)) |
Eric Liu | 124fa5d | 2018-11-28 10:30:42 +0000 | [diff] [blame] | 244 | llvm_unreachable(("Not a valid absolute path: " + AbsPath).str().c_str()); |
| 245 | for (auto &Entry : URISchemeRegistry::entries()) { |
Ilya Biryukov | 74a4acf | 2019-01-03 13:28:05 +0000 | [diff] [blame] | 246 | auto S = Entry.instantiate(); |
Eric Liu | 124fa5d | 2018-11-28 10:30:42 +0000 | [diff] [blame] | 247 | auto U = S->uriFromAbsolutePath(AbsPath); |
| 248 | // For some paths, conversion to different URI schemes is impossible. These |
| 249 | // should be just skipped. |
| 250 | if (!U) { |
| 251 | // Ignore the error. |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 252 | llvm::consumeError(U.takeError()); |
Eric Liu | 124fa5d | 2018-11-28 10:30:42 +0000 | [diff] [blame] | 253 | continue; |
| 254 | } |
| 255 | return S->getAbsolutePath(U->Authority, U->Body, HintPath); |
| 256 | } |
| 257 | // Fallback to file: scheme which doesn't do any canonicalization. |
| 258 | return AbsPath; |
| 259 | } |
| 260 | |
Ilya Biryukov | d0dee7a | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 261 | llvm::Expected<std::string> URI::includeSpelling(const URI &Uri) { |
Haojian Wu | 264eacf | 2018-04-09 15:09:44 +0000 | [diff] [blame] | 262 | auto S = findSchemeByName(Uri.Scheme); |
| 263 | if (!S) |
| 264 | return S.takeError(); |
| 265 | return S->get()->getIncludeSpelling(Uri); |
| 266 | } |
| 267 | |
Eric Liu | 9dcc79f | 2018-01-22 11:48:20 +0000 | [diff] [blame] | 268 | } // namespace clangd |
| 269 | } // namespace clang |