Gerrit Bot has uploaded this change for review.
net/http: fix 302 empty location header error
We need to add a Location check for other redirection codes, otherwise in some cases it causes a fatal error.
Example:
```
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
/**
index.php
<?php
http_response_code(302);
?>
<!DOCTYPE html>
<html>
<body>
body
</body>
</html>
**/
resp, _ := http.Get("http://example.com")
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}
```
And error:
```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x564a6681987e]
goroutine 1 [running]:
main.main
/home/test2.go:23
exit status 2
```
Change-Id: I9103806ab0da374e25dd73200f49147f97670de7
GitHub-Last-Rev: aa9265bf43749fe5097c95a6fb9892810606fcf9
GitHub-Pull-Request: golang/go#43448
---
M src/net/http/client.go
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/src/net/http/client.go b/src/net/http/client.go
index 88e2028..ba3da59 100644
--- a/src/net/http/client.go
+++ b/src/net/http/client.go
@@ -500,7 +500,13 @@
redirectMethod = reqMethod
shouldRedirect = true
includeBody = false
-
+ if resp.Header.Get("Location") == "" {
+ shouldRedirect = false
+ break
+ }
+ if ireq.GetBody == nil && ireq.outgoingLength() != 0 {
+ shouldRedirect = false
+ }
// RFC 2616 allowed automatic redirection only with GET and
// HEAD requests. RFC 7231 lifts this restriction, but we still
// restrict other methods to GET to maintain compatibility.
To view, visit change 280952. To unsubscribe, or for help writing mail filters, visit settings.
Brad Fitzpatrick abandoned this change.
To view, visit change 280952. To unsubscribe, or for help writing mail filters, visit settings.