-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for automatic gzip and deflate decompression
- Loading branch information
Showing
15 changed files
with
1,065 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
riposte-core/src/main/java/com/nike/riposte/server/handler/RiposteHandlerInternalUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.nike.riposte.server.handler; | ||
|
||
import com.nike.riposte.server.error.exception.InvalidHttpRequestException; | ||
import com.nike.riposte.server.http.HttpProcessingState; | ||
import com.nike.riposte.server.http.RequestInfo; | ||
import com.nike.riposte.server.http.impl.RequestInfoImpl; | ||
|
||
import io.netty.handler.codec.http.HttpObject; | ||
import io.netty.handler.codec.http.HttpRequest; | ||
|
||
/** | ||
* Contains utility methods for Riposte handlers. This is intentionally package-private - it is not intended for general | ||
* usage. | ||
* | ||
* @author Nic Munroe | ||
*/ | ||
class RiposteHandlerInternalUtil { | ||
|
||
static RiposteHandlerInternalUtil DEFAULT_IMPL = new RiposteHandlerInternalUtil(); | ||
|
||
RequestInfo<?> createRequestInfoFromNettyHttpRequestAndHandleStateSetupIfNecessary( | ||
HttpRequest httpRequest, HttpProcessingState state | ||
) { | ||
// If the HttpProcessingState already has a RequestInfo then we should just use that. | ||
RequestInfo<?> requestInfo = state.getRequestInfo(); | ||
if (requestInfo != null) { | ||
return requestInfo; | ||
} | ||
|
||
// No RequestInfo has been created yet. Check for an invalid Netty HttpRequest, and assuming it's good then | ||
// generate a new RequestInfo from it and set the RequestInfo on our HttpProcessingState. | ||
throwExceptionIfNotSuccessfullyDecoded(httpRequest); | ||
requestInfo = new RequestInfoImpl<>(httpRequest); | ||
state.setRequestInfo(requestInfo); | ||
|
||
return requestInfo; | ||
} | ||
|
||
void throwExceptionIfNotSuccessfullyDecoded(HttpObject httpObject) { | ||
if (httpObject.getDecoderResult() != null && httpObject.getDecoderResult().isFailure()) { | ||
throw new InvalidHttpRequestException("Detected HttpObject that was not successfully decoded.", | ||
httpObject.getDecoderResult().cause()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
riposte-core/src/main/java/com/nike/riposte/server/handler/SmartHttpContentDecompressor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.nike.riposte.server.handler; | ||
|
||
import com.nike.riposte.server.channelpipeline.ChannelAttributes; | ||
import com.nike.riposte.server.http.Endpoint; | ||
import com.nike.riposte.server.http.HttpProcessingState; | ||
import com.nike.riposte.server.http.ProxyRouterEndpoint; | ||
import com.nike.riposte.server.http.RequestInfo; | ||
|
||
import io.netty.channel.embedded.EmbeddedChannel; | ||
import io.netty.handler.codec.http.HttpContentDecompressor; | ||
|
||
/** | ||
* Extension of {@link HttpContentDecompressor} that is smart about whether it decompresses responses or not. It | ||
* inspects the {@link HttpProcessingState#getEndpointForExecution()} to see what | ||
* {@link Endpoint#isDecompressRequestPayloadAllowed(RequestInfo)} returns, and only allows automatic decompression if | ||
* that methods returns true. | ||
* | ||
* <p>{@link ProxyRouterEndpoint}s return false by default for {@link | ||
* ProxyRouterEndpoint#isDecompressRequestPayloadAllowed(RequestInfo)}, so they will not auto-decompress unless that | ||
* method is overridden for a given {@link ProxyRouterEndpoint}. Other endpoint types default to true causing them to | ||
* auto-decompress unless that method is overridden to return false. | ||
* | ||
* @author Nic Munroe | ||
*/ | ||
public class SmartHttpContentDecompressor extends HttpContentDecompressor { | ||
|
||
public SmartHttpContentDecompressor() { | ||
super(); | ||
} | ||
|
||
public SmartHttpContentDecompressor(boolean strict) { | ||
super(strict); | ||
} | ||
|
||
@Override | ||
protected EmbeddedChannel newContentDecoder(String contentEncoding) throws Exception { | ||
// We only allow decompression if the endpoint allows it. | ||
HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get(); | ||
Endpoint<?> endpoint = state.getEndpointForExecution(); | ||
|
||
if (endpointAllowsDecompression(endpoint, state)) { | ||
return super.newContentDecoder(contentEncoding); | ||
} | ||
|
||
// The endpoint does not allow decompression. Return null to indicate that this handler should not | ||
// auto-decompress this request's payload. | ||
return null; | ||
} | ||
|
||
protected boolean endpointAllowsDecompression(Endpoint<?> endpoint, HttpProcessingState state) { | ||
if (endpoint == null) | ||
return true; | ||
|
||
return endpoint.isDecompressRequestPayloadAllowed(state.getRequestInfo()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.