Skip to content

Commit 58c58b2

Browse files
committed
Support non-443 ports on HTTPS proxying as well
1 parent 28e5444 commit 58c58b2

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

extra/Lamdera/ReverseProxy.hs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,32 @@ startReverseProxy_ = do
6464
pure $ WPRResponse $ responseLBS status200 ([] & addCors request) ""
6565

6666
_ -> do
67-
case pathInfo request of
68-
"http:":"":hostname:rest -> do
67+
let
68+
-- When a request comes in with a custom defined port, we need to extract that
69+
-- port from the hostname and pass it to the proxy destination. Otherwise, the
70+
-- proxy destination will use the default port (80 for http, 443 for https).
71+
-- for example: https://example.com:3001 -> ProxyDestSecure "example.com" 3001
72+
handleWithDefaultPort :: Text -> [Text] -> Int -> (Request -> ProxyDest -> WaiProxyResponse) -> IO WaiProxyResponse
73+
handleWithDefaultPort hostname rest defaultPort wrapper = do
6974
let
70-
(hostnameClean, port) =
75+
(hostnameClean, targetPort) =
7176
case Text.splitOn ":" hostname of
7277
hostnameClean:port:[] ->
73-
(hostnameClean, port & Text.unpack & readMaybe & withDefault 80)
78+
(hostnameClean, port & Text.unpack & readMaybe & withDefault defaultPort)
7479

7580
_ ->
76-
(hostname, 80)
77-
81+
(hostname, defaultPort)
7882

79-
pure $ WPRModifiedRequest
83+
pure $ wrapper
8084
(modReq rest hostnameClean request)
81-
(ProxyDest (T.encodeUtf8 hostnameClean) port)
85+
(ProxyDest (T.encodeUtf8 hostnameClean) targetPort)
86+
87+
case pathInfo request of
88+
"http:":"":hostname:rest -> do
89+
handleWithDefaultPort hostname rest 80 WPRModifiedRequest
8290

83-
"https:":"":hostname:rest ->
84-
pure $ WPRModifiedRequestSecure
85-
(modReq rest hostname request)
86-
(ProxyDest (T.encodeUtf8 hostname) 443)
91+
"https:":"":hostname:rest -> do
92+
handleWithDefaultPort hostname rest 443 WPRModifiedRequestSecure
8793

8894
path ->
8995
pure $ WPRProxyFail $ "url missing http or https prefix: " <> Text.unpack (Text.intercalate "," path)
@@ -92,9 +98,8 @@ startReverseProxy_ = do
9298
pure res
9399

94100
Nothing ->
95-
-- Impossible for our purposes
96-
-- debug_ "HTTP request without host... dropping"
97-
pure $ WPRProxyDestSecure (ProxyDest "lamdera.com" 80)
101+
-- Impossible for our purposes, but possible with hand crafted curl shennanigans
102+
pure $ WPRProxyFail $ "request is missing host header: " <> show request
98103
)
99104
(\exception ->
100105
debug_note ("\n➡️❌ proxy error: " ++ show exception) defaultOnExc exception

0 commit comments

Comments
 (0)