blob: 7189b23c1b2d14990c93bdec7651f6fcc2d78804 [file] [log] [blame]
Sam McCalldc8f3cf2018-10-17 07:32:05 +00001//===--- JSONTransport.cpp - sending and receiving LSP messages over JSON -===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#include "Logger.h"
10#include "Protocol.h" // For LSPError
11#include "Transport.h"
12#include "llvm/Support/Errno.h"
13
Sam McCalldc8f3cf2018-10-17 07:32:05 +000014namespace clang {
15namespace clangd {
16namespace {
17
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000018llvm::json::Object encodeError(llvm::Error E) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000019 std::string Message;
20 ErrorCode Code = ErrorCode::UnknownErrorCode;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000021 if (llvm::Error Unhandled = llvm::handleErrors(
22 std::move(E), [&](const LSPError &L) -> llvm::Error {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000023 Message = L.Message;
24 Code = L.Code;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000025 return llvm::Error::success();
Sam McCalldc8f3cf2018-10-17 07:32:05 +000026 }))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000027 Message = llvm::toString(std::move(Unhandled));
Sam McCalldc8f3cf2018-10-17 07:32:05 +000028
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000029 return llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000030 {"message", std::move(Message)},
31 {"code", int64_t(Code)},
32 };
33}
34
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000035llvm::Error decodeError(const llvm::json::Object &O) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000036 std::string Msg = O.getString("message").getValueOr("Unspecified error");
37 if (auto Code = O.getInteger("code"))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000038 return llvm::make_error<LSPError>(std::move(Msg), ErrorCode(*Code));
39 return llvm::make_error<llvm::StringError>(std::move(Msg),
40 llvm::inconvertibleErrorCode());
Sam McCalldc8f3cf2018-10-17 07:32:05 +000041}
42
43class JSONTransport : public Transport {
44public:
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000045 JSONTransport(std::FILE *In, llvm::raw_ostream &Out,
46 llvm::raw_ostream *InMirror, bool Pretty, JSONStreamStyle Style)
47 : In(In), Out(Out), InMirror(InMirror ? *InMirror : llvm::nulls()),
Sam McCalldc8f3cf2018-10-17 07:32:05 +000048 Pretty(Pretty), Style(Style) {}
49
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000050 void notify(llvm::StringRef Method, llvm::json::Value Params) override {
51 sendMessage(llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000052 {"jsonrpc", "2.0"},
53 {"method", Method},
54 {"params", std::move(Params)},
55 });
56 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000057 void call(llvm::StringRef Method, llvm::json::Value Params,
58 llvm::json::Value ID) override {
59 sendMessage(llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000060 {"jsonrpc", "2.0"},
61 {"id", std::move(ID)},
62 {"method", Method},
63 {"params", std::move(Params)},
64 });
65 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000066 void reply(llvm::json::Value ID,
67 llvm::Expected<llvm::json::Value> Result) override {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000068 if (Result) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000069 sendMessage(llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000070 {"jsonrpc", "2.0"},
71 {"id", std::move(ID)},
72 {"result", std::move(*Result)},
73 });
74 } else {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000075 sendMessage(llvm::json::Object{
Sam McCalldc8f3cf2018-10-17 07:32:05 +000076 {"jsonrpc", "2.0"},
77 {"id", std::move(ID)},
78 {"error", encodeError(Result.takeError())},
79 });
80 }
81 }
82
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000083 llvm::Error loop(MessageHandler &Handler) override {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000084 while (!feof(In)) {
85 if (ferror(In))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000086 return llvm::errorCodeToError(
87 std::error_code(errno, std::system_category()));
Sam McCalldc8f3cf2018-10-17 07:32:05 +000088 if (auto JSON = readRawMessage()) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000089 if (auto Doc = llvm::json::parse(*JSON)) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +000090 vlog(Pretty ? "<<< {0:2}\n" : "<<< {0}\n", *Doc);
91 if (!handleMessage(std::move(*Doc), Handler))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000092 return llvm::Error::success(); // we saw the "exit" notification.
Sam McCalldc8f3cf2018-10-17 07:32:05 +000093 } else {
94 // Parse error. Log the raw message.
95 vlog("<<< {0}\n", *JSON);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000096 elog("JSON parse error: {0}", llvm::toString(Doc.takeError()));
Sam McCalldc8f3cf2018-10-17 07:32:05 +000097 }
98 }
99 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000100 return llvm::errorCodeToError(std::make_error_code(std::errc::io_error));
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000101 }
102
103private:
104 // Dispatches incoming message to Handler onNotify/onCall/onReply.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000105 bool handleMessage(llvm::json::Value Message, MessageHandler &Handler);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000106 // Writes outgoing message to Out stream.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000107 void sendMessage(llvm::json::Value Message) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000108 std::string S;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000109 llvm::raw_string_ostream OS(S);
110 OS << llvm::formatv(Pretty ? "{0:2}" : "{0}", Message);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000111 OS.flush();
112 Out << "Content-Length: " << S.size() << "\r\n\r\n" << S;
113 Out.flush();
114 vlog(">>> {0}\n", S);
115 }
116
117 // Read raw string messages from input stream.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000118 llvm::Optional<std::string> readRawMessage() {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000119 return Style == JSONStreamStyle::Delimited ? readDelimitedMessage()
120 : readStandardMessage();
121 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000122 llvm::Optional<std::string> readDelimitedMessage();
123 llvm::Optional<std::string> readStandardMessage();
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000124
125 std::FILE *In;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000126 llvm::raw_ostream &Out;
127 llvm::raw_ostream &InMirror;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000128 bool Pretty;
129 JSONStreamStyle Style;
130};
131
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000132bool JSONTransport::handleMessage(llvm::json::Value Message,
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000133 MessageHandler &Handler) {
134 // Message must be an object with "jsonrpc":"2.0".
135 auto *Object = Message.getAsObject();
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000136 if (!Object ||
137 Object->getString("jsonrpc") != llvm::Optional<llvm::StringRef>("2.0")) {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000138 elog("Not a JSON-RPC 2.0 message: {0:2}", Message);
139 return false;
140 }
141 // ID may be any JSON value. If absent, this is a notification.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000142 llvm::Optional<llvm::json::Value> ID;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000143 if (auto *I = Object->get("id"))
144 ID = std::move(*I);
145 auto Method = Object->getString("method");
146 if (!Method) { // This is a response.
147 if (!ID) {
148 elog("No method and no response ID: {0:2}", Message);
149 return false;
150 }
151 if (auto *Err = Object->getObject("error"))
152 return Handler.onReply(std::move(*ID), decodeError(*Err));
153 // Result should be given, use null if not.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000154 llvm::json::Value Result = nullptr;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000155 if (auto *R = Object->get("result"))
156 Result = std::move(*R);
157 return Handler.onReply(std::move(*ID), std::move(Result));
158 }
159 // Params should be given, use null if not.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000160 llvm::json::Value Params = nullptr;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000161 if (auto *P = Object->get("params"))
162 Params = std::move(*P);
163
164 if (ID)
165 return Handler.onCall(*Method, std::move(Params), std::move(*ID));
166 else
167 return Handler.onNotify(*Method, std::move(Params));
168}
169
170// Tries to read a line up to and including \n.
171// If failing, feof() or ferror() will be set.
172bool readLine(std::FILE *In, std::string &Out) {
173 static constexpr int BufSize = 1024;
174 size_t Size = 0;
175 Out.clear();
176 for (;;) {
177 Out.resize(Size + BufSize);
178 // Handle EINTR which is sent when a debugger attaches on some platforms.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000179 if (!llvm::sys::RetryAfterSignal(nullptr, ::fgets, &Out[Size], BufSize, In))
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000180 return false;
181 clearerr(In);
182 // If the line contained null bytes, anything after it (including \n) will
183 // be ignored. Fortunately this is not a legal header or JSON.
184 size_t Read = std::strlen(&Out[Size]);
185 if (Read > 0 && Out[Size + Read - 1] == '\n') {
186 Out.resize(Size + Read);
187 return true;
188 }
189 Size += Read;
190 }
191}
192
193// Returns None when:
194// - ferror() or feof() are set.
195// - Content-Length is missing or empty (protocol error)
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000196llvm::Optional<std::string> JSONTransport::readStandardMessage() {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000197 // A Language Server Protocol message starts with a set of HTTP headers,
198 // delimited by \r\n, and terminated by an empty line (\r\n).
199 unsigned long long ContentLength = 0;
200 std::string Line;
201 while (true) {
202 if (feof(In) || ferror(In) || !readLine(In, Line))
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000203 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000204 InMirror << Line;
205
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000206 llvm::StringRef LineRef(Line);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000207
208 // We allow comments in headers. Technically this isn't part
209
210 // of the LSP specification, but makes writing tests easier.
211 if (LineRef.startswith("#"))
212 continue;
213
214 // Content-Length is a mandatory header, and the only one we handle.
215 if (LineRef.consume_front("Content-Length: ")) {
216 if (ContentLength != 0) {
217 elog("Warning: Duplicate Content-Length header received. "
218 "The previous value for this message ({0}) was ignored.",
219 ContentLength);
220 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000221 llvm::getAsUnsignedInteger(LineRef.trim(), 0, ContentLength);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000222 continue;
223 } else if (!LineRef.trim().empty()) {
224 // It's another header, ignore it.
225 continue;
226 } else {
227 // An empty line indicates the end of headers.
228 // Go ahead and read the JSON.
229 break;
230 }
231 }
232
233 // The fuzzer likes crashing us by sending "Content-Length: 9999999999999999"
234 if (ContentLength > 1 << 30) { // 1024M
235 elog("Refusing to read message with long Content-Length: {0}. "
236 "Expect protocol errors",
237 ContentLength);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000238 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000239 }
240 if (ContentLength == 0) {
241 log("Warning: Missing Content-Length header, or zero-length message.");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000242 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000243 }
244
245 std::string JSON(ContentLength, '\0');
246 for (size_t Pos = 0, Read; Pos < ContentLength; Pos += Read) {
247 // Handle EINTR which is sent when a debugger attaches on some platforms.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000248 Read = llvm::sys::RetryAfterSignal(0u, ::fread, &JSON[Pos], 1,
249 ContentLength - Pos, In);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000250 if (Read == 0) {
251 elog("Input was aborted. Read only {0} bytes of expected {1}.", Pos,
252 ContentLength);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000253 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000254 }
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000255 InMirror << llvm::StringRef(&JSON[Pos], Read);
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000256 clearerr(In); // If we're done, the error was transient. If we're not done,
257 // either it was transient or we'll see it again on retry.
258 Pos += Read;
259 }
260 return std::move(JSON);
261}
262
263// For lit tests we support a simplified syntax:
264// - messages are delimited by '---' on a line by itself
265// - lines starting with # are ignored.
266// This is a testing path, so favor simplicity over performance here.
267// When returning None, feof() or ferror() will be set.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000268llvm::Optional<std::string> JSONTransport::readDelimitedMessage() {
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000269 std::string JSON;
270 std::string Line;
271 while (readLine(In, Line)) {
272 InMirror << Line;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000273 auto LineRef = llvm::StringRef(Line).trim();
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000274 if (LineRef.startswith("#")) // comment
275 continue;
276
277 // found a delimiter
278 if (LineRef.rtrim() == "---")
279 break;
280
281 JSON += Line;
282 }
283
284 if (ferror(In)) {
285 elog("Input error while reading message!");
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000286 return llvm::None;
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000287 }
288 return std::move(JSON); // Including at EOF
289}
290
291} // namespace
292
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000293std::unique_ptr<Transport> newJSONTransport(std::FILE *In,
294 llvm::raw_ostream &Out,
295 llvm::raw_ostream *InMirror,
296 bool Pretty,
Sam McCalldc8f3cf2018-10-17 07:32:05 +0000297 JSONStreamStyle Style) {
298 return llvm::make_unique<JSONTransport>(In, Out, InMirror, Pretty, Style);
299}
300
301} // namespace clangd
302} // namespace clang