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

Reworking jetty-compression for JPMS #12531

Merged
merged 27 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
aa14b45
Reworking jetty-compression for JPMS
joakime Nov 13, 2024
0a1abb0
Fix pom
joakime Nov 13, 2024
0e6f50b
Merge remote-tracking branch 'origin/jetty-12.1.x' into fix/12.1.x/co…
joakime Nov 14, 2024
9c8c9aa
Changes from review
joakime Nov 14, 2024
713dfd9
Changes from review
joakime Nov 14, 2024
6244dbd
Cleaning up source
joakime Nov 14, 2024
177500b
Merge remote-tracking branch 'origin/jetty-12.1.x' into fix/12.1.x/co…
joakime Nov 15, 2024
b068d44
Moving (include/exclude) verb to before noun
joakime Nov 15, 2024
78921e6
Renaming jetty-compression-api to jetty-compression-common
joakime Nov 15, 2024
a95c780
Renaming module compression-api to commpression-common
joakime Nov 15, 2024
af163a1
Removing class.cast(obj) usages
joakime Nov 15, 2024
fa97456
Fix typo
joakime Nov 15, 2024
a6cf5a6
always add bean
joakime Nov 15, 2024
242c62a
use modern switch/case
joakime Nov 15, 2024
dc8b219
cleanup unused source
joakime Nov 15, 2024
232a12f
configurable gzip (input|output) stream
joakime Nov 15, 2024
9d8b1e7
simplify if statement
joakime Nov 15, 2024
2974ba9
follow jetty style for mod+xml naming
joakime Nov 15, 2024
2f9a788
fix pom syntax
joakime Nov 15, 2024
6f28e1f
fix pom syntax
joakime Nov 15, 2024
9588513
fix pom syntax
joakime Nov 15, 2024
bfe4293
fix pom syntax
joakime Nov 15, 2024
c4e1c16
Changes from review
joakime Nov 18, 2024
29343db
Fixing typo on method name (plural)
joakime Nov 18, 2024
5ad683c
Fix module lib references
joakime Nov 18, 2024
cc61b43
ManagedAttribute typos
joakime Nov 18, 2024
174a392
Merge remote-tracking branch 'origin/jetty-12.1.x' into fix/12.1.x/co…
joakime Nov 18, 2024
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
10 changes: 9 additions & 1 deletion jetty-core/jetty-compression/jetty-compression-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<artifactId>jetty-http</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-io</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

module org.eclipse.jetty.compression
joakime marked this conversation as resolved.
Show resolved Hide resolved
{
requires transitive org.eclipse.jetty.http;
requires org.slf4j;

exports org.eclipse.jetty.compression;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,6 @@ public Compression(String encoding)
etagSuffixQuote = etagSuffix + "\"";
}

joakime marked this conversation as resolved.
Show resolved Hide resolved
/**
* Test if the {@code Accept-Encoding} request header and {@code Content-Length} response
* header are suitable to allow compression for the response compression implementation.
*
* @param headers the request headers
* @param contentLength the content length
* @return true if compression is allowed
*/
public boolean acceptsCompression(HttpFields headers, long contentLength)
{
if (contentLength >= 0 && contentLength < getMinCompressSize())
{
if (LOG.isDebugEnabled())
LOG.debug("{} excluded minCompressSize {}", this, headers);
return false;
}

// check the accept encoding header
if (!headers.contains(HttpHeader.ACCEPT_ENCODING, getEncodingName()))
{
if (LOG.isDebugEnabled())
LOG.debug("{} excluded not {} acceptable {}", this, getEncodingName(), headers);
return false;
}

return true;
}

