Skip to content

Commit

Permalink
Make it easier to support custom OHttpVersion implementations
Browse files Browse the repository at this point in the history
Motivation:

We should make it easier to support custom OHttpVersions by providing an easy way to override the selection

Modifications:

- Add new protected method that can be override to use a custom OHttpVersion.
- Mark a few methods final in the codecs

Result:

Easier to customize
  • Loading branch information
normanmaurer committed Dec 6, 2023
1 parent 1eada99 commit 12d85bf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ default void outerRequestUpdateHeaders(HttpHeaders headers) {
protected abstract EncapsulationParameters encapsulationParameters(HttpRequest request);

@Override
public boolean isSharable() {
public final boolean isSharable() {
return false;
}

@Override
protected void decode(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) {
protected final void decode(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) {
if (destroyed) {
throw new IllegalStateException("Already destroyed");
}
Expand Down Expand Up @@ -136,7 +136,7 @@ protected void decode(ChannelHandlerContext ctx, HttpObject msg, List<Object> ou
}

@Override
protected void encode(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) {
protected final void encode(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) {
try {
if (msg instanceof HttpRequest) {
HttpRequest innerRequest = (HttpRequest) msg;
Expand Down Expand Up @@ -184,7 +184,7 @@ protected void encode(ChannelHandlerContext ctx, HttpObject msg, List<Object> ou
}

@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
public final void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
if (!destroyed) {
destroyed = true;
cumulationBuffer.release();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,29 @@ protected void onResponse(HttpRequest request, HttpResponse response) {
}

@Override
public boolean isSharable() {
public final boolean isSharable() {
return false;
}

/**
* Select the correct {@link OHttpVersion} based on the content-type value or {@code null} if none
* could be selected.
*
* @param contentTypeValue the value of the content-type header.
* @return the version or {@code null} if none could be selected.
*/
protected OHttpVersion selectOHttpVersion(String contentTypeValue) {
if (OHttpConstants.REQUEST_CONTENT_TYPE.contentEqualsIgnoreCase(contentTypeValue)) {
return OHttpVersionDraft.INSTANCE;
}
if (OHttpConstants.CHUNKED_REQUEST_CONTENT_TYPE.contentEqualsIgnoreCase(contentTypeValue)) {
return OHttpVersionChunkDraft.INSTANCE;
}
return null;
}

@Override
protected void decode(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) {
protected final void decode(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) {
if (destroyed) {
throw new IllegalStateException("Already destroyed");
}
Expand All @@ -95,11 +112,9 @@ protected void decode(ChannelHandlerContext ctx, HttpObject msg, List<Object> ou
parser = null;
sentResponse = false;
if (req.method() == HttpMethod.POST) {
String contentTypeValue = req.headers().get(HttpHeaderNames.CONTENT_TYPE);
if (OHttpConstants.REQUEST_CONTENT_TYPE.contentEqualsIgnoreCase(contentTypeValue)) {
context = newServerContext(req, OHttpVersionDraft.INSTANCE);
} else if (OHttpConstants.CHUNKED_REQUEST_CONTENT_TYPE.contentEqualsIgnoreCase(contentTypeValue)) {
context = newServerContext(req, OHttpVersionChunkDraft.INSTANCE);
OHttpVersion version = selectOHttpVersion(req.headers().get(HttpHeaderNames.CONTENT_TYPE));
if (version != null) {
context = newServerContext(req, version);
}
}
if (context != null) {
Expand Down Expand Up @@ -131,7 +146,7 @@ protected void decode(ChannelHandlerContext ctx, HttpObject msg, List<Object> ou
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
public final void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
context = null;
if (!sentResponse && request != null) {
sentResponse = true;
Expand All @@ -147,7 +162,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
}

@Override
protected void encode(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) {
protected final void encode(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) {
try {
if (msg instanceof HttpResponse) {
serializer = null;
Expand Down Expand Up @@ -181,7 +196,7 @@ protected void encode(ChannelHandlerContext ctx, HttpObject msg, List<Object> ou
}

@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
public final void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
if (!destroyed) {
destroyed = true;
cumulationBuffer.release();
Expand Down

0 comments on commit 12d85bf

Please sign in to comment.