Repository: adamchainz/apig-wsgi

Issue:
2.5.0 Multivalue support breaks Host Header support
The new update for 2.5.0 broke for us, because we're trying to access the HTTP Host header directly, rather than accessing via the `SERVER_NAME` WSGI variable.

Was this intentional, or just a byproduct of trying to also set `SERVER_NAME` for WSGI spec compliance?


Discussion:

Patch:
diff --git a/src/apig_wsgi.py b/src/apig_wsgi.py
index efb01d2..d5ea0f0 100644
--- a/src/apig_wsgi.py
+++ b/src/apig_wsgi.py
@@ -94,10 +94,8 @@ def get_environ(event, binary_support):
 
         if key == "CONTENT_TYPE":
             environ["CONTENT_TYPE"] = values[-1]
-            continue
         elif key == "HOST":
             environ["SERVER_NAME"] = values[-1]
-            continue
         elif key == "X_FORWARDED_FOR":
             environ["REMOTE_ADDR"] = values[-1].split(", ")[0]
         elif key == "X_FORWARDED_PROTO":


Test patch:
diff --git a/tests/test_apig_wsgi.py b/tests/test_apig_wsgi.py
index 736d859..fff9c37 100644
--- a/tests/test_apig_wsgi.py
+++ b/tests/test_apig_wsgi.py
@@ -343,10 +343,60 @@ def test_special_headers(simple_app):
     simple_app.handler(event, None)
 
     assert simple_app.environ["CONTENT_TYPE"] == "text/plain"
+    assert simple_app.environ["HTTP_CONTENT_TYPE"] == "text/plain"
     assert simple_app.environ["SERVER_NAME"] == "example.com"
+    assert simple_app.environ["HTTP_HOST"] == "example.com"
     assert simple_app.environ["REMOTE_ADDR"] == "1.2.3.4"
+    assert simple_app.environ["HTTP_X_FORWARDED_FOR"] == "1.2.3.4, 5.6.7.8"
     assert simple_app.environ["wsgi.url_scheme"] == "https"
+    assert simple_app.environ["HTTP_X_FORWARDED_PROTO"] == "https"
     assert simple_app.environ["SERVER_PORT"] == "123"
+    assert simple_app.environ["HTTP_X_FORWARDED_PORT"] == "123"
+
+
+def test_special_content_type(simple_app):
+    event = make_event(headers={"Content-Type": ["text/plain"]})
+
+    simple_app.handler(event, None)
+
+    assert simple_app.environ["CONTENT_TYPE"] == "text/plain"
+    assert simple_app.environ["HTTP_CONTENT_TYPE"] == "text/plain"
+
+
+def test_special_host(simple_app):
+    event = make_event(headers={"Host": ["example.com"]})
+
+    simple_app.handler(event, None)
+
+    assert simple_app.environ["SERVER_NAME"] == "example.com"
+    assert simple_app.environ["HTTP_HOST"] == "example.com"
+
+
+def test_special_x_forwarded_for(simple_app):
+    event = make_event(headers={"X-Forwarded-For": ["1.2.3.4, 5.6.7.8"]})
+
+    simple_app.handler(event, None)
+
+    assert simple_app.environ["REMOTE_ADDR"] == "1.2.3.4"
+    assert simple_app.environ["HTTP_X_FORWARDED_FOR"] == "1.2.3.4, 5.6.7.8"
+
+
+def test_x_forwarded_proto(simple_app):
+    event = make_event(headers={"X-Forwarded-Proto": ["https"]})
+
+    simple_app.handler(event, None)
+
+    assert simple_app.environ["wsgi.url_scheme"] == "https"
+    assert simple_app.environ["HTTP_X_FORWARDED_PROTO"] == "https"
+
+
+def test_x_forwarded_port(simple_app):
+    event = make_event(headers={"X-Forwarded-Port": ["123"]})
+
+    simple_app.handler(event, None)
+
+    assert simple_app.environ["SERVER_PORT"] == "123"
+    assert simple_app.environ["HTTP_X_FORWARDED_PORT"] == "123"
 
 
 def test_no_headers(simple_app):