Add HTTP field value parsers:

ext_list:
    Iterable container of comma separated extensions, where each extension
    is a token followed an optional list of semicolon delimited parameters,
    with each parameter consisting of a name / value pair. The value can
    be a token or quoted-string.

param_list:
    Iterable container of semicolon delimited parameters, where each parameter
    is a name / value pair. The value can be a token or quoted-string.

token_list
    Iterable container of comma delimited tokens.

* Remove obsolete rfc2616 functions

* Refactor and consolidate case-insensitive string helpers
This commit is contained in:
Vinnie Falco
2016-05-24 06:17:04 -04:00
parent 5a0a47cbae
commit 7e8f5401b2
21 changed files with 1385 additions and 678 deletions

View File

@@ -54,7 +54,6 @@ unit-test http-tests :
http/read.cpp
http/reason.cpp
http/resume_context.cpp
http/rfc2616.cpp
http/rfc7230.cpp
http/status.cpp
http/streambuf_body.cpp

View File

@@ -20,7 +20,6 @@ add_executable (http-tests
read.cpp
reason.cpp
resume_context.cpp
rfc2616.cpp
rfc7230.cpp
status.cpp
streambuf_body.cpp

View File

@@ -13,7 +13,7 @@
#include <beast/core/streambuf.hpp>
#include <beast/core/write_streambuf.hpp>
#include <beast/core/detail/ci_char_traits.hpp>
#include <beast/http/rfc2616.hpp>
#include <beast/http/rfc7230.hpp>
#include <beast/unit_test/suite.hpp>
#include <boost/utility/string_ref.hpp>
#include <cassert>
@@ -135,6 +135,13 @@ public:
{
};
static
std::string
str(boost::string_ref const& s)
{
return std::string{s.data(), s.size()};
}
template<bool isRequest>
class test_parser :
public basic_parser_v1<isRequest, test_parser<isRequest>>
@@ -146,8 +153,7 @@ public:
{
if(! value_.empty())
{
rfc2616::trim_right_in_place(value_);
fields.emplace(field_, value_);
fields.emplace(field_, str(detail::trim(value_)));
field_.clear();
value_.clear();
}

View File

@@ -11,7 +11,7 @@
#include "nodejs-parser/http_parser.h"
#include <beast/http/message_v1.hpp>
#include <beast/http/rfc2616.hpp>
#include <beast/http/rfc7230.hpp>
#include <beast/core/buffer_concepts.hpp>
#include <beast/core/error.hpp>
#include <boost/asio/buffer.hpp>
@@ -611,7 +611,7 @@ nodejs_basic_parser<Derived>::check_header()
{
if (! value_.empty())
{
rfc2616::trim_right_in_place(value_);
//detail::trim(value_);
call_on_field(field_, value_,
has_on_field<Derived>{});
field_.clear();

View File

@@ -1,115 +0,0 @@
//
// Copyright (c) 2013-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Test that header file is self-contained.
#include <beast/http/rfc2616.hpp>
#include <beast/unit_test/suite.hpp>
#include <string>
#include <vector>
namespace beast {
namespace rfc2616 {
namespace test {
class rfc2616_test : public beast::unit_test::suite
{
public:
void
checkSplit(std::string const& s,
std::vector <std::string> const& expected)
{
auto const parsed = split_commas(s.begin(), s.end());
expect (parsed == expected);
}
void testSplit()
{
checkSplit("", {});
checkSplit(" ", {});
checkSplit(" ", {});
checkSplit("\t", {});
checkSplit(" \t ", {});
checkSplit(",", {});
checkSplit(",,", {});
checkSplit(" ,", {});
checkSplit(" , ,", {});
checkSplit("x", {"x"});
checkSplit(" x", {"x"});
checkSplit(" \t x", {"x"});
checkSplit("x ", {"x"});
checkSplit("x \t", {"x"});
checkSplit(" \t x \t ", {"x"});
checkSplit("\"\"", {});
checkSplit(" \"\"", {});
checkSplit("\"\" ", {});
checkSplit("\"x\"", {"x"});
checkSplit("\" \"", {" "});
checkSplit("\" x\"", {" x"});
checkSplit("\"x \"", {"x "});
checkSplit("\" x \"", {" x "});
checkSplit("\"\tx \"", {"\tx "});
checkSplit("x,y", {"x", "y"});
checkSplit("x ,\ty ", {"x", "y"});
checkSplit("x, y, z", {"x","y","z"});
checkSplit("x, \"y\", z", {"x","y","z"});
checkSplit(",,x,,\"y\",,", {"x","y"});
}
void
checkIter(std::string const& s,
std::vector<std::string> const& expected)
{
std::vector<std::string> got;
for(auto const& v : make_list(s))
got.emplace_back(v);
expect(got == expected);
}
void
testIter()
{
checkIter("x", {"x"});
checkIter(" x", {"x"});
checkIter("x\t", {"x"});
checkIter("\tx ", {"x"});
checkIter(",x", {"x"});
checkIter("x,", {"x"});
checkIter(",x,", {"x"});
checkIter(" , x\t,\t", {"x"});
checkIter("x,y", {"x", "y"});
checkIter("x, ,y ", {"x", "y"});
checkIter("\"x\"", {"x"});
}
void
testList()
{
expect(token_in_list("x", "x"));
expect(token_in_list("x,y", "x"));
expect(token_in_list("x,y", "y"));
expect(token_in_list("x, y ", "y"));
expect(token_in_list("x", "X"));
expect(token_in_list("Y", "y"));
expect(token_in_list("close, keepalive", "close"));
expect(token_in_list("close, keepalive", "keepalive"));
}
void
run()
{
testSplit();
testIter();
testList();
}
};
BEAST_DEFINE_TESTSUITE(rfc2616,http,beast);
} // test
} // rfc2616
} // beast

View File

@@ -7,3 +7,240 @@
// Test that header file is self-contained.
#include <beast/http/rfc7230.hpp>
#include <beast/http/detail/rfc7230.hpp>
#include <beast/unit_test/suite.hpp>
#include <string>
#include <vector>
namespace beast {
namespace http {
namespace test {
class rfc7230_test : public beast::unit_test::suite
{
public:
static
std::string
fmt(std::string const& s)
{
return '\'' + s + '\'';
}
static
std::string
str(boost::string_ref const& s)
{
return std::string(s.data(), s.size());
}
static
std::string
str(param_list const& c)
{
std::string s;
for(auto const& p : c)
{
s.push_back(';');
s.append(str(p.first));
s.push_back('=');
s.append(str(p.second));
}
return s;
}
void
testParamList()
{
auto const ce =
[&](std::string const& s)
{
auto const got = str(param_list{s});
expect(got == s, fmt(got));
};
auto const cs =
[&](std::string const& s, std::string const& good)
{
ce(good);
auto const got = str(param_list{s});
ce(got);
expect(got == good, fmt(got));
};
auto const cq =
[&](std::string const& s, std::string const& good)
{
auto const got = str(param_list{s});
expect(got == good, fmt(got));
};
ce("");
cs(" ;\t i =\t 1 \t", ";i=1");
cq("\t; \t xyz=1 ; ijk=\"q\\\"t\"", ";xyz=1;ijk=q\"t");
// invalid strings
cs(";", "");
cs(";,", "");
cs(";xy", "");
cs(";xy", "");
cs(";xy ", "");
cs(";xy,", "");
cq(";x=,", "");
cq(";xy=\"", "");
cq(";xy=\"\x7f", "");
cq(";xy=\"\\", "");
cq(";xy=\"\\\x01\"", "");
}
static
std::string
str(ext_list const& ex)
{
std::string s;
for(auto const& e : ex)
{
if(! s.empty())
s += ',';
s.append(str(e.first));
s += str(e.second);
}
return s;
}
void
testExtList()
{
auto const ce =
[&](std::string const& s)
{
auto const got = str(ext_list{s});
expect(got == s, fmt(got));
};
auto const cs =
[&](std::string const& s, std::string const& good)
{
ce(good);
auto const got = str(ext_list{s});
ce(got);
expect(got == good, fmt(got));
};
auto const cq =
[&](std::string const& s, std::string const& good)
{
auto const got = str(ext_list{s});
expect(got == good, fmt(got));
};
/*
ext-list = *( "," OWS ) ext *( OWS "," [ OWS ext ] )
ext = token param-list
param-list = *( OWS ";" OWS param )
param = token OWS "=" OWS ( token / quoted-string )
*/
ce("");
cs(",", "");
cs(", ", "");
cs(",\t", "");
cs(", \t", "");
cs(" ", "");
cs(" ,", "");
cs("\t,", "");
cs("\t , \t", "");
cs(",,", "");
cs(" , \t,, \t,", "");
ce("a");
ce("ab");
ce("a,b");
cs(" a ", "a");
cs("\t a, b\t , c\t", "a,b,c");
cs("a; \t i\t=\t \t1\t ", "a;i=1");
ce("a;i=1;j=2;k=3");
ce("a;i=1;j=2;k=3,b;i=4;j=5;k=6");
cq("ab;x=\" \"", "ab;x= ");
cq("ab;x=\"\\\"\"", "ab;x=\"");
expect(ext_list{"a,b;i=1,c;j=2;k=3"}.exists("A"));
expect(ext_list{"a,b;i=1,c;j=2;k=3"}.exists("b"));
expect(! ext_list{"a,b;i=1,c;j=2;k=3"}.exists("d"));
// invalid strings
cs("i j", "i");
cs(";", "");
}
static
std::string
str(token_list const& c)
{
bool first = true;
std::string s;
for(auto const& p : c)
{
if(! first)
s.push_back(',');
s.append(str(p));
first = false;
}
return s;
}
void
testTokenList()
{
auto const ce =
[&](std::string const& s)
{
auto const got = str(token_list{s});
expect(got == s, fmt(got));
};
auto const cs =
[&](std::string const& s, std::string const& good)
{
ce(good);
auto const got = str(token_list{s});
ce(got);
expect(got == good, fmt(got));
};
cs("", "");
cs(" ", "");
cs(" ", "");
cs("\t", "");
cs(" \t ", "");
cs(",", "");
cs(",,", "");
cs(" ,", "");
cs(" , ,", "");
cs(" x", "x");
cs(" \t x", "x");
cs("x ", "x");
cs("x \t", "x");
cs(" \t x \t ", "x");
ce("x,y");
cs("x ,\ty ", "x,y");
cs("x, y, z", "x,y,z");
expect(token_list{"a,b,c"}.exists("A"));
expect(token_list{"a,b,c"}.exists("b"));
expect(! token_list{"a,b,c"}.exists("d"));
// invalid
cs("x y", "x");
}
void
run()
{
testParamList();
testExtList();
testTokenList();
}
};
BEAST_DEFINE_TESTSUITE(rfc7230,http,beast);
} // test
} // http
} // beast