按顺序排列的第一个和最后一个迭代器位置



我需要解析http-header字段:

key:valuern
key:valuern

如何将值解析为两个迭代器,指示开始和结束?

我过去曾在libcurl的CURLOPT_HEADERFUNCTION中使用过这个回调:

// userdata points to instance of response
size_t header_callback(void *data, size_t size, size_t nmemb, void *userdata)
{
    auto const length = size*nmemb;
    auto const b = static_cast<char const*>(data);
    auto f = b,
         e = b + length;
    std::string key, value;
    using namespace boost::spirit::qi;
    // 2.2 Basic Rules (rfc1945)
    static const auto& tspecials = " t><@,;:\"/][?=}{:";
    static const rule<char const*, std::string()> token = +~char_(tspecials); // FIXME? should filter CTLs
    auto self = static_cast<webclient::response*>(userdata);
    if (phrase_parse(f, e, token >> ':' >> lexeme[*(char_ - eol)], space, key, value))
    {
        boost::trim(value);
        auto insertion = self->headers.insert({key, value});
        if (!insertion.second)
        {
            // merge list-valued header occurences (see rfc1945 4.2)
            insertion.first->second += ',';
            insertion.first->second.append(value);
        }
    }
    else
    {
        // roll with non seperated headers...
        std::string header(b, e);
        boost::trim(header);
        if (!header.empty())
        {
            auto insertion = self->headers.insert({header, "present"});
            logicErrAssert(insertion.second);
        }
    }
    return length;
}

请注意,header是不区分大小写的映射:

/** http://www.faqs.org/rfcs/rfc1945.html 4.2  Message Headers
 *
 * Field names are case-insensitive.
 * Header fields can be extended over multiple lines by preceding each
 * extra line with at least one SP or HT, though this is not recommended.
 *
 * Multiple HTTP-header fields with the same field-name may be present
 * in a message if and only if the entire field-value for that header
 * field is defined as a comma-separated list [i.e., #(values)]. It
 * must be possible to combine the multiple header fields into one
 * "field-name: field-value" pair, without changing the semantics of
 * the message, by appending each subsequent field-value to the first,
 * each separated by a comma.
 */
using ::misc::text_utils::ci_lookup;
typedef ci_lookup<std::string> message_headers_t;

最新更新