blob: fa86baf6c581600eaca4a02f5bfa3a052034c5d2 [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 McCalldc8f3cf2018-10-17 07:32:05 +000015
Sam McCalldc8f3cf2018-10-17 07:32:05 +000016namespace clang {
17namespace clangd {
18namespace {
19
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000020llvm::json::Object encodeError(llvm::Error E) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000021 std::string Message;
22 ErrorCode Code = ErrorCode::UnknownErrorCode;
Sam McCallc627b122020-03-04 14:04:17 +010023 // FIXME: encode cancellation errors using RequestCancelled or ContentModified
24 // as appropriate.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000025 if (llvm::Error Unhandled = llvm::handleErrors(
Sam McCall31db1e02020-04-11 18:19:50 +020026 std::move(E),
27 [&](const CancelledError &C) -> llvm::Error {
28 switch (C.Reason) {
29 case static_cast<int>(ErrorCode::ContentModified):
30 Code = ErrorCode::ContentModified;
31 Message = "Request cancelled because the document was modified";
32 break;
33 default:
34 Code = ErrorCode::RequestCancelled;
35 Message = "Request cancelled";
36 break;
37 }
38 return llvm::Error::success();
39 },
40 [&](const LSPError &L) -> llvm::Error {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000041 Message = L.Message;
42 Code = L.Code;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000043 return llvm::Error::success();
Sam McCalldc8f3cf2018-10-17 07:32:05 +000044 }))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000045 Message = llvm::toString(std::move(Unhandled));
Sam McCalldc8f3cf2018-10-17 07:32:05 +000046
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000047 return llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000048 {"message", std::move(Message)},
49 {"code", int64_t(Code)},
50 };
51}
52
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000053llvm::Error decodeError(const llvm::json::Object &O) {
Benjamin Krameradcd0262020-01-28 20:23:46 +010054 std::string Msg =
55 std::string(O.getString("message").getValueOr("Unspecified error"));
Sam McCalldc8f3cf2018-10-17 07:32:05 +000056 if (auto Code = O.getInteger("code"))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000057 return llvm::make_error<LSPError>(std::move(Msg), ErrorCode(*Code));
58 return llvm::make_error<llvm::StringError>(std::move(Msg),
59 llvm::inconvertibleErrorCode());
Sam McCalldc8f3cf2018-10-17 07:32:05 +000060}
61
62class JSONTransport : public Transport {
63public:
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000064 JSONTransport(std::FILE *In, llvm::raw_ostream &Out,
65 llvm::raw_ostream *InMirror, bool Pretty, JSONStreamStyle Style)
66 : In(In), Out(Out), InMirror(InMirror ? *InMirror : llvm::nulls()),
Sam McCalldc8f3cf2018-10-17 07:32:05 +000067 Pretty(Pretty), Style(Style) {}
68
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000069 void notify(llvm::StringRef Method, llvm::json::Value Params) override {
70 sendMessage(llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000071 {"jsonrpc", "2.0"},
72 {"method", Method},
73 {"params", std::move(Params)},
74 });
75 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000076 void call(llvm::StringRef Method, llvm::json::Value Params,
77 llvm::json::Value ID) override {
78 sendMessage(llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000079 {"jsonrpc", "2.0"},
80 {"id", std::move(ID)},
81 {"method", Method},
82 {"params", std::move(Params)},
83 });
84 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000085 void reply(llvm::json::Value ID,
86 llvm::Expected<llvm::json::Value> Result) override {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000087 if (Result) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000088 sendMessage(llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000089 {"jsonrpc", "2.0"},
90 {"id", std::move(ID)},
91 {"result", std::move(*Result)},
92 });
93 } else {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000094 sendMessage(llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000095 {"jsonrpc", "2.0"},
96 {"id", std::move(ID)},
97 {"error", encodeError(Result.takeError())},
98 });
99 }
100 }
101
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000102 llvm::Error loop(MessageHandler &Handler) override {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000103 while (!feof(In)) {
Sam McCall19ac0eaf2019-11-25 19:51:07 +0100104 if (shutdownRequested())
105 return llvm::createStringError(
106 std::make_error_code(std::errc::operation_canceled),
107 "Got signal, shutting down");
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000108 if (ferror(In))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000109 return llvm::errorCodeToError(
110 std::error_code(errno, std::system_category()));
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000111 if (auto JSON = readRawMessage()) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000112 if (auto Doc = llvm::json::parse(*JSON)) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000113 vlog(Pretty ? "<<< {0:2}\n" : "<<< {0}\n", *Doc);
114 if (!handleMessage(std::move(*Doc), Handler))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000115 return llvm::Error::success(); // we saw the "exit" notification.
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000116 } else {
117 // Parse error. Log the raw message.
118 vlog("<<< {0}\n", *JSON);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000119 elog("JSON parse error: {0}", llvm::toString(Doc.takeError()));
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000120 }
121 }
122 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000123 return llvm::errorCodeToError(std::make_error_code(std::errc::io_error));
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000124 }
125
126private:
127 // Dispatches incoming message to Handler onNotify/onCall/onReply.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000128 bool handleMessage(llvm::json::Value Message, MessageHandler &Handler);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000129 // Writes outgoing message to Out stream.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000130 void sendMessage(llvm::json::Value Message) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000131 std::string S;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000132 llvm::raw_string_ostream OS(S);
133 OS << llvm::formatv(Pretty ? "{0:2}" : "{0}", Message);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000134 OS.flush();
135 Out << "Content-Length: " << S.size() << "\r\n\r\n" << S;
136 Out.flush();
137 vlog(">>> {0}\n", S);
138 }
139
140 // Read raw string messages from input stream.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000141 llvm::Optional<std::string> readRawMessage() {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000142 return Style == JSONStreamStyle::Delimited ? readDelimitedMessage()
143 : readStandardMessage();
144 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000145 llvm::Optional<std::string> readDelimitedMessage();
146 llvm::Optional<std::string> readStandardMessage();
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000147
148 std::FILE *In;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000149 llvm::raw_ostream &Out;
150 llvm::raw_ostream &InMirror;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000151 bool Pretty;
152 JSONStreamStyle Style;
153};
154
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000155bool JSONTransport::handleMessage(llvm::json::Value Message,
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000156 MessageHandler &Handler) {
157 // Message must be an object with "jsonrpc":"2.0".
158 auto *Object = Message.getAsObject();
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000159 if (!Object ||
160 Object->getString("jsonrpc") != llvm::Optional<llvm::StringRef>("2.0")) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000161 elog("Not a JSON-RPC 2.0 message: {0:2}", Message);
162 return false;
163 }
164 // ID may be any JSON value. If absent, this is a notification.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000165 llvm::Optional<llvm::json::Value> ID;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000166 if (auto *I = Object->get("id"))
167 ID = std::move(*I);
168 auto Method = Object->getString("method");
169 if (!Method) { // This is a response.
170 if (!ID) {
171 elog("No method and no response ID: {0:2}", Message);
172 return false;
173 }
174 if (auto *Err = Object->getObject("error"))
175 return Handler.onReply(std::move(*ID), decodeError(*Err));
176 // Result should be given, use null if not.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000177 llvm::json::Value Result = nullptr;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000178 if (auto *R = Object->get("result"))
179 Result = std::move(*R);
180 return Handler.onReply(std::move(*ID), std::move(Result));
181 }
182 // Params should be given, use null if not.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000183 llvm::json::Value Params = nullptr;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000184 if (auto *P = Object->get("params"))
185 Params = std::move(*P);
186
187 if (ID)
188 return Handler.onCall(*Method, std::move(Params), std::move(*ID));
189 else
190 return Handler.onNotify(*Method, std::move(Params));
191}
192
193// Tries to read a line up to and including \n.
Sam McCall19ac0eaf2019-11-25 19:51:07 +0100194// If failing, feof(), ferror(), or shutdownRequested() will be set.
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000195bool readLine(std::FILE *In, std::string &Out) {
196 static constexpr int BufSize = 1024;
197 size_t Size = 0;
198 Out.clear();
199 for (;;) {
200 Out.resize(Size + BufSize);
201 // Handle EINTR which is sent when a debugger attaches on some platforms.
Sam McCall19ac0eaf2019-11-25 19:51:07 +0100202 if (!retryAfterSignalUnlessShutdown(
203 nullptr, [&] { return std::fgets(&Out[Size], BufSize, In); }))
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000204 return false;
205 clearerr(In);
206 // If the line contained null bytes, anything after it (including \n) will
207 // be ignored. Fortunately this is not a legal header or JSON.
208 size_t Read = std::strlen(&Out[Size]);
209 if (Read > 0 && Out[Size + Read - 1] == '\n') {
210 Out.resize(Size + Read);
211 return true;
212 }
213 Size += Read;
214 }
215}
216
217// Returns None when:
Sam McCall19ac0eaf2019-11-25 19:51:07 +0100218// - ferror(), feof(), or shutdownRequested() are set.
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000219// - Content-Length is missing or empty (protocol error)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000220llvm::Optional<std::string> JSONTransport::readStandardMessage() {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000221 // A Language Server Protocol message starts with a set of HTTP headers,
222 // delimited by \r\n, and terminated by an empty line (\r\n).
223 unsigned long long ContentLength = 0;
224 std::string Line;
225 while (true) {
226 if (feof(In) || ferror(In) || !readLine(In, Line))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000227 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000228 InMirror << Line;
229
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000230 llvm::StringRef LineRef(Line);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000231
232 // We allow comments in headers. Technically this isn't part
233
234 // of the LSP specification, but makes writing tests easier.
235 if (LineRef.startswith("#"))
236 continue;
237
238 // Content-Length is a mandatory header, and the only one we handle.
239 if (LineRef.consume_front("Content-Length: ")) {
240 if (ContentLength != 0) {
241 elog("Warning: Duplicate Content-Length header received. "
242 "The previous value for this message ({0}) was ignored.",
243 ContentLength);
244 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000245 llvm::getAsUnsignedInteger(LineRef.trim(), 0, ContentLength);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000246 continue;
247 } else if (!LineRef.trim().empty()) {
248 // It's another header, ignore it.
249 continue;
250 } else {
251 // An empty line indicates the end of headers.
252 // Go ahead and read the JSON.
253 break;
254 }
255 }
256
257 // The fuzzer likes crashing us by sending "Content-Length: 9999999999999999"
258 if (ContentLength > 1 << 30) { // 1024M
259 elog("Refusing to read message with long Content-Length: {0}. "
260 "Expect protocol errors",
261 ContentLength);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000262 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000263 }
264 if (ContentLength == 0) {
265 log("Warning: Missing Content-Length header, or zero-length message.");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000266 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000267 }
268
269 std::string JSON(ContentLength, '\0');
270 for (size_t Pos = 0, Read; Pos < ContentLength; Pos += Read) {
271 // Handle EINTR which is sent when a debugger attaches on some platforms.
Sam McCall19ac0eaf2019-11-25 19:51:07 +0100272 Read = retryAfterSignalUnlessShutdown(0, [&]{
273 return std::fread(&JSON[Pos], 1, ContentLength - Pos, In);
274 });
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000275 if (Read == 0) {
276 elog("Input was aborted. Read only {0} bytes of expected {1}.", Pos,
277 ContentLength);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000278 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000279 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000280 InMirror << llvm::StringRef(&JSON[Pos], Read);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000281 clearerr(In); // If we're done, the error was transient. If we're not done,
282 // either it was transient or we'll see it again on retry.
283 Pos += Read;
284 }
285 return std::move(JSON);
286}
287
288// For lit tests we support a simplified syntax:
289// - messages are delimited by '---' on a line by itself
290// - lines starting with # are ignored.
291// This is a testing path, so favor simplicity over performance here.
Sam McCall19ac0eaf2019-11-25 19:51:07 +0100292// When returning None, feof(), ferror(), or shutdownRequested() will be set.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000293llvm::Optional<std::string> JSONTransport::readDelimitedMessage() {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000294 std::string JSON;
295 std::string Line;
296 while (readLine(In, Line)) {
297 InMirror << Line;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000298 auto LineRef = llvm::StringRef(Line).trim();
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000299 if (LineRef.startswith("#")) // comment
300 continue;
301
302 // found a delimiter
303 if (LineRef.rtrim() == "---")
304 break;
305
306 JSON += Line;
307 }
308
Sam McCall19ac0eaf2019-11-25 19:51:07 +0100309 if (shutdownRequested())
310 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000311 if (ferror(In)) {
312 elog("Input error while reading message!");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000313 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000314 }
315 return std::move(JSON); // Including at EOF
316}
317
318} // namespace
319
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000320std::unique_ptr<Transport> newJSONTransport(std::FILE *In,
321 llvm::raw_ostream &Out,
322 llvm::raw_ostream *InMirror,
323 bool Pretty,
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000324 JSONStreamStyle Style) {
Jonas Devlieghere1c705d92019-08-14 23:52:23 +0000325 return std::make_unique<JSONTransport>(In, Out, InMirror, Pretty, Style);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000326}
327
328} // namespace clangd
329} // namespace clang