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