-
Notifications
You must be signed in to change notification settings - Fork 972
/
ChannelUpgradeHandler.java
227 lines (204 loc) · 8.32 KB
/
ChannelUpgradeHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.undertow.server.handlers;
import static org.wildfly.common.Assert.checkNotEmptyParam;
import io.undertow.Handlers;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.HttpUpgradeListener;
import io.undertow.util.CopyOnWriteMap;
import io.undertow.util.Headers;
import io.undertow.util.Methods;
import org.xnio.ChannelListener;
import org.xnio.ChannelListeners;
import org.xnio.StreamConnection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* An HTTP request handler which upgrades the HTTP request and hands it off as a socket to any XNIO consumer.
*
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
* @author Stuart Douglas
*/
public final class ChannelUpgradeHandler implements HttpHandler {
private final CopyOnWriteMap<String, List<Holder>> handlers = new CopyOnWriteMap<>();
private volatile HttpHandler nonUpgradeHandler = ResponseCodeHandler.HANDLE_404;
/**
* Add a protocol to this handler.
*
* @param productString the product string to match
* @param openListener the open listener to call
* @param handshake a handshake implementation that can be used to verify the client request and modify the response
*/
public synchronized void addProtocol(String productString, ChannelListener<? super StreamConnection> openListener, final HttpUpgradeHandshake handshake) {
addProtocol(productString, null, openListener, handshake);
}
/**
* Add a protocol to this handler.
*
* @param productString the product string to match
* @param openListener the open listener to call
* @param handshake a handshake implementation that can be used to verify the client request and modify the response
*/
public synchronized void addProtocol(String productString, HttpUpgradeListener openListener, final HttpUpgradeHandshake handshake) {
addProtocol(productString, openListener, null, handshake);
}
private synchronized void addProtocol(String productString, HttpUpgradeListener openListener, final ChannelListener<? super StreamConnection> channelListener, final HttpUpgradeHandshake handshake) {
checkNotEmptyParam("productString", productString);
if (openListener == null && channelListener == null) {
throw new IllegalArgumentException("openListener and channelListener are null");
}
if(openListener == null) {
openListener = new HttpUpgradeListener() {
@Override
public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
ChannelListeners.invokeChannelListener(streamConnection, channelListener);
}
};
}
List<Holder> list = handlers.get(productString);
if (list == null) {
handlers.put(productString, list = new CopyOnWriteArrayList<>());
}
list.add(new Holder(openListener, handshake, channelListener));
}
/**
* Add a protocol to this handler.
*
* @param productString the product string to match
* @param openListener the open listener to call
*/
public void addProtocol(String productString, ChannelListener<? super StreamConnection> openListener) {
addProtocol(productString, openListener, null);
}
/**
* Add a protocol to this handler.
*
* @param productString the product string to match
* @param openListener the open listener to call
*/
public void addProtocol(String productString, HttpUpgradeListener openListener) {
addProtocol(productString, openListener, null);
}
/**
* Remove a protocol from this handler. This will remove all upgrade handlers that match the product string
*
* @param productString the product string to match
*/
public synchronized void removeProtocol(String productString) {
handlers.remove(productString);
}
/**
* Remove a protocol from this handler.
*
* @param productString the product string to match
* @param openListener The open listener
*/
public synchronized void removeProtocol(String productString, ChannelListener<? super StreamConnection> openListener) {
List<Holder> holders = handlers.get(productString);
if (holders == null) {
return;
}
Iterator<Holder> it = holders.iterator();
while (it.hasNext()) {
Holder holder = it.next();
if (holder.channelListener == openListener) {
holders.remove(holder);
break;
}
}
if (holders.isEmpty()) {
handlers.remove(productString);
}
}
/**
* Remove a protocol from this handler.
*
* @param productString the product string to match
* @param upgradeListener The upgrade listener
*/
public synchronized void removeProtocol(String productString, HttpUpgradeListener upgradeListener) {
List<Holder> holders = handlers.get(productString);
if (holders == null) {
return;
}
Iterator<Holder> it = holders.iterator();
while (it.hasNext()) {
Holder holder = it.next();
if (holder.listener == upgradeListener) {
holders.remove(holder);
break;
}
}
if (holders.isEmpty()) {
handlers.remove(productString);
}
}
/**
* Get the non-upgrade delegate handler.
*
* @return the non-upgrade delegate handler
*/
public HttpHandler getNonUpgradeHandler() {
return nonUpgradeHandler;
}
/**
* Set the non-upgrade delegate handler.
*
* @param nonUpgradeHandler the non-upgrade delegate handler
*/
public ChannelUpgradeHandler setNonUpgradeHandler(final HttpHandler nonUpgradeHandler) {
Handlers.handlerNotNull(nonUpgradeHandler);
this.nonUpgradeHandler = nonUpgradeHandler;
return this;
}
public void handleRequest(final HttpServerExchange exchange) throws Exception {
final List<String> upgradeStrings = exchange.getRequestHeaders().get(Headers.UPGRADE);
if (upgradeStrings != null && exchange.getRequestMethod().equals(Methods.GET)) {
for (String string : upgradeStrings) {
final List<Holder> holders = handlers.get(string);
if (holders != null) {
for (Holder holder : holders) {
final HttpUpgradeListener listener = holder.listener;
if (holder.handshake != null) {
if (!holder.handshake.handleUpgrade(exchange)) {
//handshake did not match, try again
continue;
}
}
exchange.upgradeChannel(string,listener);
exchange.endExchange();
return;
}
}
}
}
nonUpgradeHandler.handleRequest(exchange);
}
private static final class Holder {
final HttpUpgradeListener listener;
final HttpUpgradeHandshake handshake;
final ChannelListener<? super StreamConnection> channelListener;
private Holder(final HttpUpgradeListener listener, final HttpUpgradeHandshake handshake, ChannelListener<? super StreamConnection> channelListener) {
this.listener = listener;
this.handshake = handshake;
this.channelListener = channelListener;
}
}
}