#1902 - Fix link header parsing for multiple links and unquoted attribute values.
Fixed the regular expression to parse link header values to properly consider the comma to end an unquoted attribute value, too. Couple of additional unit tests, too.
This commit is contained in:
@@ -23,7 +23,6 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collector;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -45,7 +44,7 @@ import com.fasterxml.jackson.annotation.JsonValue;
|
||||
public class Links implements Iterable<Link> {
|
||||
|
||||
public static final Links NONE = new Links(Collections.emptyList());
|
||||
private static final Pattern LINK_HEADER_PATTERN = Pattern.compile("(<[^>]*>(;\\s*\\w+=\"?[^\"]*\"?)+)");
|
||||
private static final Pattern LINK_HEADER_PATTERN = Pattern.compile("(<[^>]*>(;\\s*\\w+=\"?[^\",]*\"?)+)");
|
||||
|
||||
private final List<Link> links;
|
||||
|
||||
@@ -91,12 +90,12 @@ public class Links implements Iterable<Link> {
|
||||
return NONE;
|
||||
}
|
||||
|
||||
Matcher matcher = LINK_HEADER_PATTERN.matcher(source);
|
||||
List<Link> links = new ArrayList<>();
|
||||
var links = new ArrayList<Link>();
|
||||
var matcher = LINK_HEADER_PATTERN.matcher(source);
|
||||
|
||||
while (matcher.find()) {
|
||||
|
||||
Link link = Link.valueOf(matcher.group());
|
||||
var link = Link.valueOf(matcher.group());
|
||||
|
||||
if (link != null) {
|
||||
links.add(link);
|
||||
|
||||
@@ -55,10 +55,13 @@ class LinksUnitTest {
|
||||
Link.of("/somethingElse", "bar").withHreflang("de"));
|
||||
|
||||
// #1899
|
||||
static final String FIVE = "</somethingElse>;rel=boo";
|
||||
static final String FIVE = "</somethingElse?foo=one,two>;rel=boo";
|
||||
static final String SIX = "</somethingElse>; rel=bee";
|
||||
static final String LINKS3 = StringUtils.collectionToCommaDelimitedString(Arrays.asList(FIVE, SIX));
|
||||
static final Links reference3 = Links.of(Link.of("/somethingElse", "boo"), Link.of("/somethingElse", "bee"));
|
||||
static final String SEVEN = "</somethingElse>;rel=beeboo;title=sometitle";
|
||||
static final String LINKS3 = StringUtils.collectionToCommaDelimitedString(Arrays.asList(FIVE, SIX, SEVEN));
|
||||
static final Links reference3 = Links.of(Link.of("/somethingElse?foo=one,two", "boo"), //
|
||||
Link.of("/somethingElse", "bee"),
|
||||
Link.of("/somethingElse", "beeboo").withTitle("sometitle"));
|
||||
|
||||
@Test
|
||||
void parsesLinkHeaderLinks() {
|
||||
|
||||
Reference in New Issue
Block a user