llvm-project
336 строк · 11.4 Кб
1//===--- JSONTransport.cpp - sending and receiving LSP messages over JSON -===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8#include "Protocol.h" // For LSPError
9#include "Transport.h"
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 <optional>
17#include <system_error>
18
19namespace clang {
20namespace clangd {
21namespace {
22
23llvm::json::Object encodeError(llvm::Error E) {
24std::string Message;
25ErrorCode Code = ErrorCode::UnknownErrorCode;
26// FIXME: encode cancellation errors using RequestCancelled or ContentModified
27// as appropriate.
28if (llvm::Error Unhandled = llvm::handleErrors(
29std::move(E),
30[&](const CancelledError &C) -> llvm::Error {
31switch (C.Reason) {
32case static_cast<int>(ErrorCode::ContentModified):
33Code = ErrorCode::ContentModified;
34Message = "Request cancelled because the document was modified";
35break;
36default:
37Code = ErrorCode::RequestCancelled;
38Message = "Request cancelled";
39break;
40}
41return llvm::Error::success();
42},
43[&](const LSPError &L) -> llvm::Error {
44Message = L.Message;
45Code = L.Code;
46return llvm::Error::success();
47}))
48Message = llvm::toString(std::move(Unhandled));
49
50return llvm::json::Object{
51{"message", std::move(Message)},
52{"code", int64_t(Code)},
53};
54}
55
56llvm::Error decodeError(const llvm::json::Object &O) {
57llvm::StringRef Msg = O.getString("message").value_or("Unspecified error");
58if (auto Code = O.getInteger("code"))
59return llvm::make_error<LSPError>(Msg.str(), ErrorCode(*Code));
60return error(Msg.str());
61}
62
63class JSONTransport : public Transport {
64public:
65JSONTransport(std::FILE *In, llvm::raw_ostream &Out,
66llvm::raw_ostream *InMirror, bool Pretty, JSONStreamStyle Style)
67: In(In), Out(Out), InMirror(InMirror ? *InMirror : llvm::nulls()),
68Pretty(Pretty), Style(Style) {}
69
70void notify(llvm::StringRef Method, llvm::json::Value Params) override {
71sendMessage(llvm::json::Object{
72{"jsonrpc", "2.0"},
73{"method", Method},
74{"params", std::move(Params)},
75});
76}
77void call(llvm::StringRef Method, llvm::json::Value Params,
78llvm::json::Value ID) override {
79sendMessage(llvm::json::Object{
80{"jsonrpc", "2.0"},
81{"id", std::move(ID)},
82{"method", Method},
83{"params", std::move(Params)},
84});
85}
86void reply(llvm::json::Value ID,
87llvm::Expected<llvm::json::Value> Result) override {
88if (Result) {
89sendMessage(llvm::json::Object{
90{"jsonrpc", "2.0"},
91{"id", std::move(ID)},
92{"result", std::move(*Result)},
93});
94} else {
95sendMessage(llvm::json::Object{
96{"jsonrpc", "2.0"},
97{"id", std::move(ID)},
98{"error", encodeError(Result.takeError())},
99});
100}
101}
102
103llvm::Error loop(MessageHandler &Handler) override {
104std::string JSON; // Messages may be large, reuse same big buffer.
105while (!feof(In)) {
106if (shutdownRequested())
107return error(std::make_error_code(std::errc::operation_canceled),
108"Got signal, shutting down");
109if (ferror(In))
110return llvm::errorCodeToError(llvm::errnoAsErrorCode());
111if (readRawMessage(JSON)) {
112ThreadCrashReporter ScopedReporter([&JSON]() {
113auto &OS = llvm::errs();
114OS << "Signalled while processing message:\n";
115OS << JSON << "\n";
116});
117if (auto Doc = llvm::json::parse(JSON)) {
118vlog(Pretty ? "<<< {0:2}\n" : "<<< {0}\n", *Doc);
119if (!handleMessage(std::move(*Doc), Handler))
120return llvm::Error::success(); // we saw the "exit" notification.
121} else {
122// Parse error. Log the raw message.
123vlog("<<< {0}\n", JSON);
124elog("JSON parse error: {0}", llvm::toString(Doc.takeError()));
125}
126}
127}
128return llvm::errorCodeToError(std::make_error_code(std::errc::io_error));
129}
130
131private:
132// Dispatches incoming message to Handler onNotify/onCall/onReply.
133bool handleMessage(llvm::json::Value Message, MessageHandler &Handler);
134// Writes outgoing message to Out stream.
135void sendMessage(llvm::json::Value Message) {
136OutputBuffer.clear();
137llvm::raw_svector_ostream OS(OutputBuffer);
138OS << llvm::formatv(Pretty ? "{0:2}" : "{0}", Message);
139Out << "Content-Length: " << OutputBuffer.size() << "\r\n\r\n"
140<< OutputBuffer;
141Out.flush();
142vlog(">>> {0}\n", OutputBuffer);
143}
144
145// Read raw string messages from input stream.
146bool readRawMessage(std::string &JSON) {
147return Style == JSONStreamStyle::Delimited ? readDelimitedMessage(JSON)
148: readStandardMessage(JSON);
149}
150bool readDelimitedMessage(std::string &JSON);
151bool readStandardMessage(std::string &JSON);
152
153llvm::SmallVector<char, 0> OutputBuffer;
154std::FILE *In;
155llvm::raw_ostream &Out;
156llvm::raw_ostream &InMirror;
157bool Pretty;
158JSONStreamStyle Style;
159};
160
161bool JSONTransport::handleMessage(llvm::json::Value Message,
162MessageHandler &Handler) {
163// Message must be an object with "jsonrpc":"2.0".
164auto *Object = Message.getAsObject();
165if (!Object ||
166Object->getString("jsonrpc") != std::optional<llvm::StringRef>("2.0")) {
167elog("Not a JSON-RPC 2.0 message: {0:2}", Message);
168return false;
169}
170// ID may be any JSON value. If absent, this is a notification.
171std::optional<llvm::json::Value> ID;
172if (auto *I = Object->get("id"))
173ID = std::move(*I);
174auto Method = Object->getString("method");
175if (!Method) { // This is a response.
176if (!ID) {
177elog("No method and no response ID: {0:2}", Message);
178return false;
179}
180if (auto *Err = Object->getObject("error"))
181return Handler.onReply(std::move(*ID), decodeError(*Err));
182// Result should be given, use null if not.
183llvm::json::Value Result = nullptr;
184if (auto *R = Object->get("result"))
185Result = std::move(*R);
186return Handler.onReply(std::move(*ID), std::move(Result));
187}
188// Params should be given, use null if not.
189llvm::json::Value Params = nullptr;
190if (auto *P = Object->get("params"))
191Params = std::move(*P);
192
193if (ID)
194return Handler.onCall(*Method, std::move(Params), std::move(*ID));
195return Handler.onNotify(*Method, std::move(Params));
196}
197
198// Tries to read a line up to and including \n.
199// If failing, feof(), ferror(), or shutdownRequested() will be set.
200bool 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.
203static constexpr int BufSize = 128;
204size_t Size = 0;
205Out.clear();
206for (;;) {
207Out.resize_for_overwrite(Size + BufSize);
208// Handle EINTR which is sent when a debugger attaches on some platforms.
209if (!retryAfterSignalUnlessShutdown(
210nullptr, [&] { return std::fgets(&Out[Size], BufSize, In); }))
211return false;
212clearerr(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.
215size_t Read = std::strlen(&Out[Size]);
216if (Read > 0 && Out[Size + Read - 1] == '\n') {
217Out.resize(Size + Read);
218return true;
219}
220Size += Read;
221}
222}
223
224// Returns None when:
225// - ferror(), feof(), or shutdownRequested() are set.
226// - Content-Length is missing or empty (protocol error)
227bool 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).
230unsigned long long ContentLength = 0;
231llvm::SmallString<128> Line;
232while (true) {
233if (feof(In) || ferror(In) || !readLine(In, Line))
234return false;
235InMirror << Line;
236
237llvm::StringRef LineRef = Line;
238
239// We allow comments in headers. Technically this isn't part
240
241// of the LSP specification, but makes writing tests easier.
242if (LineRef.starts_with("#"))
243continue;
244
245// Content-Length is a mandatory header, and the only one we handle.
246if (LineRef.consume_front("Content-Length: ")) {
247if (ContentLength != 0) {
248elog("Warning: Duplicate Content-Length header received. "
249"The previous value for this message ({0}) was ignored.",
250ContentLength);
251}
252llvm::getAsUnsignedInteger(LineRef.trim(), 0, ContentLength);
253continue;
254}
255
256// An empty line indicates the end of headers.
257// Go ahead and read the JSON.
258if (LineRef.trim().empty())
259break;
260
261// It's another header, ignore it.
262}
263
264// The fuzzer likes crashing us by sending "Content-Length: 9999999999999999"
265if (ContentLength > 1 << 30) { // 1024M
266elog("Refusing to read message with long Content-Length: {0}. "
267"Expect protocol errors",
268ContentLength);
269return false;
270}
271if (ContentLength == 0) {
272log("Warning: Missing Content-Length header, or zero-length message.");
273return false;
274}
275
276JSON.resize(ContentLength);
277for (size_t Pos = 0, Read; Pos < ContentLength; Pos += Read) {
278// Handle EINTR which is sent when a debugger attaches on some platforms.
279Read = retryAfterSignalUnlessShutdown(0, [&]{
280return std::fread(&JSON[Pos], 1, ContentLength - Pos, In);
281});
282if (Read == 0) {
283elog("Input was aborted. Read only {0} bytes of expected {1}.", Pos,
284ContentLength);
285return false;
286}
287InMirror << llvm::StringRef(&JSON[Pos], Read);
288clearerr(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.
290Pos += Read;
291}
292return true;
293}
294
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.
300bool JSONTransport::readDelimitedMessage(std::string &JSON) {
301JSON.clear();
302llvm::SmallString<128> Line;
303while (readLine(In, Line)) {
304InMirror << Line;
305auto LineRef = Line.str().trim();
306if (LineRef.starts_with("#")) // comment
307continue;
308
309// found a delimiter
310if (LineRef.rtrim() == "---")
311break;
312
313JSON += Line;
314}
315
316if (shutdownRequested())
317return false;
318if (ferror(In)) {
319elog("Input error while reading message!");
320return false;
321}
322return true; // Including at EOF
323}
324
325} // namespace
326
327std::unique_ptr<Transport> newJSONTransport(std::FILE *In,
328llvm::raw_ostream &Out,
329llvm::raw_ostream *InMirror,
330bool Pretty,
331JSONStreamStyle Style) {
332return std::make_unique<JSONTransport>(In, Out, InMirror, Pretty, Style);
333}
334
335} // namespace clangd
336} // namespace clang
337