-
-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix message parsing for documents containing Content-Length keyword #80
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bbf2645
Fix message parsing for documents containing Content-Length keyword
dinvlad 484ee7e
Support parsing chunked messages in data_received()
dinvlad 6a5eab8
Update contributors
dinvlad 3062396
Style fixes
dinvlad 2b84099
Fix alphabetical order for contributors
dinvlad 96452b2
Relocate MESSAGE_PATTERN declaration in JsonRPCProtocol
dinvlad b48c91c
Add Changelog for #80
dinvlad 74b7291
Fix the location of Changelog entry for Unreleased version
dinvlad 468561f
Merge branch 'master' into master
dinvlad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -170,7 +170,6 @@ class JsonRPCProtocol(asyncio.Protocol): | |
|
||
This class provides bidirectional communication which is needed for LSP. | ||
""" | ||
BODY_PATTERN = re.compile(rb'\{.+?\}.*') | ||
|
||
CANCEL_REQUEST = '$/cancelRequest' | ||
|
||
|
@@ -188,6 +187,7 @@ def __init__(self, server): | |
|
||
self.fm = FeatureManager(server) | ||
self.transport = None | ||
self._message_buf = [] | ||
|
||
def __call__(self): | ||
return self | ||
|
@@ -397,18 +397,43 @@ def connection_made(self, transport: asyncio.Transport): | |
"""Method from base class, called when connection is established""" | ||
self.transport = transport | ||
|
||
MESSAGE_PATTERN = re.compile( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move this up where other class constants are (alphabetically)? |
||
rb'^(?:[^\r\n]+\r\n)*' + | ||
rb'Content-Length: (?P<length>\d+)\r\n' + | ||
rb'(?:[^\r\n]+\r\n)*\r\n' + | ||
rb'(?P<body>{.*)', | ||
re.DOTALL, | ||
) | ||
|
||
def data_received(self, data: bytes): | ||
"""Method from base class, called when server receives the data""" | ||
logger.debug('Received {}'.format(data)) | ||
|
||
for part in data.split(b'Content-Length'): | ||
try: | ||
body = JsonRPCProtocol.BODY_PATTERN.findall(part)[0] | ||
self._procedure_handler( | ||
json.loads(body.decode(self.CHARSET), | ||
object_hook=deserialize_message)) | ||
except IndexError: | ||
pass | ||
while len(data): | ||
# Append the incoming chunk to the message buffer | ||
self._message_buf.append(data) | ||
|
||
# Look for the body of the message | ||
message = b''.join(self._message_buf) | ||
found = JsonRPCProtocol.MESSAGE_PATTERN.fullmatch(message) | ||
|
||
body = found.group('body') if found else b'' | ||
length = int(found.group('length')) if found else 1 | ||
|
||
if len(body) < length: | ||
# Message is incomplete; bail until more data arrives | ||
return | ||
|
||
# Message is complete; | ||
# extract the body and any remaining data, | ||
# and reset the buffer for the next message | ||
body, data = body[:length], body[length:] | ||
self._message_buf = [] | ||
|
||
# Parse the body | ||
self._procedure_handler( | ||
json.loads(body.decode(self.CHARSET), | ||
object_hook=deserialize_message)) | ||
|
||
def notify(self, method: str, params=None): | ||
"""Sends a JSON RPC notification to the client.""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you make this alphabetical?