1 //===--- JSONTransport.cpp - sending and receiving LSP messages over JSON -===//
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 #include "Logger.h"
10 #include "Protocol.h" // For LSPError
11 #include "Transport.h"
12 #include "llvm/Support/Errno.h"
13 
14 namespace clang {
15 namespace clangd {
16 namespace {
17 
18 llvm::json::Object encodeError(llvm::Error E) {
19   std::string Message;
20   ErrorCode Code = ErrorCode::UnknownErrorCode;
21   if (llvm::Error Unhandled = llvm::handleErrors(
22           std::move(E), [&](const LSPError &L) -> llvm::Error {
23             Message = L.Message;
24             Code = L.Code;
25             return llvm::Error::success();
26           }))
27     Message = llvm::toString(std::move(Unhandled));
28 
29   return llvm::json::Object{
30       {"message", std::move(Message)},
31       {"code", int64_t(Code)},
32   };
33 }
34 
35 llvm::Error decodeError(const llvm::json::Object &O) {
36   std::string Msg = O.getString("message").getValueOr("Unspecified error");
37   if (auto Code = O.getInteger("code"))
38     return llvm::make_error<LSPError>(std::move(Msg), ErrorCode(*Code));
39   return llvm::make_error<llvm::StringError>(std::move(Msg),
40                                              llvm::inconvertibleErrorCode());
41 }
42 
43 class JSONTransport : public Transport {
44 public:
45   JSONTransport(std::FILE *In, llvm::raw_ostream &Out,
46                 llvm::raw_ostream *InMirror, bool Pretty, JSONStreamStyle Style)
47       : In(In), Out(Out), InMirror(InMirror ? *InMirror : llvm::nulls()),
48         Pretty(Pretty), Style(Style) {}
49 
50   void notify(llvm::StringRef Method, llvm::json::Value Params) override {
51     sendMessage(llvm::json::Object{
52         {"jsonrpc", "2.0"},
53         {"method", Method},
54         {"params", std::move(Params)},
55     });
56   }
57   void call(llvm::StringRef Method, llvm::json::Value Params,
58             llvm::json::Value ID) override {
59     sendMessage(llvm::json::Object{
60         {"jsonrpc", "2.0"},
61         {"id", std::move(ID)},
62         {"method", Method},
63         {"params", std::move(Params)},
64     });
65   }
66   void reply(llvm::json::Value ID,
67              llvm::Expected<llvm::json::Value> Result) override {
68     if (Result) {
69       sendMessage(llvm::json::Object{
70           {"jsonrpc", "2.0"},
71           {"id", std::move(ID)},
72           {"result", std::move(*Result)},
73       });
74     } else {
75       sendMessage(llvm::json::Object{
76           {"jsonrpc", "2.0"},
77           {"id", std::move(ID)},
78           {"error", encodeError(Result.takeError())},
79       });
80     }
81   }
82 
83   llvm::Error loop(MessageHandler &Handler) override {
84     while (!feof(In)) {
85       if (ferror(In))
86         return llvm::errorCodeToError(
87             std::error_code(errno, std::system_category()));
88       if (auto JSON = readRawMessage()) {
89         if (auto Doc = llvm::json::parse(*JSON)) {
90           vlog(Pretty ? "<<< {0:2}\n" : "<<< {0}\n", *Doc);
91           if (!handleMessage(std::move(*Doc), Handler))
92             return llvm::Error::success(); // we saw the "exit" notification.
93         } else {
94           // Parse error. Log the raw message.
95           vlog("<<< {0}\n", *JSON);
96           elog("JSON parse error: {0}", llvm::toString(Doc.takeError()));
97         }
98       }
99     }
100     return llvm::errorCodeToError(std::make_error_code(std::errc::io_error));
101   }
102 
103 private:
104   // Dispatches incoming message to Handler onNotify/onCall/onReply.
105   bool handleMessage(llvm::json::Value Message, MessageHandler &Handler);
106   // Writes outgoing message to Out stream.
107   void sendMessage(llvm::json::Value Message) {
108     std::string S;
109     llvm::raw_string_ostream OS(S);
110     OS << llvm::formatv(Pretty ? "{0:2}" : "{0}", Message);
111     OS.flush();
112     Out << "Content-Length: " << S.size() << "\r\n\r\n" << S;
113     Out.flush();
114     vlog(">>> {0}\n", S);
115   }
116 
117   // Read raw string messages from input stream.
118   llvm::Optional<std::string> readRawMessage() {
119     return Style == JSONStreamStyle::Delimited ? readDelimitedMessage()
120                                                : readStandardMessage();
121   }
122   llvm::Optional<std::string> readDelimitedMessage();
123   llvm::Optional<std::string> readStandardMessage();
124 
125   std::FILE *In;
126   llvm::raw_ostream &Out;
127   llvm::raw_ostream &InMirror;
128   bool Pretty;
129   JSONStreamStyle Style;
130 };
131 
132 bool JSONTransport::handleMessage(llvm::json::Value Message,
133                                   MessageHandler &Handler) {
134   // Message must be an object with "jsonrpc":"2.0".
135   auto *Object = Message.getAsObject();
136   if (!Object ||
137       Object->getString("jsonrpc") != llvm::Optional<llvm::StringRef>("2.0")) {
138     elog("Not a JSON-RPC 2.0 message: {0:2}", Message);
139     return false;
140   }
141   // ID may be any JSON value. If absent, this is a notification.
142   llvm::Optional<llvm::json::Value> ID;
143   if (auto *I = Object->get("id"))
144     ID = std::move(*I);
145   auto Method = Object->getString("method");
146   if (!Method) { // This is a response.
147     if (!ID) {
148       elog("No method and no response ID: {0:2}", Message);
149       return false;
150     }
151     if (auto *Err = Object->getObject("error"))
152       return Handler.onReply(std::move(*ID), decodeError(*Err));
153     // Result should be given, use null if not.
154     llvm::json::Value Result = nullptr;
155     if (auto *R = Object->get("result"))
156       Result = std::move(*R);
157     return Handler.onReply(std::move(*ID), std::move(Result));
158   }
159   // Params should be given, use null if not.
160   llvm::json::Value Params = nullptr;
161   if (auto *P = Object->get("params"))
162     Params = std::move(*P);
163 
164   if (ID)
165     return Handler.onCall(*Method, std::move(Params), std::move(*ID));
166   else
167     return Handler.onNotify(*Method, std::move(Params));
168 }
169 
170 // Tries to read a line up to and including \n.
171 // If failing, feof() or ferror() will be set.
172 bool readLine(std::FILE *In, std::string &Out) {
173   static constexpr int BufSize = 1024;
174   size_t Size = 0;
175   Out.clear();
176   for (;;) {
177     Out.resize(Size + BufSize);
178     // Handle EINTR which is sent when a debugger attaches on some platforms.
179     if (!llvm::sys::RetryAfterSignal(nullptr, ::fgets, &Out[Size], BufSize, In))
180       return false;
181     clearerr(In);
182     // If the line contained null bytes, anything after it (including \n) will
183     // be ignored. Fortunately this is not a legal header or JSON.
184     size_t Read = std::strlen(&Out[Size]);
185     if (Read > 0 && Out[Size + Read - 1] == '\n') {
186       Out.resize(Size + Read);
187       return true;
188     }
189     Size += Read;
190   }
191 }
192 
193 // Returns None when:
194 //  - ferror() or feof() are set.
195 //  - Content-Length is missing or empty (protocol error)
196 llvm::Optional<std::string> JSONTransport::readStandardMessage() {
197   // A Language Server Protocol message starts with a set of HTTP headers,
198   // delimited  by \r\n, and terminated by an empty line (\r\n).
199   unsigned long long ContentLength = 0;
200   std::string Line;
201   while (true) {
202     if (feof(In) || ferror(In) || !readLine(In, Line))
203       return llvm::None;
204     InMirror << Line;
205 
206     llvm::StringRef LineRef(Line);
207 
208     // We allow comments in headers. Technically this isn't part
209 
210     // of the LSP specification, but makes writing tests easier.
211     if (LineRef.startswith("#"))
212       continue;
213 
214     // Content-Length is a mandatory header, and the only one we handle.
215     if (LineRef.consume_front("Content-Length: ")) {
216       if (ContentLength != 0) {
217         elog("Warning: Duplicate Content-Length header received. "
218              "The previous value for this message ({0}) was ignored.",
219              ContentLength);
220       }
221       llvm::getAsUnsignedInteger(LineRef.trim(), 0, ContentLength);
222       continue;
223     } else if (!LineRef.trim().empty()) {
224       // It's another header, ignore it.
225       continue;
226     } else {
227       // An empty line indicates the end of headers.
228       // Go ahead and read the JSON.
229       break;
230     }
231   }
232 
233   // The fuzzer likes crashing us by sending "Content-Length: 9999999999999999"
234   if (ContentLength > 1 << 30) { // 1024M
235     elog("Refusing to read message with long Content-Length: {0}. "
236          "Expect protocol errors",
237          ContentLength);
238     return llvm::None;
239   }
240   if (ContentLength == 0) {
241     log("Warning: Missing Content-Length header, or zero-length message.");
242     return llvm::None;
243   }
244 
245   std::string JSON(ContentLength, '\0');
246   for (size_t Pos = 0, Read; Pos < ContentLength; Pos += Read) {
247     // Handle EINTR which is sent when a debugger attaches on some platforms.
248     Read = llvm::sys::RetryAfterSignal(0u, ::fread, &JSON[Pos], 1,
249                                        ContentLength - Pos, In);
250     if (Read == 0) {
251       elog("Input was aborted. Read only {0} bytes of expected {1}.", Pos,
252            ContentLength);
253       return llvm::None;
254     }
255     InMirror << llvm::StringRef(&JSON[Pos], Read);
256     clearerr(In); // If we're done, the error was transient. If we're not done,
257                   // either it was transient or we'll see it again on retry.
258     Pos += Read;
259   }
260   return std::move(JSON);
261 }
262 
263 // For lit tests we support a simplified syntax:
264 // - messages are delimited by '---' on a line by itself
265 // - lines starting with # are ignored.
266 // This is a testing path, so favor simplicity over performance here.
267 // When returning None, feof() or ferror() will be set.
268 llvm::Optional<std::string> JSONTransport::readDelimitedMessage() {
269   std::string JSON;
270   std::string Line;
271   while (readLine(In, Line)) {
272     InMirror << Line;
273     auto LineRef = llvm::StringRef(Line).trim();
274     if (LineRef.startswith("#")) // comment
275       continue;
276 
277     // found a delimiter
278     if (LineRef.rtrim() == "---")
279       break;
280 
281     JSON += Line;
282   }
283 
284   if (ferror(In)) {
285     elog("Input error while reading message!");
286     return llvm::None;
287   }
288   return std::move(JSON); // Including at EOF
289 }
290 
291 } // namespace
292 
293 std::unique_ptr<Transport> newJSONTransport(std::FILE *In,
294                                             llvm::raw_ostream &Out,
295                                             llvm::raw_ostream *InMirror,
296                                             bool Pretty,
297                                             JSONStreamStyle Style) {
298   return llvm::make_unique<JSONTransport>(In, Out, InMirror, Pretty, Style);
299 }
300 
301 } // namespace clangd
302 } // namespace clang
303