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" |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 12 | #include "Trace.h" |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallString.h" |
Sam McCall | 94362c6 | 2017-11-07 14:45:31 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringExtras.h" |
Sam McCall | a90f257 | 2018-02-16 16:41:42 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Chrono.h" |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Errno.h" |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame^] | 17 | #include "llvm/Support/JSON.h" |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 18 | #include "llvm/Support/SourceMgr.h" |
Ilya Biryukov | 687b92a | 2017-05-16 15:23:55 +0000 | [diff] [blame] | 19 | #include <istream> |
| 20 | |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame^] | 21 | using namespace llvm; |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | using namespace clangd; |
| 24 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 25 | namespace { |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame^] | 26 | static Key<json::Value> RequestID; |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 27 | static Key<JSONOutput *> RequestOut; |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 28 | |
| 29 | // When tracing, we trace a request and attach the repsonse in reply(). |
| 30 | // Because the Span isn't available, we find the current request using Context. |
| 31 | class RequestSpan { |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame^] | 32 | RequestSpan(llvm::json::Object *Args) : Args(Args) {} |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 33 | std::mutex Mu; |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame^] | 34 | llvm::json::Object *Args; |
Sam McCall | 24f0fa3 | 2018-01-26 11:23:33 +0000 | [diff] [blame] | 35 | static Key<std::unique_ptr<RequestSpan>> RSKey; |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 36 | |
| 37 | public: |
| 38 | // Return a context that's aware of the enclosing request, identified by Span. |
| 39 | static Context stash(const trace::Span &Span) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 40 | return Context::current().derive( |
| 41 | RSKey, std::unique_ptr<RequestSpan>(new RequestSpan(Span.Args))); |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // If there's an enclosing request and the tracer is interested, calls \p F |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame^] | 45 | // with a json::Object where request info can be added. |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 46 | template <typename Func> static void attach(Func &&F) { |
| 47 | auto *RequestArgs = Context::current().get(RSKey); |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 48 | if (!RequestArgs || !*RequestArgs || !(*RequestArgs)->Args) |
| 49 | return; |
| 50 | std::lock_guard<std::mutex> Lock((*RequestArgs)->Mu); |
| 51 | F(*(*RequestArgs)->Args); |
| 52 | } |
| 53 | }; |
Sam McCall | 24f0fa3 | 2018-01-26 11:23:33 +0000 | [diff] [blame] | 54 | Key<std::unique_ptr<RequestSpan>> RequestSpan::RSKey; |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 55 | } // namespace |
| 56 | |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame^] | 57 | void JSONOutput::writeMessage(const json::Value &Message) { |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 58 | std::string S; |
| 59 | llvm::raw_string_ostream OS(S); |
| 60 | if (Pretty) |
| 61 | OS << llvm::formatv("{0:2}", Message); |
| 62 | else |
| 63 | OS << Message; |
| 64 | OS.flush(); |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 65 | |
Sam McCall | a90f257 | 2018-02-16 16:41:42 +0000 | [diff] [blame] | 66 | { |
| 67 | std::lock_guard<std::mutex> Guard(StreamMutex); |
| 68 | Outs << "Content-Length: " << S.size() << "\r\n\r\n" << S; |
| 69 | Outs.flush(); |
| 70 | } |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 71 | log(llvm::Twine("--> ") + S + "\n"); |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 74 | void JSONOutput::log(const Twine &Message) { |
Sam McCall | a90f257 | 2018-02-16 16:41:42 +0000 | [diff] [blame] | 75 | llvm::sys::TimePoint<> Timestamp = std::chrono::system_clock::now(); |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 76 | trace::log(Message); |
Benjamin Kramer | e14bd42 | 2017-02-15 16:44:11 +0000 | [diff] [blame] | 77 | std::lock_guard<std::mutex> Guard(StreamMutex); |
Sam McCall | a90f257 | 2018-02-16 16:41:42 +0000 | [diff] [blame] | 78 | Logs << llvm::formatv("[{0:%H:%M:%S.%L}] {1}\n", Timestamp, Message); |
Benjamin Kramer | e14bd42 | 2017-02-15 16:44:11 +0000 | [diff] [blame] | 79 | Logs.flush(); |
| 80 | } |
| 81 | |
Ilya Biryukov | e6dbb58 | 2017-10-10 09:08:47 +0000 | [diff] [blame] | 82 | void JSONOutput::mirrorInput(const Twine &Message) { |
| 83 | if (!InputMirror) |
| 84 | return; |
| 85 | |
| 86 | *InputMirror << Message; |
| 87 | InputMirror->flush(); |
| 88 | } |
| 89 | |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame^] | 90 | void clangd::reply(json::Value &&Result) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 91 | auto ID = Context::current().get(RequestID); |
Sam McCall | dd0566b | 2017-11-06 15:40:30 +0000 | [diff] [blame] | 92 | if (!ID) { |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 93 | log("Attempted to reply to a notification!"); |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 94 | return; |
| 95 | } |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame^] | 96 | RequestSpan::attach([&](json::Object &Args) { Args["Reply"] = Result; }); |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 97 | Context::current() |
| 98 | .getExisting(RequestOut) |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame^] | 99 | ->writeMessage(json::Object{ |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 100 | {"jsonrpc", "2.0"}, |
| 101 | {"id", *ID}, |
| 102 | {"result", std::move(Result)}, |
| 103 | }); |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 106 | void clangd::replyError(ErrorCode code, const llvm::StringRef &Message) { |
| 107 | log("Error " + Twine(static_cast<int>(code)) + ": " + Message); |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame^] | 108 | RequestSpan::attach([&](json::Object &Args) { |
| 109 | Args["Error"] = json::Object{{"code", static_cast<int>(code)}, |
| 110 | {"message", Message.str()}}; |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 111 | }); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 112 | |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 113 | if (auto ID = Context::current().get(RequestID)) { |
| 114 | Context::current() |
| 115 | .getExisting(RequestOut) |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame^] | 116 | ->writeMessage(json::Object{ |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 117 | {"jsonrpc", "2.0"}, |
| 118 | {"id", *ID}, |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame^] | 119 | {"error", json::Object{{"code", static_cast<int>(code)}, |
| 120 | {"message", Message}}}, |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 121 | }); |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 122 | } |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame^] | 125 | void clangd::call(StringRef Method, json::Value &&Params) { |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 126 | // FIXME: Generate/Increment IDs for every request so that we can get proper |
| 127 | // replies once we need to. |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame^] | 128 | RequestSpan::attach([&](json::Object &Args) { |
| 129 | Args["Call"] = json::Object{{"method", Method.str()}, {"params", Params}}; |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 130 | }); |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 131 | Context::current() |
| 132 | .getExisting(RequestOut) |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame^] | 133 | ->writeMessage(json::Object{ |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 134 | {"jsonrpc", "2.0"}, |
| 135 | {"id", 1}, |
| 136 | {"method", Method}, |
| 137 | {"params", std::move(Params)}, |
| 138 | }); |
Marc-Andre Laperle | e7ec16a | 2017-11-03 13:39:15 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Sam McCall | 8a5dded | 2017-10-12 13:29:58 +0000 | [diff] [blame] | 141 | void JSONRPCDispatcher::registerHandler(StringRef Method, Handler H) { |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 142 | assert(!Handlers.count(Method) && "Handler already registered!"); |
| 143 | Handlers[Method] = std::move(H); |
| 144 | } |
| 145 | |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame^] | 146 | bool JSONRPCDispatcher::call(const json::Value &Message, |
| 147 | JSONOutput &Out) const { |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 148 | // Message must be an object with "jsonrpc":"2.0". |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame^] | 149 | auto *Object = Message.getAsObject(); |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 150 | if (!Object || Object->getString("jsonrpc") != Optional<StringRef>("2.0")) |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 151 | return false; |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 152 | // ID may be any JSON value. If absent, this is a notification. |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame^] | 153 | llvm::Optional<json::Value> ID; |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 154 | if (auto *I = Object->get("id")) |
| 155 | ID = std::move(*I); |
| 156 | // Method must be given. |
| 157 | auto Method = Object->getString("method"); |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 158 | if (!Method) |
| 159 | return false; |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 160 | // Params should be given, use null if not. |
Sam McCall | d20d798 | 2018-07-09 14:25:59 +0000 | [diff] [blame^] | 161 | json::Value Params = nullptr; |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 162 | if (auto *P = Object->get("params")) |
| 163 | Params = std::move(*P); |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 164 | |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 165 | auto I = Handlers.find(*Method); |
| 166 | auto &Handler = I != Handlers.end() ? I->second : UnknownHandler; |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 167 | |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 168 | // Create a Context that contains request information. |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 169 | WithContextValue WithRequestOut(RequestOut, &Out); |
| 170 | llvm::Optional<WithContextValue> WithID; |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 171 | if (ID) |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 172 | WithID.emplace(RequestID, *ID); |
Ilya Biryukov | ee27d2e | 2017-12-14 15:04:59 +0000 | [diff] [blame] | 173 | |
| 174 | // Create a tracing Span covering the whole request lifetime. |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 175 | trace::Span Tracer(*Method); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 176 | if (ID) |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 177 | SPAN_ATTACH(Tracer, "ID", *ID); |
| 178 | SPAN_ATTACH(Tracer, "Params", Params); |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 179 | |
Sam McCall | 1b475a1 | 2018-01-26 09:00:30 +0000 | [diff] [blame] | 180 | // Stash a reference to the span args, so later calls can add metadata. |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 181 | WithContext WithRequestSpan(RequestSpan::stash(Tracer)); |
| 182 | Handler(std::move(Params)); |
Benjamin Kramer | bb1cdb6 | 2017-02-07 10:28:20 +0000 | [diff] [blame] | 183 | return true; |
| 184 | } |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 185 | |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 186 | // Tries to read a line up to and including \n. |
| 187 | // If failing, feof() or ferror() will be set. |
| 188 | static bool readLine(std::FILE *In, std::string &Out) { |
| 189 | static constexpr int BufSize = 1024; |
| 190 | size_t Size = 0; |
| 191 | Out.clear(); |
| 192 | for (;;) { |
| 193 | Out.resize(Size + BufSize); |
| 194 | // Handle EINTR which is sent when a debugger attaches on some platforms. |
| 195 | if (!llvm::sys::RetryAfterSignal(nullptr, ::fgets, &Out[Size], BufSize, In)) |
| 196 | return false; |
| 197 | clearerr(In); |
| 198 | // If the line contained null bytes, anything after it (including \n) will |
| 199 | // be ignored. Fortunately this is not a legal header or JSON. |
| 200 | size_t Read = std::strlen(&Out[Size]); |
| 201 | if (Read > 0 && Out[Size + Read - 1] == '\n') { |
| 202 | Out.resize(Size + Read); |
| 203 | return true; |
| 204 | } |
| 205 | Size += Read; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // Returns None when: |
| 210 | // - ferror() or feof() are set. |
| 211 | // - Content-Length is missing or empty (protocol error) |
| 212 | static llvm::Optional<std::string> readStandardMessage(std::FILE *In, |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 213 | JSONOutput &Out) { |
| 214 | // A Language Server Protocol message starts with a set of HTTP headers, |
| 215 | // delimited by \r\n, and terminated by an empty line (\r\n). |
| 216 | unsigned long long ContentLength = 0; |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 217 | std::string Line; |
| 218 | while (true) { |
| 219 | if (feof(In) || ferror(In) || !readLine(In, Line)) |
| 220 | return llvm::None; |
Benjamin Kramer | 1d05379 | 2017-10-27 17:06:41 +0000 | [diff] [blame] | 221 | |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 222 | Out.mirrorInput(Line); |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 223 | llvm::StringRef LineRef(Line); |
Sam McCall | 8567cb3 | 2017-11-02 09:21:51 +0000 | [diff] [blame] | 224 | |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 225 | // We allow comments in headers. Technically this isn't part |
| 226 | // of the LSP specification, but makes writing tests easier. |
| 227 | if (LineRef.startswith("#")) |
| 228 | continue; |
| 229 | |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 230 | // Content-Length is a mandatory header, and the only one we handle. |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 231 | if (LineRef.consume_front("Content-Length: ")) { |
| 232 | if (ContentLength != 0) { |
| 233 | log("Warning: Duplicate Content-Length header received. " |
| 234 | "The previous value for this message (" + |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 235 | llvm::Twine(ContentLength) + ") was ignored."); |
Ilya Biryukov | 1fab4f8 | 2017-09-04 12:28:15 +0000 | [diff] [blame] | 236 | } |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 237 | llvm::getAsUnsignedInteger(LineRef.trim(), 0, ContentLength); |
| 238 | continue; |
| 239 | } else if (!LineRef.trim().empty()) { |
| 240 | // It's another header, ignore it. |
| 241 | continue; |
| 242 | } else { |
| 243 | // An empty line indicates the end of headers. |
| 244 | // Go ahead and read the JSON. |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 249 | // The fuzzer likes crashing us by sending "Content-Length: 9999999999999999" |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 250 | if (ContentLength > 1 << 30) { // 1024M |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 251 | log("Refusing to read message with long Content-Length: " + |
| 252 | Twine(ContentLength) + ". Expect protocol errors."); |
| 253 | return llvm::None; |
| 254 | } |
| 255 | if (ContentLength == 0) { |
| 256 | log("Warning: Missing Content-Length header, or zero-length message."); |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 257 | return llvm::None; |
| 258 | } |
| 259 | |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 260 | std::string JSON(ContentLength, '\0'); |
| 261 | for (size_t Pos = 0, Read; Pos < ContentLength; Pos += Read) { |
| 262 | // Handle EINTR which is sent when a debugger attaches on some platforms. |
| 263 | Read = llvm::sys::RetryAfterSignal(0u, ::fread, &JSON[Pos], 1, |
| 264 | ContentLength - Pos, In); |
| 265 | Out.mirrorInput(StringRef(&JSON[Pos], Read)); |
| 266 | if (Read == 0) { |
| 267 | log("Input was aborted. Read only " + llvm::Twine(Pos) + |
| 268 | " bytes of expected " + llvm::Twine(ContentLength) + "."); |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 269 | return llvm::None; |
| 270 | } |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 271 | clearerr(In); // If we're done, the error was transient. If we're not done, |
| 272 | // either it was transient or we'll see it again on retry. |
| 273 | Pos += Read; |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 274 | } |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 275 | return std::move(JSON); |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | // For lit tests we support a simplified syntax: |
| 279 | // - messages are delimited by '---' on a line by itself |
| 280 | // - lines starting with # are ignored. |
| 281 | // This is a testing path, so favor simplicity over performance here. |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 282 | // When returning None, feof() or ferror() will be set. |
| 283 | static llvm::Optional<std::string> readDelimitedMessage(std::FILE *In, |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 284 | JSONOutput &Out) { |
| 285 | std::string JSON; |
| 286 | std::string Line; |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 287 | while (readLine(In, Line)) { |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 288 | auto LineRef = llvm::StringRef(Line).trim(); |
| 289 | if (LineRef.startswith("#")) // comment |
| 290 | continue; |
| 291 | |
Jan Korous | 6243515 | 2018-04-23 15:55:07 +0000 | [diff] [blame] | 292 | // found a delimiter |
Jan Korous | 1bc528c | 2018-04-23 15:58:42 +0000 | [diff] [blame] | 293 | if (LineRef.rtrim() == "---") |
Jan Korous | 6243515 | 2018-04-23 15:55:07 +0000 | [diff] [blame] | 294 | break; |
| 295 | |
| 296 | JSON += Line; |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 299 | if (ferror(In)) { |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 300 | log("Input error while reading message!"); |
| 301 | return llvm::None; |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 302 | } else { // Including EOF |
Jan Korous | 6243515 | 2018-04-23 15:55:07 +0000 | [diff] [blame] | 303 | Out.mirrorInput( |
| 304 | llvm::formatv("Content-Length: {0}\r\n\r\n{1}", JSON.size(), JSON)); |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 305 | return std::move(JSON); |
| 306 | } |
| 307 | } |
| 308 | |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 309 | // The use of C-style std::FILE* IO deserves some explanation. |
| 310 | // Previously, std::istream was used. When a debugger attached on MacOS, the |
| 311 | // process received EINTR, the stream went bad, and clangd exited. |
| 312 | // A retry-on-EINTR loop around reads solved this problem, but caused clangd to |
| 313 | // sometimes hang rather than exit on other OSes. The interaction between |
| 314 | // istreams and signals isn't well-specified, so it's hard to get this right. |
| 315 | // The C APIs seem to be clearer in this respect. |
| 316 | void clangd::runLanguageServerLoop(std::FILE *In, JSONOutput &Out, |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 317 | JSONStreamStyle InputStyle, |
| 318 | JSONRPCDispatcher &Dispatcher, |
| 319 | bool &IsDone) { |
| 320 | auto &ReadMessage = |
| 321 | (InputStyle == Delimited) ? readDelimitedMessage : readStandardMessage; |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 322 | while (!IsDone && !feof(In)) { |
| 323 | if (ferror(In)) { |
| 324 | log("IO error: " + llvm::sys::StrError()); |
| 325 | return; |
| 326 | } |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 327 | if (auto JSON = ReadMessage(In, Out)) { |
| 328 | if (auto Doc = json::parse(*JSON)) { |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 329 | // Log the formatted message. |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 330 | log(llvm::formatv(Out.Pretty ? "<-- {0:2}\n" : "<-- {0}\n", *Doc)); |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 331 | // Finally, execute the action for this JSON message. |
| 332 | if (!Dispatcher.call(*Doc, Out)) |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 333 | log("JSON dispatch failed!"); |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 334 | } else { |
| 335 | // Parse error. Log the raw message. |
Sam McCall | 5ed599e | 2018-02-06 10:47:30 +0000 | [diff] [blame] | 336 | log(llvm::formatv("<-- {0}\n" , *JSON)); |
Sam McCall | d1a7a37 | 2018-01-31 13:40:48 +0000 | [diff] [blame] | 337 | log(llvm::Twine("JSON parse error: ") + |
Sam McCall | 27a07cf | 2018-06-05 09:34:46 +0000 | [diff] [blame] | 338 | llvm::toString(Doc.takeError())); |
Sam McCall | ec10902 | 2017-11-28 09:37:43 +0000 | [diff] [blame] | 339 | } |
Ilya Biryukov | afb5554 | 2017-05-16 14:40:30 +0000 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | } |