Skip to content

Commit 2e30e4b

Browse files
committed
test: 自定义Pipe,使日志支持HttpServerRequest和HttpClientResponse的请求体输出
1 parent 2c21601 commit 2e30e4b

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

src/test/java/top/meethigher/proxy/tcp/Pipe.java renamed to src/test/java/top/meethigher/proxy/Pipe.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
/*
2-
* Copyright (c) 2011-2019 Contributors to the Eclipse Foundation
3-
*
4-
* This program and the accompanying materials are made available under the
5-
* terms of the Eclipse Public License 2.0 which is available at
6-
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7-
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
8-
*
9-
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10-
*/
11-
package top.meethigher.proxy.tcp;
1+
package top.meethigher.proxy;
122

133
import io.vertx.core.*;
4+
import io.vertx.core.http.HttpClientResponse;
5+
import io.vertx.core.http.HttpServerRequest;
146
import io.vertx.core.net.NetSocket;
157
import io.vertx.core.streams.ReadStream;
168
import io.vertx.core.streams.WriteStream;
179
import org.slf4j.Logger;
1810
import org.slf4j.LoggerFactory;
1911

12+
/**
13+
* 在io.vertx.core.streams.impl.PipeImpl
14+
*/
2015
public class Pipe<T> implements io.vertx.core.streams.Pipe<T> {
2116

2217
private static final Logger log = LoggerFactory.getLogger(Pipe.class);
@@ -29,11 +24,18 @@ public class Pipe<T> implements io.vertx.core.streams.Pipe<T> {
2924
public Pipe(ReadStream<T> src) {
3025
this.src = src;
3126
this.result = Promise.promise();
32-
3327
if (src instanceof NetSocket) {
3428
NetSocket tsrc = (NetSocket) src;
3529
tsrc.remoteAddress();
3630
tsrc.localAddress();
31+
} else if (src instanceof HttpServerRequest) {
32+
HttpServerRequest req = (HttpServerRequest) src;
33+
req.connection().remoteAddress();
34+
req.connection().localAddress();
35+
} else if (src instanceof HttpClientResponse) {
36+
HttpClientResponse resp = (HttpClientResponse) src;
37+
resp.request().connection().remoteAddress();
38+
resp.request().connection().localAddress();
3739
}
3840

3941
// Set handlers now
@@ -85,7 +87,19 @@ public void to(WriteStream<T> ws, Handler<AsyncResult<Void>> completionHandler)
8587
src.handler(item -> {
8688
if (src instanceof NetSocket) {
8789
NetSocket tsrc = (NetSocket) src;
88-
log.info("{} -- {} received:\n{}", tsrc.remoteAddress(), tsrc.localAddress(),
90+
log.trace("{} -- {} received:\n{}", tsrc.remoteAddress(), tsrc.localAddress(),
91+
item.toString());
92+
} else if (src instanceof HttpServerRequest) {
93+
HttpServerRequest req = (HttpServerRequest) src;
94+
log.trace("{} -- {} received:\n{}",
95+
req.connection().remoteAddress(),
96+
req.connection().localAddress(),
97+
item.toString());
98+
} else if (src instanceof HttpClientResponse) {
99+
HttpClientResponse resp = (HttpClientResponse) src;
100+
log.trace("{} -- {} received:\n{}",
101+
resp.request().connection().remoteAddress(),
102+
resp.request().connection().localAddress(),
89103
item.toString());
90104
}
91105
ws.write(item, this::handleWriteResult);

0 commit comments

Comments
 (0)