on master help sign in
Do not strip a trailing slash from URI path

Some servers will interpret the path differently depending on if there is a trailing slash or not. Example: OK: https://gerrit-review.googlesource.com/projects/?p=gerrit 404: https://gerrit-review.googlesource.com/projects?p=gerrit

commit129ea3f21044f7c1b6123bc519a4b0534437db9e authorJohan Förberg <johan@forberg.se> dateThu 21 Jan 2021, 16:50 (6 years ago) parent3884a44
3 files changed+22−6
CHANGELOG.md+5 Network/HTTP/Req.hs+14−4 pure-tests/Network/HTTP/ReqSpec.hs+3−2
CHANGELOG.md+5
@@ -1,3 +1,8 @@
1+## Req 3.9.0
2+
3+* The `useHttpURI` and `useHttpsURI` functions now preserve trailing
4+ slashes.
5+
16## Req 3.8.0
27
38* Adjusted the value of the `httpConfigRetryJudgeException` field of
Network/HTTP/Req.hs+14−4
@@ -1029,8 +1029,7 @@ useHttpURI uri = do
10291029 urlHead <- http <$> uriHost uri
10301030 let url = case URI.uriPath uri of
10311031 Nothing -> urlHead
1032− Just (_, xs) ->
1033− foldl' (/:) urlHead (URI.unRText <$> NE.toList xs)
1032+ Just uriPath -> uriPathToUrl uriPath urlHead
10341033 return (url, uriOptions uri)
10351034
10361035-- | Just like 'useHttpURI', but expects the “https” scheme.
@@ -1042,10 +1041,21 @@ useHttpsURI uri = do
10421041 urlHead <- https <$> uriHost uri
10431042 let url = case URI.uriPath uri of
10441043 Nothing -> urlHead
1045− Just (_, xs) ->
1046− foldl' (/:) urlHead (URI.unRText <$> NE.toList xs)
1044+ Just uriPath -> uriPathToUrl uriPath urlHead
10471045 return (url, uriOptions uri)
10481046
1047+-- | Convert URI path to a 'Url'. Internal.
1048+--
1049+-- @since 3.9.0
1050+uriPathToUrl ::
1051+ (Bool, NonEmpty (URI.RText 'URI.PathPiece)) ->
1052+ Url scheme ->
1053+ Url scheme
1054+uriPathToUrl (trailingSlash, xs) urlHead =
1055+ if trailingSlash then path /: T.empty else path
1056+ where
1057+ path = foldl' (/:) urlHead (URI.unRText <$> NE.toList xs)
1058+
10491059-- | A combination of 'useHttpURI' and 'useHttpsURI' for cases when scheme
10501060-- is not known in advance.
10511061--
pure-tests/Network/HTTP/ReqSpec.hsmode 100755 → 100644+3−2
@@ -565,8 +565,9 @@ uriPort def uri =
565565-- | Get path from 'URI'.
566566uriPath :: URI -> ByteString
567567uriPath uri = fromMaybe "" $ do
568− (_, xs) <- URI.uriPath uri
569− (return . encodePathPieces . fmap URI.unRText . NE.toList) xs
568+ (trailingSlash, xs) <- URI.uriPath uri
569+ let pref = (encodePathPieces . fmap URI.unRText . NE.toList) xs
570+ return $ if trailingSlash then pref <> "/" else pref
570571
571572-- | Get query string from 'URI'.
572573uriQuery :: URI -> ByteString