ok2c commented on code in PR #693: URL: https://github.com/apache/httpcomponents-core/pull/693#discussion_r3940002945
########## httpcore5/src/main/java/org/apache/hc/core5/http/structured/StructuredFieldHeaders.java: ########## @@ -0,0 +1,208 @@ +/* + * ==================================================================== + * 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.core5.http.structured; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Objects; + +import org.apache.hc.core5.http.FormattedHeader; +import org.apache.hc.core5.http.Header; +import org.apache.hc.core5.http.MessageHeaders; +import org.apache.hc.core5.http.ParseException; +import org.apache.hc.core5.http.message.BufferedHeader; +import org.apache.hc.core5.util.Args; +import org.apache.hc.core5.util.CharArrayBuffer; +import org.apache.hc.core5.util.Tokenizer; + +/** + * Integration between Structured Field values and HttpComponents message headers. + * + * @since 5.5 + */ +public final class StructuredFieldHeaders { + + private StructuredFieldHeaders() { + } + + /** + * Parses one header as an Structured Field Item. + * + * @param header the header. + * @return the parsed Item. + * @throws ParseException if the complete field value is invalid. + */ + public static StructuredFieldItem parseItem(final Header header) throws ParseException { + final HeaderInput input = input(header); + return StructuredFieldParser.parseItem(input.value, input.cursor); + } + + /** + * Combines all matching field lines and parses them as an Structured Field Item. + * + * @param headers the message headers. + * @param name the case-insensitive field name. + * @return the parsed Item. + * @throws ParseException if the complete field value is invalid or absent. + */ + public static StructuredFieldItem parseItem(final MessageHeaders headers, final String name) + throws ParseException { + final Iterator<Header> matching = matching(headers, name); + if (!matching.hasNext()) { + return StructuredFieldParser.parseItemLines(Collections.<String>emptyList()); + } + final Header first = matching.next(); + return matching.hasNext() + ? StructuredFieldParser.parseItemLines(values(first, matching)) + : parseItem(first); + } + + /** + * Parses one header as an Structured Field List. + * + * @param header the header. + * @return the parsed List. + * @throws ParseException if the complete field value is invalid. + */ + public static StructuredFieldList parseList(final Header header) throws ParseException { + final HeaderInput input = input(header); + return StructuredFieldParser.parseList(input.value, input.cursor); + } + + /** + * Combines all matching field lines and parses them as an Structured Field List. + * + * @param headers the message headers. + * @param name the case-insensitive field name. + * @return the parsed List, empty when the field is absent. + * @throws ParseException if the complete field value is invalid. + */ + public static StructuredFieldList parseList(final MessageHeaders headers, final String name) + throws ParseException { + final Iterator<Header> matching = matching(headers, name); Review Comment: @arturobernalg This is now even worse than before. You now create an intermediate list of values making a full copy of everything instead of using the iterator and parsing out header values directly from the underlying `CharArrayBuffer`. Do you really need to handle a single value list as a special case? If so, rather create a new iterator. It would likely be massively less wasteful. -- 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]
