clangd: Tolerate additional headers

Summary:
The language server protocol specified 2 headers (Content-Length and Content-Type), but does not specify their sequence. It specifies that an empty line ends
headers. Clangd has been updated to handle arbitrary sequences of headers, extracting only the content length.

Patch by puremourning (Ben Jackson).

Reviewers: bkramer, klimek, ilya-biryukov

Reviewed By: ilya-biryukov

Subscribers: cfe-commits, ilya-biryukov

Differential Revision: https://reviews.llvm.org/D37282

llvm-svn: 312483
diff --git a/clang-tools-extra/clangd/JSONRPCDispatcher.cpp b/clang-tools-extra/clangd/JSONRPCDispatcher.cpp
index 5deb75a..2f38e9c 100644
--- a/clang-tools-extra/clangd/JSONRPCDispatcher.cpp
+++ b/clang-tools-extra/clangd/JSONRPCDispatcher.cpp
@@ -136,46 +136,68 @@
                                    JSONRPCDispatcher &Dispatcher,
                                    bool &IsDone) {
   while (In.good()) {
-    // A Language Server Protocol message starts with a HTTP header, delimited
-    // by \r\n.
-    std::string Line;
-    std::getline(In, Line);
-    if (!In.good() && errno == EINTR) {
-      In.clear();
-      continue;
+    // A Language Server Protocol message starts with a set of HTTP headers,
+    // delimited  by \r\n, and terminated by an empty line (\r\n).
+    unsigned long long ContentLength = 0;
+    while (In.good()) {
+      std::string Line;
+      std::getline(In, Line);
+      if (!In.good() && errno == EINTR) {
+        In.clear();
+        continue;
+      }
+
+      llvm::StringRef LineRef(Line);
+
+      // We allow YAML-style comments in headers. Technically this isn't part
+      // of the LSP specification, but makes writing tests easier.
+      if (LineRef.startswith("#"))
+        continue;
+
+      // Content-Type is a specified header, but does nothing.
+      // Content-Length is a mandatory header. It specifies the length of the
+      // following JSON.
+      // It is unspecified what sequence headers must be supplied in, so we
+      // allow any sequence.
+      // The end of headers is signified by an empty line.
+      if (LineRef.consume_front("Content-Length: ")) {
+        if (ContentLength != 0) {
+          Out.log("Warning: Duplicate Content-Length header received. "
+                  "The previous value for this message ("
+                  + std::to_string(ContentLength)
+                  + ") was ignored.\n");
+        }
+
+        llvm::getAsUnsignedInteger(LineRef.trim(), 0, ContentLength);
+        continue;
+      } else if (!LineRef.trim().empty()) {
+        // It's another header, ignore it.
+        continue;
+      } else {
+        // An empty line indicates the end of headers.
+        // Go ahead and read the JSON.
+        break;
+      }
     }
 
-    // Skip empty lines.
-    llvm::StringRef LineRef(Line);
-    if (LineRef.trim().empty())
-      continue;
+    if (ContentLength > 0) {
+      // Now read the JSON. Insert a trailing null byte as required by the YAML
+      // parser.
+      std::vector<char> JSON(ContentLength + 1, '\0');
+      In.read(JSON.data(), ContentLength);
 
-    // We allow YAML-style comments. Technically this isn't part of the
-    // LSP specification, but makes writing tests easier.
-    if (LineRef.startswith("#"))
-      continue;
+      // If the stream is aborted before we read ContentLength bytes, In
+      // will have eofbit and failbit set.
+      if (!In) {
+        Out.log("Input was aborted. Read only "
+                + std::to_string(In.gcount())
+                + " bytes of expected "
+                + std::to_string(ContentLength)
+                + ".\n");
+        break;
+      }
 
-    unsigned long long Len = 0;
-    // FIXME: Content-Type is a specified header, but does nothing.
-    // Content-Length is a mandatory header. It specifies the length of the
-    // following JSON.
-    if (LineRef.consume_front("Content-Length: "))
-      llvm::getAsUnsignedInteger(LineRef.trim(), 0, Len);
-
-    // Check if the next line only contains \r\n. If not this is another header,
-    // which we ignore.
-    char NewlineBuf[2];
-    In.read(NewlineBuf, 2);
-    if (std::memcmp(NewlineBuf, "\r\n", 2) != 0)
-      continue;
-
-    // Now read the JSON. Insert a trailing null byte as required by the YAML
-    // parser.
-    std::vector<char> JSON(Len + 1, '\0');
-    In.read(JSON.data(), Len);
-
-    if (Len > 0) {
-      llvm::StringRef JSONRef(JSON.data(), Len);
+      llvm::StringRef JSONRef(JSON.data(), ContentLength);
       // Log the message.
       Out.log("<-- " + JSONRef + "\n");
 
@@ -186,6 +208,9 @@
       // If we're done, exit the loop.
       if (IsDone)
         break;
+    } else {
+      Out.log( "Warning: Missing Content-Length header, or message has zero "
+               "length.\n" );
     }
   }
 }