// Copyright 2016 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "components/link_header_util/link_header_util.h" #include <algorithm> #include "base/strings/string_util.h" #include "net/http/http_util.h" namespace link_header_util { namespace { // A variation of base::StringTokenizer and net::HttpUtil::ValuesIterator. // Takes the parsing of StringTokenizer and adds support for quoted strings that // are quoted by matching <> (and does not support escaping in those strings). // Also has the behavior of ValuesIterator where it strips whitespace from all // values and only outputs non-empty values. // Only supports ',' as separator and supports "" and <> as quote chars. class ValueTokenizer { … }; // Parses the URL part of a Link header. When successful |url_begin| points // to the beginning of the url, |url_end| points to the end of the url and // |params_begin| points to the first character after the '>' character at the // end of the url. bool ExtractURL(std::string::const_iterator begin, std::string::const_iterator end, std::string::const_iterator* url_begin, std::string::const_iterator* url_end, std::string::const_iterator* params_begin) { … } } // namespace std::vector<StringIteratorPair> SplitLinkHeader(const std::string& header) { … } // Parses one link in a link header into its url and parameters. // A link is of the form "<some-url>; param1=value1; param2=value2". // Returns false if parsing the link failed, returns true on success. This // method is more lenient than the RFC. It doesn't fail on things like invalid // characters in the URL, and also doesn't verify that certain parameters should // or shouldn't be quoted strings. // If a parameter occurs more than once in the link, only the first value is // returned in params as this is the required behavior for all attributes chrome // currently cares about in link headers. bool ParseLinkHeaderValue( std::string::const_iterator begin, std::string::const_iterator end, std::string* url, std::unordered_map<std::string, std::optional<std::string>>* params) { … } } // namespace link_header_util