Skip to content
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

Merged
merged 5 commits into from
Nov 11, 2024

Conversation

marci4
Copy link
Collaborator

@marci4 marci4 commented Oct 8, 2024

Description

Related Issue

Fixes #1437
Fixes #1400

Motivation and Context

Issue

How Has This Been Tested?

Additional tests

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@@ -330,7 +330,11 @@ public String getProvidedExtensionAsServer() {

@Override
public IExtension copyInstance() {
return new PerMessageDeflateExtension();
PerMessageDeflateExtension clone = new PerMessageDeflateExtension();
clone.setThreshold(this.getThreshold());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please insert:

  • clone.setDeflater(this.getDeflater());
  • clone.setInflater(this.getInflater());

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().

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not going to reuse the deflater/inflater for multiple clients

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using reset however should be done, I agree

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not going to reuse the deflater/inflater for multiple clients

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.
https://github.com/TooTallNate/Java-WebSocket/blob/master/src/main/java/org/java_websocket/WebSocketImpl.java#L272

If we simply use the single instance provided by the user, we will get in trouble for the server implementation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we simply use the single instance provided by the user, we will get in trouble for the server implementation

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.

Copy link
Collaborator Author

@marci4 marci4 Oct 8, 2024

Choose a reason for hiding this comment

The 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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we simply use the single instance provided by the user, we will get in trouble for the server implementation

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.

Yeah I think so too. We only need the compression level, nowrap should be true.

@PhilipRoman you got any objections?

Copy link
Collaborator Author

@marci4 marci4 Oct 8, 2024

Choose a reason for hiding this comment

The 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:
new API -> setCompressionLevel()
remove getter/setter for Deflater/Inflater

@PhilipRoman
Copy link
Collaborator

Looks good, although personally I would add some calls to setDeflaterLevel in the functional tests like testDecodeFrame.

Copy link
Contributor

@robert-s-ubi robert-s-ubi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, changes are needed for Java 8 compatibility.

this.deflater = deflater;
/**
* Set the compression level used for the compressor.
* @param level the compression level (0-9)
Copy link
Contributor

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.

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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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:
https://github.com/openjdk/jdk8u/blob/master/jdk/src/share/classes/java/util/zip/Deflater.java

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:
https://github.com/openjdk/jdk11u/blob/master/src/java.base/share/classes/java/util/zip/Deflater.java

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;
private final Inflater inflater;
private final Deflater deflater;

public PerMessageDeflateExtension() {
this(Deflater.DEFAULT_COMPRESSION);
}

public PerMessageDeflateExtension(int compressionLevel) {
this.compressionLevel = compressionLevel;
this.inflater = new Inflater(true);
this.deflater = new Deflater(compressionLevel, true);
}

public int getCompressionLevel() {
return compressionLevel;
}

@OverRide
public IExtension copyInstance() {
PerMessageDeflateExtension clone = new PerMessageDeflateExtension(this.getCompressionLevel());
clone.setThreshold(this.getThreshold());
clone.setClientNoContextTakeover(this.isClientNoContextTakeover());
clone.setServerNoContextTakeover(this.isServerNoContextTakeover());
return clone;
}

@marci4 marci4 requested a review from robert-s-ubi October 23, 2024 20:46
Copy link
Contributor

@robert-s-ubi robert-s-ubi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thanks for your patience!

@marci4 marci4 merged commit 3b29042 into TooTallNate:master Nov 11, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants