2017-02-07 18:28:20 +08:00
|
|
|
//===--- JSONRPCDispatcher.cpp - Main JSON parser entry point -------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "JSONRPCDispatcher.h"
|
|
|
|
#include "ProtocolHandlers.h"
|
2017-11-02 17:21:51 +08:00
|
|
|
#include "Trace.h"
|
2017-02-07 18:28:20 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/YAMLParser.h"
|
2017-05-16 23:23:55 +08:00
|
|
|
#include <istream>
|
|
|
|
|
2017-02-07 18:28:20 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace clangd;
|
|
|
|
|
2017-02-10 22:08:40 +08:00
|
|
|
void JSONOutput::writeMessage(const Twine &Message) {
|
2017-02-07 18:28:20 +08:00
|
|
|
llvm::SmallString<128> Storage;
|
|
|
|
StringRef M = Message.toStringRef(Storage);
|
|
|
|
|
2017-02-10 22:08:40 +08:00
|
|
|
std::lock_guard<std::mutex> Guard(StreamMutex);
|
2017-02-07 18:28:20 +08:00
|
|
|
// Log without headers.
|
|
|
|
Logs << "--> " << M << '\n';
|
|
|
|
Logs.flush();
|
|
|
|
|
|
|
|
// Emit message with header.
|
|
|
|
Outs << "Content-Length: " << M.size() << "\r\n\r\n" << M;
|
|
|
|
Outs.flush();
|
|
|
|
}
|
|
|
|
|
2017-02-16 00:44:11 +08:00
|
|
|
void JSONOutput::log(const Twine &Message) {
|
2017-11-02 17:21:51 +08:00
|
|
|
trace::log(Message);
|
2017-02-16 00:44:11 +08:00
|
|
|
std::lock_guard<std::mutex> Guard(StreamMutex);
|
|
|
|
Logs << Message;
|
|
|
|
Logs.flush();
|
|
|
|
}
|
|
|
|
|
2017-10-10 17:08:47 +08:00
|
|
|
void JSONOutput::mirrorInput(const Twine &Message) {
|
|
|
|
if (!InputMirror)
|
|
|
|
return;
|
|
|
|
|
|
|
|
*InputMirror << Message;
|
|
|
|
InputMirror->flush();
|
|
|
|
}
|
|
|
|
|
2017-10-12 21:29:58 +08:00
|
|
|
void RequestContext::reply(const llvm::Twine &Result) {
|
|
|
|
if (ID.empty()) {
|
|
|
|
Out.log("Attempted to reply to a notification!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Out.writeMessage(llvm::Twine(R"({"jsonrpc":"2.0","id":)") + ID +
|
|
|
|
R"(,"result":)" + Result + "}");
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
|
2017-10-12 21:29:58 +08:00
|
|
|
void RequestContext::replyError(int code, const llvm::StringRef &Message) {
|
|
|
|
Out.log("Error " + llvm::Twine(code) + ": " + Message + "\n");
|
|
|
|
if (!ID.empty()) {
|
|
|
|
Out.writeMessage(llvm::Twine(R"({"jsonrpc":"2.0","id":)") + ID +
|
|
|
|
R"(,"error":{"code":)" + llvm::Twine(code) +
|
|
|
|
R"(,"message":")" + llvm::yaml::escape(Message) +
|
|
|
|
R"("}})");
|
|
|
|
}
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
|
2017-10-12 21:29:58 +08:00
|
|
|
void JSONRPCDispatcher::registerHandler(StringRef Method, Handler H) {
|
2017-02-07 18:28:20 +08:00
|
|
|
assert(!Handlers.count(Method) && "Handler already registered!");
|
|
|
|
Handlers[Method] = std::move(H);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-10-12 21:29:58 +08:00
|
|
|
callHandler(const llvm::StringMap<JSONRPCDispatcher::Handler> &Handlers,
|
2017-02-07 18:28:20 +08:00
|
|
|
llvm::yaml::ScalarNode *Method, llvm::yaml::ScalarNode *Id,
|
2017-10-12 21:29:58 +08:00
|
|
|
llvm::yaml::MappingNode *Params,
|
|
|
|
const JSONRPCDispatcher::Handler &UnknownHandler, JSONOutput &Out) {
|
|
|
|
llvm::SmallString<64> MethodStorage;
|
2017-11-02 17:21:51 +08:00
|
|
|
llvm::StringRef MethodStr = Method->getValue(MethodStorage);
|
|
|
|
auto I = Handlers.find(MethodStr);
|
2017-10-12 21:29:58 +08:00
|
|
|
auto &Handler = I != Handlers.end() ? I->second : UnknownHandler;
|
2017-11-02 17:21:51 +08:00
|
|
|
trace::Span Tracer(MethodStr);
|
2017-10-12 21:29:58 +08:00
|
|
|
Handler(RequestContext(Out, Id ? Id->getRawValue() : ""), Params);
|
2017-02-07 18:28:20 +08:00
|
|
|
}
|
|
|
|
|
2017-10-12 21:29:58 +08:00
|
|
|
bool JSONRPCDispatcher::call(StringRef Content, JSONOutput &Out) const {
|
2017-02-07 18:28:20 +08:00
|
|
|
llvm::SourceMgr SM;
|
|
|
|
llvm::yaml::Stream YAMLStream(Content, SM);
|
|
|
|
|
|
|
|
auto Doc = YAMLStream.begin();
|
|
|
|
if (Doc == YAMLStream.end())
|
|
|
|
return false;
|
|
|
|
|
2017-10-28 00:33:15 +08:00
|
|
|
auto *Object = dyn_cast_or_null<llvm::yaml::MappingNode>(Doc->getRoot());
|
2017-02-07 18:28:20 +08:00
|
|
|
if (!Object)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
llvm::yaml::ScalarNode *Version = nullptr;
|
|
|
|
llvm::yaml::ScalarNode *Method = nullptr;
|
|
|
|
llvm::yaml::MappingNode *Params = nullptr;
|
|
|
|
llvm::yaml::ScalarNode *Id = nullptr;
|
|
|
|
for (auto &NextKeyValue : *Object) {
|
2017-10-28 00:33:15 +08:00
|
|
|
auto *KeyString =
|
|
|
|
dyn_cast_or_null<llvm::yaml::ScalarNode>(NextKeyValue.getKey());
|
2017-02-07 18:28:20 +08:00
|
|
|
if (!KeyString)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
llvm::SmallString<10> KeyStorage;
|
|
|
|
StringRef KeyValue = KeyString->getValue(KeyStorage);
|
|
|
|
llvm::yaml::Node *Value = NextKeyValue.getValue();
|
|
|
|
if (!Value)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (KeyValue == "jsonrpc") {
|
|
|
|
// This should be "2.0". Always.
|
|
|
|
Version = dyn_cast<llvm::yaml::ScalarNode>(Value);
|
|
|
|
if (!Version || Version->getRawValue() != "\"2.0\"")
|
|
|
|
return false;
|
|
|
|
} else if (KeyValue == "method") {
|
|
|
|
Method = dyn_cast<llvm::yaml::ScalarNode>(Value);
|
|
|
|
} else if (KeyValue == "id") {
|
|
|
|
Id = dyn_cast<llvm::yaml::ScalarNode>(Value);
|
|
|
|
} else if (KeyValue == "params") {
|
|
|
|
if (!Method)
|
|
|
|
return false;
|
|
|
|
// We have to interleave the call of the function here, otherwise the
|
|
|
|
// YAMLParser will die because it can't go backwards. This is unfortunate
|
|
|
|
// because it will break clients that put the id after params. A possible
|
|
|
|
// fix would be to split the parsing and execution phases.
|
|
|
|
Params = dyn_cast<llvm::yaml::MappingNode>(Value);
|
2017-10-12 21:29:58 +08:00
|
|
|
callHandler(Handlers, Method, Id, Params, UnknownHandler, Out);
|
2017-02-07 18:28:20 +08:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// In case there was a request with no params, call the handler on the
|
|
|
|
// leftovers.
|
|
|
|
if (!Method)
|
|
|
|
return false;
|
2017-10-12 21:29:58 +08:00
|
|
|
callHandler(Handlers, Method, Id, nullptr, UnknownHandler, Out);
|
2017-02-07 18:28:20 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-05-16 22:40:30 +08:00
|
|
|
|
|
|
|
void clangd::runLanguageServerLoop(std::istream &In, JSONOutput &Out,
|
|
|
|
JSONRPCDispatcher &Dispatcher,
|
|
|
|
bool &IsDone) {
|
|
|
|
while (In.good()) {
|
2017-09-04 20:28:15 +08:00
|
|
|
// A Language Server Protocol message starts with a set of HTTP headers,
|
|
|
|
// delimited by \r\n, and terminated by an empty line (\r\n).
|
|
|
|
unsigned long long ContentLength = 0;
|
|
|
|
while (In.good()) {
|
|
|
|
std::string Line;
|
|
|
|
std::getline(In, Line);
|
|
|
|
if (!In.good() && errno == EINTR) {
|
|
|
|
In.clear();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-10 17:08:47 +08:00
|
|
|
Out.mirrorInput(Line);
|
|
|
|
// Mirror '\n' that gets consumed by std::getline, but is not included in
|
|
|
|
// the resulting Line.
|
|
|
|
// Note that '\r' is part of Line, so we don't need to mirror it
|
|
|
|
// separately.
|
|
|
|
if (!In.eof())
|
|
|
|
Out.mirrorInput("\n");
|
|
|
|
|
2017-09-04 20:28:15 +08:00
|
|
|
llvm::StringRef LineRef(Line);
|
|
|
|
|
|
|
|
// We allow YAML-style comments in headers. Technically this isn't part
|
|
|
|
// of the LSP specification, but makes writing tests easier.
|
|
|
|
if (LineRef.startswith("#"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Content-Type is a specified header, but does nothing.
|
|
|
|
// Content-Length is a mandatory header. It specifies the length of the
|
|
|
|
// following JSON.
|
|
|
|
// It is unspecified what sequence headers must be supplied in, so we
|
|
|
|
// allow any sequence.
|
|
|
|
// The end of headers is signified by an empty line.
|
|
|
|
if (LineRef.consume_front("Content-Length: ")) {
|
|
|
|
if (ContentLength != 0) {
|
|
|
|
Out.log("Warning: Duplicate Content-Length header received. "
|
2017-10-10 17:08:47 +08:00
|
|
|
"The previous value for this message (" +
|
|
|
|
std::to_string(ContentLength) + ") was ignored.\n");
|
2017-09-04 20:28:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::getAsUnsignedInteger(LineRef.trim(), 0, ContentLength);
|
|
|
|
continue;
|
|
|
|
} else if (!LineRef.trim().empty()) {
|
|
|
|
// It's another header, ignore it.
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
// An empty line indicates the end of headers.
|
|
|
|
// Go ahead and read the JSON.
|
|
|
|
break;
|
|
|
|
}
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
|
2017-10-28 01:06:41 +08:00
|
|
|
// Guard against large messages. This is usually a bug in the client code
|
|
|
|
// and we don't want to crash downstream because of it.
|
|
|
|
if (ContentLength > 1 << 30) { // 1024M
|
|
|
|
In.ignore(ContentLength);
|
|
|
|
Out.log("Skipped overly large message of " + Twine(ContentLength) +
|
|
|
|
" bytes.\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-09-04 20:28:15 +08:00
|
|
|
if (ContentLength > 0) {
|
|
|
|
std::vector<char> JSON(ContentLength + 1, '\0');
|
2017-11-02 17:21:51 +08:00
|
|
|
llvm::StringRef JSONRef;
|
|
|
|
{
|
|
|
|
trace::Span Tracer("Reading request");
|
|
|
|
// Now read the JSON. Insert a trailing null byte as required by the
|
|
|
|
// YAML parser.
|
|
|
|
In.read(JSON.data(), ContentLength);
|
|
|
|
Out.mirrorInput(StringRef(JSON.data(), In.gcount()));
|
|
|
|
|
|
|
|
// If the stream is aborted before we read ContentLength bytes, In
|
|
|
|
// will have eofbit and failbit set.
|
|
|
|
if (!In) {
|
|
|
|
Out.log("Input was aborted. Read only " +
|
|
|
|
std::to_string(In.gcount()) + " bytes of expected " +
|
|
|
|
std::to_string(ContentLength) + ".\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONRef = StringRef(JSON.data(), ContentLength);
|
2017-09-04 20:28:15 +08:00
|
|
|
}
|
|
|
|
|
2017-05-16 22:40:30 +08:00
|
|
|
// Log the message.
|
|
|
|
Out.log("<-- " + JSONRef + "\n");
|
|
|
|
|
|
|
|
// Finally, execute the action for this JSON message.
|
2017-10-12 21:29:58 +08:00
|
|
|
if (!Dispatcher.call(JSONRef, Out))
|
2017-05-16 22:40:30 +08:00
|
|
|
Out.log("JSON dispatch failed!\n");
|
|
|
|
|
|
|
|
// If we're done, exit the loop.
|
|
|
|
if (IsDone)
|
|
|
|
break;
|
2017-09-04 20:28:15 +08:00
|
|
|
} else {
|
2017-10-10 17:08:47 +08:00
|
|
|
Out.log("Warning: Missing Content-Length header, or message has zero "
|
|
|
|
"length.\n");
|
2017-05-16 22:40:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|