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

Update MediaType parser to handle parameter without value #3999

Merged
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
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, 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 @@ -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, 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 @@ -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