Skip to content
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

handle edge case were too many bytes are supplied #80

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

willmcgugan
Copy link
Contributor

@willmcgugan willmcgugan commented Sep 19, 2018

What this PR solves

  • Fixes issue on Python2.7.3 that cause a connection to reset when sending packets larger than 126 bytes.
  • Made parser a little stricter

Review

  • Ready for review

@@ -20,9 +20,6 @@
class FrameParser(Parser):
"""Parses a stream of data in to HTTP headers + WS frames."""

unpack16 = struct.Struct(b"!H").unpack
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the source of the strange traceback. On Python 2.7.3 struct.unpack failed with anything that wasn't a bytes object. On later versions, it would accept any object with the buffer protocol, i.e. bytearray.

@@ -18,8 +18,13 @@ class ParseEOF(ParseError):
"""End of Stream."""


class ParseOverflow(Exception):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new exception is thrown if data is supplied in feed when the parser has finished. This wasn't ever occurring, but at least its implemented now. It could catch issues in the future.

@@ -71,6 +71,13 @@ def feed(self, data):
raise errors.CriticalProtocolError(
text_type(error)
)
except errors.WebSocketError:
raise
except Exception as error:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mere paranoia.

@@ -39,6 +36,17 @@ def __repr__(self):
self.validate
)

# A bug in Python2.7.3 requires casting data to bytes
@classmethod
def unpack16(cls, data, _unpack16 = struct.Struct(b"!H").unpack):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The _unpack16 is in the params so that it is a local, which is faster.

while not isinstance(self._awaiting, _Awaitable):
yield self._awaiting
self._awaiting = next(self._gen)
self._gen.throw(ParseEOF("unexpected eof of file"))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is too weighty. As some point I will break it up a little. It's on my list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants