Skip to content

Commit

Permalink
Update MediaType parser to handle parameter without value (#3999) (#4000
Browse files Browse the repository at this point in the history
)

* Update MediaType parser to handle parameter without value.

Use consumeTokenIfPresent instead of consumeToken
Update consumeTokenIfPresent to return null instead of an empty string when matcher is not matched
Add unit test

* handle single empty parameter, update test case
  • Loading branch information
romain-grecourt authored Mar 24, 2022
1 parent 85bb1cb commit 853b095
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
34 changes: 19 additions & 15 deletions common/http/src/main/java/io/helidon/common/http/MediaType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2021 Oracle and/or its affiliates.
* Copyright (c) 2018, 2022 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 @@ -325,23 +325,27 @@ public static MediaType parse(String input) {
String attribute = tokenizer.consumeToken(TOKEN_MATCHER);
tokenizer.consumeCharacter('=');
final String value;
if ('"' == tokenizer.previewChar()) {
tokenizer.consumeCharacter('"');
StringBuilder valueBuilder = new StringBuilder();
while ('"' != tokenizer.previewChar()) {
if ('\\' == tokenizer.previewChar()) {
tokenizer.consumeCharacter('\\');
valueBuilder.append(tokenizer.consumeCharacter(CharMatcher.ascii()));
} else {
valueBuilder.append(tokenizer.consumeToken(QUOTED_TEXT_MATCHER));
if (tokenizer.hasMore()) {
if ('"' == tokenizer.previewChar()) {
tokenizer.consumeCharacter('"');
StringBuilder valueBuilder = new StringBuilder();
while ('"' != tokenizer.previewChar()) {
if ('\\' == tokenizer.previewChar()) {
tokenizer.consumeCharacter('\\');
valueBuilder.append(tokenizer.consumeCharacter(CharMatcher.ascii()));
} else {
valueBuilder.append(tokenizer.consumeToken(QUOTED_TEXT_MATCHER));
}
}
value = valueBuilder.toString();
tokenizer.consumeCharacter('"');
} else {
value = tokenizer.consumeTokenIfPresent(TOKEN_MATCHER);
}
if (value != null) {
parameters.put(attribute, value);
}
value = valueBuilder.toString();
tokenizer.consumeCharacter('"');
} else {
value = tokenizer.consumeToken(TOKEN_MATCHER);
}
parameters.put(attribute, value);
}
return create(type, subtype, parameters);
} catch (IllegalStateException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2022 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 @@ -46,15 +46,16 @@ public Tokenizer(String input) {
* position the to next character if matched.
*
* @param matcher matcher to use
* @return token matched
* @return token matched, or {@code null} if not matched
* @throws IllegalStateException if {@link #hasMore() } returns
* {@code false}
*/
public String consumeTokenIfPresent(CharMatcher matcher) {
checkState(hasMore(), "No more elements!");
int startPosition = position;
position = matcher.negate().indexIn(input, startPosition);
return hasMore() ? input.substring(startPosition, position)
return position == startPosition ? null
: hasMore() ? input.substring(startPosition, position)
: input.substring(startPosition);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2022 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 @@ -77,6 +77,12 @@ void parseDuplicateParameters() {
assertThat(mediaType.parameters(), is(Map.of("option", "value2")));
}

@Test
void parseEmptyParameterValue() {
assertThat(MediaType.parse("type/subtype; o1=").parameters(), is(Map.of()));
assertThat(MediaType.parse("type/subtype; o1=; o2=v2").parameters(), is(Map.of("o2", "v2")));
}

@Test
void qualityFactor() {
MediaType mediaType = MediaType.parse("unknown-type/unknown-subtype; q=0.2");
Expand Down Expand Up @@ -127,4 +133,4 @@ void testBuilt() {
assertThat(mediaType.parameters(), is(Map.of("q", "0.1", "charset", "ISO-8859-2")));
assertThat(mediaType.qualityFactor(), closeTo(0.1, 0.000001));
}
}
}

0 comments on commit 853b095

Please sign in to comment.