Support methods for parsing a message header into a sequence of tokens

This commit is contained in:
Oleg Kalnichevski 2023-06-18 22:43:02 +02:00
parent 5ba0701535
commit 6faef71798
1 changed files with 40 additions and 0 deletions

View File

@ -28,15 +28,22 @@ package org.apache.hc.client5.http.impl.cache;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.BitSet;
import java.util.function.Consumer;
import org.apache.hc.client5.http.utils.URIUtils; import org.apache.hc.client5.http.utils.URIUtils;
import org.apache.hc.core5.annotation.Internal; import org.apache.hc.core5.annotation.Internal;
import org.apache.hc.core5.http.FormattedHeader;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest; import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.URIScheme; import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.message.ParserCursor;
import org.apache.hc.core5.net.URIAuthority; import org.apache.hc.core5.net.URIAuthority;
import org.apache.hc.core5.net.URIBuilder; import org.apache.hc.core5.net.URIBuilder;
import org.apache.hc.core5.util.Args; import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.CharArrayBuffer;
import org.apache.hc.core5.util.Tokenizer;
/** /**
* HTTP cache support utilities. * HTTP cache support utilities.
@ -130,4 +137,37 @@ public final class CacheSupport {
} }
} }
private static final BitSet COMMA = Tokenizer.INIT_BITSET(',');
// This method should be provided by MessageSupport from core
public static void parseTokens(final CharSequence src, final ParserCursor cursor, final Consumer<String> consumer) {
Args.notNull(src, "Source");
Args.notNull(cursor, "Cursor");
while (!cursor.atEnd()) {
final int pos = cursor.getPos();
if (src.charAt(pos) == ',') {
cursor.updatePos(pos + 1);
}
final String token = Tokenizer.INSTANCE.parseToken(src, cursor, COMMA);
if (consumer != null) {
consumer.accept(token);
}
}
}
// This method should be provided by MessageSupport from core
public static void parseTokens(final Header header, final Consumer<String> consumer) {
Args.notNull(header, "Header");
if (header instanceof FormattedHeader) {
final CharArrayBuffer buf = ((FormattedHeader) header).getBuffer();
final ParserCursor cursor = new ParserCursor(0, buf.length());
cursor.updatePos(((FormattedHeader) header).getValuePos());
parseTokens(buf, cursor, consumer);
} else {
final String value = header.getValue();
final ParserCursor cursor = new ParserCursor(0, value.length());
parseTokens(value, cursor, consumer);
}
}
} }