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