/**
* Acquire a {@link RetainableByteBuffer} that is managed by this {@link Compression} implementation
* which is suitable for compressed output from an {@link EncoderSink} or compressed input from a {@link DecoderSource}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public void write(boolean last, ByteBuffer content, Callback callback)
}
catch (Throwable t)
{
// TODO: do we need to tell the delegate that we failed?
callback.failed(t);
return;
}
Expand Down Expand Up @@ -86,7 +85,7 @@ private enum State
COMPRESSING,
joakime marked this conversation as resolved.
Show resolved Hide resolved
// The last content is being encoded and is being flushed
FINISHING,
// The final content has been send (final state)
// The final content has been sent (final state)
FINISHED
}

Expand Down Expand Up @@ -128,7 +127,7 @@ protected void onCompleteFailure(Throwable x)
}

@Override
protected Action process() throws Throwable
protected Action process()
{
if (state.get() == State.FINISHED)
return Action.SUCCEEDED;
Expand Down
5 changes: 0 additions & 5 deletions jetty-core/jetty-compression/jetty-compression-brotli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
<artifactId>brotli4j</artifactId>
<version>${brotli4j.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId>
<version>${netty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.compression</groupId>
<artifactId>jetty-compression-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

module org.eclipse.jetty.compression.brotli
{
requires transitive org.eclipse.jetty.compression;
requires org.slf4j;
requires com.aayushatharva.brotli4j;

exports org.eclipse.jetty.compression.brotli;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.eclipse.jetty.compression.DecoderSource;
import org.eclipse.jetty.compression.EncoderConfig;
import org.eclipse.jetty.compression.EncoderSink;
import org.eclipse.jetty.compression.brotli.internal.BrotliDecoderSource;
import org.eclipse.jetty.compression.brotli.internal.BrotliEncoderSink;
import org.eclipse.jetty.http.CompressedContentFormat;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpHeader;
Expand Down Expand Up @@ -159,7 +161,7 @@ public InputStream newDecoderInputStream(InputStream in, DecoderConfig config) t
public DecoderSource newDecoderSource(Content.Source source, DecoderConfig config)
joakime marked this conversation as resolved.
Show resolved Hide resolved
{
BrotliDecoderConfig brotliDecoderConfig = BrotliDecoderConfig.class.cast(config);
joakime marked this conversation as resolved.
Show resolved Hide resolved
return new BrotliDecoderSource(this, source, brotliDecoderConfig);
return new BrotliDecoderSource(source, brotliDecoderConfig);
}

@Override
Expand All @@ -173,7 +175,7 @@ public OutputStream newEncoderOutputStream(OutputStream out, EncoderConfig confi
public EncoderSink newEncoderSink(Content.Sink sink, EncoderConfig config)
{
BrotliEncoderConfig brotliEncoderConfig = BrotliEncoderConfig.class.cast(config);
joakime marked this conversation as resolved.
Show resolved Hide resolved
return new BrotliEncoderSink(this, sink, brotliEncoderConfig);
return new BrotliEncoderSink(sink, brotliEncoderConfig);
}

private ByteOrder getByteOrder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public int getBufferSize()
@Override
public void setBufferSize(int size)
{
this.bufferSize = this.bufferSize = Math.max(MIN_BUFFER_SIZE, size);
this.bufferSize = Math.max(MIN_BUFFER_SIZE, size);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ public class BrotliEncoderConfig implements EncoderConfig
* @see <a href="https://www.brotli.org/encode.html">Encoder Defaults</a>
*/
public static final int DEFAULT_QUALITY = 11;
/**
* Default Brotli Mode (Strategy).
*
* @see <a href="https://www.brotli.org/encode.html">Encoder Defaults</a>
*/
public static final Encoder.Mode DEFAULT_MODE = Encoder.Mode.GENERIC;
/**
* Default Brotli Window.
*
Expand All @@ -41,15 +35,15 @@ public class BrotliEncoderConfig implements EncoderConfig
public static final int MIN_BUFFER_SIZE = 32;
joakime marked this conversation as resolved.
Show resolved Hide resolved

private int bufferSize = 4096;
private int strategy = 0;
private int quality = DEFAULT_QUALITY;
private Encoder.Mode mode = DEFAULT_MODE;
private int lgWindow = DEFAULT_WINDOW;

public Parameters asEncoderParams()
{
Parameters params = new Parameters();
params.setQuality(getCompressionLevel());
params.setMode(getMode());
params.setMode(Encoder.Mode.of(getStrategy()));
params.setWindow(getLgWindow());
return params;
}
Expand Down Expand Up @@ -122,15 +116,10 @@ public void setLgWindow(int window)
this.lgWindow = window;
}

public Encoder.Mode getMode()
{
return mode;
}

@Override
public int getStrategy()
{
return mode.ordinal();
return strategy;
}

/**
Expand All @@ -151,6 +140,6 @@ public void setStrategy(int strategy)
if ((strategy < 0) || (strategy > Encoder.Mode.values().length))
throw new IllegalArgumentException("Unsupported brotli strategy mode: " + strategy);

mode = Encoder.Mode.of(strategy);
this.strategy = strategy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,27 @@
// ========================================================================
//

package org.eclipse.jetty.compression.brotli;
package org.eclipse.jetty.compression.brotli.internal;

import java.io.IOException;
import java.nio.ByteBuffer;

import com.aayushatharva.brotli4j.decoder.DecoderJNI;
import org.eclipse.jetty.compression.DecoderSource;
import org.eclipse.jetty.compression.brotli.BrotliDecoderConfig;
import org.eclipse.jetty.io.Content;
import org.eclipse.jetty.util.BufferUtil;

public class BrotliDecoderSource extends DecoderSource
{
private static final ByteBuffer EMPTY_BUFFER = BufferUtil.EMPTY_BUFFER;
private final BrotliCompression compression;
private final DecoderJNI.Wrapper decoder;

public BrotliDecoderSource(BrotliCompression compression, Content.Source source, BrotliDecoderConfig config)
public BrotliDecoderSource(Content.Source source, BrotliDecoderConfig config)
{
super(source);
this.compression = compression;
try
{
this.decoder = new DecoderJNI.Wrapper(compression.getBufferSize());
this.decoder = new DecoderJNI.Wrapper(config.getBufferSize());
}
catch (IOException e)
{
Expand All @@ -58,10 +56,7 @@ protected Content.Chunk nextChunk(Content.Chunk readChunk) throws IOException
{
return last ? Content.Chunk.EOF : Content.Chunk.EMPTY;
}
case OK ->
{
decoder.push(0);
}
case OK -> decoder.push(0);
case NEEDS_MORE_INPUT ->
{
ByteBuffer input = decoder.getInputBuffer();
Expand All @@ -81,10 +76,7 @@ protected Content.Chunk nextChunk(Content.Chunk readChunk) throws IOException
// rely on status.OK to go to EOF
return Content.Chunk.from(output, false);
}
default ->
{
throw new IOException("Decoder failure: Corrupted input buffer");
}
default -> throw new IOException("Decoder failure: Corrupted input buffer");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
// ========================================================================
//

package org.eclipse.jetty.compression.brotli;
package org.eclipse.jetty.compression.brotli.internal;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicReference;

import com.aayushatharva.brotli4j.encoder.Encoder;
import com.aayushatharva.brotli4j.encoder.EncoderJNI;
import org.eclipse.jetty.compression.EncoderSink;
import org.eclipse.jetty.compression.brotli.BrotliEncoderConfig;
import org.eclipse.jetty.io.Content;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BrotliEncoderSink extends EncoderSink
{
Expand All @@ -44,23 +44,21 @@ enum State
/**
* Finish operation completed.
*/
FINISHED;
FINISHED
}

