Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Status code on unresolvable hosts. #51

Open
thekvs opened this issue May 20, 2014 · 7 comments · May be fixed by #492
Open

Status code on unresolvable hosts. #51

thekvs opened this issue May 20, 2014 · 7 comments · May be fixed by #492

Comments

@thekvs
Copy link

thekvs commented May 20, 2014

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
Copy link
Author

thekvs commented May 20, 2014

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

@elazarl
Copy link
Owner

elazarl commented Aug 23, 2014

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).

@thekvs
Copy link
Author

thekvs commented Sep 3, 2014

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.

@marbemac
Copy link

marbemac commented Dec 4, 2014

+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.

@elazarl
Copy link
Owner

elazarl commented Dec 4, 2014

I'm convinced. Would fix tonight.

On Thu, Dec 4, 2014, 5:39 PM Marc MacLeod notifications@github.com 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
#51 (comment).

@mmatczuk
Copy link

mmatczuk commented Nov 8, 2022

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 added a commit to mmatczuk/goproxy that referenced this issue Nov 8, 2022
Currently, it's not possible to distinguish target server internal error from using a wrong server or server being down.
This patch changes status code 500 to 502 in case we cannot get a response from upstream.

Below snippets from the HTTP specification:

> The HyperText Transfer Protocol (HTTP) 502 Bad Gateway
> server error response code indicates that the server, while acting as a gateway or proxy, received an invalid response from the upstream server.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/502

vs.

> The HyperText Transfer Protocol (HTTP) 500 Internal Server Error
> server error response code indicates that the server encountered an unexpected condition that prevented it from fulfilling the request.
> This error response is a generic "catch-all" response. Usually, this indicates the server cannot find a better 5xx error code to response. Sometimes, server administrators log error responses like the 500 status code with more details about the request to prevent the error from happening again in the future.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500

Fixes elazarl#51
mmatczuk added a commit to mmatczuk/goproxy that referenced this issue Nov 8, 2022
This patch allows to distinguish between status codes
* 502 - network error, failed to dial
* 504 - network error, dial timeout
* 500 - other error

Relates to elazarl#51
mmatczuk added a commit to mmatczuk/goproxy that referenced this issue Nov 8, 2022
Currently, it's not possible to distinguish target server internal error from using a wrong server or server being down.
This patch changes status code 500 to 502 in case we cannot get a response from upstream.

Below snippets from the HTTP specification:

> The HyperText Transfer Protocol (HTTP) 502 Bad Gateway
> server error response code indicates that the server, while acting as a gateway or proxy, received an invalid response from the upstream server.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/502

vs.

> The HyperText Transfer Protocol (HTTP) 500 Internal Server Error
> server error response code indicates that the server encountered an unexpected condition that prevented it from fulfilling the request.
> This error response is a generic "catch-all" response. Usually, this indicates the server cannot find a better 5xx error code to response. Sometimes, server administrators log error responses like the 500 status code with more details about the request to prevent the error from happening again in the future.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500

Fixes elazarl#51
mmatczuk added a commit to mmatczuk/goproxy that referenced this issue Nov 8, 2022
This patch allows to distinguish between status codes
* 502 - network error, failed to dial
* 504 - network error, dial timeout
* 500 - other error

Relates to elazarl#51
@mmatczuk mmatczuk linked a pull request Nov 9, 2022 that will close this issue
@lambrospetrou
Copy link

lambrospetrou commented Nov 26, 2023

@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
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants