Skip to content

Commit

Permalink
Simplify http proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
ianopolous committed Jul 10, 2023
1 parent 25ef969 commit 4a0f5e0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
22 changes: 9 additions & 13 deletions src/main/java/org/peergos/protocol/http/HttpProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public void onMessage(@NotNull Stream stream, FullHttpResponse msg) {
CompletableFuture<FullHttpResponse> req = queue.poll();
if (req != null)
req.complete(msg.copy());
else
msg.release();
msg.release();
}

public CompletableFuture<FullHttpResponse> send(FullHttpRequest req) {
Expand All @@ -58,21 +57,21 @@ public CompletableFuture<FullHttpResponse> send(FullHttpRequest req) {
}
}

public static class ResponseWriter extends SimpleChannelInboundHandler<HttpObject> {
private final Consumer<HttpObject> replyProcessor;
public static class ResponseWriter extends SimpleChannelInboundHandler<HttpContent> {
private final Consumer<HttpContent> replyProcessor;

public ResponseWriter(Consumer<HttpObject> replyProcessor) {
public ResponseWriter(Consumer<HttpContent> replyProcessor) {
this.replyProcessor = replyProcessor;
}

@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject reply) {
protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpContent reply) {
replyProcessor.accept(reply);
}
}

public interface HttpRequestProcessor {
void handle(Stream stream, HttpRequest msg, Consumer<HttpObject> replyHandler);
void handle(Stream stream, HttpRequest msg, Consumer<HttpContent> replyHandler);
}

public static class Receiver implements ProtocolMessageHandler<HttpRequest>, HttpController {
Expand All @@ -82,11 +81,8 @@ public Receiver(HttpRequestProcessor requestHandler) {
this.requestHandler = requestHandler;
}

private void sendReply(HttpObject reply, Stream p2pstream) {
if (reply instanceof HttpContent)
p2pstream.writeAndFlush(((HttpContent) reply).retain());
else
p2pstream.writeAndFlush(reply);
private void sendReply(HttpContent reply, Stream p2pstream) {
p2pstream.writeAndFlush(reply.copy());
}

@Override
Expand All @@ -102,7 +98,7 @@ public CompletableFuture<FullHttpResponse> send(FullHttpRequest req) {
private static final NioEventLoopGroup pool = new NioEventLoopGroup();
public static void proxyRequest(HttpRequest msg,
SocketAddress proxyTarget,
Consumer<HttpObject> replyHandler) {
Consumer<HttpContent> replyHandler) {
Bootstrap b = new Bootstrap()
.group(pool)
.channel(NioSocketChannel.class)
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/peergos/HttpProxyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void p2pProxyRequest() throws IOException {
node2.start().join();

// start local server with fixed HTTP response
byte[] httpReply = new byte[1024*1024];
byte[] httpReply = new byte[577*1024];
new Random(42).nextBytes(httpReply);
HttpServer localhostServer = HttpServer.create(proxyTarget, 20);
localhostServer.createContext("/", ex -> {
Expand Down

0 comments on commit 4a0f5e0

Please sign in to comment.