blob: 4921035b6dbb328833001f596ff318b6cb77b1b5 [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
10#include "Transport.h"
11#include "llvm/Support/Errno.h"
12
Sam McCalldc8f3cf2018-10-17 07:32:05 +000013namespace clang {
14namespace clangd {
15namespace {
16
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000017llvm::json::Object encodeError(llvm::Error E) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000018 std::string Message;
19 ErrorCode Code = ErrorCode::UnknownErrorCode;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000020 if (llvm::Error Unhandled = llvm::handleErrors(
21 std::move(E), [&](const LSPError &L) -> llvm::Error {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000022 Message = L.Message;
23 Code = L.Code;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000024 return llvm::Error::success();
Sam McCalldc8f3cf2018-10-17 07:32:05 +000025 }))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000026 Message = llvm::toString(std::move(Unhandled));
Sam McCalldc8f3cf2018-10-17 07:32:05 +000027
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000028 return llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000029 {"message", std::move(Message)},
30 {"code", int64_t(Code)},
31 };
32}
33
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000034llvm::Error decodeError(const llvm::json::Object &O) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000035 std::string Msg = O.getString("message").getValueOr("Unspecified error");
36 if (auto Code = O.getInteger("code"))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000037 return llvm::make_error<LSPError>(std::move(Msg), ErrorCode(*Code));
38 return llvm::make_error<llvm::StringError>(std::move(Msg),
39 llvm::inconvertibleErrorCode());
Sam McCalldc8f3cf2018-10-17 07:32:05 +000040}
41
42class JSONTransport : public Transport {
43public:
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000044 JSONTransport(std::FILE *In, llvm::raw_ostream &Out,
45 llvm::raw_ostream *InMirror, bool Pretty, JSONStreamStyle Style)
46 : In(In), Out(Out), InMirror(InMirror ? *InMirror : llvm::nulls()),
Sam McCalldc8f3cf2018-10-17 07:32:05 +000047 Pretty(Pretty), Style(Style) {}
48
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000049 void notify(llvm::StringRef Method, llvm::json::Value Params) override {
50 sendMessage(llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000051 {"jsonrpc", "2.0"},
52 {"method", Method},
53 {"params", std::move(Params)},
54 });
55 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000056 void call(llvm::StringRef Method, llvm::json::Value Params,
57 llvm::json::Value ID) override {
58 sendMessage(llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000059 {"jsonrpc", "2.0"},
60 {"id", std::move(ID)},
61 {"method", Method},
62 {"params", std::move(Params)},
63 });
64 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000065 void reply(llvm::json::Value ID,
66 llvm::Expected<llvm::json::Value> Result) override {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000067 if (Result) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000068 sendMessage(llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000069 {"jsonrpc", "2.0"},
70 {"id", std::move(ID)},
71 {"result", std::move(*Result)},
72 });
73 } else {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000074 sendMessage(llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000075 {"jsonrpc", "2.0"},
76 {"id", std::move(ID)},
77 {"error", encodeError(Result.takeError())},
78 });
79 }
80 }
81
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000082 llvm::Error loop(MessageHandler &Handler) override {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000083 while (!feof(In)) {
84 if (ferror(In))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000085 return llvm::errorCodeToError(
86 std::error_code(errno, std::system_category()));
Sam McCalldc8f3cf2018-10-17 07:32:05 +000087 if (auto JSON = readRawMessage()) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000088 if (auto Doc = llvm::json::parse(*JSON)) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000089 vlog(Pretty ? "<<< {0:2}\n" : "<<< {0}\n", *Doc);
90 if (!handleMessage(std::move(*Doc), Handler))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000091 return llvm::Error::success(); // we saw the "exit" notification.
Sam McCalldc8f3cf2018-10-17 07:32:05 +000092 } else {
93 // Parse error. Log the raw message.
94 vlog("<<< {0}\n", *JSON);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000095 elog("JSON parse error: {0}", llvm::toString(Doc.takeError()));
Sam McCalldc8f3cf2018-10-17 07:32:05 +000096 }
97 }
98 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000099 return llvm::errorCodeToError(std::make_error_code(std::errc::io_error));
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000100 }
101
102private:
103 // Dispatches incoming message to Handler onNotify/onCall/onReply.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000104 bool handleMessage(llvm::json::Value Message, MessageHandler &Handler);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000105 // Writes outgoing message to Out stream.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000106 void sendMessage(llvm::json::Value Message) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000107 std::string S;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000108 llvm::raw_string_ostream OS(S);
109 OS << llvm::formatv(Pretty ? "{0:2}" : "{0}", Message);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000110 OS.flush();
111 Out << "Content-Length: " << S.size() << "\r\n\r\n" << S;
112 Out.flush();
113 vlog(">>> {0}\n", S);
114 }
115
116 // Read raw string messages from input stream.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000117 llvm::Optional<std::string> readRawMessage() {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000118 return Style == JSONStreamStyle::Delimited ? readDelimitedMessage()
119 : readStandardMessage();
120 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000121 llvm::Optional<std::string> readDelimitedMessage();
122 llvm::Optional<std::string> readStandardMessage();
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000123
124 std::FILE *In;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000125 llvm::raw_ostream &Out;
126 llvm::raw_ostream &InMirror;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000127 bool Pretty;
128 JSONStreamStyle Style;
129};
130
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000131bool JSONTransport::handleMessage(llvm::json::Value Message,
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000132 MessageHandler &Handler) {
133 // Message must be an object with "jsonrpc":"2.0".
134 auto *Object = Message.getAsObject();
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000135 if (!Object ||
136 Object->getString("jsonrpc") != llvm::Optional<llvm::StringRef>("2.0")) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000137 elog("Not a JSON-RPC 2.0 message: {0:2}", Message);
138 return false;
139 }
140 // ID may be any JSON value. If absent, this is a notification.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000141 llvm::Optional<llvm::json::Value> ID;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000142 if (auto *I = Object->get("id"))
143 ID = std::move(*I);
144 auto Method = Object->getString("method");
145 if (!Method) { // This is a response.
146 if (!ID) {
147 elog("No method and no response ID: {0:2}", Message);
148 return false;
149 }
150 if (auto *Err = Object->getObject("error"))
151 return Handler.onReply(std::move(*ID), decodeError(*Err));
152 // Result should be given, use null if not.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000153 llvm::json::Value Result = nullptr;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000154 if (auto *R = Object->get("result"))
155 Result = std::move(*R);
156 return Handler.onReply(std::move(*ID), std::move(Result));
157 }
158 // Params should be given, use null if not.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000159 llvm::json::Value Params = nullptr;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000160 if (auto *P = Object->get("params"))
161 Params = std::move(*P);
162
163 if (ID)
164 return Handler.onCall(*Method, std::move(Params), std::move(*ID));
165 else
166 return Handler.onNotify(*Method, std::move(Params));
167}
168
169// Tries to read a line up to and including \n.
170// If failing, feof() or ferror() will be set.
171bool readLine(std::FILE *In, std::string &Out) {
172 static constexpr int BufSize = 1024;
173 size_t Size = 0;
174 Out.clear();
175 for (;;) {
176 Out.resize(Size + BufSize);
177 // Handle EINTR which is sent when a debugger attaches on some platforms.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000178 if (!llvm::sys::RetryAfterSignal(nullptr, ::fgets, &Out[Size], BufSize, In))
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000179 return false;
180 clearerr(In);
181 // If the line contained null bytes, anything after it (including \n) will
182 // be ignored. Fortunately this is not a legal header or JSON.
183 size_t Read = std::strlen(&Out[Size]);
184 if (Read > 0 && Out[Size + Read - 1] == '\n') {
185 Out.resize(Size + Read);
186 return true;
187 }
188 Size += Read;
189 }
190}
191
192// Returns None when:
193// - ferror() or feof() are set.
194// - Content-Length is missing or empty (protocol error)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000195llvm::Optional<std::string> JSONTransport::readStandardMessage() {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000196 // A Language Server Protocol message starts with a set of HTTP headers,
197 // delimited by \r\n, and terminated by an empty line (\r\n).
198 unsigned long long ContentLength = 0;
199 std::string Line;
200 while (true) {
201 if (feof(In) || ferror(In) || !readLine(In, Line))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000202 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000203 InMirror << Line;
204
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000205 llvm::StringRef LineRef(Line);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000206
207 // We allow comments in headers. Technically this isn't part
208
209 // of the LSP specification, but makes writing tests easier.
210 if (LineRef.startswith("#"))
211 continue;
212
213 // Content-Length is a mandatory header, and the only one we handle.
214 if (LineRef.consume_front("Content-Length: ")) {
215 if (ContentLength != 0) {
216 elog("Warning: Duplicate Content-Length header received. "
217 "The previous value for this message ({0}) was ignored.",
218 ContentLength);
219 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000220 llvm::getAsUnsignedInteger(LineRef.trim(), 0, ContentLength);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000221 continue;
222 } else if (!LineRef.trim().empty()) {
223 // It's another header, ignore it.
224 continue;
225 } else {
226 // An empty line indicates the end of headers.
227 // Go ahead and read the JSON.
228 break;
229 }
230 }
231
232 // The fuzzer likes crashing us by sending "Content-Length: 9999999999999999"
233 if (ContentLength > 1 << 30) { // 1024M
234 elog("Refusing to read message with long Content-Length: {0}. "
235 "Expect protocol errors",
236 ContentLength);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000237 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000238 }
239 if (ContentLength == 0) {
240 log("Warning: Missing Content-Length header, or zero-length message.");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000241 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000242 }
243
244 std::string JSON(ContentLength, '\0');
245 for (size_t Pos = 0, Read; Pos < ContentLength; Pos += Read) {
246 // Handle EINTR which is sent when a debugger attaches on some platforms.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000247 Read = llvm::sys::RetryAfterSignal(0u, ::fread, &JSON[Pos], 1,
248 ContentLength - Pos, In);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000249 if (Read == 0) {
250 elog("Input was aborted. Read only {0} bytes of expected {1}.", Pos,
251 ContentLength);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000252 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000253 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000254 InMirror << llvm::StringRef(&JSON[Pos], Read);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000255 clearerr(In); // If we're done, the error was transient. If we're not done,
256 // either it was transient or we'll see it again on retry.
257 Pos += Read;
258 }
259 return std::move(JSON);
260}
261
262// For lit tests we support a simplified syntax:
263// - messages are delimited by '---' on a line by itself
264// - lines starting with # are ignored.
265// This is a testing path, so favor simplicity over performance here.
266// When returning None, feof() or ferror() will be set.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000267llvm::Optional<std::string> JSONTransport::readDelimitedMessage() {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000268 std::string JSON;
269 std::string Line;
270 while (readLine(In, Line)) {
271 InMirror << Line;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000272 auto LineRef = llvm::StringRef(Line).trim();
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000273 if (LineRef.startswith("#")) // comment
274 continue;
275
276 // found a delimiter
277 if (LineRef.rtrim() == "---")
278 break;
279
280 JSON += Line;
281 }
282
283 if (ferror(In)) {
284 elog("Input error while reading message!");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000285 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000286 }
287 return std::move(JSON); // Including at EOF
288}
289
290} // namespace
291
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000292std::unique_ptr<Transport> newJSONTransport(std::FILE *In,
293 llvm::raw_ostream &Out,
294 llvm::raw_ostream *InMirror,
295 bool Pretty,
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000296 JSONStreamStyle Style) {
Jonas Devlieghere1c705d92019-08-14 23:52:23 +0000297 return std::make_unique<JSONTransport>(In, Out, InMirror, Pretty, Style);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000298}
299
300} // namespace clangd
301} // namespace clang