Skip to content

Commit 33f94b3

Browse files
author
hiram
committed
添加filter
1 parent 6c1d085 commit 33f94b3

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

02nio/nio02/src/main/java/io/github/kimmking/gateway/NettyServerApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class NettyServerApplication {
1010

1111
public static void main(String[] args) {
1212
String proxyServer = System.getProperty("proxyServer","http://localhost:8088");
13-
// String proxyServer = System.getProperty("proxyServer","http://www.baidu.com:80");
13+
//String proxyServer = System.getProperty("proxyServer","http://www.baidu.com");
1414
String proxyPort = System.getProperty("proxyPort","8888");
1515

1616
// http://localhost:8888/api/hello ==> gateway API
@@ -19,7 +19,7 @@ public static void main(String[] args) {
1919
int port = Integer.parseInt(proxyPort);
2020
System.out.println(GATEWAY_NAME + " " + GATEWAY_VERSION +" starting...");
2121
HttpInboundServer server = new HttpInboundServer(port, proxyServer);
22-
System.out.println(GATEWAY_NAME + " " + GATEWAY_VERSION +" started at http://localhost:" + port + " for server:" + proxyServer);
22+
System.out.println(GATEWAY_NAME + " " + GATEWAY_VERSION +" started at http://localhost:" + port + "/api/hello for server:" + proxyServer + "/api/hello");
2323
try {
2424
server.run();
2525
} catch (Exception ex){
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.github.kimmking.gateway.filter;
2+
3+
import io.netty.buffer.Unpooled;
4+
import io.netty.channel.ChannelFutureListener;
5+
import io.netty.channel.ChannelHandlerContext;
6+
import io.netty.handler.codec.http.*;
7+
8+
import java.io.UnsupportedEncodingException;
9+
10+
import static io.netty.handler.codec.http.HttpResponseStatus.*;
11+
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
12+
13+
14+
/**
15+
* 过滤http请求,如果是Post就返回错误信息,get请求就通过
16+
*/
17+
public class HttpMethodRequestFilter implements HttpRequestFilter {
18+
19+
@Override
20+
public void filter(final FullHttpRequest fullRequest, final ChannelHandlerContext ctx) {
21+
HttpMethod method = fullRequest.method();
22+
if (method.equals(HttpMethod.POST)) {
23+
FullHttpResponse response = null;
24+
try {
25+
response = new DefaultFullHttpResponse(HTTP_1_1, METHOD_NOT_ALLOWED);
26+
} catch (Exception e) {
27+
e.printStackTrace();
28+
response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT);
29+
} finally {
30+
if (fullRequest != null) {
31+
if (!HttpUtil.isKeepAlive(fullRequest)) {
32+
ctx.write(response).addListener(ChannelFutureListener.CLOSE);
33+
} else {
34+
//response.headers().set(CONNECTION, KEEP_ALIVE);
35+
ctx.write(response);
36+
}
37+
}
38+
ctx.flush();
39+
ctx.close();
40+
}
41+
}
42+
}
43+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.github.kimmking.gateway.filter;
2+
3+
import io.netty.channel.ChannelHandlerContext;
4+
import io.netty.handler.codec.http.DefaultFullHttpResponse;
5+
import io.netty.handler.codec.http.FullHttpRequest;
6+
7+
import static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;
8+
import static io.netty.handler.codec.http.HttpResponseStatus.NO_CONTENT;
9+
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
10+
11+
public class UriHttpRequestFilter implements HttpRequestFilter{
12+
@Override
13+
public void filter(final FullHttpRequest fullRequest, ChannelHandlerContext ctx) {
14+
String uri = fullRequest.uri();
15+
System.out.println("uri: " + uri);
16+
if (!uri.contains("api/hello")) {
17+
DefaultFullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND);
18+
ctx.write(response);
19+
ctx.flush();
20+
ctx.close();
21+
}
22+
}
23+
}

02nio/nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundHandler.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.kimmking.gateway.inbound;
22

33
import io.github.kimmking.gateway.filter.HttpMethodRequestFilter;
4+
import io.github.kimmking.gateway.filter.UriHttpRequestFilter;
45
import io.github.kimmking.gateway.outbound.httpclient4.HttpOutboundHandler;
56
import io.github.kimmking.gateway.outbound.netty4.NettyHttpClient;
67
import io.github.kimmking.gateway.outbound.netty4.NettyHttpOutboundHandler;
@@ -42,8 +43,10 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
4243
// }
4344

4445
//handler.handle(fullRequest, ctx);
45-
HttpMethodRequestFilter myFirstHttpRequestFilter = new HttpMethodRequestFilter();
46-
myFirstHttpRequestFilter.filter(fullRequest, ctx);
46+
HttpMethodRequestFilter methodHttpRequestFilter = new HttpMethodRequestFilter();
47+
methodHttpRequestFilter.filter(fullRequest, ctx);
48+
// UriHttpRequestFilter uriHttpRequestFilter = new UriHttpRequestFilter();
49+
// uriHttpRequestFilter.filter(fullRequest, ctx);
4750
handler.connect(fullRequest, ctx);
4851
} catch(Exception e) {
4952
e.printStackTrace();

02nio/nio02/src/main/java/io/github/kimmking/gateway/outbound/netty4/NettyHttpClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public void initChannel(SocketChannel ch) throws Exception {
4848
request.content().readableBytes());*/
4949
// Start the client.
5050
String host = backendUrl.replaceAll("/", "").split(":")[1];
51-
int port = Integer.parseInt(backendUrl.replaceAll("/", "").split(":")[2]);
51+
String[] split = backendUrl.replaceAll("/", "").split(":");
52+
int port = split.length < 3 ? 80 : Integer.parseInt(split[2]);
5253
ChannelFuture f = b.connect(host, port).sync();
5354
/*f.channel().write(request);
5455
f.channel().flush();*/

0 commit comments

Comments
 (0)