| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 1 | //===--- JSONRPCDispatcher.cpp - Main JSON parser entry point -------------===// |
| 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 | |
| 10 | #include "JSONRPCDispatcher.h" |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 11 | #include "JSONExpr.h" |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 12 | #include "ProtocolHandlers.h" |
| Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 13 | #include "Trace.h" |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SmallString.h" |
| Sam McCall | 94362c6 | 2017-11-07 14:45:31 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringExtras.h" |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 16 | #include "llvm/Support/SourceMgr.h" |
| 17 | #include "llvm/Support/YAMLParser.h" |
| Ilya Biryukov | 687b92a | 2017-05-16 15:23:55 +0000 | [diff] [blame] | 18 | #include <istream> |
| 19 | |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | using namespace clangd; |
| 22 | |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 23 | void JSONOutput::writeMessage(const json::Expr &Message) { |
| 24 | std::string S; |
| 25 | llvm::raw_string_ostream OS(S); |
| 26 | if (Pretty) |
| 27 | OS << llvm::formatv("{0:2}", Message); |
| 28 | else |
| 29 | OS << Message; |
| 30 | OS.flush(); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 31 | |
| Benjamin Kramer | d0b2ccd | 2017-02-10 14:08:40 +0000 | [diff] [blame] | 32 | std::lock_guard<std::mutex> Guard(StreamMutex); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 33 | // Log without headers. |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 34 | Logs << "--> " << S << '\n'; |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 35 | Logs.flush(); |
| 36 | |
| 37 | // Emit message with header. |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 38 | Outs << "Content-Length: " << S.size() << "\r\n\r\n" << S; |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 39 | Outs.flush(); |
| 40 | } |
| 41 | |
| Benjamin Kramer | e14bd42 | 2017-02-15 16:44:11 +0000 | [diff] [blame] | 42 | void JSONOutput::log(const Twine &Message) { |
| Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 43 | trace::log(Message); |
| Benjamin Kramer | e14bd42 | 2017-02-15 16:44:11 +0000 | [diff] [blame] | 44 | std::lock_guard<std::mutex> Guard(StreamMutex); |
| 45 | Logs << Message; |
| 46 | Logs.flush(); |
| 47 | } |
| 48 | |
| Ilya Biryukov | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame] | 49 | void JSONOutput::mirrorInput(const Twine &Message) { |
| 50 | if (!InputMirror) |
| 51 | return; |
| 52 | |
| 53 | *InputMirror << Message; |
| 54 | InputMirror->flush(); |
| 55 | } |
| 56 | |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 57 | void RequestContext::reply(json::Expr &&Result) { |
| 58 | if (!ID) { |
| Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 59 | Out.log("Attempted to reply to a notification!\n"); |
| 60 | return; |
| 61 | } |
| Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 62 | SPAN_ATTACH(tracer(), "Reply", Result); |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 63 | Out.writeMessage(json::obj{ |
| 64 | {"jsonrpc", "2.0"}, |
| 65 | {"id", *ID}, |
| 66 | {"result", std::move(Result)}, |
| 67 | }); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| Ilya Biryukov | 9e11c4c | 2017-11-15 18:04:56 +0000 | [diff] [blame] | 70 | void RequestContext::replyError(ErrorCode code, |
| 71 | const llvm::StringRef &Message) { |
| Haojian Wu | 2375c92 | 2017-11-07 10:21:02 +0000 | [diff] [blame] | 72 | Out.log("Error " + Twine(static_cast<int>(code)) + ": " + Message + "\n"); |
| Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 73 | SPAN_ATTACH(tracer(), "Error", |
| 74 | (json::obj{{"code", static_cast<int>(code)}, |
| 75 | {"message", Message.str()}})); |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 76 | if (ID) { |
| 77 | Out.writeMessage(json::obj{ |
| 78 | {"jsonrpc", "2.0"}, |
| 79 | {"id", *ID}, |
| Ilya Biryukov | 9e11c4c | 2017-11-15 18:04:56 +0000 | [diff] [blame] | 80 | {"error", |
| 81 | json::obj{{"code", static_cast<int>(code)}, {"message", Message}}}, |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 82 | }); |
| Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 83 | } |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 86 | void RequestContext::call(StringRef Method, json::Expr &&Params) { |
| Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 87 | // FIXME: Generate/Increment IDs for every request so that we can get proper |
| 88 | // replies once we need to. |
| Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 89 | SPAN_ATTACH(tracer(), "Call", |
| 90 | (json::obj{{"method", Method.str()}, {"params", Params}})); |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 91 | Out.writeMessage(json::obj{ |
| 92 | {"jsonrpc", "2.0"}, |
| 93 | {"id", 1}, |
| 94 | {"method", Method}, |
| 95 | {"params", std::move(Params)}, |
| 96 | }); |
| Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 99 | void JSONRPCDispatcher::registerHandler(StringRef Method, Handler H) { |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 100 | assert(!Handlers.count(Method) && "Handler already registered!"); |
| 101 | Handlers[Method] = std::move(H); |
| 102 | } |
| 103 | |
| 104 | static void |
| Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 105 | callHandler(const llvm::StringMap<JSONRPCDispatcher::Handler> &Handlers, |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 106 | llvm::yaml::ScalarNode *Method, llvm::Optional<json::Expr> ID, |
| Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 107 | llvm::yaml::MappingNode *Params, |
| 108 | const JSONRPCDispatcher::Handler &UnknownHandler, JSONOutput &Out) { |
| 109 | llvm::SmallString<64> MethodStorage; |
| Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 110 | llvm::StringRef MethodStr = Method->getValue(MethodStorage); |
| 111 | auto I = Handlers.find(MethodStr); |
| Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 112 | auto &Handler = I != Handlers.end() ? I->second : UnknownHandler; |
| Sam McCall | 9cfd9c9 | 2017-11-23 17:12:04 +0000 | [diff] [blame] | 113 | Handler(RequestContext(Out, MethodStr, std::move(ID)), Params); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 116 | bool JSONRPCDispatcher::call(StringRef Content, JSONOutput &Out) const { |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 117 | llvm::SourceMgr SM; |
| 118 | llvm::yaml::Stream YAMLStream(Content, SM); |
| 119 | |
| 120 | auto Doc = YAMLStream.begin(); |
| 121 | if (Doc == YAMLStream.end()) |
| 122 | return false; |
| 123 | |
| Benjamin Kramer | decd8a7 | 2017-10-27 16:33:15 +0000 | [diff] [blame] | 124 | auto *Object = dyn_cast_or_null<llvm::yaml::MappingNode>(Doc->getRoot()); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 125 | if (!Object) |
| 126 | return false; |
| 127 | |
| 128 | llvm::yaml::ScalarNode *Version = nullptr; |
| 129 | llvm::yaml::ScalarNode *Method = nullptr; |
| 130 | llvm::yaml::MappingNode *Params = nullptr; |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 131 | llvm::Optional<json::Expr> ID; |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 132 | for (auto &NextKeyValue : *Object) { |
| Benjamin Kramer | decd8a7 | 2017-10-27 16:33:15 +0000 | [diff] [blame] | 133 | auto *KeyString = |
| 134 | dyn_cast_or_null<llvm::yaml::ScalarNode>(NextKeyValue.getKey()); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 135 | if (!KeyString) |
| 136 | return false; |
| 137 | |
| 138 | llvm::SmallString<10> KeyStorage; |
| 139 | StringRef KeyValue = KeyString->getValue(KeyStorage); |
| 140 | llvm::yaml::Node *Value = NextKeyValue.getValue(); |
| 141 | if (!Value) |
| 142 | return false; |
| 143 | |
| 144 | if (KeyValue == "jsonrpc") { |
| 145 | // This should be "2.0". Always. |
| 146 | Version = dyn_cast<llvm::yaml::ScalarNode>(Value); |
| 147 | if (!Version || Version->getRawValue() != "\"2.0\"") |
| 148 | return false; |
| 149 | } else if (KeyValue == "method") { |
| 150 | Method = dyn_cast<llvm::yaml::ScalarNode>(Value); |
| 151 | } else if (KeyValue == "id") { |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 152 | // ID may be either a string or a number. |
| 153 | if (auto *IdNode = dyn_cast<llvm::yaml::ScalarNode>(Value)) { |
| 154 | llvm::SmallString<32> S; |
| 155 | llvm::StringRef V = IdNode->getValue(S); |
| 156 | if (IdNode->getRawValue().startswith("\"")) { |
| 157 | ID.emplace(V.str()); |
| 158 | } else { |
| 159 | double D; |
| Sam McCall | 94362c6 | 2017-11-07 14:45:31 +0000 | [diff] [blame] | 160 | // FIXME: this is locale-sensitive. |
| 161 | if (llvm::to_float(V, D)) |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 162 | ID.emplace(D); |
| 163 | } |
| 164 | } |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 165 | } else if (KeyValue == "params") { |
| 166 | if (!Method) |
| 167 | return false; |
| 168 | // We have to interleave the call of the function here, otherwise the |
| 169 | // YAMLParser will die because it can't go backwards. This is unfortunate |
| 170 | // because it will break clients that put the id after params. A possible |
| 171 | // fix would be to split the parsing and execution phases. |
| 172 | Params = dyn_cast<llvm::yaml::MappingNode>(Value); |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 173 | callHandler(Handlers, Method, std::move(ID), Params, UnknownHandler, Out); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 174 | return true; |
| 175 | } else { |
| 176 | return false; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // In case there was a request with no params, call the handler on the |
| 181 | // leftovers. |
| 182 | if (!Method) |
| 183 | return false; |
| Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 184 | callHandler(Handlers, Method, std::move(ID), nullptr, UnknownHandler, Out); |
| Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 185 | |
| 186 | return true; |
| 187 | } |
| Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 188 | |
| 189 | void clangd::runLanguageServerLoop(std::istream &In, JSONOutput &Out, |
| 190 | JSONRPCDispatcher &Dispatcher, |
| 191 | bool &IsDone) { |
| 192 | while (In.good()) { |
| Ilya Biryukov | 1fab4f8 | 2017-09-04 12:28:15 +0000 | [diff] [blame] | 193 | // A Language Server Protocol message starts with a set of HTTP headers, |
| 194 | // delimited by \r\n, and terminated by an empty line (\r\n). |
| 195 | unsigned long long ContentLength = 0; |
| 196 | while (In.good()) { |
| 197 | std::string Line; |
| 198 | std::getline(In, Line); |
| 199 | if (!In.good() && errno == EINTR) { |
| 200 | In.clear(); |
| 201 | continue; |
| 202 | } |
| 203 | |
| Ilya Biryukov | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame] | 204 | Out.mirrorInput(Line); |
| 205 | // Mirror '\n' that gets consumed by std::getline, but is not included in |
| 206 | // the resulting Line. |
| 207 | // Note that '\r' is part of Line, so we don't need to mirror it |
| 208 | // separately. |
| 209 | if (!In.eof()) |
| 210 | Out.mirrorInput("\n"); |
| 211 | |
| Ilya Biryukov | 1fab4f8 | 2017-09-04 12:28:15 +0000 | [diff] [blame] | 212 | llvm::StringRef LineRef(Line); |
| 213 | |
| 214 | // We allow YAML-style comments in headers. Technically this isn't part |
| 215 | // of the LSP specification, but makes writing tests easier. |
| 216 | if (LineRef.startswith("#")) |
| 217 | continue; |
| 218 | |
| 219 | // Content-Type is a specified header, but does nothing. |
| 220 | // Content-Length is a mandatory header. It specifies the length of the |
| 221 | // following JSON. |
| 222 | // It is unspecified what sequence headers must be supplied in, so we |
| 223 | // allow any sequence. |
| 224 | // The end of headers is signified by an empty line. |
| 225 | if (LineRef.consume_front("Content-Length: ")) { |
| 226 | if (ContentLength != 0) { |
| 227 | Out.log("Warning: Duplicate Content-Length header received. " |
| Ilya Biryukov | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame] | 228 | "The previous value for this message (" + |
| 229 | std::to_string(ContentLength) + ") was ignored.\n"); |
| Ilya Biryukov | 1fab4f8 | 2017-09-04 12:28:15 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | llvm::getAsUnsignedInteger(LineRef.trim(), 0, ContentLength); |
| 233 | continue; |
| 234 | } else if (!LineRef.trim().empty()) { |
| 235 | // It's another header, ignore it. |
| 236 | continue; |
| 237 | } else { |
| 238 | // An empty line indicates the end of headers. |
| 239 | // Go ahead and read the JSON. |
| 240 | break; |
| 241 | } |
| Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| Benjamin Kramer | 1d05379 | 2017-10-27 17:06:41 +0000 | [diff] [blame] | 244 | // Guard against large messages. This is usually a bug in the client code |
| 245 | // and we don't want to crash downstream because of it. |
| 246 | if (ContentLength > 1 << 30) { // 1024M |
| 247 | In.ignore(ContentLength); |
| 248 | Out.log("Skipped overly large message of " + Twine(ContentLength) + |
| 249 | " bytes.\n"); |
| 250 | continue; |
| 251 | } |
| 252 | |
| Ilya Biryukov | 1fab4f8 | 2017-09-04 12:28:15 +0000 | [diff] [blame] | 253 | if (ContentLength > 0) { |
| Ilya Biryukov | 1fab4f8 | 2017-09-04 12:28:15 +0000 | [diff] [blame] | 254 | std::vector<char> JSON(ContentLength + 1, '\0'); |
| Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 255 | llvm::StringRef JSONRef; |
| 256 | { |
| Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 257 | // Now read the JSON. Insert a trailing null byte as required by the |
| 258 | // YAML parser. |
| 259 | In.read(JSON.data(), ContentLength); |
| 260 | Out.mirrorInput(StringRef(JSON.data(), In.gcount())); |
| Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 261 | |
| Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 262 | // If the stream is aborted before we read ContentLength bytes, In |
| 263 | // will have eofbit and failbit set. |
| 264 | if (!In) { |
| 265 | Out.log("Input was aborted. Read only " + |
| 266 | std::to_string(In.gcount()) + " bytes of expected " + |
| 267 | std::to_string(ContentLength) + ".\n"); |
| 268 | break; |
| 269 | } |
| 270 | |
| 271 | JSONRef = StringRef(JSON.data(), ContentLength); |
| Ilya Biryukov | 1fab4f8 | 2017-09-04 12:28:15 +0000 | [diff] [blame] | 272 | } |
| Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 273 | |
| Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 274 | // Log the message. |
| 275 | Out.log("<-- " + JSONRef + "\n"); |
| 276 | |
| 277 | // Finally, execute the action for this JSON message. |
| Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 278 | if (!Dispatcher.call(JSONRef, Out)) |
| Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 279 | Out.log("JSON dispatch failed!\n"); |
| 280 | |
| 281 | // If we're done, exit the loop. |
| 282 | if (IsDone) |
| 283 | break; |
| Ilya Biryukov | 1fab4f8 | 2017-09-04 12:28:15 +0000 | [diff] [blame] | 284 | } else { |
| Ilya Biryukov | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame] | 285 | Out.log("Warning: Missing Content-Length header, or message has zero " |
| 286 | "length.\n"); |
| Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | } |