1 //===--- JSONTransport.cpp - sending and receiving LSP messages over JSON -===//
3 // 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
7 //===----------------------------------------------------------------------===//
8 #include "Protocol.h" // For LSPError
10 #include "support/Cancellation.h"
11 #include "support/Logger.h"
12 #include "support/Shutdown.h"
13 #include "support/ThreadCrashReporter.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/Support/Error.h"
16 #include <system_error>
22 llvm::json::Object
encodeError(llvm::Error E
) {
24 ErrorCode Code
= ErrorCode::UnknownErrorCode
;
25 // FIXME: encode cancellation errors using RequestCancelled or ContentModified
27 if (llvm::Error Unhandled
= llvm::handleErrors(
29 [&](const CancelledError
&C
) -> llvm::Error
{
31 case static_cast<int>(ErrorCode::ContentModified
):
32 Code
= ErrorCode::ContentModified
;
33 Message
= "Request cancelled because the document was modified";
36 Code
= ErrorCode::RequestCancelled
;
37 Message
= "Request cancelled";
40 return llvm::Error::success();
42 [&](const LSPError
&L
) -> llvm::Error
{
45 return llvm::Error::success();
47 Message
= llvm::toString(std::move(Unhandled
));
49 return llvm::json::Object
{
50 {"message", std::move(Message
)},
51 {"code", int64_t(Code
)},
55 llvm::Error
decodeError(const llvm::json::Object
&O
) {
56 llvm::StringRef Msg
= O
.getString("message").value_or("Unspecified error");
57 if (auto Code
= O
.getInteger("code"))
58 return llvm::make_error
<LSPError
>(Msg
.str(), ErrorCode(*Code
));
59 return error(Msg
.str());
62 class JSONTransport
: public Transport
{
64 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()),
67 Pretty(Pretty
), Style(Style
) {}
69 void notify(llvm::StringRef Method
, llvm::json::Value Params
) override
{
70 sendMessage(llvm::json::Object
{
73 {"params", std::move(Params
)},
76 void call(llvm::StringRef Method
, llvm::json::Value Params
,
77 llvm::json::Value ID
) override
{
78 sendMessage(llvm::json::Object
{
80 {"id", std::move(ID
)},
82 {"params", std::move(Params
)},
85 void reply(llvm::json::Value ID
,
86 llvm::Expected
<llvm::json::Value
> Result
) override
{
88 sendMessage(llvm::json::Object
{
90 {"id", std::move(ID
)},
91 {"result", std::move(*Result
)},
94 sendMessage(llvm::json::Object
{
96 {"id", std::move(ID
)},
97 {"error", encodeError(Result
.takeError())},
102 llvm::Error
loop(MessageHandler
&Handler
) override
{
103 std::string JSON
; // Messages may be large, reuse same big buffer.
105 if (shutdownRequested())
106 return error(std::make_error_code(std::errc::operation_canceled
),
107 "Got signal, shutting down");
109 return llvm::errorCodeToError(
110 std::error_code(errno
, std::system_category()));
111 if (readRawMessage(JSON
)) {
112 ThreadCrashReporter
ScopedReporter([&JSON
]() {
113 auto &OS
= llvm::errs();
114 OS
<< "Signalled while processing message:\n";
117 if (auto Doc
= llvm::json::parse(JSON
)) {
118 vlog(Pretty
? "<<< {0:2}\n" : "<<< {0}\n", *Doc
);
119 if (!handleMessage(std::move(*Doc
), Handler
))
120 return llvm::Error::success(); // we saw the "exit" notification.
122 // Parse error. Log the raw message.
123 vlog("<<< {0}\n", JSON
);
124 elog("JSON parse error: {0}", llvm::toString(Doc
.takeError()));
128 return llvm::errorCodeToError(std::make_error_code(std::errc::io_error
));
132 // Dispatches incoming message to Handler onNotify/onCall/onReply.
133 bool handleMessage(llvm::json::Value Message
, MessageHandler
&Handler
);
134 // Writes outgoing message to Out stream.
135 void sendMessage(llvm::json::Value Message
) {
136 OutputBuffer
.clear();
137 llvm::raw_svector_ostream
OS(OutputBuffer
);
138 OS
<< llvm::formatv(Pretty
? "{0:2}" : "{0}", Message
);
139 Out
<< "Content-Length: " << OutputBuffer
.size() << "\r\n\r\n"
142 vlog(">>> {0}\n", OutputBuffer
);
145 // Read raw string messages from input stream.
146 bool readRawMessage(std::string
&JSON
) {
147 return Style
== JSONStreamStyle::Delimited
? readDelimitedMessage(JSON
)
148 : readStandardMessage(JSON
);
150 bool readDelimitedMessage(std::string
&JSON
);
151 bool readStandardMessage(std::string
&JSON
);
153 llvm::SmallVector
<char, 0> OutputBuffer
;
155 llvm::raw_ostream
&Out
;
156 llvm::raw_ostream
&InMirror
;
158 JSONStreamStyle Style
;
161 bool JSONTransport::handleMessage(llvm::json::Value Message
,
162 MessageHandler
&Handler
) {
163 // Message must be an object with "jsonrpc":"2.0".
164 auto *Object
= Message
.getAsObject();
166 Object
->getString("jsonrpc") != llvm::Optional
<llvm::StringRef
>("2.0")) {
167 elog("Not a JSON-RPC 2.0 message: {0:2}", Message
);
170 // ID may be any JSON value. If absent, this is a notification.
171 llvm::Optional
<llvm::json::Value
> ID
;
172 if (auto *I
= Object
->get("id"))
174 auto Method
= Object
->getString("method");
175 if (!Method
) { // This is a response.
177 elog("No method and no response ID: {0:2}", Message
);
180 if (auto *Err
= Object
->getObject("error"))
181 return Handler
.onReply(std::move(*ID
), decodeError(*Err
));
182 // Result should be given, use null if not.
183 llvm::json::Value Result
= nullptr;
184 if (auto *R
= Object
->get("result"))
185 Result
= std::move(*R
);
186 return Handler
.onReply(std::move(*ID
), std::move(Result
));
188 // Params should be given, use null if not.
189 llvm::json::Value Params
= nullptr;
190 if (auto *P
= Object
->get("params"))
191 Params
= std::move(*P
);
194 return Handler
.onCall(*Method
, std::move(Params
), std::move(*ID
));
195 return Handler
.onNotify(*Method
, std::move(Params
));
198 // Tries to read a line up to and including \n.
199 // If failing, feof(), ferror(), or shutdownRequested() will be set.
200 bool readLine(std::FILE *In
, llvm::SmallVectorImpl
<char> &Out
) {
201 // Big enough to hold any reasonable header line. May not fit content lines
202 // in delimited mode, but performance doesn't matter for that mode.
203 static constexpr int BufSize
= 128;
207 Out
.resize_for_overwrite(Size
+ BufSize
);
208 // Handle EINTR which is sent when a debugger attaches on some platforms.
209 if (!retryAfterSignalUnlessShutdown(
210 nullptr, [&] { return std::fgets(&Out
[Size
], BufSize
, In
); }))
213 // If the line contained null bytes, anything after it (including \n) will
214 // be ignored. Fortunately this is not a legal header or JSON.
215 size_t Read
= std::strlen(&Out
[Size
]);
216 if (Read
> 0 && Out
[Size
+ Read
- 1] == '\n') {
217 Out
.resize(Size
+ Read
);
224 // Returns None when:
225 // - ferror(), feof(), or shutdownRequested() are set.
226 // - Content-Length is missing or empty (protocol error)
227 bool JSONTransport::readStandardMessage(std::string
&JSON
) {
228 // A Language Server Protocol message starts with a set of HTTP headers,
229 // delimited by \r\n, and terminated by an empty line (\r\n).
230 unsigned long long ContentLength
= 0;
231 llvm::SmallString
<128> Line
;
233 if (feof(In
) || ferror(In
) || !readLine(In
, Line
))
237 llvm::StringRef LineRef
= Line
;
239 // We allow comments in headers. Technically this isn't part
241 // of the LSP specification, but makes writing tests easier.
242 if (LineRef
.startswith("#"))
245 // Content-Length is a mandatory header, and the only one we handle.
246 if (LineRef
.consume_front("Content-Length: ")) {
247 if (ContentLength
!= 0) {
248 elog("Warning: Duplicate Content-Length header received. "
249 "The previous value for this message ({0}) was ignored.",
252 llvm::getAsUnsignedInteger(LineRef
.trim(), 0, ContentLength
);
256 // An empty line indicates the end of headers.
257 // Go ahead and read the JSON.
258 if (LineRef
.trim().empty())
261 // It's another header, ignore it.
264 // The fuzzer likes crashing us by sending "Content-Length: 9999999999999999"
265 if (ContentLength
> 1 << 30) { // 1024M
266 elog("Refusing to read message with long Content-Length: {0}. "
267 "Expect protocol errors",
271 if (ContentLength
== 0) {
272 log("Warning: Missing Content-Length header, or zero-length message.");
276 JSON
.resize(ContentLength
);
277 for (size_t Pos
= 0, Read
; Pos
< ContentLength
; Pos
+= Read
) {
278 // Handle EINTR which is sent when a debugger attaches on some platforms.
279 Read
= retryAfterSignalUnlessShutdown(0, [&]{
280 return std::fread(&JSON
[Pos
], 1, ContentLength
- Pos
, In
);
283 elog("Input was aborted. Read only {0} bytes of expected {1}.", Pos
,
287 InMirror
<< llvm::StringRef(&JSON
[Pos
], Read
);
288 clearerr(In
); // If we're done, the error was transient. If we're not done,
289 // either it was transient or we'll see it again on retry.
295 // For lit tests we support a simplified syntax:
296 // - messages are delimited by '---' on a line by itself
297 // - lines starting with # are ignored.
298 // This is a testing path, so favor simplicity over performance here.
299 // When returning false: feof(), ferror(), or shutdownRequested() will be set.
300 bool JSONTransport::readDelimitedMessage(std::string
&JSON
) {
302 llvm::SmallString
<128> Line
;
303 while (readLine(In
, Line
)) {
305 auto LineRef
= Line
.str().trim();
306 if (LineRef
.startswith("#")) // comment
310 if (LineRef
.rtrim() == "---")
316 if (shutdownRequested())
319 elog("Input error while reading message!");
322 return true; // Including at EOF
327 std::unique_ptr
<Transport
> newJSONTransport(std::FILE *In
,
328 llvm::raw_ostream
&Out
,
329 llvm::raw_ostream
*InMirror
,
331 JSONStreamStyle Style
) {
332 return std::make_unique
<JSONTransport
>(In
, Out
, InMirror
, Pretty
, Style
);
335 } // namespace clangd