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