-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clone PerMessageDeflateExtension values correctly #1439
Changes from 4 commits
a566891
1e9fbbf
fe7635b
4f4aed5
dfca00b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,23 +53,28 @@ public class PerMessageDeflateExtension extends CompressionExtension { | |
// For WebSocketClients, this variable holds the extension parameters that client himself has requested. | ||
private Map<String, String> requestedParameters = new LinkedHashMap<>(); | ||
|
||
private Inflater inflater = new Inflater(true); | ||
private Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true); | ||
|
||
public Inflater getInflater() { | ||
return inflater; | ||
} | ||
private int deflaterLevel = Deflater.DEFAULT_COMPRESSION; | ||
|
||
public void setInflater(Inflater inflater) { | ||
this.inflater = inflater; | ||
} | ||
private Inflater inflater = new Inflater(true); | ||
private Deflater deflater = new Deflater(this.deflaterLevel, true); | ||
|
||
public Deflater getDeflater() { | ||
return deflater; | ||
/** | ||
* Get the compression level used for the compressor. | ||
* @return the compression level (0-9) | ||
*/ | ||
public int getDeflaterLevel() { | ||
return this.deflaterLevel; | ||
} | ||
|
||
public void setDeflater(Deflater deflater) { | ||
this.deflater = deflater; | ||
/** | ||
* Set the compression level used for the compressor. | ||
* @param level the compression level (0-9) | ||
*/ | ||
public void setDeflaterLevel(int level) { | ||
this.deflater.setLevel(level); | ||
this.deflaterLevel = level; | ||
//If the compression level is changed, the next invocation of deflate will compress the input available so far with the old level (and may be flushed); the new level will take effect only after that invocation. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, this does not work in Java 8: While this matches the Java 8 Deflater Javadoc, the implementation does not match the documentation: The set level is never picked up as far as I can tell. The "setParams" boolean is never evaluated. It is in Java 11, though, so it works there: So, for Java 8 compatibility, the Deflater instance has to be created with the configured compression level. How about not having setDeflaterLevel at all, but rather making it an optional constructor argument: private final int compressionLevel; public PerMessageDeflateExtension() { public PerMessageDeflateExtension(int compressionLevel) { public int getCompressionLevel() { @OverRide |
||
this.deflater.deflate(new byte[0]); | ||
} | ||
|
||
/** | ||
|
@@ -166,15 +171,15 @@ We can check the getRemaining() method to see whether the data we supplied has b | |
Note that this behavior doesn't occur if the message is "first compressed and then fragmented". | ||
*/ | ||
if (inflater.getRemaining() > 0) { | ||
inflater = new Inflater(true); | ||
inflater.reset(); | ||
decompress(inputFrame.getPayloadData().array(), output); | ||
} | ||
|
||
if (inputFrame.isFin()) { | ||
decompress(TAIL_BYTES, output); | ||
// If context takeover is disabled, inflater can be reset. | ||
if (clientNoContextTakeover) { | ||
inflater = new Inflater(true); | ||
inflater.reset(); | ||
} | ||
} | ||
} catch (DataFormatException e) { | ||
|
@@ -244,8 +249,7 @@ public void encodeFrame(Framedata inputFrame) { | |
} | ||
|
||
if (serverNoContextTakeover) { | ||
deflater.end(); | ||
deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true); | ||
deflater.reset(); | ||
} | ||
} | ||
|
||
|
@@ -330,7 +334,12 @@ public String getProvidedExtensionAsServer() { | |
|
||
@Override | ||
public IExtension copyInstance() { | ||
return new PerMessageDeflateExtension(); | ||
PerMessageDeflateExtension clone = new PerMessageDeflateExtension(); | ||
clone.setThreshold(this.getThreshold()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please insert:
and replace the new Inflater() calls in lines 169 and 177 with inflater.reset(), and the deflater.end() & new Deflater() calls in lines 247/248 with deflater.reset(). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are not going to reuse the deflater/inflater for multiple clients There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using reset however should be done, I agree There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Could you elaborate? If the user of this library wants to hand in a different inflater/deflater, why should that not be used? Specific use case: I want to use BEST_COMPRESSION, not DEFAULT_COMPRESSION, to get the most out of compression (and both my servers and clients are powerful enough to put up that extra effort). That's why I hand in a new Deflater(Deflater.BEST_COMPRESSION, true) instance. I'd like that to work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem is that we create a new instance of a Draft (and therefore the provided extension with the custom deflater/inflater) for every new client connecting to the server. If we simply use the single instance provided by the user, we will get in trouble for the server implementation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ah, thanks, I see. I was just lucky that I was only using 1 server and client for my WebSocket compression tests. Indeed, then an API change is needed to set Inflater and Deflater factories. Or, if we think that this flexibility is not needed, a setCompressionLevel(int level); API would do for me. Then this class would always instantiate the Inflater and Deflater objects, always with nowrap=true. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking more into the documentation, I think that nowrap will always be true in our case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah I think so too. We only need the compression level, nowrap should be true. @PhilipRoman you got any objections? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My bad, there is no getter for the level/strategy of a Deflater. Therefore: |
||
clone.setClientNoContextTakeover(this.isClientNoContextTakeover()); | ||
clone.setServerNoContextTakeover(this.isServerNoContextTakeover()); | ||
clone.setDeflaterLevel(this.getDeflaterLevel()); | ||
return clone; | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though the Deflater Javadoc says the same, the implementation actually also accepts DEFAULT_COMPRESSION, which is -1. The getter above would then return -1 as well.