Skip to content

Commit 5d84099

Browse files
authored
Merge pull request TooTallNate#1232 from marci4/Issue1230
2 parents 2c9b091 + 8f1f8e4 commit 5d84099

File tree

2 files changed

+41
-23
lines changed

2 files changed

+41
-23
lines changed

src/main/java/org/java_websocket/drafts/Draft_6455.java

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,23 @@ public class Draft_6455 extends Draft {
115115
/**
116116
* Attribute for the used extension in this draft
117117
*/
118-
private IExtension extension = new DefaultExtension();
118+
private IExtension negotiatedExtension = new DefaultExtension();
119+
120+
/**
121+
* Attribute for the default extension
122+
*/
123+
private IExtension defaultExtension = new DefaultExtension();
119124

120125
/**
121126
* Attribute for all available extension in this draft
122127
*/
123128
private List<IExtension> knownExtensions;
124129

130+
/**
131+
* Current active extension used to decode messages
132+
*/
133+
private IExtension currentDecodingExtension;
134+
125135
/**
126136
* Attribute for the used protocol in this draft
127137
*/
@@ -241,10 +251,11 @@ public Draft_6455(List<IExtension> inputExtensions, List<IProtocol> inputProtoco
241251
knownExtensions.addAll(inputExtensions);
242252
//We always add the DefaultExtension to implement the normal RFC 6455 specification
243253
if (!hasDefault) {
244-
knownExtensions.add(this.knownExtensions.size(), extension);
254+
knownExtensions.add(this.knownExtensions.size(), negotiatedExtension);
245255
}
246256
knownProtocols.addAll(inputProtocols);
247257
maxFrameSize = inputMaxFrameSize;
258+
currentDecodingExtension = null;
248259
}
249260

250261
@Override
@@ -259,9 +270,9 @@ public HandshakeState acceptHandshakeAsServer(ClientHandshake handshakedata)
259270
String requestedExtension = handshakedata.getFieldValue(SEC_WEB_SOCKET_EXTENSIONS);
260271
for (IExtension knownExtension : knownExtensions) {
261272
if (knownExtension.acceptProvidedExtensionAsServer(requestedExtension)) {
262-
extension = knownExtension;
273+
negotiatedExtension = knownExtension;
263274
extensionState = HandshakeState.MATCHED;
264-
log.trace("acceptHandshakeAsServer - Matching extension found: {}", extension);
275+
log.trace("acceptHandshakeAsServer - Matching extension found: {}", negotiatedExtension);
265276
break;
266277
}
267278
}
@@ -316,9 +327,9 @@ public HandshakeState acceptHandshakeAsClient(ClientHandshake request, ServerHan
316327
String requestedExtension = response.getFieldValue(SEC_WEB_SOCKET_EXTENSIONS);
317328
for (IExtension knownExtension : knownExtensions) {
318329
if (knownExtension.acceptProvidedExtensionAsClient(requestedExtension)) {
319-
extension = knownExtension;
330+
negotiatedExtension = knownExtension;
320331
extensionState = HandshakeState.MATCHED;
321-
log.trace("acceptHandshakeAsClient - Matching extension found: {}", extension);
332+
log.trace("acceptHandshakeAsClient - Matching extension found: {}", negotiatedExtension);
322333
break;
323334
}
324335
}
@@ -337,7 +348,7 @@ public HandshakeState acceptHandshakeAsClient(ClientHandshake request, ServerHan
337348
* @return the extension which is used or null, if handshake is not yet done
338349
*/
339350
public IExtension getExtension() {
340-
return extension;
351+
return negotiatedExtension;
341352
}
342353

343354
/**
@@ -562,8 +573,20 @@ private Framedata translateSingleFrame(ByteBuffer buffer)
562573
frame.setRSV3(rsv3);
563574
payload.flip();
564575
frame.setPayload(payload);
565-
getExtension().isFrameValid(frame);
566-
getExtension().decodeFrame(frame);
576+
if (frame.getOpcode() != Opcode.CONTINUOUS) {
577+
// Prioritize the negotiated extension
578+
if (frame.isRSV1() || frame.isRSV2() || frame.isRSV3()) {
579+
currentDecodingExtension = getExtension();
580+
} else {
581+
// No encoded message, so we can use the default one
582+
currentDecodingExtension = defaultExtension;
583+
}
584+
}
585+
if (currentDecodingExtension == null) {
586+
currentDecodingExtension = defaultExtension;
587+
}
588+
currentDecodingExtension.isFrameValid(frame);
589+
currentDecodingExtension.decodeFrame(frame);
567590
if (log.isTraceEnabled()) {
568591
log.trace("afterDecoding({}): {}", frame.getPayloadData().remaining(),
569592
(frame.getPayloadData().remaining() > 1000 ? "too big to display"
@@ -780,10 +803,10 @@ public List<Framedata> createFrames(String text, boolean mask) {
780803
@Override
781804
public void reset() {
782805
incompleteframe = null;
783-
if (extension != null) {
784-
extension.reset();
806+
if (negotiatedExtension != null) {
807+
negotiatedExtension.reset();
785808
}
786-
extension = new DefaultExtension();
809+
negotiatedExtension = new DefaultExtension();
787810
protocol = null;
788811
}
789812

@@ -1116,15 +1139,15 @@ public boolean equals(Object o) {
11161139
if (maxFrameSize != that.getMaxFrameSize()) {
11171140
return false;
11181141
}
1119-
if (extension != null ? !extension.equals(that.getExtension()) : that.getExtension() != null) {
1142+
if (negotiatedExtension != null ? !negotiatedExtension.equals(that.getExtension()) : that.getExtension() != null) {
11201143
return false;
11211144
}
11221145
return protocol != null ? protocol.equals(that.getProtocol()) : that.getProtocol() == null;
11231146
}
11241147

11251148
@Override
11261149
public int hashCode() {
1127-
int result = extension != null ? extension.hashCode() : 0;
1150+
int result = negotiatedExtension != null ? negotiatedExtension.hashCode() : 0;
11281151
result = 31 * result + (protocol != null ? protocol.hashCode() : 0);
11291152
result = 31 * result + (maxFrameSize ^ (maxFrameSize >>> 32));
11301153
return result;

src/main/java/org/java_websocket/extensions/permessage_deflate/PerMessageDeflateExtension.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ public void decodeFrame(Framedata inputFrame) throws InvalidDataException {
142142
return;
143143
}
144144

145+
if (!inputFrame.isRSV1() && inputFrame.getOpcode() != Opcode.CONTINUOUS) {
146+
return;
147+
}
148+
145149
// RSV1 bit must be set only for the first frame.
146150
if (inputFrame.getOpcode() == Opcode.CONTINUOUS && inputFrame.isRSV1()) {
147151
throw new InvalidDataException(CloseFrame.POLICY_VALIDATION,
148152
"RSV1 bit can only be set for the first frame.");
149153
}
150-
// If rsv1 is not set, we dont have a compressed message
151-
if (!inputFrame.isRSV1()) {
152-
return;
153-
}
154154

155155
// Decompressed output buffer.
156156
ByteArrayOutputStream output = new ByteArrayOutputStream();
@@ -181,11 +181,6 @@ We can check the getRemaining() method to see whether the data we supplied has b
181181
throw new InvalidDataException(CloseFrame.POLICY_VALIDATION, e.getMessage());
182182
}
183183

184-
// RSV1 bit must be cleared after decoding, so that other extensions don't throw an exception.
185-
if (inputFrame.isRSV1()) {
186-
((DataFrame) inputFrame).setRSV1(false);
187-
}
188-
189184
// Set frames payload to the new decompressed data.
190185
((FramedataImpl1) inputFrame)
191186
.setPayload(ByteBuffer.wrap(output.toByteArray(), 0, output.size()));

0 commit comments

Comments
 (0)