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