2018-01-22 19:48:20 +08:00
|
|
|
//===---- URI.h - File URIs with schemes -------------------------*- C++-*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "URI.h"
|
2018-09-25 19:47:14 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2018-01-22 19:48:20 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/Format.h"
|
2018-09-25 18:47:46 +08:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
2018-01-22 19:48:20 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2018-09-25 18:47:46 +08:00
|
|
|
#include <algorithm>
|
2018-01-22 19:48:20 +08:00
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
LLVM_INSTANTIATE_REGISTRY(clang::clangd::URISchemeRegistry)
|
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
using namespace llvm;
|
2018-01-22 19:48:20 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace {
|
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
inline Error make_string_error(const Twine &Message) {
|
|
|
|
return make_error<StringError>(Message, inconvertibleErrorCode());
|
2018-01-22 19:48:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief This manages file paths in the file system. All paths in the scheme
|
|
|
|
/// are absolute (with leading '/').
|
|
|
|
class FileSystemScheme : public URIScheme {
|
|
|
|
public:
|
|
|
|
static const char *Scheme;
|
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
Expected<std::string> getAbsolutePath(StringRef /*Authority*/, StringRef Body,
|
|
|
|
StringRef /*HintPath*/) const override {
|
2018-01-22 19:48:20 +08:00
|
|
|
if (!Body.startswith("/"))
|
|
|
|
return make_string_error("File scheme: expect body to be an absolute "
|
|
|
|
"path starting with '/': " +
|
|
|
|
Body);
|
|
|
|
// For Windows paths e.g. /X:
|
|
|
|
if (Body.size() > 2 && Body[0] == '/' && Body[2] == ':')
|
|
|
|
Body.consume_front("/");
|
2018-10-20 23:30:37 +08:00
|
|
|
SmallVector<char, 16> Path(Body.begin(), Body.end());
|
|
|
|
sys::path::native(Path);
|
2018-01-22 19:48:20 +08:00
|
|
|
return std::string(Path.begin(), Path.end());
|
|
|
|
}
|
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
Expected<URI> uriFromAbsolutePath(StringRef AbsolutePath) const override {
|
2018-01-22 19:48:20 +08:00
|
|
|
using namespace llvm::sys;
|
|
|
|
|
|
|
|
std::string Body;
|
|
|
|
// For Windows paths e.g. X:
|
|
|
|
if (AbsolutePath.size() > 1 && AbsolutePath[1] == ':')
|
|
|
|
Body = "/";
|
|
|
|
Body += path::convert_to_slash(AbsolutePath);
|
2018-01-29 23:37:46 +08:00
|
|
|
return URI(Scheme, /*Authority=*/"", Body);
|
2018-01-22 19:48:20 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *FileSystemScheme::Scheme = "file";
|
|
|
|
|
|
|
|
static URISchemeRegistry::Add<FileSystemScheme>
|
|
|
|
X(FileSystemScheme::Scheme,
|
|
|
|
"URI scheme for absolute paths in the file system.");
|
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
Expected<std::unique_ptr<URIScheme>> findSchemeByName(StringRef Scheme) {
|
2018-01-22 19:48:20 +08:00
|
|
|
for (auto I = URISchemeRegistry::begin(), E = URISchemeRegistry::end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (I->getName() != Scheme)
|
|
|
|
continue;
|
|
|
|
return I->instantiate();
|
|
|
|
}
|
|
|
|
return make_string_error("Can't find scheme: " + Scheme);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool shouldEscape(unsigned char C) {
|
|
|
|
// Unreserved characters.
|
2018-02-07 20:12:06 +08:00
|
|
|
if ((C >= 'a' && C <= 'z') || (C >= 'A' && C <= 'Z') ||
|
|
|
|
(C >= '0' && C <= '9'))
|
2018-01-22 19:48:20 +08:00
|
|
|
return false;
|
|
|
|
switch (C) {
|
|
|
|
case '-':
|
|
|
|
case '_':
|
|
|
|
case '.':
|
|
|
|
case '~':
|
|
|
|
case '/': // '/' is only reserved when parsing.
|
2018-10-09 18:29:54 +08:00
|
|
|
// ':' is only reserved for relative URI paths, which clangd doesn't produce.
|
|
|
|
case ':':
|
2018-01-22 19:48:20 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Encodes a string according to percent-encoding.
|
|
|
|
/// - Unreserved characters are not escaped.
|
|
|
|
/// - Reserved characters always escaped with exceptions like '/'.
|
|
|
|
/// - All other characters are escaped.
|
2018-10-20 23:30:37 +08:00
|
|
|
std::string percentEncode(StringRef Content) {
|
2018-01-22 19:48:20 +08:00
|
|
|
std::string Result;
|
2018-10-20 23:30:37 +08:00
|
|
|
raw_string_ostream OS(Result);
|
2018-01-22 19:48:20 +08:00
|
|
|
for (unsigned char C : Content)
|
|
|
|
if (shouldEscape(C))
|
2018-10-20 23:30:37 +08:00
|
|
|
OS << '%' << format_hex_no_prefix(C, 2, /*Upper = */ true);
|
2018-01-22 19:48:20 +08:00
|
|
|
else
|
|
|
|
OS << C;
|
|
|
|
|
|
|
|
OS.flush();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Decodes a string according to percent-encoding.
|
2018-10-20 23:30:37 +08:00
|
|
|
std::string percentDecode(StringRef Content) {
|
2018-01-22 19:48:20 +08:00
|
|
|
std::string Result;
|
|
|
|
for (auto I = Content.begin(), E = Content.end(); I != E; ++I) {
|
|
|
|
if (*I != '%') {
|
|
|
|
Result += *I;
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-20 23:30:37 +08:00
|
|
|
if (*I == '%' && I + 2 < Content.end() && isHexDigit(*(I + 1)) &&
|
|
|
|
isHexDigit(*(I + 2))) {
|
|
|
|
Result.push_back(hexFromNibbles(*(I + 1), *(I + 2)));
|
2018-01-22 19:48:20 +08:00
|
|
|
I += 2;
|
|
|
|
} else
|
|
|
|
Result.push_back(*I);
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
bool isValidScheme(StringRef Scheme) {
|
2018-09-25 18:47:46 +08:00
|
|
|
if (Scheme.empty())
|
|
|
|
return false;
|
2018-10-20 23:30:37 +08:00
|
|
|
if (!isAlpha(Scheme[0]))
|
2018-09-25 18:47:46 +08:00
|
|
|
return false;
|
|
|
|
return std::all_of(Scheme.begin() + 1, Scheme.end(), [](char C) {
|
2018-10-20 23:30:37 +08:00
|
|
|
return isAlnum(C) || C == '+' || C == '.' || C == '-';
|
2018-09-25 18:47:46 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-22 19:48:20 +08:00
|
|
|
} // namespace
|
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
URI::URI(StringRef Scheme, StringRef Authority, StringRef Body)
|
2018-01-29 23:37:46 +08:00
|
|
|
: Scheme(Scheme), Authority(Authority), Body(Body) {
|
|
|
|
assert(!Scheme.empty());
|
|
|
|
assert((Authority.empty() || Body.startswith("/")) &&
|
|
|
|
"URI body must start with '/' when authority is present.");
|
2018-01-22 19:48:20 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 23:37:46 +08:00
|
|
|
std::string URI::toString() const {
|
2018-01-22 19:48:20 +08:00
|
|
|
std::string Result;
|
2018-10-20 23:30:37 +08:00
|
|
|
raw_string_ostream OS(Result);
|
2018-01-22 19:48:20 +08:00
|
|
|
OS << percentEncode(Scheme) << ":";
|
|
|
|
if (Authority.empty() && Body.empty())
|
|
|
|
return OS.str();
|
|
|
|
// If authority if empty, we only print body if it starts with "/"; otherwise,
|
|
|
|
// the URI is invalid.
|
2018-10-20 23:30:37 +08:00
|
|
|
if (!Authority.empty() || StringRef(Body).startswith("/"))
|
2018-01-22 19:48:20 +08:00
|
|
|
OS << "//" << percentEncode(Authority);
|
|
|
|
OS << percentEncode(Body);
|
|
|
|
OS.flush();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
Expected<URI> URI::parse(StringRef OrigUri) {
|
2018-01-29 23:37:46 +08:00
|
|
|
URI U;
|
2018-10-20 23:30:37 +08:00
|
|
|
StringRef Uri = OrigUri;
|
2018-01-22 19:48:20 +08:00
|
|
|
|
|
|
|
auto Pos = Uri.find(':');
|
2018-10-20 23:30:37 +08:00
|
|
|
if (Pos == StringRef::npos)
|
2018-01-22 19:48:20 +08:00
|
|
|
return make_string_error("Scheme must be provided in URI: " + OrigUri);
|
2018-09-25 18:47:46 +08:00
|
|
|
auto SchemeStr = Uri.substr(0, Pos);
|
|
|
|
U.Scheme = percentDecode(SchemeStr);
|
|
|
|
if (!isValidScheme(U.Scheme))
|
2018-10-20 23:30:37 +08:00
|
|
|
return make_string_error(
|
|
|
|
formatv("Invalid scheme: {0} (decoded: {1})", SchemeStr, U.Scheme));
|
2018-01-22 19:48:20 +08:00
|
|
|
Uri = Uri.substr(Pos + 1);
|
|
|
|
if (Uri.consume_front("//")) {
|
|
|
|
Pos = Uri.find('/');
|
|
|
|
U.Authority = percentDecode(Uri.substr(0, Pos));
|
|
|
|
Uri = Uri.substr(Pos);
|
|
|
|
}
|
|
|
|
U.Body = percentDecode(Uri);
|
|
|
|
return U;
|
|
|
|
}
|
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
Expected<URI> URI::create(StringRef AbsolutePath, StringRef Scheme) {
|
|
|
|
if (!sys::path::is_absolute(AbsolutePath))
|
2018-01-22 19:48:20 +08:00
|
|
|
return make_string_error("Not a valid absolute path: " + AbsolutePath);
|
|
|
|
auto S = findSchemeByName(Scheme);
|
|
|
|
if (!S)
|
|
|
|
return S.takeError();
|
|
|
|
return S->get()->uriFromAbsolutePath(AbsolutePath);
|
|
|
|
}
|
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
Expected<URI> URI::create(StringRef AbsolutePath,
|
|
|
|
const std::vector<std::string> &Schemes) {
|
|
|
|
if (!sys::path::is_absolute(AbsolutePath))
|
2018-09-06 20:54:43 +08:00
|
|
|
return make_string_error("Not a valid absolute path: " + AbsolutePath);
|
|
|
|
for (const auto &Scheme : Schemes) {
|
|
|
|
auto URI = URI::create(AbsolutePath, Scheme);
|
|
|
|
// For some paths, conversion to different URI schemes is impossible. These
|
|
|
|
// should be just skipped.
|
|
|
|
if (!URI) {
|
|
|
|
// Ignore the error.
|
2018-10-20 23:30:37 +08:00
|
|
|
consumeError(URI.takeError());
|
2018-09-06 20:54:43 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return URI;
|
|
|
|
}
|
2018-10-20 23:30:37 +08:00
|
|
|
return make_string_error("Couldn't convert " + AbsolutePath +
|
|
|
|
" to any given scheme: " + join(Schemes, ", "));
|
2018-09-06 20:54:43 +08:00
|
|
|
}
|
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
URI URI::createFile(StringRef AbsolutePath) {
|
2018-01-29 23:37:46 +08:00
|
|
|
auto U = create(AbsolutePath, "file");
|
|
|
|
if (!U)
|
|
|
|
llvm_unreachable(llvm::toString(U.takeError()).c_str());
|
|
|
|
return std::move(*U);
|
|
|
|
}
|
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
Expected<std::string> URI::resolve(const URI &Uri, StringRef HintPath) {
|
2018-01-22 19:48:20 +08:00
|
|
|
auto S = findSchemeByName(Uri.Scheme);
|
|
|
|
if (!S)
|
|
|
|
return S.takeError();
|
|
|
|
return S->get()->getAbsolutePath(Uri.Authority, Uri.Body, HintPath);
|
|
|
|
}
|
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
Expected<std::string> URI::includeSpelling(const URI &Uri) {
|
2018-04-09 23:09:44 +08:00
|
|
|
auto S = findSchemeByName(Uri.Scheme);
|
|
|
|
if (!S)
|
|
|
|
return S.takeError();
|
|
|
|
return S->get()->getIncludeSpelling(Uri);
|
|
|
|
}
|
|
|
|
|
2018-01-22 19:48:20 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|