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