Search code examples
c++parsingboostboost-spirit

Boost.Spirit HTTP header parser, tracing


I have implemented the HTTP header parser with Boost.Spirit :

#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/classic.hpp>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/fusion/include/map.hpp>
#include <boost/fusion/adapted/struct.hpp>
#include <iostream>
#include <string>

using namespace boost;

typedef std::map<std::string, std::string> header_fields_t;
struct HttpRequestHeader
{
    std::string _method;
    std::string _uri;
    std::string _http_version;
    header_fields_t _header_fields;
};

BOOST_FUSION_ADAPT_STRUCT
(
    HttpRequestHeader,
    (std::string, _method)
    (std::string, _uri)
    (std::string, _http_version)
    (header_fields_t, _header_fields)
)

template <typename Iterator>
struct HttpHeaderGrammar: spirit::qi::grammar < Iterator,      
HttpRequestHeader(), spirit::ascii::space_type >
{
    HttpHeaderGrammar() : HttpHeaderGrammar::base_type(http_header,     "HttpHeaderGrammar Grammar")
    {
        auto eol_rule = spirit::qi::no_skip[spirit::qi::lit('\r')] >> spirit::qi::no_skip[spirit::qi::lit('\n')];
        method = spirit::qi::lexeme[+spirit::qi::alpha];
        uri = spirit::qi::lexeme[+(spirit::qi::char_ - spirit::qi::space)];
        http_ver = spirit::lexeme[spirit::qi::lit("HTTP/") >> +(spirit::qi::char_("0-9."))];

        auto field_key = spirit::lexeme[+(spirit::qi::char_("a-zA-Z-") | spirit::qi::digit)];
        auto field_value = spirit::lexeme[+(spirit::qi::char_ - '\r' - '\n')];
        fields = *(field_key >> ':' >> field_value >> eol_rule);
        http_header =
            method >> uri >> http_ver >> eol_rule
            >> fields
            ;
        BOOST_SPIRIT_DEBUG_NODES((method)(uri)(http_ver)(fields))
    }

    spirit::qi::rule<Iterator, std::string(), spirit::ascii::space_type> method;
    spirit::qi::rule<Iterator, std::string(), spirit::ascii::space_type> uri;
    spirit::qi::rule<Iterator, std::string(), spirit::ascii::space_type> http_ver;
    spirit::qi::rule<Iterator, std::map<std::string, std::string>(), spirit::ascii::space_type> fields;
    spirit::qi::rule<Iterator, HttpRequestHeader(),     spirit::ascii::space_type> http_header;
};

int main(int argc, char* argv[])
{
    std::cout << "/////////////////////////////////////////////////////////\n\n";
    std::cout << "Expression parser...\n\n";
    std::cout <<     "/////////////////////////////////////////////////////////\n\n";

    typedef std::string::const_iterator iterator_type;
    HttpHeaderGrammar<iterator_type> httpGrammar;
    BOOST_SPIRIT_DEBUG_NODE(httpGrammar);

    HttpRequestHeader httpHeader;

    std::string str(
        "CONNECT www.tutorialspoint.com HTTP/1.1\r\n"
        "User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)\r\n");

    {
        std::string::const_iterator iter = str.begin();
        std::string::const_iterator end = str.end();
        bool r = spirit::qi::phrase_parse(iter, end, httpGrammar,     spirit::ascii::space, httpHeader);

        if (r && iter == end)
        {
            std::cout << "-------------------------\n";
            std::cout << "Parsing succeeded\n";
            std::cout << "-------------------------\n";
        }
        else
        {
            std::string rest(iter, end);
            std::cout << "-------------------------\n";
            std::cout << "Parsing failed\n";
            std::cout << "stopped at: \": " << rest << "\"\n";
            std::cout << "-------------------------\n";
        }
    }

    std::cout << "Bye... :-) \n\n";
    return 0;
}

Finally, I'm faced 2 problems :

1) When running on windows, it works and parsing succeeds. When running on linux, parsing fails. While debugging, I noticed that http request text keeps "...\\r\\n..." and looks like Boost.Spirit incorrectly proceeds these symbols. I hardly understand what's wrong adn how to fix.

2) I don't see any trace output. What I'm doing wrong to make Boost.Spirit print tracing output?

-Thanks


