1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
//===--- JSONTransport.cpp - sending and receiving LSP messages over JSON -===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/Tools/lsp-server-support/Transport.h"
#include "mlir/Tools/lsp-server-support/Logging.h"
#include "mlir/Tools/lsp-server-support/Protocol.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/Errno.h"
#include "llvm/Support/Error.h"
#include <optional>
#include <system_error>
#include <utility>
using namespace mlir;
using namespace mlir::lsp;
//===----------------------------------------------------------------------===//
// Reply
//===----------------------------------------------------------------------===//
namespace {
/// Function object to reply to an LSP call.
/// Each instance must be called exactly once, otherwise:
/// - if there was no reply, an error reply is sent
/// - if there were multiple replies, only the first is sent
class Reply {
public:
Reply(const llvm::json::Value &id, StringRef method, JSONTransport &transport,
std::mutex &transportOutputMutex);
Reply(Reply &&other);
Reply &operator=(Reply &&) = delete;
Reply(const Reply &) = delete;
Reply &operator=(const Reply &) = delete;
void operator()(llvm::Expected<llvm::json::Value> reply);
private:
StringRef method;
std::atomic<bool> replied = {false};
llvm::json::Value id;
JSONTransport *transport;
std::mutex &transportOutputMutex;
};
} // namespace
Reply::Reply(const llvm::json::Value &id, llvm::StringRef method,
JSONTransport &transport, std::mutex &transportOutputMutex)
: id(id), transport(&transport),
transportOutputMutex(transportOutputMutex) {}
Reply::Reply(Reply &&other)
: replied(other.replied.load()), id(std::move(other.id)),
transport(other.transport),
transportOutputMutex(other.transportOutputMutex) {
other.transport = nullptr;
}
void Reply::operator()(llvm::Expected<llvm::json::Value> reply) {
if (replied.exchange(true)) {
Logger::error("Replied twice to message {0}({1})", method, id);
assert(false && "must reply to each call only once!");
return;
}
assert(transport && "expected valid transport to reply to");
std::lock_guard<std::mutex> transportLock(transportOutputMutex);
if (reply) {
Logger::info("--> reply:{0}({1})", method, id);
transport->reply(std::move(id), std::move(reply));
} else {
llvm::Error error = reply.takeError();
Logger::info("--> reply:{0}({1})", method, id, error);
transport->reply(std::move(id), std::move(error));
}
}
//===----------------------------------------------------------------------===//
// MessageHandler
//===----------------------------------------------------------------------===//
bool MessageHandler::onNotify(llvm::StringRef method, llvm::json::Value value) {
Logger::info("--> {0}", method);
if (method == "exit")
return false;
if (method == "$cancel") {
// TODO: Add support for cancelling requests.
} else {
auto it = notificationHandlers.find(method);
if (it != notificationHandlers.end())
it->second(std::move(value));
}
return true;
}
bool MessageHandler::onCall(llvm::StringRef method, llvm::json::Value params,
llvm::json::Value id) {
Logger::info("--> {0}({1})", method, id);
Reply reply(id, method, transport, transportOutputMutex);
auto it = methodHandlers.find(method);
if (it != methodHandlers.end()) {
it->second(std::move(params), std::move(reply));
} else {
reply(llvm::make_error<LSPError>("method not found: " + method.str(),
ErrorCode::MethodNotFound));
}
return true;
}
bool MessageHandler::onReply(llvm::json::Value id,
llvm::Expected<llvm::json::Value> result) {
// TODO: Add support for reply callbacks when support for outgoing messages is
// added. For now, we just log an error on any replies received.
Callback<llvm::json::Value> replyHandler =
[&id](llvm::Expected<llvm::json::Value> result) {
Logger::error(
"received a reply with ID {0}, but there was no such call", id);
if (!result)
llvm::consumeError(result.takeError());
};
// Log and run the reply handler.
if (result)
replyHandler(std::move(result));
else
replyHandler(result.takeError());
return true;
}
//===----------------------------------------------------------------------===//
// JSONTransport
//===----------------------------------------------------------------------===//
/// Encode the given error as a JSON object.
static llvm::json::Object encodeError(llvm::Error error) {
std::string message;
ErrorCode code = ErrorCode::UnknownErrorCode;
auto handlerFn = [&](const LSPError &lspError) -> llvm::Error {
message = lspError.message;
code = lspError.code;
return llvm::Error::success();
};
if (llvm::Error unhandled = llvm::handleErrors(std::move(error), handlerFn))
message = llvm::toString(std::move(unhandled));
return llvm::json::Object{
{"message", std::move(message)},
{"code", int64_t(code)},
};
}
/// Decode the given JSON object into an error.
llvm::Error decodeError(const llvm::json::Object &o) {
StringRef msg = o.getString("message").value_or("Unspecified error");
if (std::optional<int64_t> code = o.getInteger("code"))
return llvm::make_error<LSPError>(msg.str(), ErrorCode(*code));
return llvm::make_error<llvm::StringError>(llvm::inconvertibleErrorCode(),
msg.str());
}
void JSONTransport::notify(StringRef method, llvm::json::Value params) {
sendMessage(llvm::json::Object{
{"jsonrpc", "2.0"},
{"method", method},
{"params", std::move(params)},
});
}
void JSONTransport::call(StringRef method, llvm::json::Value params,
llvm::json::Value id) {
sendMessage(llvm::json::Object{
{"jsonrpc", "2.0"},
{"id", std::move(id)},
{"method", method},
{"params", std::move(params)},
});
}
void JSONTransport::reply(llvm::json::Value id,
llvm::Expected<llvm::json::Value> result) {
if (result) {
return sendMessage(llvm::json::Object{
{"jsonrpc", "2.0"},
{"id", std::move(id)},
{"result", std::move(*result)},
});
}
sendMessage(llvm::json::Object{
{"jsonrpc", "2.0"},
{"id", std::move(id)},
{"error", encodeError(result.takeError())},
});
}
llvm::Error JSONTransport::run(MessageHandler &handler) {
std::string json;
while (!feof(in)) {
if (ferror(in)) {
return llvm::errorCodeToError(
std::error_code(errno, std::system_category()));
}
if (succeeded(readMessage(json))) {
if (llvm::Expected<llvm::json::Value> doc = llvm::json::parse(json)) {
if (!handleMessage(std::move(*doc), handler))
return llvm::Error::success();
} else {
Logger::error("JSON parse error: {0}", llvm::toString(doc.takeError()));
}
}
}
return llvm::errorCodeToError(std::make_error_code(std::errc::io_error));
}
void JSONTransport::sendMessage(llvm::json::Value msg) {
outputBuffer.clear();
llvm::raw_svector_ostream os(outputBuffer);
os << llvm::formatv(prettyOutput ? "{0:2}\n" : "{0}", msg);
out << "Content-Length: " << outputBuffer.size() << "\r\n\r\n"
<< outputBuffer;
out.flush();
Logger::debug(">>> {0}\n", outputBuffer);
}
bool JSONTransport::handleMessage(llvm::json::Value msg,
MessageHandler &handler) {
// Message must be an object with "jsonrpc":"2.0".
llvm::json::Object *object = msg.getAsObject();
if (!object ||
object->getString("jsonrpc") != std::optional<StringRef>("2.0"))
return false;
// `id` may be any JSON value. If absent, this is a notification.
std::optional<llvm::json::Value> id;
if (llvm::json::Value *i = object->get("id"))
id = std::move(*i);
std::optional<StringRef> method = object->getString("method");
// This is a response.
if (!method) {
if (!id)
return false;
if (auto *err = object->getObject("error"))
return handler.onReply(std::move(*id), decodeError(*err));
// result should be given, use null if not.
llvm::json::Value result = nullptr;
if (llvm::json::Value *r = object->get("result"))
result = std::move(*r);
return handler.onReply(std::move(*id), std::move(result));
}
// Params should be given, use null if not.
llvm::json::Value params = nullptr;
if (llvm::json::Value *p = object->get("params"))
params = std::move(*p);
if (id)
return handler.onCall(*method, std::move(params), std::move(*id));
return handler.onNotify(*method, std::move(params));
}
/// Tries to read a line up to and including \n.
/// If failing, feof(), ferror(), or shutdownRequested() will be set.
LogicalResult readLine(std::FILE *in, SmallVectorImpl<char> &out) {
// Big enough to hold any reasonable header line. May not fit content lines
// in delimited mode, but performance doesn't matter for that mode.
static constexpr int bufSize = 128;
size_t size = 0;
out.clear();
for (;;) {
out.resize_for_overwrite(size + bufSize);
if (!std::fgets(&out[size], bufSize, in))
return failure();
clearerr(in);
// If the line contained null bytes, anything after it (including \n) will
// be ignored. Fortunately this is not a legal header or JSON.
size_t read = std::strlen(&out[size]);
if (read > 0 && out[size + read - 1] == '\n') {
out.resize(size + read);
return success();
}
size += read;
}
}
// Returns std::nullopt when:
// - ferror(), feof(), or shutdownRequested() are set.
// - Content-Length is missing or empty (protocol error)
LogicalResult JSONTransport::readStandardMessage(std::string &json) {
// 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;
llvm::SmallString<128> line;
while (true) {
if (feof(in) || ferror(in) || failed(readLine(in, line)))
return failure();
// Content-Length is a mandatory header, and the only one we handle.
StringRef lineRef = line;
if (lineRef.consume_front("Content-Length: ")) {
llvm::getAsUnsignedInteger(lineRef.trim(), 0, contentLength);
} 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;
}
}
// The fuzzer likes crashing us by sending "Content-Length: 9999999999999999"
if (contentLength == 0 || contentLength > 1 << 30)
return failure();
json.resize(contentLength);
for (size_t pos = 0, read; pos < contentLength; pos += read) {
read = std::fread(&json[pos], 1, contentLength - pos, in);
if (read == 0)
return failure();
// If we're done, the error was transient. If we're not done, either it was
// transient or we'll see it again on retry.
clearerr(in);
pos += read;
}
return success();
}
/// For lit tests we support a simplified syntax:
/// - messages are delimited by '// -----' on a line by itself
/// - lines starting with // are ignored.
/// This is a testing path, so favor simplicity over performance here.
/// When returning failure: feof(), ferror(), or shutdownRequested() will be
/// set.
LogicalResult JSONTransport::readDelimitedMessage(std::string &json) {
json.clear();
llvm::SmallString<128> line;
while (succeeded(readLine(in, line))) {
StringRef lineRef = line.str().trim();
if (lineRef.startswith("//")) {
// Found a delimiter for the message.
if (lineRef == "// -----")
break;
continue;
}
json += line;
}
return failure(ferror(in));
}
|