Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 1 | //===--- JSONTransport.cpp - sending and receiving LSP messages over JSON -===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | #include "Logger.h" |
| 9 | #include "Protocol.h" // For LSPError |
| 10 | #include "Transport.h" |
| 11 | #include "llvm/Support/Errno.h" |
| 12 | |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 13 | namespace clang { |
| 14 | namespace clangd { |
| 15 | namespace { |
| 16 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 17 | llvm::json::Object encodeError(llvm::Error E) { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 18 | std::string Message; |
| 19 | ErrorCode Code = ErrorCode::UnknownErrorCode; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 20 | if (llvm::Error Unhandled = llvm::handleErrors( |
| 21 | std::move(E), [&](const LSPError &L) -> llvm::Error { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 22 | Message = L.Message; |
| 23 | Code = L.Code; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 24 | return llvm::Error::success(); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 25 | })) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 26 | Message = llvm::toString(std::move(Unhandled)); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 27 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 28 | return llvm::json::Object{ |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 29 | {"message", std::move(Message)}, |
| 30 | {"code", int64_t(Code)}, |
| 31 | }; |
| 32 | } |
| 33 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 34 | llvm::Error decodeError(const llvm::json::Object &O) { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 35 | std::string Msg = O.getString("message").getValueOr("Unspecified error"); |
| 36 | if (auto Code = O.getInteger("code")) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 37 | return llvm::make_error<LSPError>(std::move(Msg), ErrorCode(*Code)); |
| 38 | return llvm::make_error<llvm::StringError>(std::move(Msg), |
| 39 | llvm::inconvertibleErrorCode()); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | class JSONTransport : public Transport { |
| 43 | public: |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 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()), |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 47 | Pretty(Pretty), Style(Style) {} |
| 48 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 49 | void notify(llvm::StringRef Method, llvm::json::Value Params) override { |
| 50 | sendMessage(llvm::json::Object{ |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 51 | {"jsonrpc", "2.0"}, |
| 52 | {"method", Method}, |
| 53 | {"params", std::move(Params)}, |
| 54 | }); |
| 55 | } |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 56 | void call(llvm::StringRef Method, llvm::json::Value Params, |
| 57 | llvm::json::Value ID) override { |
| 58 | sendMessage(llvm::json::Object{ |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 59 | {"jsonrpc", "2.0"}, |
| 60 | {"id", std::move(ID)}, |
| 61 | {"method", Method}, |
| 62 | {"params", std::move(Params)}, |
| 63 | }); |
| 64 | } |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 65 | void reply(llvm::json::Value ID, |
| 66 | llvm::Expected<llvm::json::Value> Result) override { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 67 | if (Result) { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 68 | sendMessage(llvm::json::Object{ |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 69 | {"jsonrpc", "2.0"}, |
| 70 | {"id", std::move(ID)}, |
| 71 | {"result", std::move(*Result)}, |
| 72 | }); |
| 73 | } else { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 74 | sendMessage(llvm::json::Object{ |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 75 | {"jsonrpc", "2.0"}, |
| 76 | {"id", std::move(ID)}, |
| 77 | {"error", encodeError(Result.takeError())}, |
| 78 | }); |
| 79 | } |
| 80 | } |
| 81 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 82 | llvm::Error loop(MessageHandler &Handler) override { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 83 | while (!feof(In)) { |
| 84 | if (ferror(In)) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 85 | return llvm::errorCodeToError( |
| 86 | std::error_code(errno, std::system_category())); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 87 | if (auto JSON = readRawMessage()) { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 88 | if (auto Doc = llvm::json::parse(*JSON)) { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 89 | vlog(Pretty ? "<<< {0:2}\n" : "<<< {0}\n", *Doc); |
| 90 | if (!handleMessage(std::move(*Doc), Handler)) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 91 | return llvm::Error::success(); // we saw the "exit" notification. |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 92 | } else { |
| 93 | // Parse error. Log the raw message. |
| 94 | vlog("<<< {0}\n", *JSON); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 95 | elog("JSON parse error: {0}", llvm::toString(Doc.takeError())); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | } |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 99 | return llvm::errorCodeToError(std::make_error_code(std::errc::io_error)); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | private: |
| 103 | // Dispatches incoming message to Handler onNotify/onCall/onReply. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 104 | bool handleMessage(llvm::json::Value Message, MessageHandler &Handler); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 105 | // Writes outgoing message to Out stream. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 106 | void sendMessage(llvm::json::Value Message) { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 107 | std::string S; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 108 | llvm::raw_string_ostream OS(S); |
| 109 | OS << llvm::formatv(Pretty ? "{0:2}" : "{0}", Message); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 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. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 117 | llvm::Optional<std::string> readRawMessage() { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 118 | return Style == JSONStreamStyle::Delimited ? readDelimitedMessage() |
| 119 | : readStandardMessage(); |
| 120 | } |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 121 | llvm::Optional<std::string> readDelimitedMessage(); |
| 122 | llvm::Optional<std::string> readStandardMessage(); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 123 | |
| 124 | std::FILE *In; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 125 | llvm::raw_ostream &Out; |
| 126 | llvm::raw_ostream &InMirror; |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 127 | bool Pretty; |
| 128 | JSONStreamStyle Style; |
| 129 | }; |
| 130 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 131 | bool JSONTransport::handleMessage(llvm::json::Value Message, |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 132 | MessageHandler &Handler) { |
| 133 | // Message must be an object with "jsonrpc":"2.0". |
| 134 | auto *Object = Message.getAsObject(); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 135 | if (!Object || |
| 136 | Object->getString("jsonrpc") != llvm::Optional<llvm::StringRef>("2.0")) { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 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. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 141 | llvm::Optional<llvm::json::Value> ID; |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 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. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 153 | llvm::json::Value Result = nullptr; |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 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. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 159 | llvm::json::Value Params = nullptr; |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 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. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 178 | if (!llvm::sys::RetryAfterSignal(nullptr, ::fgets, &Out[Size], BufSize, In)) |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 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) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 195 | llvm::Optional<std::string> JSONTransport::readStandardMessage() { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 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)) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 202 | return llvm::None; |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 203 | InMirror << Line; |
| 204 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 205 | llvm::StringRef LineRef(Line); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 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 | } |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 220 | llvm::getAsUnsignedInteger(LineRef.trim(), 0, ContentLength); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 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); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 237 | return llvm::None; |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 238 | } |
| 239 | if (ContentLength == 0) { |
| 240 | log("Warning: Missing Content-Length header, or zero-length message."); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 241 | return llvm::None; |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 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. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 247 | Read = llvm::sys::RetryAfterSignal(0u, ::fread, &JSON[Pos], 1, |
| 248 | ContentLength - Pos, In); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 249 | if (Read == 0) { |
| 250 | elog("Input was aborted. Read only {0} bytes of expected {1}.", Pos, |
| 251 | ContentLength); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 252 | return llvm::None; |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 253 | } |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 254 | InMirror << llvm::StringRef(&JSON[Pos], Read); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 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. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 267 | llvm::Optional<std::string> JSONTransport::readDelimitedMessage() { |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 268 | std::string JSON; |
| 269 | std::string Line; |
| 270 | while (readLine(In, Line)) { |
| 271 | InMirror << Line; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 272 | auto LineRef = llvm::StringRef(Line).trim(); |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 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!"); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 285 | return llvm::None; |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 286 | } |
| 287 | return std::move(JSON); // Including at EOF |
| 288 | } |
| 289 | |
| 290 | } // namespace |
| 291 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 292 | std::unique_ptr<Transport> newJSONTransport(std::FILE *In, |
| 293 | llvm::raw_ostream &Out, |
| 294 | llvm::raw_ostream *InMirror, |
| 295 | bool Pretty, |
Sam McCall | dc8f3cf | 2018-10-17 07:32:05 +0000 | [diff] [blame] | 296 | JSONStreamStyle Style) { |
| 297 | return llvm::make_unique<JSONTransport>(In, Out, InMirror, Pretty, Style); |
| 298 | } |
| 299 | |
| 300 | } // namespace clangd |
| 301 | } // namespace clang |