blob: e9a203d77f2ac2b59ef42ea16c43fccb18bdae51 [file] [log] [blame]
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +00001//===--- 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 McCalldd0566b2017-11-06 15:40:30 +000011#include "JSONExpr.h"
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000012#include "ProtocolHandlers.h"
Sam McCall8567cb32017-11-02 09:21:51 +000013#include "Trace.h"
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000014#include "llvm/ADT/SmallString.h"
Sam McCall94362c62017-11-07 14:45:31 +000015#include "llvm/ADT/StringExtras.h"
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000016#include "llvm/Support/SourceMgr.h"
Ilya Biryukov687b92a2017-05-16 15:23:55 +000017#include <istream>
18
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000019using namespace clang;
20using namespace clangd;
21
Ilya Biryukov940901e2017-12-13 12:51:22 +000022namespace {
23static Key<std::unique_ptr<trace::Span>> RequestSpan;
24static Key<json::Expr> RequestID;
25static Key<JSONOutput *> RequestOut;
26} // namespace
27
Sam McCalldd0566b2017-11-06 15:40:30 +000028void 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 Kramerbb1cdb62017-02-07 10:28:20 +000036
Benjamin Kramerd0b2ccd2017-02-10 14:08:40 +000037 std::lock_guard<std::mutex> Guard(StreamMutex);
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000038 // Log without headers.
Sam McCalldd0566b2017-11-06 15:40:30 +000039 Logs << "--> " << S << '\n';
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000040 Logs.flush();
41
42 // Emit message with header.
Sam McCalldd0566b2017-11-06 15:40:30 +000043 Outs << "Content-Length: " << S.size() << "\r\n\r\n" << S;
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000044 Outs.flush();
45}
46
Ilya Biryukov940901e2017-12-13 12:51:22 +000047void JSONOutput::log(const Context &Ctx, const Twine &Message) {
Sam McCall8567cb32017-11-02 09:21:51 +000048 trace::log(Message);
Benjamin Kramere14bd422017-02-15 16:44:11 +000049 std::lock_guard<std::mutex> Guard(StreamMutex);
Sam McCall318fbeb2017-11-30 23:21:34 +000050 Logs << Message << '\n';
Benjamin Kramere14bd422017-02-15 16:44:11 +000051 Logs.flush();
52}
53
Ilya Biryukove6dbb582017-10-10 09:08:47 +000054void JSONOutput::mirrorInput(const Twine &Message) {
55 if (!InputMirror)
56 return;
57
58 *InputMirror << Message;
59 InputMirror->flush();
60}
61
Ilya Biryukov940901e2017-12-13 12:51:22 +000062void clangd::reply(const Context &Ctx, json::Expr &&Result) {
63 auto ID = Ctx.get(RequestID);
Sam McCalldd0566b2017-11-06 15:40:30 +000064 if (!ID) {
Ilya Biryukov940901e2017-12-13 12:51:22 +000065 log(Ctx, "Attempted to reply to a notification!");
Sam McCall8a5dded2017-10-12 13:29:58 +000066 return;
67 }
Ilya Biryukov940901e2017-12-13 12:51:22 +000068
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 Kramerbb1cdb62017-02-07 10:28:20 +000078}
79
Ilya Biryukov940901e2017-12-13 12:51:22 +000080void 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 McCall8a5dded2017-10-12 13:29:58 +000096 }
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +000097}
98
Ilya Biryukov940901e2017-12-13 12:51:22 +000099void clangd::call(const Context &Ctx, StringRef Method, json::Expr &&Params) {
Marc-Andre Laperlee7ec16a2017-11-03 13:39:15 +0000100 // FIXME: Generate/Increment IDs for every request so that we can get proper
101 // replies once we need to.
Ilya Biryukov940901e2017-12-13 12:51:22 +0000102 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 Laperlee7ec16a2017-11-03 13:39:15 +0000112}
113
Sam McCall8a5dded2017-10-12 13:29:58 +0000114void JSONRPCDispatcher::registerHandler(StringRef Method, Handler H) {
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000115 assert(!Handlers.count(Method) && "Handler already registered!");
116 Handlers[Method] = std::move(H);
117}
118
Sam McCallec109022017-11-28 09:37:43 +0000119bool 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 Kramerbb1cdb62017-02-07 10:28:20 +0000123 return false;
Sam McCallec109022017-11-28 09:37:43 +0000124 // ID may be any JSON value. If absent, this is a notification.
Sam McCalldd0566b2017-11-06 15:40:30 +0000125 llvm::Optional<json::Expr> ID;
Sam McCallec109022017-11-28 09:37:43 +0000126 if (auto *I = Object->get("id"))
127 ID = std::move(*I);
128 // Method must be given.
129 auto Method = Object->getString("method");
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000130 if (!Method)
131 return false;
Sam McCallec109022017-11-28 09:37:43 +0000132 // 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 Kramerbb1cdb62017-02-07 10:28:20 +0000136
Sam McCallec109022017-11-28 09:37:43 +0000137 auto I = Handlers.find(*Method);
138 auto &Handler = I != Handlers.end() ? I->second : UnknownHandler;
Ilya Biryukov940901e2017-12-13 12:51:22 +0000139
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 McCallec109022017-11-28 09:37:43 +0000151 Handler(std::move(Ctx), std::move(Params));
Benjamin Kramerbb1cdb62017-02-07 10:28:20 +0000152 return true;
153}
Ilya Biryukovafb55542017-05-16 14:40:30 +0000154
155void clangd::runLanguageServerLoop(std::istream &In, JSONOutput &Out,
156 JSONRPCDispatcher &Dispatcher,
157 bool &IsDone) {
158 while (In.good()) {
Ilya Biryukov1fab4f82017-09-04 12:28:15 +0000159 // 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 Biryukove6dbb582017-10-10 09:08:47 +0000170 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 Biryukov1fab4f82017-09-04 12:28:15 +0000178 llvm::StringRef LineRef(Line);
179
Sam McCallec109022017-11-28 09:37:43 +0000180 // We allow comments in headers. Technically this isn't part
Ilya Biryukov1fab4f82017-09-04 12:28:15 +0000181 // 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 Biryukov940901e2017-12-13 12:51:22 +0000193 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 Biryukov1fab4f82017-09-04 12:28:15 +0000197 }
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 Biryukovafb55542017-05-16 14:40:30 +0000209 }
210
Benjamin Kramer1d053792017-10-27 17:06:41 +0000211 // 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 Biryukov940901e2017-12-13 12:51:22 +0000215 log(Context::empty(), "Skipped overly large message of " +
216 Twine(ContentLength) + " bytes.\n");
Benjamin Kramer1d053792017-10-27 17:06:41 +0000217 continue;
218 }
219
Ilya Biryukov1fab4f82017-09-04 12:28:15 +0000220 if (ContentLength > 0) {
Sam McCallec109022017-11-28 09:37:43 +0000221 std::vector<char> JSON(ContentLength);
Sam McCall8567cb32017-11-02 09:21:51 +0000222 llvm::StringRef JSONRef;
223 {
Sam McCall8567cb32017-11-02 09:21:51 +0000224 In.read(JSON.data(), ContentLength);
225 Out.mirrorInput(StringRef(JSON.data(), In.gcount()));
Ilya Biryukovafb55542017-05-16 14:40:30 +0000226
Sam McCall8567cb32017-11-02 09:21:51 +0000227 // If the stream is aborted before we read ContentLength bytes, In
228 // will have eofbit and failbit set.
229 if (!In) {
Ilya Biryukov940901e2017-12-13 12:51:22 +0000230 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 McCall8567cb32017-11-02 09:21:51 +0000234 break;
235 }
236
237 JSONRef = StringRef(JSON.data(), ContentLength);
Ilya Biryukov1fab4f82017-09-04 12:28:15 +0000238 }
Ilya Biryukovafb55542017-05-16 14:40:30 +0000239
Sam McCallec109022017-11-28 09:37:43 +0000240 if (auto Doc = json::parse(JSONRef)) {
241 // Log the formatted message.
Ilya Biryukov940901e2017-12-13 12:51:22 +0000242 log(Context::empty(),
243 llvm::formatv(Out.Pretty ? "<-- {0:2}\n" : "<-- {0}\n", *Doc));
Sam McCallec109022017-11-28 09:37:43 +0000244 // Finally, execute the action for this JSON message.
245 if (!Dispatcher.call(*Doc, Out))
Ilya Biryukov940901e2017-12-13 12:51:22 +0000246 log(Context::empty(), "JSON dispatch failed!\n");
Sam McCallec109022017-11-28 09:37:43 +0000247 } else {
248 // Parse error. Log the raw message.
Ilya Biryukov940901e2017-12-13 12:51:22 +0000249 log(Context::empty(), "<-- " + JSONRef + "\n");
250 log(Context::empty(), llvm::Twine("JSON parse error: ") +
251 llvm::toString(Doc.takeError()) + "\n");
Sam McCallec109022017-11-28 09:37:43 +0000252 }
Ilya Biryukovafb55542017-05-16 14:40:30 +0000253
254 // If we're done, exit the loop.
255 if (IsDone)
256 break;
Ilya Biryukov1fab4f82017-09-04 12:28:15 +0000257 } else {
Ilya Biryukov940901e2017-12-13 12:51:22 +0000258 log(Context::empty(),
259 "Warning: Missing Content-Length header, or message has zero "
260 "length.\n");
Ilya Biryukovafb55542017-05-16 14:40:30 +0000261 }
262 }
263}