Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -13,20 +13,26 @@
*
* Copyright 2010–2011 ApexIdentity Inc.
* Portions Copyright 2011-2015 ForgeRock AS.
* Portions Copyright 2026 3A Systems, LLC.
*/

package org.forgerock.http.decoder.internal;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.util.Objects;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
import java.util.zip.ZipException;

import org.forgerock.http.decoder.Decoder;

/**
* Decodes an entity encoded with <strong>{@code deflate}</strong> encoding. See
* in <a href="http://www.ietf.org/rfc/rfc2616.txt">RFC 2616</a> §3.5 for more information.
* <a href="https://www.rfc-editor.org/rfc/rfc9110.html#section-8.4.1.2">RFC 9110 §8.4.1.2</a>.
* Accepts the zlib format and, for compatibility, raw DEFLATE when no zlib header is present.
*/
public class DeflateDecoder implements Decoder {

Expand All @@ -37,6 +43,111 @@ public String getKey() {

@Override
public InputStream decode(InputStream in) throws IOException {
return new InflaterInputStream(in, new Inflater(true));
Objects.requireNonNull(in);
return new InputStream() {
private InputStream decoded;
private IOException failure;
private boolean closed;

private void checkOpen() throws IOException {
if (closed) {
throw new IOException("Stream closed");
}
}

private InputStream decoded() throws IOException {
checkOpen();
if (failure != null) {
throw failure;
}
if (decoded == null) {
try {
decoded = decodeStream(in);
} catch (IOException e) {
// The header may already have been consumed. Never retry at a new offset.
failure = e;
throw e;
} catch (RuntimeException | Error e) {
failure = new IOException("Deflate header detection failed", e);
throw e;
}
}
return decoded;
}

@Override
public int read() throws IOException {
return decoded().read();
}

@Override
public int read(byte[] bytes, int offset, int length) throws IOException {
checkOpen();
Objects.checkFromIndexSize(offset, length, bytes.length);
return length == 0 ? 0 : decoded().read(bytes, offset, length);
}

@Override
public long skip(long count) throws IOException {
checkOpen();
if (count < 0) {
throw new IllegalArgumentException("negative skip length");
}
return count == 0 ? 0 : decoded().skip(count);
}

@Override
public int available() throws IOException {
checkOpen();
// Sniffing the header here could block while waiting for data.
return decoded == null ? 0 : decoded.available();
}

@Override
public void close() throws IOException {
if (!closed) {
closed = true;
if (decoded == null) {
in.close();
} else {
decoded.close();
}
}
}
};
}

private InputStream decodeStream(InputStream in) throws IOException {
PushbackInputStream input = new PushbackInputStream(in, 2);
int cmf = input.read();
int flg = input.read();
if (flg == -1) {
throw new EOFException("Truncated deflate header");
}
input.unread(flg);
input.unread(cmf);

// RFC 1950: DEFLATE compression, a window of at most 32 KiB, and valid FCHECK bits.
boolean zlib = (cmf & 0x0f) == 8 && (cmf >>> 4) <= 7 && ((cmf << 8) | flg) % 31 == 0;
if (zlib && (flg & 0x20) != 0) {
throw new ZipException("Preset dictionaries are not supported for deflate content encoding");
}
// Do not retry corrupt zlib data as raw DEFLATE: preserve checksum and truncation errors.
return new InflaterInputStream(input, newInflater(!zlib)) {
@Override
public void close() throws IOException {
try {
super.close();
} finally {
// InflaterInputStream does not end an explicitly supplied Inflater.
inf.end();
}
}
};
}

// The decoded stream owns the inflater and must end it even if closing the source fails.
Inflater newInflater(boolean raw) {
return new Inflater(raw);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*
* Copyright 2010–2011 ApexIdentity Inc.
* Portions Copyright 2011-2015 ForgeRock AS.
* Portions Copyright 2026 3A Systems, LLC.
*/

package org.forgerock.http.header;
Expand Down Expand Up @@ -86,6 +87,11 @@ public static ContentEncodingHeader valueOf(final String string) {
/**
* Returns an input stream that decodes the specified input stream, given
* the content-codings that are specified in the {@code codings} list.
* The caller must close the returned stream, which also closes {@code in}.
* If constructing the decoder chain fails after a wrapper has been created,
* the wrappers and {@code in} are closed. If no wrapper has been created,
* {@code in} remains the caller's responsibility. Failures during cleanup
* are suppressed on the original exception.
*
* @param in
* the input stream to decode.
Expand All @@ -96,16 +102,30 @@ public static ContentEncodingHeader valueOf(final String string) {
* if an unsupported content-encoding is specified.
*/
public InputStream decode(InputStream in) throws IOException {
// decode in the reverse order that encoding was applied
for (ListIterator<String> i = codings.listIterator(codings.size()); i.hasPrevious();) {
String name = i.previous();
Decoder decoder = Decoder.SERVICES.get(name);
if (decoder == null) {
throw new UnsupportedEncodingException(name);
InputStream decoded = in;
try {
// decode in the reverse order that encoding was applied
for (ListIterator<String> i = codings.listIterator(codings.size()); i.hasPrevious();) {
String name = i.previous();
Decoder decoder = Decoder.SERVICES.get(name);
if (decoder == null) {
throw new UnsupportedEncodingException(name);
}
decoded = decoder.decode(decoded);
}
in = decoder.decode(in);
return decoded;
} catch (IOException | RuntimeException e) {
if (decoded != in) {
try {
decoded.close();
} catch (IOException | RuntimeException closeFailure) {
if (closeFailure != e) {
e.addSuppressed(closeFailure);
}
}
}
throw e;
}
return in;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* information: "Portions Copyright [year] [name of copyright owner]".
*
* Copyright 2014-2016 ForgeRock AS.
* Portions Copyright 2026 3A Systems, LLC.
*/

package org.forgerock.http.protocol;
Expand Down Expand Up @@ -151,6 +152,13 @@ public void close() {
* entity. This method does not push or pop branches. It does, however,
* decode the content according to the {@code Content-Encoding} header if it
* is present in the message.
* If constructing the decoder chain fails after a wrapper has been created,
* {@code close()} is called on the current stream. Depending on the stream
* implementation, this may make it and its child branches unreadable. Content
* set with {@link #setBytes(byte[])} or {@link #setString(String)} is not closed,
* but its position may have advanced. Call {@link #push()} before copying
* and {@link #pop()} in a {@code finally}
* block to preserve the previous state on failure.
*
* @param out
* The destination writer.
Expand All @@ -168,6 +176,13 @@ public void copyDecodedContentTo(final OutputStream out) throws IOException {
* entity. This method does not push or pop branches. It does, however,
* decode the content according to the {@code Content-Encoding} and
* {@code Content-Type} headers if they are present in the message.
* If constructing the decoder chain fails after a wrapper has been created,
* {@code close()} is called on the current stream. Depending on the stream
* implementation, this may make it and its child branches unreadable. Content
* set with {@link #setBytes(byte[])} or {@link #setString(String)} is not closed,
* but its position may have advanced. Call {@link #push()} before copying
* and {@link #pop()} in a {@code finally}
* block to preserve the previous state on failure.
*
* @param out
* The destination writer.
Expand Down
Loading
Loading