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