Skip to content

Commit 8ecbcef

Browse files
committed
Attach request_uri, request_method and http_headers on the context
- _http_headers is replaced by http_headers - _http_headers used to hold more than headers; this is now fixed - http_headers hold all headers that start with HTTP_ or REMOTE_ or SERVER_ - the query-string of a GET request is already available as context.request - IdpHinting micro-service is now using the new properties Signed-off-by: Ivan Kanakarakis <[email protected]>
1 parent 7e7241f commit 8ecbcef

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/satosa/micro_services/idp_hinting.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import logging
2-
from urllib.parse import parse_qs
32

43
from .base import RequestMicroService
54
from ..exception import SATOSAConfigurationError
@@ -42,15 +41,17 @@ def process(self, context, data):
4241
:param data: the internal request
4342
"""
4443
target_entity_id = context.get_decoration(context.KEY_TARGET_ENTITYID)
45-
qs_raw = context._http_headers['QUERY_STRING']
46-
if target_entity_id or not qs_raw:
44+
query_string = context.request
45+
46+
an_issuer_is_already_selected = bool(target_entity_id)
47+
query_string_is_missing = not query_string
48+
if an_issuer_is_already_selected or query_string_is_missing:
4749
return super().process(context, data)
4850

49-
qs = parse_qs(qs_raw)
5051
hints = (
5152
entity_id
5253
for param in self.idp_hint_param_names
53-
for entity_id in qs.get(param, [None])
54+
for entity_id in query_string.get(param, [])
5455
if entity_id
5556
)
5657
hint = next(hints, None)

src/satosa/proxy_server.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,17 @@ def unpack_request(environ, content_length=0):
6565
return data
6666

6767

68-
def unpack_http_headers(environ):
69-
headers = ('REQUEST_METHOD', 'PATH_INFO', 'REQUEST_URI',
70-
'QUERY_STRING', 'SERVER_NAME', 'REMOTE_ADDR',
71-
'HTTP_HOST', 'HTTP_USER_AGENT', 'HTTP_ACCEPT_LANGUAGE')
72-
return {k:v for k,v in environ.items() if k in headers}
68+
def collect_http_headers(environ):
69+
headers = {
70+
header_name: header_value
71+
for header_name, header_value in environ.items()
72+
if (
73+
header_name.startswith("HTTP_")
74+
or header_name.startswith("REMOTE_")
75+
or header_name.startswith("SERVER_")
76+
)
77+
}
78+
return headers
7379

7480

7581
class ToBytesMiddleware(object):
@@ -116,7 +122,9 @@ def __call__(self, environ, start_response, debug=False):
116122
body = io.BytesIO(environ['wsgi.input'].read(content_length))
117123
environ['wsgi.input'] = body
118124
context.request = unpack_request(environ, content_length)
119-
context._http_headers = unpack_http_headers(environ)
125+
context.request_uri = environ.get("REQUEST_URI")
126+
context.request_method = environ.get("REQUEST_METHOD")
127+
context.http_headers = collect_http_headers(environ)
120128
environ['wsgi.input'].seek(0)
121129

122130
context.cookie = environ.get("HTTP_COOKIE", "")

0 commit comments

Comments
 (0)