Skip to content

Commit 9124632

Browse files
committed
version 0.0.0.9 some thing update
1. add cookies support 2. add filter status (next/block)
1 parent 008c7f8 commit 9124632

File tree

10 files changed

+184
-36
lines changed

10 files changed

+184
-36
lines changed

src/top/gunplan/netty/GunCoreWorker.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import top.gunplan.netty.protocol.GunNetResponseInterface;
55
import top.gunplan.nio.utils.GunBytesUtil;
6+
67
import java.io.IOException;
78
import java.lang.reflect.InvocationTargetException;
89
import java.nio.ByteBuffer;
@@ -11,7 +12,6 @@
1112

1213

1314
/**
14-
*
1515
* @author dosdrtt
1616
*/
1717

@@ -25,6 +25,7 @@ public GunCoreWorker(final List<GunNettyFilter> filters, final GunBootServer.Gun
2525

2626

2727
}
28+
2829
@Override
2930
public synchronized void run() {
3031
byte[] readbata = null;
@@ -37,18 +38,25 @@ public synchronized void run() {
3738
} catch (Exception e) {
3839
this.handel.dealExceptionEvent(e);
3940
}
40-
if (readbata != null)
41-
{
41+
if (readbata != null) {
4242
final GunRequestFilterDto gunFilterObj = new GunRequestFilterDto(readbata);
43-
this.filters.forEach(netty -> netty.doRequestFilter(gunFilterObj));
43+
for (GunNettyFilter filter : this.filters) {
44+
if (!filter.doRequestFilter(gunFilterObj)) {
45+
break;
46+
}
47+
}
4448
GunNetResponseInterface respObject = null;
4549
try {
4650
respObject = this.handel.dealDataEvent(gunFilterObj.getObject());
4751
} catch (IOException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
4852
this.handel.dealExceptionEvent(e);
4953
}
5054
GunResponseFilterDto responseFilterDto = new GunResponseFilterDto(respObject);
51-
this.filters.forEach(netty -> netty.doResponseFilter(responseFilterDto));
55+
for (GunNettyFilter filter : this.filters) {
56+
if (!filter.doResponseFilter(responseFilterDto)) {
57+
break;
58+
}
59+
}
5260

5361
if (responseFilterDto.getRespobj().isReturn()) {
5462
try {

src/top/gunplan/netty/GunNettyFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public interface GunNettyFilter extends GunHandle {
1111
*
1212
* @param filterDto input to the filter's deal Object
1313
*/
14-
void doRequestFilter(GunRequestFilterDto filterDto);
14+
boolean doRequestFilter(GunRequestFilterDto filterDto);
1515

1616
/**
1717
* @param filterDto input to the filter's deal Object
1818
*/
19-
void doResponseFilter(GunResponseFilterDto filterDto);
19+
boolean doResponseFilter(GunResponseFilterDto filterDto);
2020
}

src/top/gunplan/netty/filters/GunStdHttp2Filter.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@
88
import top.gunplan.netty.protocol.GunNetRequestInterface;
99
import top.gunplan.netty.protocol.GunNetResponseInterface;
1010

11+
/**
12+
* @author dosdrtt
13+
*/
1114
@GunNetFilterOrder
1215
public class GunStdHttp2Filter implements GunNettyFilter {
1316
@Override
14-
public void doRequestFilter(GunRequestFilterDto filterDto) {
17+
public boolean doRequestFilter(GunRequestFilterDto filterDto) {
1518
GunNetRequestInterface protocl = new GunHttp2RequestProtocl();
1619
protocl.unSerialize(filterDto.getSrc());
1720
filterDto.setObject(protocl);
21+
return true;
1822
}
1923

2024
@Override
21-
public void doResponseFilter(GunResponseFilterDto filterDto) {
22-
25+
public boolean doResponseFilter(GunResponseFilterDto filterDto) {
26+
return true;
2327
}
2428

2529
}

src/top/gunplan/netty/handles/GunStdHttpHandle.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import top.gunplan.netty.protocol.GunHttp2RequestProtocl;
1010
import top.gunplan.netty.handles.http.GunHttpMappingHandle;
1111
import top.gunplan.netty.protocol.GunHttp2ResponseInterface;
12-
import top.gunplan.nio.utils.DirectoryUtils;
12+
import top.gunplan.nio.utils.GunBaseLogUtil;
13+
import top.gunplan.nio.utils.GunDirectoryUtil;
14+
import top.gunplan.nio.utils.GunStringUtil;
1315

1416
import java.io.File;
1517
import java.io.IOException;
@@ -29,7 +31,7 @@ public GunStdHttpHandle(final String handlePackName) {
2931
ClassLoader loader = this.getClass().getClassLoader();
3032
List<File> classfiles;
3133
try {
32-
classfiles = DirectoryUtils.scanAllFilesFromDirectory(loader.getResource("").getPath().replace("%20", " ") + handlePackName.replace(".", "/"));
34+
classfiles = GunDirectoryUtil.scanAllFilesFromDirectory(loader.getResource("").getPath().replace("%20", " ") + handlePackName.replace(".", "/"));
3335
} catch (IOException e) {
3436
throw new GunException(e);
3537
}
@@ -63,24 +65,25 @@ public GunNetResponseInterface dealDataEvent(GunNetRequestInterface requestInter
6365

6466
localUrlMapping.set(urlMapping);
6567
GunHttp2RequestProtocl request = ((GunHttp2RequestProtocl) requestInterface);
66-
return findHandelandRun(request.getRequestUrl()).doResponse(request);
68+
GunHttpMappingHandle<GunHttp2ResponseInterface> runner = null;
69+
try {
70+
runner = findHandelandRun(request.getRequestUrl());
71+
} catch (Exception exp) {
72+
GunBaseLogUtil.error(exp.getMessage());
73+
}
74+
return runner.doResponse(request);
6775
// localUrlMapping.get().
6876
}
6977

70-
private GunHttpMappingHandle<GunHttp2ResponseInterface> findHandelandRun(String requestUrl) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
71-
72-
{
78+
private GunHttpMappingHandle<GunHttp2ResponseInterface> findHandelandRun(String requestUrl) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
7379
HashMap<String, Class<? extends GunHttpMappingHandle<GunHttp2ResponseInterface>>> dealmap = localUrlMapping.get();
7480
Class<? extends GunHttpMappingHandle<GunHttp2ResponseInterface>> dealhandel = dealmap.get(requestUrl);
75-
int fg = 0;
76-
if (dealhandel == null) {
77-
for (int i = requestUrl.length() - 1; i >= 0; i--) {
78-
if (requestUrl.charAt(i) == '/') {
79-
fg = i;
80-
break;
81-
}
81+
while (dealhandel == null) {
82+
requestUrl = GunStringUtil.removeLastUrl(requestUrl);
83+
dealhandel = dealmap.get(requestUrl + "*");
84+
if ("/".equals(requestUrl) && dealhandel == null) {
85+
throw new GunException("404 or 404 pages not found");
8286
}
83-
dealhandel = dealmap.get(requestUrl.substring(0, fg+1) + "*");
8487
}
8588
GunHttpMappingHandle<GunHttp2ResponseInterface> instance = dealhandel.getConstructor().newInstance();
8689
return instance;

src/top/gunplan/netty/impl/GunBootServerImpl.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.sun.istack.internal.NotNull;
44
import top.gunplan.netty.*;
55
import top.gunplan.netty.anno.GunNetFilterOrder;
6+
import top.gunplan.nio.utils.GunBaseLogUtil;
67

78
import java.io.IOException;
89
import java.lang.annotation.Annotation;
@@ -118,10 +119,14 @@ public synchronized void sync() throws IOException {
118119

119120
while (bootSelector.select() > 0) {
120121
Iterator keyIterator = bootSelector.selectedKeys().iterator();
121-
while (keyIterator.hasNext()) {
122-
SelectionKey sk = (SelectionKey) keyIterator.next();
123-
this.toDealConnection(sk);
124-
keyIterator.remove();
122+
try {
123+
while (keyIterator.hasNext()) {
124+
SelectionKey sk = (SelectionKey) keyIterator.next();
125+
this.toDealConnection(sk);
126+
keyIterator.remove();
127+
}
128+
} catch (Exception exp) {
129+
GunBaseLogUtil.error(exp.getLocalizedMessage());
125130
}
126131
}
127132
}

src/top/gunplan/netty/impl/example/GunOutputHander.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,33 @@
55
import top.gunplan.netty.protocol.GunHttp2RequestProtocl;
66
import top.gunplan.netty.protocol.GunNetRequestInterface;
77
import top.gunplan.netty.protocol.GunNetResponseInterface;
8-
import top.gunplan.nio.utils.BaseGunLog;
9-
10-
import java.io.IOException;
8+
import top.gunplan.nio.utils.GunBaseLogUtil;
119

1210
public class GunOutputHander implements GunBootServer.GunNetHandle {
1311
{
14-
BaseGunLog.setLevel(0);
12+
GunBaseLogUtil.setLevel(0);
1513
}
1614

1715
@Override
1816
public GunNetResponseInterface dealDataEvent(GunNetRequestInterface m) {
1917
if (m instanceof GunHttp2RequestProtocl) {
2018
GunHttp2RequestProtocl httpProtocl = ((GunHttp2RequestProtocl) m);
2119
httpProtocl.getRequstHead().forEach((s, s2) ->
22-
BaseGunLog.info(s + " " + "->" + " " + s2)
20+
GunBaseLogUtil.info(s + " " + "->" + " " + s2)
2321
);
2422
}
2523
return null;
2624
}
2725

2826
@Override
2927
public GunNetResponseInterface dealConnEvent(GunNetRequestInterface m) throws GunException {
30-
BaseGunLog.error("CONNECTED ");
28+
GunBaseLogUtil.error("CONNECTED ");
3129
return null;
3230
}
3331

3432
@Override
3533
public void dealCloseEvent() {
36-
BaseGunLog.error("CLOSED ");
34+
GunBaseLogUtil.error("CLOSED ");
3735
}
3836

3937
@Override

src/top/gunplan/netty/protocol/BaseGunHttp2Response.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package top.gunplan.netty.protocol;
22

3+
import java.util.*;
4+
35
/**
46
* @author dosdrtt
57
*/
@@ -8,8 +10,27 @@ public abstract class BaseGunHttp2Response implements GunHttp2ResponseInterface
810
private GunHttpStdInfo.statusCode code = GunHttpStdInfo.statusCode.OK;
911
private GunHttpStdInfo.ContentType contentType = GunHttpStdInfo.ContentType.TEXT_JSON;
1012

13+
14+
public BaseGunHttp2Response() {
15+
this.mmap.put("Server", "windos iis server 2003");
16+
this.mmap.put("date", new Date().toString());
17+
this.mmap.put("Connection", "keep-alive");
18+
this.cookies.add(new GunHttpStdInfo.GunCookies("iisSession", UUID.randomUUID().toString()));
19+
20+
}
21+
22+
private Map<String, String> mmap = new HashMap<>(4);
23+
private List<GunHttpStdInfo.GunCookies> cookies = new ArrayList<>(1);
24+
1125
private boolean iswrite;
1226

27+
public Map<String, String> getMmap() {
28+
return mmap;
29+
}
30+
31+
public void setMmap(Map<String, String> mmap) {
32+
this.mmap = mmap;
33+
}
1334

1435
public GunHttpStdInfo.HttpProtoclType getProtoclType() {
1536
return protoclType;
@@ -48,15 +69,31 @@ public void setIswrite(boolean iswrite) {
4869
this.iswrite = iswrite;
4970
}
5071

72+
public List<GunHttpStdInfo.GunCookies> getCookies() {
73+
return cookies;
74+
}
75+
76+
public void addCookie(GunHttpStdInfo.GunCookies cookies) {
77+
this.cookies.add(cookies);
78+
}
79+
5180
@Override
5281
public byte[] serialize() {
82+
Map<String, String> httpHead = this.mmap;
5383
StringBuilder http2resp = new StringBuilder();
5484
http2resp.append(protoclType.getVal());
5585
http2resp.append(" ");
5686
http2resp.append(code.getVal());
5787
http2resp.append(" ");
5888
http2resp.append(code).append("\r\n");
5989
http2resp.append("Content-Type:").append(contentType.getVal()).append("\r\n");
90+
for (String key : httpHead.keySet()) {
91+
http2resp.append(key).append(":").append(httpHead.get(key)).append("\r\n");
92+
}
93+
for (GunHttpStdInfo.GunCookies cookie : cookies) {
94+
http2resp.append("Set-Cookie").append(":").append(cookie.toString()).append("\r\n");
95+
}
96+
6097
http2resp.append("Content-Length:").append(this.toResponse().length()).append("\r\n\r\n");
6198
http2resp.append(this.toResponse());
6299

src/top/gunplan/netty/protocol/GunHttp2ResponseInterface.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22

33
public interface GunHttp2ResponseInterface extends GunNetResponseInterface{
44
String toResponse();
5+
6+
7+
58
}

src/top/gunplan/netty/protocol/GunHttpStdInfo.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,62 @@
11
package top.gunplan.netty.protocol;
22

33
public class GunHttpStdInfo {
4-
enum GunHttpRequestType {
4+
public static class GunCookies {
5+
private String key;
6+
private String value;
7+
private String path;
8+
private String expries;
9+
10+
public GunCookies(String key, String value) {
11+
this(key, value, "/", null);
12+
}
13+
14+
public GunCookies(String key, String value, String path, String expries) {
15+
this.key = key;
16+
this.value = value;
17+
this.path = path;
18+
this.expries = expries;
19+
}
20+
21+
public String getKey() {
22+
return key;
23+
}
24+
25+
public void setKey(String key) {
26+
this.key = key;
27+
}
28+
29+
public String getValue() {
30+
return value;
31+
}
32+
33+
public void setValue(String value) {
34+
this.value = value;
35+
}
36+
37+
public String getPath() {
38+
return path;
39+
}
40+
41+
public void setPath(String path) {
42+
this.path = path;
43+
}
44+
45+
public String getExpries() {
46+
return expries;
47+
}
48+
49+
public void setExpries(String expries) {
50+
this.expries = expries;
51+
}
52+
53+
@Override
54+
public String toString() {
55+
return this.key + "=" + this.value + ";" + "path=" + path + ";expries=" + expries;
56+
}
57+
}
58+
59+
public enum GunHttpRequestType {
560
/**
661
*
762
*
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package top.gunplan.netty.test;
2+
3+
4+
import top.gunplan.netty.anno.GunHttpmapping;
5+
import top.gunplan.netty.handles.http.GunHttpMappingHandle;
6+
import top.gunplan.netty.protocol.BaseGunHttp2Response;
7+
import top.gunplan.netty.protocol.GunHttp2ResponseInterface;
8+
import top.gunplan.netty.protocol.GunHttpStdInfo;
9+
import top.gunplan.netty.protocol.GunNetRequestInterface;
10+
11+
@GunHttpmapping(mappingRule = "/*")
12+
public class _404_Not_Found implements GunHttpMappingHandle<GunHttp2ResponseInterface> {
13+
14+
@Override
15+
public GunHttp2ResponseInterface doResponse(GunNetRequestInterface protocl) {
16+
BaseGunHttp2Response response = new BaseGunHttp2Response() {
17+
@Override
18+
public String toResponse() {
19+
return "<!DOCTYPE html>" +
20+
"<html>" +
21+
"<head>" +
22+
"</head>" +
23+
"<body>" +
24+
"<h1>404 n0t found</h1>" +
25+
"" +
26+
"</body>" +
27+
"</html>";
28+
}
29+
};
30+
response.setIswrite(true);
31+
response.setProtoclType(GunHttpStdInfo.HttpProtoclType.HTTP2_0);
32+
response.setContentType(GunHttpStdInfo.ContentType.TEXT_HTML);
33+
return response;
34+
}
35+
}

0 commit comments

Comments
 (0)