@@ -401,9 +401,8 @@ internal class _HTTPURLProtocol: _NativeProtocol {
401
401
easyHandle. timeoutTimer = _TimeoutSource ( queue: task. workQueue, milliseconds: timeoutInterval, handler: timeoutHandler)
402
402
easyHandle. set ( automaticBodyDecompression: true )
403
403
easyHandle. set ( requestMethod: request. httpMethod ?? " GET " )
404
- if request. httpMethod == " HEAD " {
405
- easyHandle. set ( noBody: true )
406
- }
404
+ // Always set the status as it may change if a HEAD is converted to a GET.
405
+ easyHandle. set ( noBody: request. httpMethod == " HEAD " )
407
406
}
408
407
409
408
/// What action to take
@@ -591,7 +590,7 @@ internal extension _HTTPURLProtocol {
591
590
// For now, we'll notify the delegate, but won't pause the transfer,
592
591
// and we'll disregard the completion handler:
593
592
switch response. statusCode {
594
- case 301 , 302 , 303 , 307 :
593
+ case 301 , 302 , 303 , 305 ... 308 :
595
594
break
596
595
default :
597
596
self . client? . urlProtocol ( self , didReceive: response, cacheStoragePolicy: . notAllowed)
@@ -619,19 +618,26 @@ internal extension _HTTPURLProtocol {
619
618
return nil
620
619
}
621
620
621
+ let method = fromRequest. httpMethod ?? " GET "
622
622
// Check for a redirect:
623
623
switch response. statusCode {
624
- //TODO: Should we do this for 300 "Multiple Choices", too?
625
- case 301 , 302 , 303 :
626
- // Change into "GET":
627
- return ( " GET " , targetURL)
628
- case 307 :
629
- // Re-use existing method:
630
- return ( fromRequest. httpMethod ?? " GET " , targetURL)
631
- default :
632
- return nil
624
+ case 301 , 302 :
625
+ // Change "POST" into "GET" but leave other methods unchanged:
626
+ let newMethod = ( method == " POST " ) ? " GET " : method
627
+ return ( newMethod, targetURL)
628
+
629
+ case 303 :
630
+ return ( " GET " , targetURL)
631
+
632
+ case 305 ... 308 :
633
+ // Re-use existing method:
634
+ return ( method, targetURL)
635
+
636
+ default :
637
+ return nil
633
638
}
634
639
}
640
+
635
641
guard let ( method, targetURL) = methodAndURL ( ) else { return nil }
636
642
var request = fromRequest
637
643
request. httpMethod = method
0 commit comments