Skip to content

Commit e2a6af8

Browse files
committed
Fixing field case. Draft 75 is case-sensitive while Draft 76 is case-insensitive.
1 parent 9f4348e commit e2a6af8

File tree

3 files changed

+61
-27
lines changed

3 files changed

+61
-27
lines changed

src/WebSocketClient.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,23 @@ private void sendClientHandshake(WebSocket conn) throws IOException {
320320
WebSocketHandshake clientHandshake = new WebSocketHandshake();
321321
clientHandshake.setType(ClientServerType.CLIENT);
322322
clientHandshake.setDraft(getDraft());
323-
clientHandshake.put("request-uri", requestURI);
324-
clientHandshake.put("host", host);
325-
clientHandshake.put("origin", origin);
323+
324+
if (getDraft() == Draft.DRAFT75) {
325+
clientHandshake.put("Request-Uri", requestURI);
326+
clientHandshake.put("Host", host);
327+
clientHandshake.put("Origin", origin);
328+
} else {
329+
clientHandshake.put("request-uri", requestURI);
330+
clientHandshake.put("host", host);
331+
clientHandshake.put("origin", origin);
332+
}
333+
334+
326335
if (subprotocol != null) {
327336
if (getDraft() == Draft.DRAFT75) {
328-
clientHandshake.put("websocket-protocol", subprotocol);
337+
clientHandshake.put("Websocket-Protocol", subprotocol);
329338
} else {
330-
clientHandshake.put("sec-webSocket-protocol", subprotocol);
339+
clientHandshake.put("sec-websocket-protocol", subprotocol);
331340
}
332341
}
333342

src/WebSocketHandshake.java

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,22 @@ public void parseHandshake() {
112112
if (this.handshakeType == ClientServerType.SERVER) {
113113
int sp1 = line.indexOf(" ");
114114
int sp2 = line.indexOf(" ",sp1+1);
115-
String httpVersion = line.substring(0,sp1);
115+
String httpVersion = line.substring(0,sp1);
116116
String statusCode = line.substring(sp1+1,sp2);
117117
String reasonPhrase = line.substring(sp2+1,line.length());
118-
put("http-version", httpVersion);
119-
put("status-code", statusCode);
120-
put("reason-phrase", reasonPhrase);
118+
String httpVersionKey = "HTTP-Version";
119+
String statusCodeKey = "Status-Code";
120+
String reasonPhraseKey = "Reason-Phrase";
121+
122+
if (this.handshakeDraft == Draft.DRAFT76) {
123+
httpVersionKey = httpVersionKey.toLowerCase();
124+
statusCodeKey = statusCodeKey.toLowerCase();
125+
reasonPhraseKey = reasonPhraseKey.toLowerCase();
126+
}
127+
128+
put(httpVersionKey, httpVersion);
129+
put(statusCodeKey, statusCode);
130+
put(reasonPhraseKey, reasonPhrase);
121131
}
122132

123133
if (this.handshakeType == ClientServerType.CLIENT) {
@@ -126,9 +136,20 @@ public void parseHandshake() {
126136
String method = line.substring(0,sp1);
127137
String requestURI = line.substring(sp1+1,sp2);
128138
String httpVersion = line.substring(sp2+1,line.length());
129-
put("method", method);
130-
put("request-uri", requestURI);
131-
put("http-version", httpVersion);
139+
String methodKey = "Method";
140+
String requestURIKey = "Request-URI";
141+
String httpVersionKey = "HTTP-Version";
142+
143+
144+
if (this.handshakeDraft == Draft.DRAFT76) {
145+
methodKey = methodKey.toLowerCase();
146+
requestURIKey = httpVersionKey.toLowerCase();
147+
httpVersionKey = httpVersionKey.toLowerCase();
148+
}
149+
150+
put(methodKey, method);
151+
put(requestURIKey, requestURI);
152+
put(httpVersionKey, httpVersion);
132153
}
133154

134155
// parse fields
@@ -206,10 +227,10 @@ public void buildHandshake() {
206227
"Connection: Upgrade\r\n";
207228

208229
if (this.handshakeDraft == Draft.DRAFT75) {
209-
responseHandshake += "WebSocket-Origin: " + getProperty("origin") + "\r\n" +
210-
"WebSocket-Location: ws://" + getProperty("host") + getProperty("request-uri") + "\r\n";
230+
responseHandshake += "WebSocket-Origin: " + getProperty("Origin") + "\r\n" +
231+
"WebSocket-Location: ws://" + getProperty("Host") + getProperty("Request-URI") + "\r\n";
211232
if (containsKey("WebSocket-Protocol")) {
212-
responseHandshake += "WebSocket-Protocol: " + getProperty("websocket-protocol") + "\r\n";
233+
responseHandshake += "WebSocket-Protocol: " + getProperty("Websocket-Protocol") + "\r\n";
213234
}
214235
}
215236

@@ -243,14 +264,18 @@ public void buildHandshake() {
243264

244265
if (this.handshakeType == ClientServerType.CLIENT) {
245266

246-
String requestHandshake = "GET " + getProperty("request-uri") + " HTTP/1.1\r\n" +
267+
String requestURI = (getDraft() == Draft.DRAFT75) ? getProperty("Request-URI") : getProperty("request-uri");
268+
String host = (getDraft() == Draft.DRAFT75) ? getProperty("Host") : getProperty("host");
269+
String origin = (getDraft() == Draft.DRAFT75) ? getProperty("Origin") : getProperty("origin");
270+
271+
String requestHandshake = "GET " + requestURI + " HTTP/1.1\r\n" +
247272
"Upgrade: WebSocket\r\n" +
248273
"Connection: Upgrade\r\n" +
249-
"Host: " + getProperty("host") + "\r\n" +
250-
"Origin: " + getProperty("origin") + "\r\n";
274+
"Host: " + host + "\r\n" +
275+
"Origin: " + origin + "\r\n";
251276

252-
if (getDraft() == Draft.DRAFT75 && containsKey("websocket-protocol")) {
253-
requestHandshake += "WebSocket-Protocol: " + getProperty("websocket-protocol") + "\r\n";
277+
if (getDraft() == Draft.DRAFT75 && containsKey("Websocket-Protocol")) {
278+
requestHandshake += "WebSocket-Protocol: " + getProperty("Websocket-Protocol") + "\r\n";
254279
}
255280

256281
if (getDraft() == Draft.DRAFT76 ) {

src/WebSocketServer.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,9 @@ public boolean onHandshakeRecieved(WebSocket conn, WebSocketHandshake handshake)
373373

374374
private boolean handleHandshake75(WebSocket conn, WebSocketHandshake handshake) throws IOException {
375375

376-
if (!handshake.getProperty("method").equals("GET")
377-
|| !handshake.getProperty("http-version").equals("HTTP/1.1")
378-
|| !handshake.getProperty("request-uri").startsWith("/")) {
376+
if (!handshake.getProperty("Method").equals("GET")
377+
|| !handshake.getProperty("HTTP-Version").equals("HTTP/1.1")
378+
|| !handshake.getProperty("Request-URI").startsWith("/")) {
379379
return false;
380380
}
381381

@@ -409,11 +409,11 @@ private boolean handleHandshake75(WebSocket conn, WebSocketHandshake handshake)
409409
WebSocketHandshake serverHandshake = new WebSocketHandshake();
410410
serverHandshake.setType(ClientServerType.SERVER);
411411
serverHandshake.setDraft(Draft.DRAFT75);
412-
serverHandshake.put("host", handshake.getProperty("Host"));
413-
serverHandshake.put("request-uri", handshake.getProperty("request-uri"));
414-
serverHandshake.put("origin", handshake.getProperty("Origin"));
412+
serverHandshake.put("Host", handshake.getProperty("Host"));
413+
serverHandshake.put("Request-Uri", handshake.getProperty("request-uri"));
414+
serverHandshake.put("Origin", handshake.getProperty("Origin"));
415415
if (handshake.containsKey("Websocket-Protocol")) {
416-
serverHandshake.put("websocket-protocol", handshake.getProperty("Websocket-Protocol"));
416+
serverHandshake.put("Websocket-Protocol", handshake.getProperty("Websocket-Protocol"));
417417
}
418418

419419
conn.socketChannel().write(ByteBuffer.wrap(serverHandshake.getHandshake()));

0 commit comments

Comments
 (0)