feat: Determine URL protocol based on hostname#900
Conversation
Codecov Report
@@ Coverage Diff @@
## master #900 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 26 27 +1
Lines 1608 1697 +89
Branches 328 347 +19
=========================================
+ Hits 1608 1697 +89
Continue to review full report at Codecov.
|
5987173 to
11308f8
Compare
This will make connections to `localhost` use HTTP. This is insecure but useful for testing. In a future change, we will allow setting transport options at client instantiation time.
11308f8 to
7dc5ce3
Compare
busunkim96
left a comment
There was a problem hiding this comment.
Could you add a unit test that exercises protocol_for?
| Tracking issue: | ||
| https://github.com/googleapis/gapic-generator-python/issues/901 | ||
| #} | ||
| def protocol_for(host: str) -> str: |
There was a problem hiding this comment.
(Since It looks like this function shouldn't be called by end users)
| def protocol_for(host: str) -> str: | |
| def _protocol_for(host: str) -> str: |
| LOCALHOST='localhost' | ||
| LOCALHOST_LEN=len(LOCALHOST) | ||
|
|
||
| if host.startswith(LOCALHOST) and (len(host)==LOCALHOST_LEN or host[LOCALHOST_LEN]==":"): | ||
| return 'http' |
There was a problem hiding this comment.
I have a slight preference for regex match here instead (especially since the docstring already seems to describe this in terms of regex :)) https://docs.python.org/3/library/re.html
There was a problem hiding this comment.
Done. It's more compact. The performance impact shouldn't be too bad, especially as I memoize the regex compilation.
- mark it private with leading underscore - use regex for host matching
vchudnov-g
left a comment
There was a problem hiding this comment.
Made the code changes. Still need to implement the unit test. (Need to figure out how to run the tests locally first!)
| Tracking issue: | ||
| https://github.com/googleapis/gapic-generator-python/issues/901 | ||
| #} | ||
| def protocol_for(host: str) -> str: |
| LOCALHOST='localhost' | ||
| LOCALHOST_LEN=len(LOCALHOST) | ||
|
|
||
| if host.startswith(LOCALHOST) and (len(host)==LOCALHOST_LEN or host[LOCALHOST_LEN]==":"): | ||
| return 'http' |
There was a problem hiding this comment.
Done. It's more compact. The performance impact shouldn't be too bad, especially as I memoize the regex compilation.
| """Determine the URL protocol to use for connecting to the given host. | ||
|
|
||
| This function returns 'http' for hosts that match | ||
| `^localhost(:.*)?$` and `https` for all others. This is intended |
There was a problem hiding this comment.
Do we have anything like this in the other languages? Overall this looks suspicious I would prefer to not have something like this. Determining protocol from host name does not seem reliable and also we are modifying production code for testing purposes. Also what if the host is specified as an IP (land specifically 127.0.0.1)?
There was a problem hiding this comment.
Yes. This was discussed in an internal doc; I'll ping you the link. We're specifically looking for localhost because yes, this is a temporary special case to allow testing (see #901 for the long-term solution)
| str: The protocol (`http` or `https`) to use for connecting to `host`. | ||
| """ | ||
| if not hasattr(_protocol_for, 'regex'): | ||
| _protocol_for.regex = re.compile("^localhost(:.*)?$") |
There was a problem hiding this comment.
Will this regex be executed for every call made? If yes, it will have some performance implications (not big probably, but here we sacrifice production code to support rare testing case, which is suboptimal).
There was a problem hiding this comment.
The compilation of the regex will only happen once because I have it memoized via this attribute.
The matching will happen for every call for now, though we could refactor things so the protocol is only computed once per client and stored in an attribute. However, given that the performance hit is small, I'm inclined to think it's preferable to keep things simple.
|
I'm going to close this PR due to it being stale but please feel free to re-open and update it. |
This will make connections to
localhostuse HTTP. This is insecure but useful for testing. In a future change, we will allow setting transport options at client instantiation time.