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
22 changes: 2 additions & 20 deletions zap/src/main/java/org/parosproxy/paros/network/HttpMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@
import org.zaproxy.zap.extension.httppanel.Message;
import org.zaproxy.zap.extension.httpsessions.HttpSession;
import org.zaproxy.zap.model.NameValuePair;
import org.zaproxy.zap.network.HttpEncoding;
import org.zaproxy.zap.network.HttpEncodingDeflate;
import org.zaproxy.zap.network.HttpEncodingGzip;
import org.zaproxy.zap.network.HttpRequestBody;
import org.zaproxy.zap.network.HttpResponseBody;
import org.zaproxy.zap.users.User;
Expand All @@ -112,22 +109,7 @@ public class HttpMessage implements Message {
public static final String MESSAGE_TYPE = "HTTP";

private static final HttpEncodingsHandler DEFAULT_CONTENT_ENCODINGS_HANDLER =
(header, body) -> {
String encoding = header.getHeader(HttpHeader.CONTENT_ENCODING);
if (encoding == null || encoding.isEmpty()) {
body.setContentEncodings(List.of());
return;
}

List<HttpEncoding> encodings = List.of();
if (encoding.contains(HttpHeader.DEFLATE)) {
encodings = List.of(HttpEncodingDeflate.getSingleton());
} else if (encoding.contains(HttpHeader.GZIP)) {
encodings = List.of(HttpEncodingGzip.getSingleton());
}

body.setContentEncodings(encodings);
};
(header, body) -> body.setContentEncodings(List.of());

private static final CharsetProvider DEFAULT_CHARSET_PROVIDER =
(header, body) -> header.getCharset();
Expand Down Expand Up @@ -454,7 +436,7 @@ public void setResponseHeader(String resHeader) throws HttpMalformedHeaderExcept
/**
* Sets the content encodings defined in the header into the body.
*
* <p><strong>Note:</strong> By default supports only {@code gzip} and {@code deflate}.
* <p><strong>Note:</strong> Default content encodings are provided by an add-on.
*
* @param header the header.
* @param body the body.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
import java.io.InputStream;
import java.io.OutputStream;

/**
* @deprecated (2.18.0) No longer in use, it will be removed in a future release.
*/
@Deprecated(since = "2.18.0", forRemoval = true)
abstract class AbstractStreamHttpEncoding implements HttpEncoding {

private static final int BUFFER_SIZE = 2048;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
* The {@link HttpEncoding} for the {@code deflate} coding.
*
* @since 2.10.0
* @deprecated (2.18.0) No longer in use, it will be removed in a future release.
*/
@SuppressWarnings("removal")
@Deprecated(since = "2.18.0", forRemoval = true)
public class HttpEncodingDeflate extends AbstractStreamHttpEncoding {

private static final HttpEncodingDeflate SINGLETON = new HttpEncodingDeflate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
* The {@link HttpEncoding} for the {@code gzip}/{@code x-gzip} coding.
*
* @since 2.10.0
* @deprecated (2.18.0) No longer in use, it will be removed in a future release.
*/
@SuppressWarnings("removal")
@Deprecated(since = "2.18.0", forRemoval = true)
public class HttpEncodingGzip extends AbstractStreamHttpEncoding {

private static final HttpEncodingGzip SINGLETON = new HttpEncodingGzip();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
package org.parosproxy.paros.network;

import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
Expand All @@ -35,12 +34,9 @@
import java.io.InputStream;
import java.net.Socket;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
import org.zaproxy.zap.network.HttpEncodingDeflate;
import org.zaproxy.zap.network.HttpEncodingGzip;
import org.zaproxy.zap.network.HttpRequestBody;

/** Unit test for {@link HttpInputStream}. */
Expand Down Expand Up @@ -81,34 +77,18 @@ void shouldReadRequestBodyWithUnknownLength(int length) throws Exception {
assertThat(httpBody.getBytes(), is(equalTo(data)));
}

@Test
void shouldSetGzipEncodingToBody() throws Exception {
// Given
HttpHeader httpHeader = mock(HttpHeader.class);
given(httpHeader.getContentLength()).willReturn(0);
given(httpHeader.getHeader(HttpHeader.CONTENT_ENCODING)).willReturn(HttpHeader.GZIP);
HttpInputStream httpInputStream = new HttpInputStream(createSocket(EMPTY_BODY));
// When
HttpRequestBody httpBody = httpInputStream.readRequestBody(httpHeader);
// Then
assertThat(
httpBody.getContentEncodings(),
is(equalTo(asList(HttpEncodingGzip.getSingleton()))));
}

@Test
void shouldSetDeflateEncodingToBody() throws Exception {
@ParameterizedTest
@ValueSource(strings = {HttpHeader.GZIP, HttpHeader.DEFLATE, "Encoding Not Supported"})
void shouldNotSetContentEncodingToBodyByDefault(String contentEncoding) throws Exception {
// Given
HttpHeader httpHeader = mock(HttpHeader.class);
given(httpHeader.getContentLength()).willReturn(0);
given(httpHeader.getHeader(HttpHeader.CONTENT_ENCODING)).willReturn(HttpHeader.DEFLATE);
given(httpHeader.getHeader(HttpHeader.CONTENT_ENCODING)).willReturn(contentEncoding);
HttpInputStream httpInputStream = new HttpInputStream(createSocket(EMPTY_BODY));
// When
HttpRequestBody httpBody = httpInputStream.readRequestBody(httpHeader);
// Then
assertThat(
httpBody.getContentEncodings(),
is(equalTo(asList(HttpEncodingDeflate.getSingleton()))));
assertThat(httpBody.getContentEncodings(), is(empty()));
}

@ParameterizedTest
Expand All @@ -126,20 +106,6 @@ void shouldNotSetContentEncodingToBodyIfContentEncodingIsNotPresentOrIsEmpty(
assertThat(httpBody.getContentEncodings(), is(empty()));
}

@Test
void shouldNotSetContentEncodingToBodyIfContentEncodingNotSupported() throws Exception {
// Given
HttpHeader httpHeader = mock(HttpHeader.class);
given(httpHeader.getContentLength()).willReturn(0);
given(httpHeader.getHeader(HttpHeader.CONTENT_ENCODING))
.willReturn("Encoding Not Supported");
HttpInputStream httpInputStream = new HttpInputStream(createSocket(EMPTY_BODY));
// When
HttpRequestBody httpBody = httpInputStream.readRequestBody(httpHeader);
// Then
assertThat(httpBody.getContentEncodings(), is(empty()));
}

private static Socket createSocket(byte[] data) throws IOException {
InputStream inputStream = new ByteArrayInputStream(data);
Socket socket = mock(Socket.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
*/
package org.parosproxy.paros.network;

import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -58,8 +56,6 @@
import org.parosproxy.paros.network.HttpMessage.HttpEncodingsHandler;
import org.zaproxy.zap.extension.httpsessions.HttpSession;
import org.zaproxy.zap.network.HttpEncoding;
import org.zaproxy.zap.network.HttpEncodingDeflate;
import org.zaproxy.zap.network.HttpEncodingGzip;
import org.zaproxy.zap.network.HttpRequestBody;
import org.zaproxy.zap.network.HttpResponseBody;
import org.zaproxy.zap.testutils.Log4jTestAppender;
Expand Down Expand Up @@ -369,27 +365,31 @@ void shouldRemoveContentTypeWhenMutatingMethodFromPost(String method) throws Exc
@Test
void shouldSetContentEncodingsWhenSettingRequestBodyByte() {
// Given
HttpEncoding encoding = mock(HttpEncoding.class);
HttpMessage.setContentEncodingsHandler(gzipHandler(encoding));
HttpRequestHeader header = mock(HttpRequestHeader.class);
given(header.getHeader(HttpHeader.CONTENT_ENCODING)).willReturn(HttpHeader.GZIP);
HttpRequestBody body = mock(HttpRequestBody.class);
HttpMessage message = new HttpMessage(header, body);
// When
message.setRequestBody(new byte[0]);
// Then
assertThat(encodings(body), is(not(empty())));
assertThat(encodings(body), is(equalTo(List.of(encoding))));
}

@Test
void shouldSetContentEncodingsWhenSettingRequestBodyString() {
// Given
HttpEncoding encoding = mock(HttpEncoding.class);
HttpMessage.setContentEncodingsHandler(gzipHandler(encoding));
HttpRequestHeader header = mock(HttpRequestHeader.class);
given(header.getHeader(HttpHeader.CONTENT_ENCODING)).willReturn(HttpHeader.GZIP);
HttpRequestBody body = mock(HttpRequestBody.class);
HttpMessage message = new HttpMessage(header, body);
// When
message.setRequestBody("Body");
// Then
assertThat(encodings(body), is(not(empty())));
assertThat(encodings(body), is(equalTo(List.of(encoding))));
}

@Test
Expand All @@ -410,6 +410,8 @@ void shouldNotSetContentEncodingsWhenSettingHttpResponseBody() {
@Test
void shouldSetContentEncodingsWhenSettingResponseBodyByte() {
// Given
HttpEncoding encoding = mock(HttpEncoding.class);
HttpMessage.setContentEncodingsHandler(gzipHandler(encoding));
HttpResponseHeader header = mock(HttpResponseHeader.class);
given(header.getHeader(HttpHeader.CONTENT_ENCODING)).willReturn(HttpHeader.GZIP);
HttpResponseBody body = mock(HttpResponseBody.class);
Expand All @@ -419,12 +421,14 @@ void shouldSetContentEncodingsWhenSettingResponseBodyByte() {
// When
message.setResponseBody(new byte[0]);
// Then
assertThat(encodings(body), is(not(empty())));
assertThat(encodings(body), is(equalTo(List.of(encoding))));
}

@Test
void shouldSetContentEncodingsWhenSettingResponseBodyString() {
// Given
HttpEncoding encoding = mock(HttpEncoding.class);
HttpMessage.setContentEncodingsHandler(gzipHandler(encoding));
HttpResponseHeader header = mock(HttpResponseHeader.class);
given(header.getHeader(HttpHeader.CONTENT_ENCODING)).willReturn(HttpHeader.GZIP);
HttpResponseBody body = mock(HttpResponseBody.class);
Expand All @@ -434,32 +438,21 @@ void shouldSetContentEncodingsWhenSettingResponseBodyString() {
// When
message.setResponseBody("Body");
// Then
assertThat(encodings(body), is(not(empty())));
assertThat(encodings(body), is(equalTo(List.of(encoding))));
}

@ParameterizedTest
@ValueSource(strings = {HttpHeader.GZIP, "x-gzip"})
void shouldSetGzipEncodingToBody(String contentEncodingHeader) {
@ValueSource(
strings = {HttpHeader.GZIP, "x-gzip", HttpHeader.DEFLATE, "Encoding Not Supported"})
void shouldNotSetContentEncodingToBodyByDefault(String contentEncodingHeader) {
// Given
HttpHeader header = mock(HttpHeader.class);
given(header.getHeader(HttpHeader.CONTENT_ENCODING)).willReturn(contentEncodingHeader);
HttpBody body = mock(HttpBody.class);
// When
HttpMessage.setContentEncodings(header, body);
// Then
verify(body).setContentEncodings(asList(HttpEncodingGzip.getSingleton()));
}

@Test
void shouldSetDeflateEncodingToBody() {
// Given
HttpHeader header = mock(HttpHeader.class);
given(header.getHeader(HttpHeader.CONTENT_ENCODING)).willReturn(HttpHeader.DEFLATE);
HttpBody body = mock(HttpBody.class);
// When
HttpMessage.setContentEncodings(header, body);
// Then
verify(body).setContentEncodings(asList(HttpEncodingDeflate.getSingleton()));
verify(body).setContentEncodings(List.of());
}

@ParameterizedTest
Expand All @@ -476,18 +469,6 @@ void shouldNotSetContentEncodingToBodyIfContentEncodingIsNotPresentOrIsEmpty(
verify(body).setContentEncodings(Collections.emptyList());
}

@Test
void shouldNotSetContentEncodingToBodyIfContentEncodingNotSupported() {
// Given
HttpHeader header = mock(HttpHeader.class);
given(header.getHeader(HttpHeader.CONTENT_ENCODING)).willReturn("Encoding Not Supported");
HttpBody body = mock(HttpBody.class);
// When
HttpMessage.setContentEncodings(header, body);
// Then
verify(body).setContentEncodings(Collections.emptyList());
}

@Test
void shouldUseContentEncodingsHandlerSet() {
// Given
Expand Down Expand Up @@ -847,6 +828,14 @@ private static List<HttpEncoding> encodings(HttpBody body) {
return arg.getValue();
}

private static HttpEncodingsHandler gzipHandler(HttpEncoding encoding) {
return (header, body) -> {
if (HttpHeader.GZIP.equals(header.getHeader(HttpHeader.CONTENT_ENCODING))) {
body.setContentEncodings(List.of(encoding));
}
};
}

private void withLoggerAppender() {
testAppender = new Log4jTestAppender();
LoggerContext context = LoggerContext.getContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.junit.jupiter.api.Test;

/** Unit test for {@link HttpEncodingDeflate}. */
@SuppressWarnings({"deprecation", "removal"})
class HttpEncodingDeflateUnitTest {

private static final byte[] CONTENT = "Content 123 ABC".getBytes(StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.junit.jupiter.api.Test;

/** Unit test for {@link HttpEncodingGzip}. */
@SuppressWarnings({"deprecation", "removal"})
class HttpEncodingGzipUnitTest {

private static final byte[] CONTENT = "Content 123 ABC".getBytes(StandardCharsets.UTF_8);
Expand Down
Loading