-
Notifications
You must be signed in to change notification settings - Fork 89
Static Type-checking for httptools #100
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
Open
Vizonex
wants to merge
7
commits into
MagicStack:master
Choose a base branch
from
Vizonex:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d1d9d9e
Add static protocol object for type checking
Vizonex 02e8f3d
Create url_parser.pyi
Vizonex 6011b57
Create parser.pyi
Vizonex a248458
add protocol module
Vizonex b2a3833
Merge branch 'master' of https://github.com/MagicStack/httptools
KRRT7 01308ee
add spacing and semi black formatting
KRRT7 459ead9
Merge pull request #1 from KRRT7/master
Vizonex 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from typing import Union, Any | ||
from array import array | ||
from .protocol import HTTPProtocol | ||
|
||
|
||
|
||
class HttpParser: | ||
def __init__(self, protocol:Union[HTTPProtocol, Any]) -> None: | ||
""" | ||
protocol -- a Python object with the following methods | ||
(all optional): | ||
|
||
- on_message_begin() | ||
- on_url(url: bytes) | ||
- on_header(name: bytes, value: bytes) | ||
- on_headers_complete() | ||
- on_body(body: bytes) | ||
- on_message_complete() | ||
- on_chunk_header() | ||
- on_chunk_complete() | ||
- on_status(status: bytes) | ||
""" | ||
|
||
def get_http_version(self) -> str: | ||
"""Return an HTTP protocol version.""" | ||
... | ||
def should_keep_alive(self) -> bool: | ||
"""Return ``True`` if keep-alive mode is preferred.""" | ||
... | ||
def should_upgrade(self) -> bool: | ||
"""Return ``True`` if the parsed request is a valid Upgrade request. | ||
The method exposes a flag set just before on_headers_complete. | ||
Calling this method earlier will only yield `False`.""" | ||
... | ||
def feed_data(self, data:Union[bytes, bytearray, memoryview, array]) -> None: | ||
"""Feed data to the parser. | ||
|
||
Will eventually trigger callbacks on the ``protocol`` | ||
object. | ||
|
||
On HTTP upgrade, this method will raise an | ||
``HttpParserUpgrade`` exception, with its sole argument | ||
set to the offset of the non-HTTP data in ``data``. | ||
""" | ||
|
||
|
||
class HttpRequestParser(HttpParser): | ||
"""Used for parsing http requests from the server's side""" | ||
|
||
def get_method(self) -> bytes: | ||
"""Return HTTP request method (GET, HEAD, etc)""" | ||
|
||
|
||
class HttpResponseParser(HttpParser): | ||
"""Used for parsing http requests from the client's side""" | ||
|
||
def get_status_code(self) -> int: | ||
"""Return the status code of the HTTP response""" |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from typing import Protocol | ||
|
||
class HTTPProtocol(Protocol): | ||
"""Used for providing static type-checking when parsing through the http protocol""" | ||
|
||
def on_message_begin() -> None:... | ||
def on_url(url: bytes) -> None:... | ||
def on_header(name: bytes, value: bytes) -> None:... | ||
def on_headers_complete() -> None:... | ||
def on_body(body: bytes) -> None:... | ||
def on_message_complete() -> None:... | ||
def on_chunk_header() -> None:... | ||
def on_chunk_complete() -> None:... | ||
def on_status(status: bytes) -> None:... | ||
|
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from typing import Union | ||
from array import array | ||
|
||
class URL: | ||
schema:bytes | ||
host:bytes | ||
port:int | ||
path:bytes | ||
query:bytes | ||
fragment:bytes | ||
userinfo:bytes | ||
|
||
def parse_url(url:Union[bytes, bytearray, memoryview, array]) -> URL: | ||
"""Parse URL strings into a structured Python object. | ||
|
||
Returns an instance of ``httptools.URL`` class with the | ||
following attributes: | ||
|
||
- schema: bytes | ||
- host: bytes | ||
- port: int | ||
- path: bytes | ||
- query: bytes | ||
- fragment: bytes | ||
- userinfo: bytes | ||
""" | ||
... | ||
|
||
|
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.
I think you should add spaces after colons for type annotations