1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.http.websocketx;
17
18 import org.jboss.netty.channel.Channel;
19 import org.jboss.netty.channel.ChannelFuture;
20 import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
21 import org.jboss.netty.handler.codec.http.HttpHeaders.Names;
22 import org.jboss.netty.handler.codec.http.HttpRequest;
23 import org.jboss.netty.handler.codec.http.HttpResponse;
24 import org.jboss.netty.handler.codec.http.HttpResponseStatus;
25 import org.jboss.netty.handler.codec.http.HttpVersion;
26
27
28
29
30 public class WebSocketServerHandshakerFactory {
31
32 private final String webSocketURL;
33
34 private final String subprotocols;
35
36 private final boolean allowExtensions;
37
38 private final long maxFramePayloadLength;
39
40
41
42
43
44
45
46
47
48 public WebSocketServerHandshakerFactory(String webSocketURL, String subprotocols, boolean allowExtensions) {
49 this(webSocketURL, subprotocols, allowExtensions, Long.MAX_VALUE);
50 }
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public WebSocketServerHandshakerFactory(String webSocketURL, String subprotocols, boolean allowExtensions,
67 long maxFramePayloadLength) {
68 this.webSocketURL = webSocketURL;
69 this.subprotocols = subprotocols;
70 this.allowExtensions = allowExtensions;
71 this.maxFramePayloadLength = maxFramePayloadLength;
72 }
73
74
75
76
77
78
79
80 public WebSocketServerHandshaker newHandshaker(HttpRequest req) {
81
82 String version = req.headers().get(Names.SEC_WEBSOCKET_VERSION);
83
84 if (version != null) {
85 if (version.equals(WebSocketVersion.V13.toHttpHeaderValue())) {
86
87 return new WebSocketServerHandshaker13(
88 webSocketURL, subprotocols, allowExtensions, maxFramePayloadLength);
89 } else if (version.equals(WebSocketVersion.V08.toHttpHeaderValue())) {
90
91 return new WebSocketServerHandshaker08(
92 webSocketURL, subprotocols, allowExtensions, maxFramePayloadLength);
93 } else if (version.equals(WebSocketVersion.V07.toHttpHeaderValue())) {
94
95 return new WebSocketServerHandshaker07(
96 webSocketURL, subprotocols, allowExtensions, maxFramePayloadLength);
97 } else {
98 return null;
99 }
100 } else {
101
102 return new WebSocketServerHandshaker00(
103 webSocketURL, subprotocols, maxFramePayloadLength);
104 }
105 }
106
107
108
109
110
111
112
113 public ChannelFuture sendUnsupportedWebSocketVersionResponse(Channel channel) {
114 HttpResponse res = new DefaultHttpResponse(
115 HttpVersion.HTTP_1_1,
116 HttpResponseStatus.SWITCHING_PROTOCOLS);
117 res.setStatus(HttpResponseStatus.UPGRADE_REQUIRED);
118 res.headers().set(Names.SEC_WEBSOCKET_VERSION, WebSocketVersion.V13.toHttpHeaderValue());
119 return channel.write(res);
120 }
121
122 }