blob: d82dca167f3b10ee024e7c6cea3f9bcda68d1069 [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//===----------------------------------------------------------------------===//
8#include "Logger.h"
9#include "Protocol.h" // For LSPError
Sam McCall19ac0eaf2019-11-25 19:51:07 +010010#include "Shutdown.h"
Sam McCalldc8f3cf2018-10-17 07:32:05 +000011#include "Transport.h"
12#include "llvm/Support/Errno.h"
Sam McCall19ac0eaf2019-11-25 19:51:07 +010013#include "llvm/Support/Error.h"
Sam McCalldc8f3cf2018-10-17 07:32:05 +000014
Sam McCalldc8f3cf2018-10-17 07:32:05 +000015namespace clang {
16namespace clangd {
17namespace {
18
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000019llvm::json::Object encodeError(llvm::Error E) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000020 std::string Message;
21 ErrorCode Code = ErrorCode::UnknownErrorCode;
Sam McCallc627b122020-03-04 14:04:17 +010022 // FIXME: encode cancellation errors using RequestCancelled or ContentModified
23 // as appropriate.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000024 if (llvm::Error Unhandled = llvm::handleErrors(
25 std::move(E), [&](const LSPError &L) -> llvm::Error {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000026 Message = L.Message;
27 Code = L.Code;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000028 return llvm::Error::success();
Sam McCalldc8f3cf2018-10-17 07:32:05 +000029 }))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000030 Message = llvm::toString(std::move(Unhandled));
Sam McCalldc8f3cf2018-10-17 07:32:05 +000031
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000032 return llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000033 {"message", std::move(Message)},
34 {"code", int64_t(Code)},
35 };
36}
37
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000038llvm::Error decodeError(const llvm::json::Object &O) {
Benjamin Krameradcd0262020-01-28 20:23:46 +010039 std::string Msg =
40 std::string(O.getString("message").getValueOr("Unspecified error"));
Sam McCalldc8f3cf2018-10-17 07:32:05 +000041 if (auto Code = O.getInteger("code"))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000042 return llvm::make_error<LSPError>(std::move(Msg), ErrorCode(*Code));
43 return llvm::make_error<llvm::StringError>(std::move(Msg),
44 llvm::inconvertibleErrorCode());
Sam McCalldc8f3cf2018-10-17 07:32:05 +000045}
46
47class JSONTransport : public Transport {
48public:
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000049 JSONTransport(std::FILE *In, llvm::raw_ostream &Out,
50 llvm::raw_ostream *InMirror, bool Pretty, JSONStreamStyle Style)
51 : In(In), Out(Out), InMirror(InMirror ? *InMirror : llvm::nulls()),
Sam McCalldc8f3cf2018-10-17 07:32:05 +000052 Pretty(Pretty), Style(Style) {}
53
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000054 void notify(llvm::StringRef Method, llvm::json::Value Params) override {
55 sendMessage(llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000056 {"jsonrpc", "2.0"},
57 {"method", Method},
58 {"params", std::move(Params)},
59 });
60 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000061 void call(llvm::StringRef Method, llvm::json::Value Params,
62 llvm::json::Value ID) override {
63 sendMessage(llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000064 {"jsonrpc", "2.0"},
65 {"id", std::move(ID)},
66 {"method", Method},
67 {"params", std::move(Params)},
68 });
69 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000070 void reply(llvm::json::Value ID,
71 llvm::Expected<llvm::json::Value> Result) override {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000072 if (Result) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000073 sendMessage(llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000074 {"jsonrpc", "2.0"},
75 {"id", std::move(ID)},
76 {"result", std::move(*Result)},
77 });
78 } else {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000079 sendMessage(llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000080 {"jsonrpc", "2.0"},
81 {"id", std::move(ID)},
82 {"error", encodeError(Result.takeError())},
83 });
84 }
85 }
86
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000087 llvm::Error loop(MessageHandler &Handler) override {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000088 while (!feof(In)) {
Sam McCall19ac0eaf2019-11-25 19:51:07 +010089 if (shutdownRequested())
90 return llvm::createStringError(
91 std::make_error_code(std::errc::operation_canceled),
92 "Got signal, shutting down");
Sam McCalldc8f3cf2018-10-17 07:32:05 +000093 if (ferror(In))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000094 return llvm::errorCodeToError(
95 std::error_code(errno, std::system_category()));
Sam McCalldc8f3cf2018-10-17 07:32:05 +000096 if (auto JSON = readRawMessage()) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000097 if (auto Doc = llvm::json::parse(*JSON)) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000098 vlog(Pretty ? "<<< {0:2}\n" : "<<< {0}\n", *Doc);
99 if (!handleMessage(std::move(*Doc), Handler))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000100 return llvm::Error::success(); // we saw the "exit" notification.
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000101 } else {
102 // Parse error. Log the raw message.
103 vlog("<<< {0}\n", *JSON);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000104 elog("JSON parse error: {0}", llvm::toString(Doc.takeError()));
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000105 }
106 }
107 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000108 return llvm::errorCodeToError(std::make_error_code(std::errc::io_error));
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000109 }
110
111private:
112 // Dispatches incoming message to Handler onNotify/onCall/onReply.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000113 bool handleMessage(llvm::json::Value Message, MessageHandler &Handler);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000114 // Writes outgoing message to Out stream.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000115 void sendMessage(llvm::json::Value Message) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000116 std::string S;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000117 llvm::raw_string_ostream OS(S);
118 OS << llvm::formatv(Pretty ? "{0:2}" : "{0}", Message);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000119 OS.flush();
120 Out << "Content-Length: " << S.size() << "\r\n\r\n" << S;
121 Out.flush();
122 vlog(">>> {0}\n", S);
123 }
124
125 // Read raw string messages from input stream.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000126 llvm::Optional<std::string> readRawMessage() {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000127 return Style == JSONStreamStyle::Delimited ? readDelimitedMessage()
128 : readStandardMessage();
129 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000130 llvm::Optional<std::string> readDelimitedMessage();
131 llvm::Optional<std::string> readStandardMessage();
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000132
133 std::FILE *In;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000134 llvm::raw_ostream &Out;
135 llvm::raw_ostream &InMirror;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000136 bool Pretty;
137 JSONStreamStyle Style;
138};
139
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000140bool JSONTransport::handleMessage(llvm::json::Value Message,
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000141 MessageHandler &Handler) {
142 // Message must be an object with "jsonrpc":"2.0".
143 auto *Object = Message.getAsObject();
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000144 if (!Object ||
145 Object->getString("jsonrpc") != llvm::Optional<llvm::StringRef>("2.0")) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000146 elog("Not a JSON-RPC 2.0 message: {0:2}", Message);
147 return false;
148 }
149 // ID may be any JSON value. If absent, this is a notification.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000150 llvm::Optional<llvm::json::Value> ID;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000151 if (auto *I = Object->get("id"))
152 ID = std::move(*I);
153 auto Method = Object->getString("method");
154 if (!Method) { // This is a response.
155 if (!ID) {
156 elog("No method and no response ID: {0:2}", Message);
157 return false;
158 }
159 if (auto *Err = Object->getObject("error"))
160 return Handler.onReply(std::move(*ID), decodeError(*Err));
161 // Result should be given, use null if not.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000162 llvm::json::Value Result = nullptr;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000163 if (auto *R = Object->get("result"))
164 Result = std::move(*R);
165 return Handler.onReply(std::move(*ID), std::move(Result));
166 }
167 // Params should be given, use null if not.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000168 llvm::json::Value Params = nullptr;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000169 if (auto *P = Object->get("params"))
170 Params = std::move(*P);
171
172 if (ID)
173 return Handler.onCall(*Method, std::move(Params), std::move(*ID));
174 else
175 return Handler.onNotify(*Method, std::move(Params));
176}
177
178// Tries to read a line up to and including \n.
Sam McCall19ac0eaf2019-11-25 19:51:07 +0100179// If failing, feof(), ferror(), or shutdownRequested() will be set.
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000180bool readLine(std::FILE *In, std::string &Out) {
181 static constexpr int BufSize = 1024;
182 size_t Size = 0;
183 Out.clear();
184 for (;;) {
185 Out.resize(Size + BufSize);
186 // Handle EINTR which is sent when a debugger attaches on some platforms.
Sam McCall19ac0eaf2019-11-25 19:51:07 +0100187 if (!retryAfterSignalUnlessShutdown(
188 nullptr, [&] { return std::fgets(&Out[Size], BufSize, In); }))
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000189 return false;
190 clearerr(In);
191 // If the line contained null bytes, anything after it (including \n) will
192 // be ignored. Fortunately this is not a legal header or JSON.
193 size_t Read = std::strlen(&Out[Size]);
194 if (Read > 0 && Out[Size + Read - 1] == '\n') {
195 Out.resize(Size + Read);
196 return true;
197 }
198 Size += Read;
199 }
200}
201
202// Returns None when:
Sam McCall19ac0eaf2019-11-25 19:51:07 +0100203// - ferror(), feof(), or shutdownRequested() are set.
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000204// - Content-Length is missing or empty (protocol error)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000205llvm::Optional<std::string> JSONTransport::readStandardMessage() {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000206 // A Language Server Protocol message starts with a set of HTTP headers,
207 // delimited by \r\n, and terminated by an empty line (\r\n).
208 unsigned long long ContentLength = 0;
209 std::string Line;
210 while (true) {
211 if (feof(In) || ferror(In) || !readLine(In, Line))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000212 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000213 InMirror << Line;
214
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000215 llvm::StringRef LineRef(Line);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000216
217 // We allow comments in headers. Technically this isn't part
218
219 // of the LSP specification, but makes writing tests easier.
220 if (LineRef.startswith("#"))
221 continue;
222
223 // Content-Length is a mandatory header, and the only one we handle.
224 if (LineRef.consume_front("Content-Length: ")) {
225 if (ContentLength != 0) {
226 elog("Warning: Duplicate Content-Length header received. "
227 "The previous value for this message ({0}) was ignored.",
228 ContentLength);
229 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000230 llvm::getAsUnsignedInteger(LineRef.trim(), 0, ContentLength);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000231 continue;
232 } else if (!LineRef.trim().empty()) {
233 // It's another header, ignore it.
234 continue;
235 } else {
236 // An empty line indicates the end of headers.
237 // Go ahead and read the JSON.
238 break;
239 }
240 }
241
242 // The fuzzer likes crashing us by sending "Content-Length: 9999999999999999"
243 if (ContentLength > 1 << 30) { // 1024M
244 elog("Refusing to read message with long Content-Length: {0}. "
245 "Expect protocol errors",
246 ContentLength);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000247 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000248 }
249 if (ContentLength == 0) {
250 log("Warning: Missing Content-Length header, or zero-length message.");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000251 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000252 }
253
254 std::string JSON(ContentLength, '\0');
255 for (size_t Pos = 0, Read; Pos < ContentLength; Pos += Read) {
256 // Handle EINTR which is sent when a debugger attaches on some platforms.
Sam McCall19ac0eaf2019-11-25 19:51:07 +0100257 Read = retryAfterSignalUnlessShutdown(0, [&]{
258 return std::fread(&JSON[Pos], 1, ContentLength - Pos, In);
259 });
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000260 if (Read == 0) {
261 elog("Input was aborted. Read only {0} bytes of expected {1}.", Pos,
262 ContentLength);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000263 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000264 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000265 InMirror << llvm::StringRef(&JSON[Pos], Read);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000266 clearerr(In); // If we're done, the error was transient. If we're not done,
267 // either it was transient or we'll see it again on retry.
268 Pos += Read;
269 }
270 return std::move(JSON);
271}
272
273// For lit tests we support a simplified syntax:
274// - messages are delimited by '---' on a line by itself
275// - lines starting with # are ignored.
276// This is a testing path, so favor simplicity over performance here.
Sam McCall19ac0eaf2019-11-25 19:51:07 +0100277// When returning None, feof(), ferror(), or shutdownRequested() will be set.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000278llvm::Optional<std::string> JSONTransport::readDelimitedMessage() {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000279 std::string JSON;
280 std::string Line;
281 while (readLine(In, Line)) {
282 InMirror << Line;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000283 auto LineRef = llvm::StringRef(Line).trim();
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000284 if (LineRef.startswith("#")) // comment
285 continue;
286
287 // found a delimiter
288 if (LineRef.rtrim() == "---")
289 break;
290
291 JSON += Line;
292 }
293
Sam McCall19ac0eaf2019-11-25 19:51:07 +0100294 if (shutdownRequested())
295 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000296 if (ferror(In)) {
297 elog("Input error while reading message!");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000298 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000299 }
300 return std::move(JSON); // Including at EOF
301}
302
303} // namespace
304
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000305std::unique_ptr<Transport> newJSONTransport(std::FILE *In,
306 llvm::raw_ostream &Out,
307 llvm::raw_ostream *InMirror,
308 bool Pretty,
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000309 JSONStreamStyle Style) {
Jonas Devlieghere1c705d92019-08-14 23:52:23 +0000310 return std::make_unique<JSONTransport>(In, Out, InMirror, Pretty, Style);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000311}
312
313} // namespace clangd
314} // namespace clang