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