goproxy icon indicating copy to clipboard operation
goproxy copied to clipboard

Status code on unresolvable hosts.

Open thekvs opened this issue 10 years ago • 7 comments

If we try to access through goproxy host which doesn't have DNS record goproxy returns 500 (Internal Server Error) HTTP status code, while squid-cache returns 502 (Bad Gateway) HTTP status code. Shouldn't goproxy simulate squid's behavior?

$ curl -v --proxy 127.0.0.1:3129  --url http://no.exists/
* About to connect() to proxy 127.0.0.1 port 3129 (#0)
*   Trying 127.0.0.1...
* Adding handle: conn: 0x1d71bd0
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x1d71bd0) send_pipe: 1, recv_pipe: 0
* Connected to 127.0.0.1 (127.0.0.1) port 3129 (#0)
> GET http://no.exists/ HTTP/1.1
> User-Agent: curl/7.32.0
> Host: no.exists
> Accept: */*
> Proxy-Connection: Keep-Alive
> 
< HTTP/1.1 500 Internal Server Error
< Content-Type: text/plain; charset=utf-8
< Date: Tue, 20 May 2014 13:13:02 GMT
< Content-Length: 41
< 
dial tcp: lookup no.exists: no such host
* Connection #0 to host 127.0.0.1 left intact
$ 

thekvs avatar May 20 '14 13:05 thekvs

Actually squid responds with 503 status code:

HTTP/1.1 503 Service Unavailable
Server: squid/3.3.8
Mime-Version: 1.0
Date: Tue, 20 May 2014 14:47:47 GMT
Content-Type: text/html
Content-Length: 3640
X-Squid-Error: ERR_DNS_FAIL 0
Vary: Accept-Language
Content-Language: en
X-Cache: MISS from ubuntu-v
X-Cache-Lookup: MISS from ubuntu-v:3128
Via: 1.1 ubuntu-v (squid/3.3.8)
Connection: keep-alive

thekvs avatar May 20 '14 14:05 thekvs

I apologize for not getting back to you until now.

I'm really not sure what should be the correct response. Can you help me by specifying the use case?

How do you use this information (or is it that you just want to be squid-compliant).

elazarl avatar Aug 23 '14 19:08 elazarl

I just want to somehow detect this situation and show to a user some custom more or less nice looking error page and intercepting 500 "Internal Server Error" seems to me not a very good idea. I think that squid's behavior is more logical in this case.

thekvs avatar Sep 03 '14 18:09 thekvs

+1 On this one. Some of my users are running their own servers (say on localhost:3000), and it'd be great if goproxy could make it easy to distinguish between a server that's not running (ie the user forgot to start it) and a server that responded with a 500.

marbemac avatar Dec 04 '14 15:12 marbemac

I'm convinced. Would fix tonight.

On Thu, Dec 4, 2014, 5:39 PM Marc MacLeod [email protected] wrote:

+1 On this one. Some of my users are running their own servers (say on localhost:3000), and it'd be great if goproxy could make it easy to distinguish between a server that's not running (ie the user forgot to start it) and a server that responded with a 500.

— Reply to this email directly or view it on GitHub https://github.com/elazarl/goproxy/issues/51#issuecomment-65650255.

elazarl avatar Dec 04 '14 15:12 elazarl

Any update on that? The correct behavior is to return 502 (not 503) since this is a proxy specific error code. Service Unavailable is a "come back later" I'm busy response.

mmatczuk avatar Nov 08 '22 11:11 mmatczuk

@elazarl Is there any progress on this issue, or was it somehow addressed in a different way?

Right now I do the following to handle this:

proxy.OnResponse().DoFunc(func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response {
	var dnsError *net.DNSError
	if errors.As(ctx.Error, &dnsError) {
		// Do not leak our server's address.
		dnsError.Server = "<server-redacted>"
		return goproxy.NewResponse(ctx.Req, goproxy.ContentTypeText, http.StatusBadGateway, dnsError.Error())
	}
	return resp
})

lambrospetrou avatar Nov 26 '23 21:11 lambrospetrou