palantir / python-jsonrpc-server

A Python 2 and 3 asynchronous JSON RPC server
MIT License
83 stars 36 forks source link

Allow any order of headers in base protocol #9

Open kristopher-h opened 6 years ago

kristopher-h commented 6 years ago

Hi,

Currently python-jsonrpc-server requires the Content-Length header to be sent first. I guess the following snippet is causing the issue:

        content_length = self._content_length(line)

        # Blindly consume all header lines
        while line and line.strip():
             line = self._rfile.readline()

I think it would be good to allow reading headers in any order. The LSP specification doesn't mention anything about order, even though it is listed in one, leaving it a bit open for interpretation.

Let me know if you would like a PR with a suggestion for a fix (assuming you agree with my conclusion that is).

Regards

gatesn commented 6 years ago

Yup, completely agree. I went for the naive thing just to avoid taking a dependency on some lib with header parsing. Since there’s only one header we really care about though this should still be quite simple

kristopher-h commented 6 years ago

I did PR to allow for parsing headers in any order here https://github.com/palantir/python-jsonrpc-server/pull/11. I added some additional unit tests to cover some new scenarios as well. If I understood correctly, the intention was that the listen method should never raise. But instead just break on error (corrupt headers/messages)? Feel free to have a look.