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
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

clone.setClientNoContextTakeover(this.isClientNoContextTakeover());
clone.setServerNoContextTakeover(this.isServerNoContextTakeover());
return clone;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,36 @@ public void testSetClientNoContextTakeover() {
@Test
public void testCopyInstance() {
PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension();
IExtension newDeflateExtension = deflateExtension.copyInstance();
assertEquals(deflateExtension.toString(), newDeflateExtension.toString());
PerMessageDeflateExtension newDeflateExtension = (PerMessageDeflateExtension)deflateExtension.copyInstance();
assertEquals("PerMessageDeflateExtension", newDeflateExtension.toString());
// Also check the values
assertEquals(deflateExtension.getThreshold(), newDeflateExtension.getThreshold());
assertEquals(deflateExtension.isClientNoContextTakeover(), newDeflateExtension.isClientNoContextTakeover());
assertEquals(deflateExtension.isServerNoContextTakeover(), newDeflateExtension.isServerNoContextTakeover());
// Adjust this to the factory
//assertEquals(deflateExtension.getDeflater(), newDeflateExtension.getDeflater());
//assertEquals(deflateExtension.getInflater(), newDeflateExtension.getInflater());

deflateExtension = new PerMessageDeflateExtension();
deflateExtension.setThreshold(512);
deflateExtension.setServerNoContextTakeover(false);
deflateExtension.setClientNoContextTakeover(true);
newDeflateExtension = (PerMessageDeflateExtension)deflateExtension.copyInstance();

assertEquals(deflateExtension.getThreshold(), newDeflateExtension.getThreshold());
assertEquals(deflateExtension.isClientNoContextTakeover(), newDeflateExtension.isClientNoContextTakeover());
assertEquals(deflateExtension.isServerNoContextTakeover(), newDeflateExtension.isServerNoContextTakeover());


deflateExtension = new PerMessageDeflateExtension();
deflateExtension.setThreshold(64);
deflateExtension.setServerNoContextTakeover(true);
deflateExtension.setClientNoContextTakeover(false);
newDeflateExtension = (PerMessageDeflateExtension)deflateExtension.copyInstance();

assertEquals(deflateExtension.getThreshold(), newDeflateExtension.getThreshold());
assertEquals(deflateExtension.isClientNoContextTakeover(), newDeflateExtension.isClientNoContextTakeover());
assertEquals(deflateExtension.isServerNoContextTakeover(), newDeflateExtension.isServerNoContextTakeover());
}

@Test
Expand Down Expand Up @@ -222,4 +250,11 @@ public void testSetDeflater() {
assertEquals(deflateExtension.getDeflater().finished(),
new Deflater(Deflater.DEFAULT_COMPRESSION, false).finished());
}
@Test
public void testDefaults() {
PerMessageDeflateExtension deflateExtension = new PerMessageDeflateExtension();
assertFalse(deflateExtension.isClientNoContextTakeover());
assertTrue(deflateExtension.isServerNoContextTakeover());
assertEquals(1024, deflateExtension.getThreshold());
}
}
Loading