-
-
Notifications
You must be signed in to change notification settings - Fork 618
Bail out early for non-HTTP but HTTP looking protocols #972
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
Merged
abhinavsingh
merged 7 commits into
develop
from
bail-out-early-for-non-http-but-http-looking-proto
Jan 17, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
227144c
Add support in `Url` to parse all types of schemes
abhinavsingh 473bd9f
.
abhinavsingh f0810bc
Merge branch 'develop' into bail-out-early-for-non-http-but-http-look…
abhinavsingh 4781ed0
Guard handler against http looking protocol but not web or proxy requ…
abhinavsingh bd80e49
Fix condition for web server protocol detection
abhinavsingh d234339
doc happy
abhinavsingh 7d576aa
Update flags and type check imports only
abhinavsingh 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
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
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
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 |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ | |
| """ | ||
| from typing import Optional, Tuple | ||
|
|
||
| from ..common.constants import COLON, SLASH, HTTP_URL_PREFIX, HTTPS_URL_PREFIX, AT | ||
| from ..common.constants import COLON, SLASH, AT | ||
| from ..common.utils import text_ | ||
|
|
||
|
|
||
|
|
@@ -68,29 +68,41 @@ def from_bytes(cls, raw: bytes) -> 'Url': | |
| For a HTTPS connect tunnel, url is like ``httpbin.org:443`` | ||
| For a HTTP proxy request, url is like ``http://httpbin.org/get`` | ||
|
|
||
| proxy.py internally never expects a https scheme in the request line. | ||
| But `Url` class provides support for parsing any scheme present in the URLs. | ||
| e.g. ftp, icap etc. | ||
|
|
||
| If a url with no scheme is parsed, e.g. ``//host/abc.js``, then scheme | ||
| defaults to `http`. | ||
|
Contributor
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. Same here, reference syntax used, but you probably wanted it to be rst. |
||
|
|
||
| Further: | ||
| 1) URL may contain unicode characters | ||
| 2) URL may contain IPv4 and IPv6 format addresses instead of domain names | ||
|
|
||
| We use heuristics based approach for our URL parser. | ||
| """ | ||
| # SLASH == 47, check if URL starts with single slash but not double slash | ||
| is_single_slash = raw[0] == 47 | ||
| is_double_slash = is_single_slash and len(raw) >= 2 and raw[1] == 47 | ||
| if is_single_slash and not is_double_slash: | ||
| starts_with_single_slash = raw[0] == 47 | ||
| starts_with_double_slash = starts_with_single_slash and \ | ||
| len(raw) >= 2 and \ | ||
| raw[1] == 47 | ||
| if starts_with_single_slash and \ | ||
| not starts_with_double_slash: | ||
| return cls(remainder=raw) | ||
| is_http = raw.startswith(HTTP_URL_PREFIX) | ||
| is_https = raw.startswith(HTTPS_URL_PREFIX) | ||
| if is_http or is_https or is_double_slash: | ||
| rest = raw[len(b'https://'):] \ | ||
| if is_https \ | ||
| else raw[len(b'http://'):] \ | ||
| if is_http \ | ||
| else raw[len(SLASH + SLASH):] | ||
| scheme = None | ||
| rest = None | ||
| if not starts_with_double_slash: | ||
| # Find scheme | ||
| parts = raw.split(b'://', 1) | ||
| if len(parts) == 2: | ||
| scheme = parts[0] | ||
| rest = parts[1] | ||
| else: | ||
| rest = raw[len(SLASH + SLASH):] | ||
| if scheme is not None or starts_with_double_slash: | ||
| assert rest is not None | ||
| parts = rest.split(SLASH, 1) | ||
| username, password, host, port = Url._parse(parts[0]) | ||
| return cls( | ||
| scheme=b'https' if is_https else b'http', | ||
| scheme=scheme if not starts_with_double_slash else b'http', | ||
| username=username, | ||
| password=password, | ||
| hostname=host, | ||
|
|
||
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
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.
This is not inline code but a reference.