ok2c commented on code in PR #420: URL: https://github.com/apache/httpcomponents-client/pull/420#discussion_r1131123099
########## httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheControlHeaderParser.java: ########## @@ -0,0 +1,211 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ +package org.apache.hc.client5.http.impl.cache; + +import org.apache.hc.client5.http.cookie.MalformedCookieException; +import org.apache.hc.core5.annotation.Contract; +import org.apache.hc.core5.annotation.Internal; +import org.apache.hc.core5.annotation.ThreadingBehavior; +import org.apache.hc.core5.http.FormattedHeader; +import org.apache.hc.core5.http.Header; +import org.apache.hc.core5.util.Args; +import org.apache.hc.core5.util.CharArrayBuffer; +import org.apache.hc.core5.util.Tokenizer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.BitSet; + + +/** + * A parser for the HTTP Cache-Control header that can be used to extract information about caching directives. + * <p> + * This class is thread-safe and has a singleton instance ({@link #INSTANCE}). + * </p> + * <p> + * The {@link #parse(Header)} method takes an HTTP header and returns a {@link CacheControlHeader} object containing + * the relevant caching directives. The header can be either a {@link FormattedHeader} object, which contains a + * pre-parsed {@link CharArrayBuffer}, or a plain {@link Header} object, in which case the value will be parsed and + * stored in a new {@link CharArrayBuffer}. + * </p> + * <p> + * This parser only supports two directives: "max-age" and "s-maxage". If either of these directives are present in the + * header, their values will be parsed and stored in the {@link CacheControlHeader} object. If both directives are + * present, the value of "s-maxage" takes precedence. + * </p> + * <p> + * Example usage: + * </p> + * <pre> + * Header header = new BasicHeader("Cache-Control", "max-age=60, s-maxage=120"); + * CacheControlHeaderParser parser = CacheControlHeaderParser.INSTANCE; + * CacheControlHeader cacheControlHeader = parser.parse(header); + * long maxAgeSeconds = cacheControlHeader.getMaxAgeSeconds(); // returns 120 + * </pre> + */ +@Internal +@Contract(threading = ThreadingBehavior.SAFE) +class CacheControlHeaderParser { + + /** + * The character used to indicate a parameter's value in the Cache-Control header. + */ + private final static char EQUAL_CHAR = '='; + + /** + * The singleton instance of this parser. + */ + public static final CacheControlHeaderParser INSTANCE = new CacheControlHeaderParser(); + + /** + * The logger for this class. + */ + private static final Logger LOG = LoggerFactory.getLogger(CacheControlHeaderParser.class); + + /** + * The delimiter character used to separate caching directives in the header. + */ + private static final char PARAM_DELIMITER = ';'; + + /** + * The delimiter character used to separate values within a caching directive. + */ + private static final char VALUE_DELIMITER = '='; + + /** + * The set of characters that can delimit a token in the header. + */ + private static final BitSet TOKEN_DELIMS = Tokenizer.INIT_BITSET(VALUE_DELIMITER, PARAM_DELIMITER, ','); + + /** + * The set of characters that can delimit a value in the header. + */ + private static final BitSet VALUE_DELIMS = Tokenizer.INIT_BITSET(VALUE_DELIMITER, PARAM_DELIMITER, ','); + + /** + * The token parser used to extract values from the header. + */ + private final Tokenizer tokenParser; + + /** + * Constructs a new instance of this parser. + */ + protected CacheControlHeaderParser() { + super(); + this.tokenParser = Tokenizer.INSTANCE; + } + + /** + * Parses the specified header and returns a {@link CacheControlHeader} object containing the relevant caching + * directives. + * + * @param header the header to parse + * @return a {@link CacheControlHeader} object containing the relevant caching directives + * @throws IllegalArgumentException if the header is {@code null} + * @throws MalformedCookieException if the value of the "max-age" or "s-maxage" field is malformed + */ + public final CacheControlHeader parse(final Header header) throws MalformedCookieException { + Args.notNull(header, "Header"); + + final CacheControlHeader cacheControlHeader = new CacheControlHeader(); + final CharArrayBuffer buffer; + final Tokenizer.Cursor cursor; + if (header instanceof FormattedHeader) { + buffer = ((FormattedHeader) header).getBuffer(); + cursor = new Tokenizer.Cursor(((FormattedHeader) header).getValuePos(), buffer.length()); + } else { + final String s = header.getValue(); + if (s == null) { + if (LOG.isWarnEnabled()) { Review Comment: @arturobernalg No need for a warning here. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
