2
2
3
3
import static io .netty .handler .codec .http .HttpHeaders .isKeepAlive ;
4
4
import static io .netty .handler .codec .http .HttpHeaders .setContentLength ;
5
- import static io .netty .handler .codec .http .HttpMethod .GET ;
6
5
import static io .netty .handler .codec .http .HttpResponseStatus .BAD_REQUEST ;
7
- import static io .netty .handler .codec .http .HttpResponseStatus .FORBIDDEN ;
8
- import static io .netty .handler .codec .http .HttpResponseStatus .NOT_FOUND ;
9
6
import static io .netty .handler .codec .http .HttpVersion .HTTP_1_1 ;
10
- import io .mqtt .handler .http .HttpTransport ;
11
- import io .mqtt .handler .http .HttpJsonpTransport ;
12
7
import io .mqtt .handler .http .HttpSessionStore ;
8
+ import io .mqtt .handler .http .HttpTransport ;
13
9
import io .netty .buffer .ByteBuf ;
14
10
import io .netty .buffer .Unpooled ;
15
11
import io .netty .channel .ChannelFuture ;
16
12
import io .netty .channel .ChannelFutureListener ;
13
+ import io .netty .channel .ChannelHandler .Sharable ;
17
14
import io .netty .channel .ChannelHandlerContext ;
18
15
import io .netty .channel .SimpleChannelInboundHandler ;
19
- import io .netty .channel .ChannelHandler .Sharable ;
20
16
import io .netty .handler .codec .http .DefaultFullHttpResponse ;
21
17
import io .netty .handler .codec .http .FullHttpRequest ;
22
18
import io .netty .handler .codec .http .FullHttpResponse ;
23
19
import io .netty .handler .codec .http .HttpRequest ;
24
20
import io .netty .handler .timeout .ReadTimeoutException ;
25
- import io .netty .util .AttributeKey ;
26
21
import io .netty .util .CharsetUtil ;
27
22
import io .netty .util .internal .logging .InternalLogger ;
28
23
import io .netty .util .internal .logging .InternalLoggerFactory ;
@@ -40,10 +35,6 @@ public class HttpRequestHandler extends
40
35
41
36
private static Map <String , HttpTransport > transportMap = new HashMap <String , HttpTransport >(
42
37
1 );
43
- static {
44
- HttpJsonpTransport httpJsonpTransport = new HttpJsonpTransport ();
45
- transportMap .put (HttpJsonpTransport .PREFIX , httpJsonpTransport );
46
- }
47
38
48
39
public HttpRequestHandler (String websocketUri ) {
49
40
this .websocketUri = websocketUri ;
@@ -53,30 +44,18 @@ public HttpRequestHandler(String websocketUri) {
53
44
protected void channelRead0 (ChannelHandlerContext ctx , FullHttpRequest req )
54
45
throws Exception {
55
46
if (!req .getDecoderResult ().isSuccess ()) {
47
+ logger .debug ("invalid http request" );
56
48
sendHttpResponse (ctx , req , new DefaultFullHttpResponse (HTTP_1_1 ,
57
49
BAD_REQUEST ));
58
50
return ;
59
51
}
60
52
61
53
if (req .getUri ().equalsIgnoreCase (this .websocketUri )) {
54
+ logger .debug ("it is websocket request" );
62
55
ctx .fireChannelRead (req .retain ());
63
56
return ;
64
57
}
65
58
66
- // Allow only GET methods.
67
- if (req .getMethod () != GET ) {
68
- sendHttpResponse (ctx , req , new DefaultFullHttpResponse (HTTP_1_1 ,
69
- FORBIDDEN ));
70
- return ;
71
- }
72
-
73
- if ("/favicon.ico" .equals (req .getUri ())) {
74
- FullHttpResponse res = new DefaultFullHttpResponse (HTTP_1_1 ,
75
- NOT_FOUND );
76
- sendHttpResponse (ctx , req , res );
77
- return ;
78
- }
79
-
80
59
HttpTransport transport = getTransport (req );
81
60
if (transport == null ) {
82
61
sendHttpResponse (ctx , req , new DefaultFullHttpResponse (HTTP_1_1 ,
@@ -116,7 +95,6 @@ private HttpTransport getTransport(HttpRequest req) {
116
95
}
117
96
118
97
return null ;
119
-
120
98
}
121
99
122
100
private static void sendHttpResponse (ChannelHandlerContext ctx ,
@@ -136,4 +114,11 @@ private static void sendHttpResponse(ChannelHandlerContext ctx,
136
114
f .addListener (ChannelFutureListener .CLOSE );
137
115
}
138
116
}
117
+
118
+ public static void registerTransport (HttpTransport httpTransport ) {
119
+ if (httpTransport == null )
120
+ return ;
121
+
122
+ transportMap .put (httpTransport .getPrefixUri (), httpTransport );
123
+ }
139
124
}
0 commit comments