private static final Logger LOG = LoggerFactory.getLogger(BrotliEncoderSink.class);
private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0);
private final BrotliCompression compression;
private final EncoderJNI.Wrapper encoder;
private final ByteBuffer inputBuffer;
private final AtomicReference<State> state = new AtomicReference<State>(State.PROCESSING);
private final AtomicReference<State> state = new AtomicReference<>(State.PROCESSING);

public BrotliEncoderSink(BrotliCompression compression, Content.Sink sink, BrotliEncoderConfig config)
public BrotliEncoderSink(Content.Sink sink, BrotliEncoderConfig config)
{
super(sink);
this.compression = compression;
try
{
this.encoder = new EncoderJNI.Wrapper(config.getBufferSize(), config.getCompressionLevel(), config.getLgWindow(), config.getMode());
Encoder.Mode mode = Encoder.Mode.of(config.getStrategy());
this.encoder = new EncoderJNI.Wrapper(config.getBufferSize(), config.getCompressionLevel(), config.getLgWindow(), mode);
this.inputBuffer = encoder.getInputBuffer();
}
catch (IOException e)
Expand Down Expand Up @@ -94,7 +92,7 @@ protected WriteRecord encode(boolean last, ByteBuffer content)
}

// the only place the input buffer gets set.
int len = BufferUtil.put(content, inputBuffer);
BufferUtil.put(content, inputBuffer);
// do not flip input buffer, that's not what Brotli4j expects/wants.
}
// content is fully consumed.
Expand Down Expand Up @@ -174,9 +172,4 @@ protected void release()
{
this.encoder.destroy();
}

private State getState()
{
return state.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

module org.eclipse.jetty.compression.gzip
{
requires transitive org.eclipse.jetty.compression;
requires org.slf4j;

exports org.eclipse.jetty.compression.gzip;
}
Loading
Loading