Skip to content

Commit c38b7eb

Browse files
committed
Make lsp-mode parser more robust
1 parent 04086bd commit c38b7eb

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

lsp-mode.el

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4766,7 +4766,9 @@ WORKSPACE is the active workspace."
47664766
(let* ((body-sep-pos (string-match-p "\r\n\r\n" chunk)))
47674767
(if body-sep-pos
47684768
;; We've got all the headers, handle them all at once:
4769-
(let* ((header-raw (substring chunk 0 body-sep-pos))
4769+
(let* ((header-raw (substring chunk (or (string-match-p "Content-Length" chunk)
4770+
(error "Unable to find Content-Length header."))
4771+
body-sep-pos))
47704772
(content (substring chunk (+ body-sep-pos 4)))
47714773
(headers
47724774
(mapcar 'lsp--parse-header

test/lsp-io-test.el

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,39 +38,45 @@
3838

3939
(ert-deftest lsp--parser-read--multibyte ()
4040
(let* ((p (make-lsp--parser :workspace lsp--test-workspace))
41-
(message-in "Content-Length: 3\r\n\r\n\xe2\x80\x99")
41+
(message-in "Content-Length: 3\r\n\r\n\xe2\x80\x99")
4242
(messages (lsp--parser-read p message-in)))
4343
(should (equal messages '("")))))
4444

4545
(ert-deftest lsp--parser-read--multibyte-nospace ()
4646
(let* ((p (make-lsp--parser :workspace lsp--test-workspace))
47-
(message-in "Content-Length:3\r\n\r\n\xe2\x80\x99")
47+
(message-in "Content-Length:3\r\n\r\n\xe2\x80\x99")
4848
(messages (lsp--parser-read p message-in)))
4949
(should (equal messages '("")))))
5050

5151
(ert-deftest lsp--parser-read--multibyte-received ()
5252
(let* ((p (make-lsp--parser :workspace lsp--test-workspace))
53-
(message-in "Content-Length: 1152\r\n\r\n{\"jsonrpc\":\"2.0\",\"method\":\"sts/highlight\",\"params\":{\"doc\":{\"version\":0,\"uri\":\"file:///home/kyoncho/Public/Desktop/hellow/hello-world/src/main/java/com/example/helloworld/HelloWorldController.java\"},\"codeLenses\":[{\"range\":{\"start\":{\"line\":9,\"character\":0},\"end\":{\"line\":9,\"character\":11}},\"command\":{\"title\":\"← SampleBean\",\"command\":\"sts.showHoverAtPosition\",\"arguments\":[{\"line\":9,\"character\":0}]},\"data\":\"← SampleBean\"},{\"range\":{\"start\":{\"line\":14,\"character\":4},\"end\":{\"line\":14,\"character\":14}},\"command\":{\"title\":\"← SampleBean\",\"command\":\"sts.showHoverAtPosition\",\"arguments\":[{\"line\":14,\"character\":4}]},\"data\":\"← SampleBean\"},{\"range\":{\"start\":{\"line\":17,\"character\":4},\"end\":{\"line\":17,\"character\":31}},\"command\":{\"title\":\"http://127.0.0.1:8080/hello-world\",\"command\":\"sts.open.url\",\"arguments\":[\"http://127.0.0.1:8080/hello-world\"]},\"data\":\"http://127.0.0.1:8080/hello-world\"},{\"range\":{\"start\":{\"line\":25,\"character\":4},\"end\":{\"line\":25,\"character\":32}},\"command\":{\"title\":\"http://127.0.0.1:8080/hello-world2\",\"command\":\"sts.open.url\",\"arguments\":[\"http://127.0.0.1:8080/hello-world2\"]},\"data\":\"http://127.0.0.1:8080/hello-world2\"}]}}")
53+
(message-in "Content-Length: 1152\r\n\r\n{\"jsonrpc\":\"2.0\",\"method\":\"sts/highlight\",\"params\":{\"doc\":{\"version\":0,\"uri\":\"file:///home/kyoncho/Public/Desktop/hellow/hello-world/src/main/java/com/example/helloworld/HelloWorldController.java\"},\"codeLenses\":[{\"range\":{\"start\":{\"line\":9,\"character\":0},\"end\":{\"line\":9,\"character\":11}},\"command\":{\"title\":\"← SampleBean\",\"command\":\"sts.showHoverAtPosition\",\"arguments\":[{\"line\":9,\"character\":0}]},\"data\":\"← SampleBean\"},{\"range\":{\"start\":{\"line\":14,\"character\":4},\"end\":{\"line\":14,\"character\":14}},\"command\":{\"title\":\"← SampleBean\",\"command\":\"sts.showHoverAtPosition\",\"arguments\":[{\"line\":14,\"character\":4}]},\"data\":\"← SampleBean\"},{\"range\":{\"start\":{\"line\":17,\"character\":4},\"end\":{\"line\":17,\"character\":31}},\"command\":{\"title\":\"http://127.0.0.1:8080/hello-world\",\"command\":\"sts.open.url\",\"arguments\":[\"http://127.0.0.1:8080/hello-world\"]},\"data\":\"http://127.0.0.1:8080/hello-world\"},{\"range\":{\"start\":{\"line\":25,\"character\":4},\"end\":{\"line\":25,\"character\":32}},\"command\":{\"title\":\"http://127.0.0.1:8080/hello-world2\",\"command\":\"sts.open.url\",\"arguments\":[\"http://127.0.0.1:8080/hello-world2\"]},\"data\":\"http://127.0.0.1:8080/hello-world2\"}]}}")
5454
(messages (lsp--parser-read p message-in)))
5555
(should (equal messages '("{\"jsonrpc\":\"2.0\",\"method\":\"sts/highlight\",\"params\":{\"doc\":{\"version\":0,\"uri\":\"file:///home/kyoncho/Public/Desktop/hellow/hello-world/src/main/java/com/example/helloworld/HelloWorldController.java\"},\"codeLenses\":[{\"range\":{\"start\":{\"line\":9,\"character\":0},\"end\":{\"line\":9,\"character\":11}},\"command\":{\"title\":\"← SampleBean\",\"command\":\"sts.showHoverAtPosition\",\"arguments\":[{\"line\":9,\"character\":0}]},\"data\":\"← SampleBean\"},{\"range\":{\"start\":{\"line\":14,\"character\":4},\"end\":{\"line\":14,\"character\":14}},\"command\":{\"title\":\"← SampleBean\",\"command\":\"sts.showHoverAtPosition\",\"arguments\":[{\"line\":14,\"character\":4}]},\"data\":\"← SampleBean\"},{\"range\":{\"start\":{\"line\":17,\"character\":4},\"end\":{\"line\":17,\"character\":31}},\"command\":{\"title\":\"http://127.0.0.1:8080/hello-world\",\"command\":\"sts.open.url\",\"arguments\":[\"http://127.0.0.1:8080/hello-world\"]},\"data\":\"http://127.0.0.1:8080/hello-world\"},{\"range\":{\"start\":{\"line\":25,\"character\":4},\"end\":{\"line\":25,\"character\":32}},\"command\":{\"title\":\"http://127.0.0.1:8080/hello-world2\",\"command\":\"sts.open.url\",\"arguments\":[\"http://127.0.0.1:8080/hello-world2\"]},\"data\":\"http://127.0.0.1:8080/hello-world2\"}]}}")))))
5656

5757
(ert-deftest lsp--parser-read--multiple-chunks ()
5858
(let* ((p (make-lsp--parser :workspace lsp--test-workspace)))
59-
(should (equal (lsp--parser-read p "Content-Length: 14\r\n\r\n{") nil))
60-
(should (equal (lsp--parser-read p "\"somedata\":1") nil))
61-
(should (equal (lsp--parser-read p "}Content-Length: 14\r\n\r\n{")
62-
'("{\"somedata\":1}")))
63-
(should (equal (lsp--parser-read p "\"somedata\":2}")
64-
'("{\"somedata\":2}")))))
59+
(should (equal (lsp--parser-read p "Content-Length: 14\r\n\r\n{") nil))
60+
(should (equal (lsp--parser-read p "\"somedata\":1") nil))
61+
(should (equal (lsp--parser-read p "}Content-Length: 14\r\n\r\n{")
62+
'("{\"somedata\":1}")))
63+
(should (equal (lsp--parser-read p "\"somedata\":2}")
64+
'("{\"somedata\":2}")))))
6565

6666
(ert-deftest lsp--parser-read--multiple-multibyte-chunks ()
6767
(let* ((p (make-lsp--parser :workspace lsp--test-workspace)))
68-
(should (equal (lsp--parser-read p "Content-Length: 18\r") nil))
69-
(should (equal (lsp--parser-read p "\n\r\n{\"somedata\":\"\xe2\x80") nil))
70-
(should (equal (lsp--parser-read p "\x99\"}Content-Length: 14\r\n\r\n{")
71-
'("{\"somedata\":\"\"}")))
72-
(should (equal (lsp--parser-read p "\"somedata\":2}")
73-
'("{\"somedata\":2}")))))
68+
(should (equal (lsp--parser-read p "Content-Length: 18\r") nil))
69+
(should (equal (lsp--parser-read p "\n\r\n{\"somedata\":\"\xe2\x80") nil))
70+
(should (equal (lsp--parser-read p "\x99\"}Content-Length: 14\r\n\r\n{")
71+
'("{\"somedata\":\"\"}")))
72+
(should (equal (lsp--parser-read p "\"somedata\":2}")
73+
'("{\"somedata\":2}")))))
74+
75+
(ert-deftest lsp--non-related-content-on-stdout ()
76+
(let* ((p (make-lsp--parser :workspace lsp--test-workspace)))
77+
(let* ((p (make-lsp--parser :workspace lsp--test-workspace)))
78+
(should (equal (lsp--parser-read p "ConentOnStdoutContent-Length: 14\r\n\r\n{\"somedata\":1}")
79+
'("{\"somedata\":1}"))))))
7480

7581
(ert-deftest lsp--parser-read--ignored-messages ()
7682
(lsp--on-notification

0 commit comments

Comments
 (0)