Skip to content

Commit

Permalink
Merge pull request #50 from Peergos/simplify/http-proxy
Browse files Browse the repository at this point in the history
Further simplify http proxy
  • Loading branch information
ianopolous authored Jul 10, 2023
2 parents ba44e72 + 0adcd92 commit 2fe8f1d
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/main/java/org/peergos/protocol/http/HttpProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpCon
}

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

public static class Receiver implements ProtocolMessageHandler<HttpRequest>, HttpController {
public static class Receiver implements ProtocolMessageHandler<FullHttpRequest>, HttpController {
private final HttpRequestProcessor requestHandler;

public Receiver(HttpRequestProcessor requestHandler) {
Expand All @@ -86,7 +86,7 @@ private void sendReply(HttpContent reply, Stream p2pstream) {
}

@Override
public void onMessage(@NotNull Stream stream, HttpRequest msg) {
public void onMessage(@NotNull Stream stream, FullHttpRequest msg) {
requestHandler.handle(stream, msg, reply -> sendReply(reply, stream));
}

Expand All @@ -96,7 +96,7 @@ public CompletableFuture<FullHttpResponse> send(FullHttpRequest req) {
}

private static final NioEventLoopGroup pool = new NioEventLoopGroup();
public static void proxyRequest(HttpRequest msg,
public static void proxyRequest(FullHttpRequest msg,
SocketAddress proxyTarget,
Consumer<HttpContent> replyHandler) {
Bootstrap b = new Bootstrap()
Expand All @@ -115,16 +115,10 @@ protected void initChannel(Channel ch) throws Exception {
ChannelFuture fut = b.connect(proxyTarget);
Channel ch = fut.channel();

HttpRequest retained = retain(msg);
FullHttpRequest retained = msg.retain();
fut.addListener(x -> ch.writeAndFlush(retained));
}

private static HttpRequest retain(HttpRequest req) {
if (req instanceof FullHttpRequest)
return ((FullHttpRequest) req).retain();
return req;
}

private static final long TRAFFIC_LIMIT = Long.MAX_VALUE; // This is the total inbound or outbound traffic allowed, not a rate
private final HttpRequestProcessor handler;

Expand All @@ -133,7 +127,7 @@ public HttpProtocol(HttpRequestProcessor handler) {
this.handler = (s, req, replyHandler) -> handler.handle(s, setHost(req, s), replyHandler);
}

public static HttpRequest setHost(HttpRequest req, Stream source) {
public static FullHttpRequest setHost(FullHttpRequest req, Stream source) {
req.headers().set(HttpHeaderNames.HOST,source.remotePeerId().toBase58());
return req;
}
Expand Down

0 comments on commit 2fe8f1d

Please sign in to comment.