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.handler.codec.http.DefaultHttpResponse;
20 import org.jboss.netty.handler.codec.http.HttpHeaders.Names;
21 import org.jboss.netty.handler.codec.http.HttpRequest;
22 import org.jboss.netty.handler.codec.http.HttpResponse;
23 import org.jboss.netty.handler.codec.http.HttpResponseStatus;
24 import org.jboss.netty.handler.codec.http.HttpVersion;
25
26
27
28
29 public class WebSocketServerHandshakerFactory {
30
31 private final String webSocketURL;
32
33 private final String subprotocols;
34
35 private final boolean allowExtensions;
36
37 private final long maxFramePayloadLength;
38
39
40
41
42
43
44
45
46
47 public WebSocketServerHandshakerFactory(String webSocketURL, String subprotocols, boolean allowExtensions) {
48 this(webSocketURL, subprotocols, allowExtensions, Long.MAX_VALUE);
49 }
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public WebSocketServerHandshakerFactory(String webSocketURL, String subprotocols, boolean allowExtensions,
66 long maxFramePayloadLength) {
67 this.webSocketURL = webSocketURL;
68 this.subprotocols = subprotocols;
69 this.allowExtensions = allowExtensions;
70 this.maxFramePayloadLength = maxFramePayloadLength;
71 }
72
73
74
75
76
77
78
79 public WebSocketServerHandshaker newHandshaker(HttpRequest req) {
80
81 String version = req.getHeader(Names.SEC_WEBSOCKET_VERSION);
82 if (version != null) {
83 if (version.equals(WebSocketVersion.V13.toHttpHeaderValue())) {
84
85 return new WebSocketServerHandshaker13(
86 webSocketURL, subprotocols, allowExtensions, maxFramePayloadLength);
87 } else if (version.equals(WebSocketVersion.V08.toHttpHeaderValue())) {
88
89 return new WebSocketServerHandshaker08(
90 webSocketURL, subprotocols, allowExtensions, maxFramePayloadLength);
91 } else {
92 return null;
93 }
94 } else {
95
96 return new WebSocketServerHandshaker00(
97 webSocketURL, subprotocols, maxFramePayloadLength);
98 }
99 }
100
101
102
103
104
105
106
107 public void sendUnsupportedWebSocketVersionResponse(Channel channel) {
108 HttpResponse res = new DefaultHttpResponse(
109 HttpVersion.HTTP_1_1,
110 HttpResponseStatus.SWITCHING_PROTOCOLS);
111 res.setStatus(HttpResponseStatus.UPGRADE_REQUIRED);
112 res.setHeader(Names.SEC_WEBSOCKET_VERSION, WebSocketVersion.V13.toHttpHeaderValue());
113 channel.write(res);
114 }
115
116 }