Skip to content
Open
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 @@ -18,6 +18,7 @@
package org.apache.hadoop.ozone.s3;

import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.NoSuchElementException;
import javax.servlet.Filter;
Expand Down Expand Up @@ -56,6 +57,9 @@ public String getHeader(String name) {
if (name.equalsIgnoreCase("Content-Type")) {
return null;
}
if (name.equalsIgnoreCase(HeaderPreprocessor.ORIGINAL_CONTENT_TYPE)) {
return "";
}
return super.getHeader(name);
}

Expand All @@ -64,6 +68,9 @@ public Enumeration<String> getHeaders(String name) {
if ("Content-Type".equalsIgnoreCase(name)) {
return null;
}
if (HeaderPreprocessor.ORIGINAL_CONTENT_TYPE.equalsIgnoreCase(name)) {
return Collections.enumeration(Collections.singletonList(""));
}
return super.getHeaders(name);
}

Expand All @@ -84,33 +91,36 @@ public void destroy() {
}

/**
* Enumeration Wrapper which removes Content-Type from the original
* enumeration.
* Enumeration Wrapper which replaces Content-Type with the internal header
* used to preserve its original value.
*/
public static class EnumerationWrapper implements Enumeration<String> {

private final Enumeration<String> original;

private String nextElement;

private boolean contentTypeReplaced;

public EnumerationWrapper(Enumeration<String> original) {
this.original = original;
step();
}

private void step() {
if (original.hasMoreElements()) {
while (original.hasMoreElements()) {
nextElement = original.nextElement();
} else {
nextElement = null;
}
if ("Content-Type".equalsIgnoreCase(nextElement)) {
if (original.hasMoreElements()) {
nextElement = original.nextElement();
if ("Content-Type".equalsIgnoreCase(nextElement)) {
if (!contentTypeReplaced) {
nextElement = HeaderPreprocessor.ORIGINAL_CONTENT_TYPE;
contentTypeReplaced = true;
return;
}
} else {
nextElement = null;
return;
}
}
nextElement = null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Collections;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicReference;
import javax.servlet.http.HttpServletRequest;
import org.apache.hadoop.ozone.s3.EmptyContentTypeFilter.EnumerationWrapper;
import org.junit.jupiter.api.Test;

Expand All @@ -42,6 +48,9 @@ public void enumerationWithContentType() {
new EnumerationWrapper(values.elements());

assertTrue(enumerationWrapper.hasMoreElements());
assertEquals(HeaderPreprocessor.ORIGINAL_CONTENT_TYPE,
enumerationWrapper.nextElement());
assertTrue(enumerationWrapper.hasMoreElements());
assertEquals("1", enumerationWrapper.nextElement());
assertTrue(enumerationWrapper.hasMoreElements());
assertEquals("2", enumerationWrapper.nextElement());
Expand All @@ -56,7 +65,33 @@ public void enumerationWithOneContentType() {
final EnumerationWrapper enumerationWrapper =
new EnumerationWrapper(values.elements());

assertTrue(enumerationWrapper.hasMoreElements());
assertEquals(HeaderPreprocessor.ORIGINAL_CONTENT_TYPE,
enumerationWrapper.nextElement());
assertFalse(enumerationWrapper.hasMoreElements());
}

@Test
public void preserveEmptyContentType() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getContentType()).thenReturn("");
when(request.getHeaderNames()).thenReturn(
Collections.enumeration(Collections.singletonList(HeaderPreprocessor.CONTENT_TYPE)));

AtomicReference<HttpServletRequest> wrappedRequest =
new AtomicReference<>();
new EmptyContentTypeFilter().doFilter(request, null,
(filteredRequest, response) -> wrappedRequest.set(
(HttpServletRequest) filteredRequest));

assertNull(wrappedRequest.get().getContentType());
assertNull(wrappedRequest.get().getHeader(HeaderPreprocessor.CONTENT_TYPE));
assertEquals("", wrappedRequest.get().getHeader(HeaderPreprocessor.ORIGINAL_CONTENT_TYPE));
assertEquals(Collections.singletonList(""),
Collections.list(wrappedRequest.get().getHeaders(HeaderPreprocessor.ORIGINAL_CONTENT_TYPE)));
assertEquals(Collections.singletonList(
HeaderPreprocessor.ORIGINAL_CONTENT_TYPE), Collections.list(
wrappedRequest.get().getHeaderNames()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
import static org.apache.hadoop.ozone.s3.endpoint.EndpointTestUtils.assertErrorResponse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;
import org.apache.hadoop.ozone.s3.HeaderPreprocessor;
import org.apache.hadoop.ozone.s3.exception.S3ErrorTable;
import org.junit.jupiter.api.Test;

Expand All @@ -46,6 +48,18 @@ public void testLowerCaseHeaderMapRemovesKeysCaseInsensitively() {
assertFalse(headers.containsKey("authorization"));
}

@Test
public void testRestoreEmptyContentType() {
MultivaluedMap<String, String> rawHeaders = new MultivaluedHashMap<>();
rawHeaders.putSingle(HeaderPreprocessor.ORIGINAL_CONTENT_TYPE, "");

AWSSignatureProcessor.LowerCaseKeyStringMap headers =
AWSSignatureProcessor.LowerCaseKeyStringMap.fromHeaderMap(rawHeaders);

assertTrue(headers.containsKey(HeaderPreprocessor.CONTENT_TYPE));
assertEquals("", headers.get(HeaderPreprocessor.CONTENT_TYPE));
}

@Test
public void testOutOfRangeExpiresPreSignedUrlReturns403() throws Exception {
// A pre-signed URL whose X-Amz-Expires is out of range must be rejected
Expand Down
Loading