Solution

  • Indeed a few things going on:

    • can't assign expressions to auto (see https://stackoverflow.com/a/31294109/2417774)
    • can't match eol when skipping whitespace (use blank)
    • no need to add lexemes, just drop skippers; Boost spirit skipper issues
    • graph is already all characters except whitespace
    • consider making the grammar more specific (+alpha for the verb is soliciting problems)
    • stop including 10-year obsolete "classic" headers.

    Here you go: Live On Coliru

    #define BOOST_SPIRIT_DEBUG
    #include <boost/spirit/include/qi.hpp>
    #include <boost/fusion/include/std_pair.hpp>
    #include <map>
    #include <iostream>
    
    typedef std::map<std::string, std::string> header_fields_t;
    
    struct HttpRequestHeader
    {
        std::string _method;
        std::string _uri;
        std::string _http_version;
        header_fields_t _header_fields;
    };
    
    BOOST_FUSION_ADAPT_STRUCT(HttpRequestHeader, _method, _uri, _http_version, _header_fields)
    
    namespace qi = boost::spirit::qi;
    
    template <typename Iterator, typename Skipper = qi::ascii::blank_type>
    struct HttpHeaderGrammar: qi::grammar <Iterator, HttpRequestHeader(), Skipper> {
        HttpHeaderGrammar() : HttpHeaderGrammar::base_type(http_header, "HttpHeaderGrammar Grammar") {
            method        = +qi::alpha;
            uri           = +qi::graph;
            http_ver      = "HTTP/" >> +qi::char_("0-9.");
    
            field_key     = +qi::char_("0-9a-zA-Z-");
            field_value   = +~qi::char_("\r\n");
    
            fields = *(field_key >> ':' >> field_value >> qi::lexeme["\r\n"]);
    
            http_header = method >> uri >> http_ver >> qi::lexeme["\r\n"] >> fields;
    
            BOOST_SPIRIT_DEBUG_NODES((method)(uri)(http_ver)(fields)(http_header))
        }
    
      private:
        qi::rule<Iterator, std::map<std::string, std::string>(), Skipper> fields;
        qi::rule<Iterator, HttpRequestHeader(), Skipper> http_header;
        // lexemes
        qi::rule<Iterator, std::string()> method, uri, http_ver;
        qi::rule<Iterator, std::string()> field_key, field_value;
    };
    
    int main()
    {
        typedef std::string::const_iterator It;
        HttpHeaderGrammar<It> httpGrammar;
    
        HttpRequestHeader httpHeader;
    
        std::string str(
            "CONNECT www.tutorialspoint.com HTTP/1.1\r\n"
            "User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)\r\n");
    
        It iter = str.begin(), end = str.end();
        bool r = phrase_parse(iter, end, httpGrammar, qi::ascii::blank, httpHeader);
    
        if (r && iter == end) {
            std::cout << "Parsing succeeded\n";
        } else {
            std::cout << "Parsing failed\n";
            std::cout << "stopped at: \"" << std::string(iter, end) << "\"\n";
        }
    
        std::cout << "Bye... :-) \n\n";
    }
    

    Output:

    Parsing succeeded
    Bye... :-) 
    

    Debug Info:

    <http_header>
      <try>CONNECT www.tutorial</try>
      <method>
        <try>CONNECT www.tutorial</try>
        <success> www.tutorialspoint.</success>
        <attributes>[[C, O, N, N, E, C, T]]</attributes>
      </method>
      <uri>
        <try>www.tutorialspoint.c</try>
        <success> HTTP/1.1\r\nUser-Agen</success>
        <attributes>[[w, w, w, ., t, u, t, o, r, i, a, l, s, p, o, i, n, t, ., c, o, m]]</attributes>
      </uri>
      <http_ver>
        <try>HTTP/1.1\r\nUser-Agent</try>
        <success>\r\nUser-Agent: Mozill</success>
        <attributes>[[1, ., 1]]</attributes>
      </http_ver>
      <fields>
        <try>User-Agent: Mozilla/</try>
        <success></success>
        <attributes>[[[[U, s, e, r, -, A, g, e, n, t], [M, o, z, i, l, l, a, /, 4, ., 0,  , (, c, o, m, p, a, t, i, b, l, e, ;,  , M, S, I, E, 5, ., 0, 1, ;,  , W, i, n, d, o, w, s,  , N, T, )]]]]</attributes>
      </fields>
      <success></success>
      <attributes>[[[C, O, N, N, E, C, T], [w, w, w, ., t, u, t, o, r, i, a, l, s, p, o, i, n, t, ., c, o, m], [1, ., 1], [[[U, s, e, r, -, A, g, e, n, t], [M, o, z, i, l, l, a, /, 4, ., 0,  , (, c, o, m, p, a, t, i, b, l, e, ;,  , M, S, I, E, 5, ., 0, 1, ;,  , W, i, n, d, o, w, s,  , N, T, )]]]]]</attributes>
    </http_header>