-
-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#406 added X-Scheme/X-Forwarded-Proto headers resolving
- Loading branch information
Showing
3 changed files
with
51 additions
and
8 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import types | ||
|
||
|
||
def _start_request_decorator(func): | ||
def wrapper(self, *args, **kwargs): | ||
delegate = func(*args, **kwargs) | ||
|
||
_decorate(delegate, 'headers_received', _headers_received_decorator) | ||
|
||
return delegate | ||
|
||
return wrapper | ||
|
||
|
||
def _headers_received_decorator(func): | ||
def wrapper(self, start_line, headers, *args, **kwargs): | ||
proto_header = headers.get('X-Scheme', headers.get('X-Forwarded-Proto')) | ||
|
||
if proto_header: | ||
# use only the last proto entry if there is more than one | ||
proto_header = proto_header.split(',')[-1].strip() | ||
if proto_header in ('http', 'https'): | ||
self.request_conn.context.protocol = proto_header | ||
|
||
return func(start_line, headers, *args, **kwargs) | ||
|
||
return wrapper | ||
|
||
|
||
def _decorate(obj, method_name, decorator): | ||
original_method = getattr(obj, method_name) | ||
new_method = types.MethodType(decorator(original_method), obj) | ||
setattr(obj, method_name, new_method) | ||
|
||
|
||
def autoapply_xheaders(application): | ||
_decorate(application, 'start_request', _start_request_decorator) |