@@ -689,28 +689,32 @@ internal extension _HTTPURLProtocol {
689
689
return request
690
690
}
691
691
692
- let scheme = request. url? . scheme
693
- let host = request. url? . host
694
- let port = request. url? . port
695
-
696
- var components = URLComponents ( )
697
- components. scheme = scheme
698
- components. host = host
699
- // Use the original port if the new URL does not contain a host
700
- // ie Location: /foo => <original host>:<original port>/Foo
701
- // but Location: newhost/foo will ignore the original port
702
- if targetURL. host == nil {
703
- components. port = port
704
- }
705
- //The path must either begin with "/" or be an empty string.
706
- if targetURL. relativeString. first != " / " {
707
- components. path = " / " + targetURL. relativeString
692
+ guard
693
+ let fromUrl = fromRequest. url,
694
+ var components = URLComponents ( url: fromUrl, resolvingAgainstBaseURL: false )
695
+ else { return nil }
696
+
697
+ // If the new URL contains a host, use the host and port from the new URL.
698
+ // Otherwise, the host and port from the original URL are used.
699
+ if targetURL. host != nil {
700
+ components. host = targetURL. host
701
+ components. port = targetURL. port
702
+ }
703
+
704
+ // The path must either begin with "/" or be an empty string.
705
+ if targetURL. path. hasPrefix ( " / " ) {
706
+ components. path = targetURL. path
708
707
} else {
709
- components. path = targetURL. relativeString
708
+ components. path = " / " + targetURL. path
710
709
}
710
+
711
+ // The query and fragment components are set separately to prevent them from being
712
+ // percent encoded again.
713
+ components. percentEncodedQuery = targetURL. query
714
+ components. percentEncodedFragment = targetURL. fragment
711
715
712
- guard let urlString = components. string else { fatalError ( " Invalid URL " ) }
713
- request. url = URL ( string : urlString )
716
+ guard let url = components. url else { fatalError ( " Invalid URL " ) }
717
+ request. url = url
714
718
715
719
// Inherit the timeout from the previous request
716
720
request. timeoutInterval = fromRequest. timeoutInterval
0 commit comments