Customize handling of http 502#12
Conversation
| FullHttpResponse response = ProxyUtils.createFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_GATEWAY, body); | ||
| HttpResponseStatus status = HttpResponseStatus.BAD_GATEWAY; | ||
|
|
||
| if (cause instanceof SSLHandshakeException) { |
There was a problem hiding this comment.
the check may be improved
There was a problem hiding this comment.
instead of doing this can we implement a method which can be overridden? this way implementors would be able to customize the message based on their own logic. if the writeBadGateway was protected for instance we could override it.
|
@mjallday we can not just simply override |
| @@ -0,0 +1,37 @@ | |||
| package org.littleshoot.proxy; | |||
There was a problem hiding this comment.
The code in this class is copied from writeBadRequest method of ClientToProxyConnection class
|
@mjallday we need to come up with versioning system here.. |
| * @param httpRequest the HttpRequest that is resulting in the Bad Gateway response | ||
| * @return true if the connection will be kept open, or false if it will be disconnected | ||
| */ | ||
| private boolean writeBadGateway(HttpRequest httpRequest) { |
There was a problem hiding this comment.
@viacheslav-fomin-main i see that the respondWithShortCircuitResponse method calls currentFilters.proxyToClientResponse.
i'm wondering if it's possible for us to implement similar functionality in that method without making this change.
e.g.
@Override
public HttpObject handleProxyToClientResponse(HttpObject httpObject) {
if (httpObect.statusCode() == HttpResponseStatus.BAD_GATEWAY) {
// logic here
}
}
| import io.netty.handler.codec.http.FullHttpResponse; | ||
| import io.netty.handler.codec.http.HttpRequest; | ||
|
|
||
| public interface ServerConnectionFailureHttpResponseComposer { |
There was a problem hiding this comment.
unsure about the naming on this. it seems wrong to limit it to just server connection failures. how about just HttpResponseComposer?
There was a problem hiding this comment.
well, it should contain failure in it as it expects throwable cause.. it can not be used for constructing OK response.. so FailureHttpResponseComposer? @mjallday
| **************************************************************************/ | ||
|
|
||
| /** | ||
| * Tells the client that something went wrong trying to proxy its request. If the Bad Gateway is a response to |
There was a problem hiding this comment.
we should keep this comment i believe.
| * | ||
| * @param unrecoverableFailureHttpResponseComposer | ||
| * @return | ||
| */ |
| * @return true if the connection will be kept open, or false if it will be disconnected | ||
| */ | ||
| @Override | ||
| public FullHttpResponse compose(HttpRequest httpRequest, Throwable cause) { |
There was a problem hiding this comment.
we should unit test for this component.
| } | ||
|
|
||
| public FullHttpResponse compose(HttpRequest httpRequest) { | ||
| return this.compose(httpRequest, null); |
There was a problem hiding this comment.
seems like a leaky abstraction to me. let's just stick to compose(httpRequest, null); and not expose this method
| } | ||
|
|
||
| return respondWithShortCircuitResponse(response); | ||
| private boolean writeBadGateway (HttpRequest httpRequest) { |
| // don't allow any body content in response to a HEAD request | ||
| response.content().clear(); | ||
| } | ||
| return response; |
There was a problem hiding this comment.
the following part will be common between various implementations
FullHttpResponse response = ProxyUtils.createFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_GATEWAY, body);
if (ProxyUtils.isHEAD(httpRequest)) {
// don't allow any body content in response to a HEAD request
response.content().clear();
}
return response;so we should abstract it away
There was a problem hiding this comment.
diff --git a/src/main/java/org/littleshoot/proxy/BadGatewayFailureHttpResponseComposer.java b/src/main/java/org/littleshoot/proxy/BadGatewayFailureHttpResponseComposer.java
index 26bcd78..b7e8abd 100644
--- a/src/main/java/org/littleshoot/proxy/BadGatewayFailureHttpResponseComposer.java
+++ b/src/main/java/org/littleshoot/proxy/BadGatewayFailureHttpResponseComposer.java
@@ -20,7 +20,7 @@ public final class BadGatewayFailureHttpResponseComposer implements ServerConnec
*/
@Override
public FullHttpResponse compose(HttpRequest httpRequest, Throwable cause) {
- String body = "Bad Gateway: " + httpRequest.getUri();
+ String body = provideCustomMessage(httpRequest, cause);
FullHttpResponse response = ProxyUtils.createFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_GATEWAY, body);
@@ -31,6 +31,10 @@ public final class BadGatewayFailureHttpResponseComposer implements ServerConnec
return response;
}
+ protected String provideCustomMessage(HttpRequest httpRequest, Throwable cause) {
+ return "Bad Gateway: " + httpRequest.getUri();
+ }
+
public FullHttpResponse compose(HttpRequest httpRequest) {
return this.compose(httpRequest, null);
}There was a problem hiding this comment.
this way you custom implementation would be a specialization of the default impl provided by BadGatewayFailureHttpResponseComposer.
osklyarenko
left a comment
There was a problem hiding this comment.
pls provide a better abstraction and add missing tests.
|
|
||
| FullHttpResponse response = ProxyUtils.createFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_GATEWAY, body); | ||
|
|
||
| if (ProxyUtils.isHEAD(httpRequest)) { |
There was a problem hiding this comment.

well, it is tested by the original littleproxy tests.. but okay, lets add additional test
@osklyarenko
Prerequisite to https://github.com/verygoodsecurity/vault/pull/1380.
Adds ability to set up a custom http response on upstream server connection failure.