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

Issue #6774 - WebClient should have a mode that is resilient to bad media/content types #6999

Merged
merged 4 commits into from
Jun 16, 2023
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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@

import io.helidon.common.http.Http.DateTime;
import io.helidon.common.http.Http.HeaderValue;
import io.helidon.common.media.type.ParserMode;

import static io.helidon.common.http.Http.Header.ACCEPT_PATCH;
import static io.helidon.common.http.Http.Header.EXPIRES;
Expand All @@ -36,12 +37,24 @@
public interface ClientResponseHeaders extends Headers {
/**
* Create a new instance from headers parsed from client response.
* Strict media type parsing mode is used for {@code Content-Type} header.
*
* @param responseHeaders client response headers
* @return immutable instance of client response HTTP headers
*/
static ClientResponseHeaders create(Headers responseHeaders) {
return new ClientResponseHeadersImpl(responseHeaders);
return new ClientResponseHeadersImpl(responseHeaders, ParserMode.STRICT);
}

/**
* Create a new instance from headers parsed from client response.
*
* @param responseHeaders client response headers
* @param parserMode media type parsing mode
* @return immutable instance of client response HTTP headers
*/
static ClientResponseHeaders create(Headers responseHeaders, ParserMode parserMode) {
return new ClientResponseHeadersImpl(responseHeaders, parserMode);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,13 +18,18 @@

import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

import io.helidon.common.media.type.ParserMode;

class ClientResponseHeadersImpl implements ClientResponseHeaders {
private final Headers headers;
private final ParserMode parserMode;

ClientResponseHeadersImpl(Headers headers) {
ClientResponseHeadersImpl(Headers headers, ParserMode parserMode) {
this.headers = headers;
this.parserMode = parserMode;
}

@Override
Expand All @@ -47,6 +52,16 @@ public Http.HeaderValue get(Http.HeaderName name) {
return headers.get(name);
}

@Override
public Optional<HttpMediaType> contentType() {
if (parserMode == ParserMode.RELAXED) {
return contains(HeaderEnum.CONTENT_TYPE)
? Optional.of(HttpMediaType.create(get(HeaderEnum.CONTENT_TYPE).value(), parserMode))
: Optional.empty();
}
return headers.contentType();
}

@Override
public int size() {
return headers.size();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2023, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@

import io.helidon.common.media.type.MediaType;
import io.helidon.common.media.type.MediaTypes;
import io.helidon.common.media.type.ParserMode;

/**
* Media type used in HTTP headers, in addition to the media type definition, these may contain additional
Expand Down Expand Up @@ -106,12 +107,24 @@ static HttpMediaType create(MediaType mediaType) {

/**
* Parse media type from the provided string.
* Strict media type parsing mode is used.
*
* @param mediaTypeString media type string
* @return HTTP media type parsed from the string
*/
static HttpMediaType create(String mediaTypeString) {
return Builder.parse(mediaTypeString);
return Builder.parse(mediaTypeString, ParserMode.STRICT);
}

/**
* Parse media type from the provided string.
*
* @param mediaTypeString media type string
* @param parserMode media type parsing mode
* @return HTTP media type parsed from the string
*/
static HttpMediaType create(String mediaTypeString, ParserMode parserMode) {
return Builder.parse(mediaTypeString, parserMode);
}

/**
Expand Down Expand Up @@ -310,7 +323,7 @@ MediaType mediaType() {
return mediaType;
}

private static HttpMediaType parse(String mediaTypeString) {
private static HttpMediaType parse(String mediaTypeString, ParserMode parserMode) {
// text/plain; charset=UTF-8

Builder b = builder();
Expand All @@ -337,7 +350,7 @@ private static HttpMediaType parse(String mediaTypeString) {
}
}
} else {
b.mediaType(MediaTypes.create(mediaTypeString));
b.mediaType(MediaTypes.create(mediaTypeString, parserMode));
}
return b.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2022 Oracle and/or its affiliates.
* Copyright (c) 2018, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@

import io.helidon.common.media.type.MediaType;
import io.helidon.common.media.type.MediaTypes;
import io.helidon.common.media.type.ParserMode;

import org.junit.jupiter.api.Test;

Expand All @@ -30,6 +31,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsIterableWithSize.iterableWithSize;
import static org.hamcrest.number.IsCloseTo.closeTo;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* Unit test for {@link MediaType}.
Expand Down Expand Up @@ -134,4 +136,21 @@ void testBuilt() {
assertThat(mediaType.parameters(), is(Map.of("q", "0.1", "charset", "ISO-8859-2")));
assertThat(mediaType.qualityFactor(), closeTo(0.1, 0.000001));
}

// Calling create method with "text" argument shall throw IllegalArgumentException in strict mode.
@Test
void parseInvalidTextInStrictMode() {
assertThrows(IllegalArgumentException.class, () -> {
Tomas-Kraus marked this conversation as resolved.
Show resolved Hide resolved
HttpMediaType.create("text");
},
"Cannot parse media type: text");
}

// Calling create method with "text" argument shall return "text/plain" in relaxed mode.
@Test
void parseInvalidTextInRelaxedMode() {
HttpMediaType type = HttpMediaType.create("text", ParserMode.RELAXED);
assertThat(type.text(), is("text/plain"));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,10 +16,25 @@

package io.helidon.common.media.type;

import java.util.Optional;

record MediaTypeImpl(String type, String subtype, String text) implements MediaType {
static MediaType parse(String fullType) {

private static final System.Logger LOGGER = System.getLogger(MediaTypeImpl.class.getName());

static MediaType parse(String fullType, ParserMode parserMode) {
int slashIndex = fullType.indexOf('/');
if (slashIndex < 1) {
if (parserMode == ParserMode.RELAXED) {
Optional<MediaType> maybeRelaxedType = ParserMode.findRelaxedMediaType(fullType);
if (maybeRelaxedType.isPresent()) {
LOGGER.log(System.Logger.Level.DEBUG,
() -> String.format("Invalid media type value \"%s\" replaced with \"%s\"",
fullType,
maybeRelaxedType.get().text()));
return maybeRelaxedType.get();
}
}
throw new IllegalArgumentException("Cannot parse media type: " + fullType);
}
return new MediaTypeImpl(fullType.substring(0, slashIndex),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022 Oracle and/or its affiliates.
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -151,13 +151,26 @@ public static MediaType create(String type, String subtype) {

/**
* Create a new media type from the full media type string.
* Strict media type parsing mode is used.
*
* @param fullType media type string, such as {@code application/json}
* @return media type for the string
*/
public static MediaType create(String fullType) {
MediaTypeEnum types = MediaTypeEnum.find(fullType);
return types == null ? MediaTypeImpl.parse(fullType) : types;
return types == null ? MediaTypeImpl.parse(fullType, ParserMode.STRICT) : types;
}

/**
* Create a new media type from the full media type string.
*
* @param fullType media type string, such as {@code application/json}
* @param parserMode media type parsing mode
* @return media type for the string
*/
public static MediaType create(String fullType, ParserMode parserMode) {
MediaTypeEnum types = MediaTypeEnum.find(fullType);
return types == null ? MediaTypeImpl.parse(fullType, parserMode) : types;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.common.media.type;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
* Media type parsing mode.
*/
public enum ParserMode {

/**
* Strict mode (default).
* Media type must match known name.
*/
STRICT,
/**
* Relaxed mode.
* Apply additional rules to identify unknown media types.
*/
RELAXED;

// Relaxed media types mapping
private static final Map<String, MediaType> RELAXED_TYPES = Map.of(
"text", MediaTypes.TEXT_PLAIN // text -> text/plain
);

/**
* Find relaxed media type mapping for provided value.
*
* @param value source media type value
* @return mapped media type value or {@code Optional.empty()}
* when no mapping for given value exists
*/
static Optional<MediaType> findRelaxedMediaType(String value) {
Objects.requireNonNull(value);
MediaType relaxedValue = RELAXED_TYPES.get(value);
return relaxedValue != null ? Optional.of(relaxedValue) : Optional.empty();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
5 changes: 5 additions & 0 deletions nima/tests/integration/webclient/webclient/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<groupId>io.helidon.nima.webclient</groupId>
<artifactId>helidon-nima-webclient</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.nima.webserver</groupId>
<artifactId>helidon-nima-webserver</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.nima.testing.junit5</groupId>
<artifactId>helidon-nima-testing-junit5-webserver</artifactId>
Expand Down
Loading