blob: eb5a83882b2bd5108d8b3630b4f8d8ec20091e97 [file] [log] [blame]
Sam McCalldc8f3cf2018-10-17 07:32:05 +00001//===--- JSONTransport.cpp - sending and receiving LSP messages over JSON -===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Sam McCalldc8f3cf2018-10-17 07:32:05 +00006//
7//===----------------------------------------------------------------------===//
Sam McCalldc8f3cf2018-10-17 07:32:05 +00008#include "Protocol.h" // For LSPError
9#include "Transport.h"
Sam McCallad97ccf2020-04-28 17:49:17 +020010#include "support/Cancellation.h"
11#include "support/Logger.h"
12#include "support/Shutdown.h"
Sam McCalldc8f3cf2018-10-17 07:32:05 +000013#include "llvm/Support/Errno.h"
Sam McCall19ac0eaf2019-11-25 19:51:07 +010014#include "llvm/Support/Error.h"
Sam McCall687e1d72020-09-14 11:33:12 +020015#include <system_error>
Sam McCalldc8f3cf2018-10-17 07:32:05 +000016
Sam McCalldc8f3cf2018-10-17 07:32:05 +000017namespace clang {
18namespace clangd {
19namespace {
20
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000021llvm::json::Object encodeError(llvm::Error E) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000022 std::string Message;
23 ErrorCode Code = ErrorCode::UnknownErrorCode;
Sam McCallc627b122020-03-04 14:04:17 +010024 // FIXME: encode cancellation errors using RequestCancelled or ContentModified
25 // as appropriate.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000026 if (llvm::Error Unhandled = llvm::handleErrors(
Sam McCall31db1e02020-04-11 18:19:50 +020027 std::move(E),
28 [&](const CancelledError &C) -> llvm::Error {
29 switch (C.Reason) {
30 case static_cast<int>(ErrorCode::ContentModified):
31 Code = ErrorCode::ContentModified;
32 Message = "Request cancelled because the document was modified";
33 break;
34 default:
35 Code = ErrorCode::RequestCancelled;
36 Message = "Request cancelled";
37 break;
38 }
39 return llvm::Error::success();
40 },
41 [&](const LSPError &L) -> llvm::Error {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000042 Message = L.Message;
43 Code = L.Code;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000044 return llvm::Error::success();
Sam McCalldc8f3cf2018-10-17 07:32:05 +000045 }))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000046 Message = llvm::toString(std::move(Unhandled));
Sam McCalldc8f3cf2018-10-17 07:32:05 +000047
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000048 return llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000049 {"message", std::move(Message)},
50 {"code", int64_t(Code)},
51 };
52}
53
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000054llvm::Error decodeError(const llvm::json::Object &O) {
Sam McCall30667c92020-07-08 21:49:38 +020055 llvm::StringRef Msg = O.getString("message").getValueOr("Unspecified error");
Sam McCalldc8f3cf2018-10-17 07:32:05 +000056 if (auto Code = O.getInteger("code"))
Sam McCall30667c92020-07-08 21:49:38 +020057 return llvm::make_error<LSPError>(Msg.str(), ErrorCode(*Code));
58 return error(Msg.str());
Sam McCalldc8f3cf2018-10-17 07:32:05 +000059}
60
61class JSONTransport : public Transport {
62public:
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000063 JSONTransport(std::FILE *In, llvm::raw_ostream &Out,
64 llvm::raw_ostream *InMirror, bool Pretty, JSONStreamStyle Style)
65 : In(In), Out(Out), InMirror(InMirror ? *InMirror : llvm::nulls()),
Sam McCalldc8f3cf2018-10-17 07:32:05 +000066 Pretty(Pretty), Style(Style) {}
67
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000068 void notify(llvm::StringRef Method, llvm::json::Value Params) override {
69 sendMessage(llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000070 {"jsonrpc", "2.0"},
71 {"method", Method},
72 {"params", std::move(Params)},
73 });
74 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000075 void call(llvm::StringRef Method, llvm::json::Value Params,
76 llvm::json::Value ID) override {
77 sendMessage(llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000078 {"jsonrpc", "2.0"},
79 {"id", std::move(ID)},
80 {"method", Method},
81 {"params", std::move(Params)},
82 });
83 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000084 void reply(llvm::json::Value ID,
85 llvm::Expected<llvm::json::Value> Result) override {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000086 if (Result) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000087 sendMessage(llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000088 {"jsonrpc", "2.0"},
89 {"id", std::move(ID)},
90 {"result", std::move(*Result)},
91 });
92 } else {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000093 sendMessage(llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000094 {"jsonrpc", "2.0"},
95 {"id", std::move(ID)},
96 {"error", encodeError(Result.takeError())},
97 });
98 }
99 }
100
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000101 llvm::Error loop(MessageHandler &Handler) override {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000102 while (!feof(In)) {
Sam McCall19ac0eaf2019-11-25 19:51:07 +0100103 if (shutdownRequested())
Sam McCall687e1d72020-09-14 11:33:12 +0200104 return error(std::make_error_code(std::errc::operation_canceled),
105 "Got signal, shutting down");
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000106 if (ferror(In))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000107 return llvm::errorCodeToError(
108 std::error_code(errno, std::system_category()));
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000109 if (auto JSON = readRawMessage()) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000110 if (auto Doc = llvm::json::parse(*JSON)) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000111 vlog(Pretty ? "<<< {0:2}\n" : "<<< {0}\n", *Doc);
112 if (!handleMessage(std::move(*Doc), Handler))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000113 return llvm::Error::success(); // we saw the "exit" notification.
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000114 } else {
115 // Parse error. Log the raw message.
116 vlog("<<< {0}\n", *JSON);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000117 elog("JSON parse error: {0}", llvm::toString(Doc.takeError()));
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000118 }
119 }
120 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000121 return llvm::errorCodeToError(std::make_error_code(std::errc::io_error));
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000122 }
123
124private:
125 // Dispatches incoming message to Handler onNotify/onCall/onReply.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000126 bool handleMessage(llvm::json::Value Message, MessageHandler &Handler);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000127 // Writes outgoing message to Out stream.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000128 void sendMessage(llvm::json::Value Message) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000129 std::string S;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000130 llvm::raw_string_ostream OS(S);
131 OS << llvm::formatv(Pretty ? "{0:2}" : "{0}", Message);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000132 OS.flush();
133 Out << "Content-Length: " << S.size() << "\r\n\r\n" << S;
134 Out.flush();
135 vlog(">>> {0}\n", S);
136 }
137
138 // Read raw string messages from input stream.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000139 llvm::Optional<std::string> readRawMessage() {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000140 return Style == JSONStreamStyle::Delimited ? readDelimitedMessage()
141 : readStandardMessage();
142 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000143 llvm::Optional<std::string> readDelimitedMessage();
144 llvm::Optional<std::string> readStandardMessage();
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000145
146 std::FILE *In;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000147 llvm::raw_ostream &Out;
148 llvm::raw_ostream &InMirror;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000149 bool Pretty;
150 JSONStreamStyle Style;
151};
152
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000153bool JSONTransport::handleMessage(llvm::json::Value Message,
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000154 MessageHandler &Handler) {
155 // Message must be an object with "jsonrpc":"2.0".
156 auto *Object = Message.getAsObject();
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000157 if (!Object ||
158 Object->getString("jsonrpc") != llvm::Optional<llvm::StringRef>("2.0")) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000159 elog("Not a JSON-RPC 2.0 message: {0:2}", Message);
160 return false;
161 }
162 // ID may be any JSON value. If absent, this is a notification.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000163 llvm::Optional<llvm::json::Value> ID;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000164 if (auto *I = Object->get("id"))
165 ID = std::move(*I);
166 auto Method = Object->getString("method");
167 if (!Method) { // This is a response.
168 if (!ID) {
169 elog("No method and no response ID: {0:2}", Message);
170 return false;
171 }
172 if (auto *Err = Object->getObject("error"))
173 return Handler.onReply(std::move(*ID), decodeError(*Err));
174 // Result should be given, use null if not.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000175 llvm::json::Value Result = nullptr;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000176 if (auto *R = Object->get("result"))
177 Result = std::move(*R);
178 return Handler.onReply(std::move(*ID), std::move(Result));
179 }
180 // Params should be given, use null if not.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000181 llvm::json::Value Params = nullptr;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000182 if (auto *P = Object->get("params"))
183 Params = std::move(*P);
184
185 if (ID)
186 return Handler.onCall(*Method, std::move(Params), std::move(*ID));
187 else
188 return Handler.onNotify(*Method, std::move(Params));
189}
190
191// Tries to read a line up to and including \n.
Sam McCall19ac0eaf2019-11-25 19:51:07 +0100192// If failing, feof(), ferror(), or shutdownRequested() will be set.
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000193bool readLine(std::FILE *In, std::string &Out) {
194 static constexpr int BufSize = 1024;
195 size_t Size = 0;
196 Out.clear();
197 for (;;) {
198 Out.resize(Size + BufSize);
199 // Handle EINTR which is sent when a debugger attaches on some platforms.
Sam McCall19ac0eaf2019-11-25 19:51:07 +0100200 if (!retryAfterSignalUnlessShutdown(
201 nullptr, [&] { return std::fgets(&Out[Size], BufSize, In); }))
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000202 return false;
203 clearerr(In);
204 // If the line contained null bytes, anything after it (including \n) will
205 // be ignored. Fortunately this is not a legal header or JSON.
206 size_t Read = std::strlen(&Out[Size]);
207 if (Read > 0 && Out[Size + Read - 1] == '\n') {
208 Out.resize(Size + Read);
209 return true;
210 }
211 Size += Read;
212 }
213}
214
215// Returns None when:
Sam McCall19ac0eaf2019-11-25 19:51:07 +0100216// - ferror(), feof(), or shutdownRequested() are set.
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000217// - Content-Length is missing or empty (protocol error)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000218llvm::Optional<std::string> JSONTransport::readStandardMessage() {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000219 // A Language Server Protocol message starts with a set of HTTP headers,
220 // delimited by \r\n, and terminated by an empty line (\r\n).
221 unsigned long long ContentLength = 0;
222 std::string Line;
223 while (true) {
224 if (feof(In) || ferror(In) || !readLine(In, Line))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000225 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000226 InMirror << Line;
227
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000228 llvm::StringRef LineRef(Line);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000229
230 // We allow comments in headers. Technically this isn't part
231
232 // of the LSP specification, but makes writing tests easier.
233 if (LineRef.startswith("#"))
234 continue;
235
236 // Content-Length is a mandatory header, and the only one we handle.
237 if (LineRef.consume_front("Content-Length: ")) {
238 if (ContentLength != 0) {
239 elog("Warning: Duplicate Content-Length header received. "
240 "The previous value for this message ({0}) was ignored.",
241 ContentLength);
242 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000243 llvm::getAsUnsignedInteger(LineRef.trim(), 0, ContentLength);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000244 continue;
245 } else if (!LineRef.trim().empty()) {
246 // It's another header, ignore it.
247 continue;
248 } else {
249 // An empty line indicates the end of headers.
250 // Go ahead and read the JSON.
251 break;
252 }
253 }
254
255 // The fuzzer likes crashing us by sending "Content-Length: 9999999999999999"
256 if (ContentLength > 1 << 30) { // 1024M
257 elog("Refusing to read message with long Content-Length: {0}. "
258 "Expect protocol errors",
259 ContentLength);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000260 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000261 }
262 if (ContentLength == 0) {
263 log("Warning: Missing Content-Length header, or zero-length message.");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000264 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000265 }
266
267 std::string JSON(ContentLength, '\0');
268 for (size_t Pos = 0, Read; Pos < ContentLength; Pos += Read) {
269 // Handle EINTR which is sent when a debugger attaches on some platforms.
Sam McCall19ac0eaf2019-11-25 19:51:07 +0100270 Read = retryAfterSignalUnlessShutdown(0, [&]{
271 return std::fread(&JSON[Pos], 1, ContentLength - Pos, In);
272 });
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000273 if (Read == 0) {
274 elog("Input was aborted. Read only {0} bytes of expected {1}.", Pos,
275 ContentLength);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000276 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000277 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000278 InMirror << llvm::StringRef(&JSON[Pos], Read);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000279 clearerr(In); // If we're done, the error was transient. If we're not done,
280 // either it was transient or we'll see it again on retry.
281 Pos += Read;
282 }
283 return std::move(JSON);
284}
285
286// For lit tests we support a simplified syntax:
287// - messages are delimited by '---' on a line by itself
288// - lines starting with # are ignored.
289// This is a testing path, so favor simplicity over performance here.
Sam McCall19ac0eaf2019-11-25 19:51:07 +0100290// When returning None, feof(), ferror(), or shutdownRequested() will be set.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000291llvm::Optional<std::string> JSONTransport::readDelimitedMessage() {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000292 std::string JSON;
293 std::string Line;
294 while (readLine(In, Line)) {
295 InMirror << Line;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000296 auto LineRef = llvm::StringRef(Line).trim();
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000297 if (LineRef.startswith("#")) // comment
298 continue;
299
300 // found a delimiter
301 if (LineRef.rtrim() == "---")
302 break;
303
304 JSON += Line;
305 }
306
Sam McCall19ac0eaf2019-11-25 19:51:07 +0100307 if (shutdownRequested())
308 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000309 if (ferror(In)) {
310 elog("Input error while reading message!");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000311 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000312 }
313 return std::move(JSON); // Including at EOF
314}
315
316} // namespace
317
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000318std::unique_ptr<Transport> newJSONTransport(std::FILE *In,
319 llvm::raw_ostream &Out,
320 llvm::raw_ostream *InMirror,
321 bool Pretty,
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000322 JSONStreamStyle Style) {
Jonas Devlieghere1c705d92019-08-14 23:52:23 +0000323 return std::make_unique<JSONTransport>(In, Out, InMirror, Pretty, Style);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000324}
325
326} // namespace clangd
327} // namespace clang