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