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

log response body when netty passes in a ByteBuf instead of a HttpContent #978

Merged
merged 1 commit into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpMessage;

final class Buffering implements State {
Expand All @@ -19,12 +18,11 @@ public State without() {
}

@Override
public State buffer(final HttpMessage message, final HttpContent content) {
final ByteBuf source = content.content();
final int index = source.readerIndex();
buffer.ensureWritable(source.readableBytes());
source.readBytes(buffer, source.readableBytes());
source.readerIndex(index);
public State buffer(final HttpMessage message, final ByteBuf content) {
final int index = content.readerIndex();
buffer.ensureWritable(content.readableBytes());
content.readBytes(buffer, content.readableBytes());
content.readerIndex(index);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.zalando.logbook.netty;

import io.netty.handler.codec.http.HttpContent;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.HttpMessage;
import lombok.AllArgsConstructor;

Expand All @@ -15,7 +15,7 @@ public State with() {
}

@Override
public State buffer(final HttpMessage message, final HttpContent content) {
public State buffer(final HttpMessage message, final ByteBuf content) {
buffering.buffer(message, content);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package org.zalando.logbook.netty;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.LastHttpContent;
import javax.annotation.concurrent.NotThreadSafe;
import lombok.RequiredArgsConstructor;
import org.apiguardian.api.API;
import org.zalando.logbook.Logbook;

import javax.annotation.concurrent.NotThreadSafe;

import static org.apiguardian.api.API.Status.EXPERIMENTAL;
import static org.zalando.fauxpas.FauxPas.throwingRunnable;
import static org.zalando.logbook.Logbook.RequestWritingStage;
Expand Down Expand Up @@ -46,7 +45,8 @@ public void write(
this.requestStage = logbook.process(request);
});

runIf(message, HttpContent.class, request::buffer);
runIf(message, HttpContent.class, content -> request.buffer(content.content()));
runIf(message, ByteBuf.class, request::buffer);

runIf(message, LastHttpContent.class, content ->
sequence.set(0, throwingRunnable(requestStage::write)));
Expand All @@ -64,7 +64,8 @@ public void channelRead(
this.responseStage = requestStage.process(response);
});

runIf(message, HttpContent.class, response::buffer);
runIf(message, HttpContent.class, content -> response.buffer(content.content()));
runIf(message, ByteBuf.class, response::buffer);

runIf(message, LastHttpContent.class, content ->
sequence.set(1, throwingRunnable(responseStage::write)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.zalando.logbook.netty;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
Expand Down Expand Up @@ -45,7 +46,8 @@ public void channelRead(
this.requestStage = logbook.process(request);
});

runIf(message, HttpContent.class, request::buffer);
runIf(message, HttpContent.class, content -> request.buffer(content.content()));
runIf(message, ByteBuf.class, request::buffer);

runIf(message, LastHttpContent.class, content ->
sequence.set(0, throwingRunnable(requestStage::write)));
Expand All @@ -64,7 +66,10 @@ public void write(
this.responseStage = requestStage.process(response);
});

runIf(message, HttpContent.class, response::buffer);
runIf(message, HttpContent.class, content -> request.buffer(content.content()));

runIf(message, HttpContent.class, content -> response.buffer(content.content()));
runIf(message, ByteBuf.class, response::buffer);

runIf(message, LastHttpContent.class, content ->
sequence.set(1, throwingRunnable(responseStage::write)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.zalando.logbook.netty;

import io.netty.handler.codec.http.HttpContent;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.HttpMessage;

import static io.netty.handler.codec.http.HttpUtil.getContentLength;
import static io.netty.handler.codec.http.LastHttpContent.EMPTY_LAST_CONTENT;

final class Offering implements State {

Expand All @@ -14,8 +13,8 @@ public State without() {
}

@Override
public State buffer(final HttpMessage message, final HttpContent content) {
if (content.equals(EMPTY_LAST_CONTENT)) {
public State buffer(final HttpMessage message, final ByteBuf content) {
if (content.equals(Unpooled.EMPTY_BUFFER)) {
// saves us from allocating an unnecessary buffer
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package org.zalando.logbook.netty;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpRequest;
import lombok.AllArgsConstructor;
import org.zalando.logbook.HttpHeaders;
import org.zalando.logbook.Origin;

import javax.annotation.Nullable;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

import javax.annotation.Nullable;
import lombok.AllArgsConstructor;
import org.zalando.logbook.HttpHeaders;
import org.zalando.logbook.Origin;
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
import static io.netty.handler.codec.http.HttpHeaderNames.HOST;
import static lombok.AccessLevel.PRIVATE;
Expand Down Expand Up @@ -112,7 +110,7 @@ public org.zalando.logbook.HttpRequest withoutBody() {
return this;
}

void buffer(final HttpContent content) {
void buffer(final ByteBuf content) {
state.updateAndGet(state -> state.buffer(request, content));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package org.zalando.logbook.netty;

import io.netty.handler.codec.http.HttpContent;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.HttpResponse;
import lombok.AllArgsConstructor;
import org.zalando.logbook.HttpHeaders;
import org.zalando.logbook.Origin;

import javax.annotation.Nullable;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicReference;

import javax.annotation.Nullable;
import lombok.AllArgsConstructor;
import org.zalando.logbook.HttpHeaders;
import org.zalando.logbook.Origin;
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;

@AllArgsConstructor
Expand Down Expand Up @@ -67,7 +65,7 @@ public org.zalando.logbook.HttpResponse withoutBody() {
return this;
}

void buffer(final HttpContent content) {
void buffer(final ByteBuf content) {
state.updateAndGet(state -> state.buffer(response, content));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.zalando.logbook.netty;

import io.netty.handler.codec.http.HttpContent;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.HttpMessage;

interface State {
Expand All @@ -14,7 +14,7 @@ default State without() {
}

default State buffer(
final HttpMessage message, final HttpContent content) {
final HttpMessage message, final ByteBuf content) {
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package org.zalando.logbook.netty;

import io.netty.handler.codec.http.DefaultHttpContent;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.HttpRequest;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.concurrent.atomic.AtomicReference;

import static io.netty.buffer.Unpooled.wrappedBuffer;
import static io.netty.handler.codec.http.HttpMethod.GET;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
Expand All @@ -34,8 +32,8 @@ void shouldKeepBufferingWhenIgnoring() {
Assertions.assertEquals("foobar", body);
}

private DefaultHttpContent content(final String s) {
return new DefaultHttpContent(wrappedBuffer(s.getBytes(UTF_8)));
private ByteBuf content(final String s) {
return wrappedBuffer(s.getBytes(UTF_8));
}

}