Skip to content

Commit

Permalink
Revert "Escape reserved characters in URI path after rendering"
Browse files Browse the repository at this point in the history
This reverts commit 12aaa6a.
  • Loading branch information
alexbiehl committed Feb 14, 2024
1 parent 12aaa6a commit b79ccdc
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 37 deletions.
21 changes: 7 additions & 14 deletions src/Scarf/Gateway/Rule.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import Data.Text (Text)
import Data.Text qualified as Text
import Data.Text.Encoding qualified as Text
import Network.HTTP.Types.URI (Query, parseQuery, parseQueryText, renderQuery)
import Network.URI (URI (..), escapeURIString, isUnreserved, parseRelativeReference, parseURI)
import Network.URI (URI (..), parseRelativeReference, parseURI)
import Network.Wai
( pathInfo,
rawQueryString,
Expand Down Expand Up @@ -643,11 +643,15 @@ rewriteQueryString ::
Wai.Request ->
ByteString
rewriteQueryString redirectTarget request
-- Fast path: In case there are no query parameters on the request
-- there is no need for rewriting.
| [] <- Wai.queryString request =
Text.encodeUtf8 redirectTarget
-- Parse the redirect target as a URI to extract, combine and replace
-- the query part of it.
-- TODO: there are an aweful lot of string conversions going on,
-- maybe there's a more direct way.
| Just uri@URI {uriQuery, uriPath} <- parseURI (Text.unpack redirectTarget) =
| Just uri@URI {uriQuery} <- parseURI (Text.unpack redirectTarget) =
let redirectTargetQuery =
parseQuery (Text.encodeUtf8 (Text.pack uriQuery))

Expand All @@ -672,18 +676,7 @@ rewriteQueryString redirectTarget request
)
in Text.encodeUtf8 $
Text.pack $
show
( uri
{ uriQuery =
Text.unpack renderedQuery,
uriPath =
-- We go over the URI path to escape anything that is worth escaping.
-- Unfortunately rendering the URI doesn't do any escaping automatically.
escapeURIString
(\c -> c == '/' || isUnreserved c)
uriPath
}
)
show (uri {uriQuery = Text.unpack renderedQuery})
-- In case the redirectTarget didn't parse as a URI we are not doing anything
-- and ideally shouldn't happen.
| otherwise =
Expand Down
Empty file.
12 changes: 0 additions & 12 deletions test/golden/file-package-escaping.output.yaml

This file was deleted.

11 changes: 0 additions & 11 deletions test/golden/file-package-escaping.yaml

This file was deleted.

0 comments on commit b79ccdc

Please sign in to comment.