From fb2f777b4db575d5126cb4f8ed3cab59be686828 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Tue, 6 Jun 2017 14:04:17 -0700 Subject: [PATCH] Use field in basic_parser --- examples/doc_http_samples.hpp | 5 ++-- include/beast/http/basic_parser.hpp | 6 ++--- include/beast/http/field.hpp | 1 + include/beast/http/impl/basic_parser.ipp | 32 +++++++++++------------- include/beast/http/impl/field.ipp | 3 ++- include/beast/http/parser.hpp | 12 ++++++--- test/http/field.cpp | 1 + test/http/parser_bench.cpp | 5 ++-- test/http/test_parser.hpp | 5 ++-- 9 files changed, 37 insertions(+), 33 deletions(-) diff --git a/examples/doc_http_samples.hpp b/examples/doc_http_samples.hpp index c5912b98..f42e7c6e 100644 --- a/examples/doc_http_samples.hpp +++ b/examples/doc_http_samples.hpp @@ -890,7 +890,8 @@ class custom_parser /// Called after receiving a header field. void on_field( - string_view name, // The field name + field f, // The known-field enumeration constant + string_view name, // The field name string. string_view value, // The field value error_code& ec); // The error returned to the caller, if any @@ -951,7 +952,7 @@ on_response(int status, string_view reason, template void custom_parser:: -on_field(string_view name, +on_field(field f, string_view name, string_view value, error_code& ec) { } diff --git a/include/beast/http/basic_parser.hpp b/include/beast/http/basic_parser.hpp index 7941986b..c5b5dc90 100644 --- a/include/beast/http/basic_parser.hpp +++ b/include/beast/http/basic_parser.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -394,9 +395,8 @@ private: char const* last, error_code& ec); void - do_field(string_view name, - string_view value, - error_code& ec); + do_field(field f, + string_view value, error_code& ec); }; } // http diff --git a/include/beast/http/field.hpp b/include/beast/http/field.hpp index 252f685d..3691622f 100644 --- a/include/beast/http/field.hpp +++ b/include/beast/http/field.hpp @@ -239,6 +239,7 @@ enum class field : unsigned short proxy_authenticate, proxy_authentication_info, proxy_authorization, + proxy_connection, proxy_features, proxy_instruction, public_, diff --git a/include/beast/http/impl/basic_parser.ipp b/include/beast/http/impl/basic_parser.ipp index ba7b010a..6489592c 100644 --- a/include/beast/http/impl/basic_parser.ipp +++ b/include/beast/http/impl/basic_parser.ipp @@ -678,12 +678,12 @@ parse_fields(char const*& p, auto it2 = term - 2; detail::skip_ows(p, it2); detail::skip_ows_rev(it2, p); - auto const value = - make_string(p, it2); - do_field(name, value, ec); + auto const f = string_to_field(name); + auto const value = make_string(p, it2); + do_field(f, value, ec); if(ec) return; - impl().on_field(name, value, ec); + impl().on_field(f, name, value, ec); if(ec) return; p = term; @@ -723,12 +723,12 @@ parse_fields(char const*& p, p = term; } } - string_view value{ - s.data(), s.size()}; - do_field(name, value, ec); + auto const f = string_to_field(name); + string_view const value{s.data(), s.size()}; + do_field(f, value, ec); if(ec) return; - impl().on_field(name, value, ec); + impl().on_field(f, name, value, ec); if(ec) return; } @@ -738,14 +738,12 @@ parse_fields(char const*& p, template void basic_parser:: -do_field( - string_view name, - string_view value, - error_code& ec) +do_field(field f, + string_view value, error_code& ec) { // Connection - if(strieq("connection", name) || - strieq("proxy-connection", name)) + if(f == field::connection || + f == field::proxy_connection) { auto const list = opt_token_list{value}; if(! validate_list(list)) @@ -788,7 +786,7 @@ do_field( } // Content-Length - if(strieq("content-length", name)) + if(f == field::content_length) { if(f_ & flagContentLength) { @@ -818,7 +816,7 @@ do_field( } // Transfer-Encoding - if(strieq("transfer-encoding", name)) + if(f == field::transfer_encoding) { if(f_ & flagChunked) { @@ -850,7 +848,7 @@ do_field( } // Upgrade - if(strieq("upgrade", name)) + if(f == field::upgrade) { f_ |= flagUpgrade; ec = {}; diff --git a/include/beast/http/impl/field.ipp b/include/beast/http/impl/field.ipp index c88d36ad..c9772f61 100644 --- a/include/beast/http/impl/field.ipp +++ b/include/beast/http/impl/field.ipp @@ -21,7 +21,7 @@ namespace detail { class field_strings { using array_type = - std::array; + std::array; array_type v_; @@ -257,6 +257,7 @@ public: "Proxy-Authenticate", "Proxy-Authentication-Info", "Proxy-Authorization", + "Proxy-Connection", "Proxy-Features", "Proxy-Instruction", "Public", diff --git a/include/beast/http/parser.hpp b/include/beast/http/parser.hpp index 9019a5eb..a3857ba8 100644 --- a/include/beast/http/parser.hpp +++ b/include/beast/http/parser.hpp @@ -204,11 +204,15 @@ private: } void - on_field(string_view name, - string_view value, - error_code&) + on_field(field f, string_view name, + string_view value, error_code&) { - m_.insert(name, value); + if(f != field::unknown) + // VFALCO Note we are not preserving the + // capitalization of the field name. + m_.insert(f, value); + else + m_.insert(name, value); } void diff --git a/test/http/field.cpp b/test/http/field.cpp index b9eabb5b..5a13e0fa 100644 --- a/test/http/field.cpp +++ b/test/http/field.cpp @@ -253,6 +253,7 @@ public: match(field::proxy_authenticate, "Proxy-Authenticate"); match(field::proxy_authentication_info, "Proxy-Authentication-Info"); match(field::proxy_authorization, "Proxy-Authorization"); + match(field::proxy_connection, "Proxy-Connection"); match(field::proxy_features, "Proxy-Features"); match(field::proxy_instruction, "Proxy-Instruction"); match(field::public_, "Public"); diff --git a/test/http/parser_bench.cpp b/test/http/parser_bench.cpp index eae2a603..6e3023cd 100644 --- a/test/http/parser_bench.cpp +++ b/test/http/parser_bench.cpp @@ -178,9 +178,8 @@ public: } void - on_field(string_view, - string_view, - error_code&) + on_field(field, + string_view, string_view, error_code&) { } diff --git a/test/http/test_parser.hpp b/test/http/test_parser.hpp index 15a1e967..f8df6cbd 100644 --- a/test/http/test_parser.hpp +++ b/test/http/test_parser.hpp @@ -75,9 +75,8 @@ public: } void - on_field(string_view, - string_view, - error_code& ec) + on_field(field f, string_view, + string_view, error_code& ec) { got_on_field = true; if(fc_)