Go – 运行时错误:无效的内存地址或空指针解引用

huangapple go评论95阅读模式
英文:

Go - runtime error: invalid memory address or nil pointer dereference

问题

我正在尝试使用Go语言创建一个代理服务器,该服务器可以修改API请求的主体中的某些值。然而,当发送请求时,会出现以下恐慌错误,并且请求失败:

2015/05/03 14:17:52 http: panic serving 192.168.1.139:42818: runtime error: invalid memory address or nil pointer dereference
goroutine 72 [running]:
net/http.func·011()
    /usr/lib/go/src/pkg/net/http/server.go:1100 +0xb1
runtime.panic(0x8258ee0, 0x83b3733)
    /usr/lib/go/src/pkg/runtime/panic.c:248 +0x176
main.viewResponse(0x0, 0x1861b470, 0x1860ed01)
    /home/bradley/turbocharger/proxy.go:40 +0xa2
github.com/elazarl/goproxy.FuncRespHandler.Handle(0x82e6480, 0x0, 0x1861b470, 0x1)
    /home/bradley/gopath/src/github.com/elazarl/goproxy/actions.go:35 +0x39
github.com/elazarl/goproxy.func·016(0x0, 0x1861b470, 0x827a768)
    /home/bradley/gopath/src/github.com/elazarl/goproxy/dispatcher.go:279 +0x18f
github.com/elazarl/goproxy.FuncRespHandler.Handle(0x1868c9b0, 0x0, 0x1861b470, 0x4)
    /home/bradley/gopath/src/github.com/elazarl/goproxy/actions.go:35 +0x39
github.com/elazarl/goproxy.(*ProxyHttpServer).filterResponse(0x18682640, 0x0, 0x1861b470, 0x0)
    /home/bradley/gopath/src/github.com/elazarl/goproxy/proxy.go:69 +0x95
github.com/elazarl/goproxy.(*ProxyHttpServer).ServeHTTP(0x18682640, 0xb74dd780, 0x18601260, 0x18600bd0)
    /home/bradley/gopath/src/github.com/elazarl/goproxy/proxy.go:115 +0x39b
net/http.serverHandler.ServeHTTP(0x18681dc0, 0xb74dd780, 0x18601260, 0x18600bd0)
    /usr/lib/go/src/pkg/net/http/server.go:1673 +0x155
net/http.(*conn).serve(0x18628a00)
    /usr/lib/go/src/pkg/net/http/server.go:1174 +0x8c6
created by net/http.(*Server).Serve
    /usr/lib/go/src/pkg/net/http/server.go:1721 +0x2be

以下是脚本代码:

func viewResponse(response *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
    log.Printf("Response: %s", response.Status)

    return response
}
英文:

I am trying to use Go to make a proxy server which changes certain values in the body of a request to an API however when the request is sent the following panic occurs and the request fails:

2015/05/03 14:17:52 http: panic serving 192.168.1.139:42818: runtime error: invalid memory address or nil pointer dereference
goroutine 72 [running]:
net/http.func·011()
	/usr/lib/go/src/pkg/net/http/server.go:1100 +0xb1
runtime.panic(0x8258ee0, 0x83b3733)
	/usr/lib/go/src/pkg/runtime/panic.c:248 +0x176
main.viewResponse(0x0, 0x1861b470, 0x1860ed01)
	/home/bradley/turbocharger/proxy.go:40 +0xa2
github.com/elazarl/goproxy.FuncRespHandler.Handle(0x82e6480, 0x0, 0x1861b470, 0x1)
	/home/bradley/gopath/src/github.com/elazarl/goproxy/actions.go:35 +0x39
github.com/elazarl/goproxy.func·016(0x0, 0x1861b470, 0x827a768)
 	/home/bradley/gopath/src/github.com/elazarl/goproxy/dispatcher.go:279 +0x18f
github.com/elazarl/goproxy.FuncRespHandler.Handle(0x1868c9b0, 0x0, 0x1861b470, 0x4)
	/home/bradley/gopath/src/github.com/elazarl/goproxy/actions.go:35 +0x39
github.com/elazarl/goproxy.(*ProxyHttpServer).filterResponse(0x18682640, 0x0, 0x1861b470, 0x0)
	/home/bradley/gopath/src/github.com/elazarl/goproxy/proxy.go:69 +0x95
github.com/elazarl/goproxy.(*ProxyHttpServer).ServeHTTP(0x18682640, 0xb74dd780, 0x18601260, 0x18600bd0)
	/home/bradley/gopath/src/github.com/elazarl/goproxy/proxy.go:115 +0x39b
net/http.serverHandler.ServeHTTP(0x18681dc0, 0xb74dd780, 0x18601260, 0x18600bd0)
	/usr/lib/go/src/pkg/net/http/server.go:1673 +0x155
net/http.(*conn).serve(0x18628a00)
	/usr/lib/go/src/pkg/net/http/server.go:1174 +0x8c6
created by net/http.(*Server).Serve
	/usr/lib/go/src/pkg/net/http/server.go:1721 +0x2be

Here is the script:

func viewResponse(response *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
	log.Printf("Response: %s", response.Status)

	return response
}

答案1

得分: 1

responsenil

根据文档: "如果发生错误,resp 将为 nil,ctx.RoundTrip.Error 将包含错误。"
https://github.com/elazarl/goproxy/blob/master/actions.go#L22-L28

因此,正确实现响应处理程序的方式应该是这样的:

func viewResponse(response *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
    if response == nil {
        log.Printf("错误: %s", ctx.RoundTrip.Error)
    } else {
        log.Printf("响应: %s", response.Status)
    }
    return response
}
英文:

response is nil.

From the doc: "In case of error, resp will be nil, and ctx.RoundTrip.Error will contain the error."
https://github.com/elazarl/goproxy/blob/master/actions.go#L22-L28

So the correct way to implement your response handler is something like this:

<!-- language: go -->

func viewResponse(response *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
    if response == nil {
        log.Printf(&quot;Error: %s&quot;, ctx.RoundTrip.Error)
    } else {
        log.Printf(&quot;Response: %s&quot;, response.Status)
    }
    return response
}

huangapple
  • 本文由 发表于 2015年5月3日 21:43:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/30014508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定