Files
rippled/include/beast/http/parser.hpp
Vinnie Falco 47dc31d8c2 Refactor beast core, http, tests, and examples:
* Fix warnings
* Port cmake scripts to linux
* Add command line options for running test suites
* Add examples to CMakeLists
* Return std::uint64_t from writer::content_length
* basic_parser::write takes asio::const_buffer instead of pointer and size
* Turn message test back on now that it passes
* Rename to http::headers, use std::allocator, remove http_headers
* http::message::method is now a string
* Refactor to_string for ConstBufferSequence
* Remove chunk_encode from the public interface
* Initialize members for default constructed iterators
* Disallow default construction for dependent buffer sequences

Refactor http::message serialization:

* Serialization no longer creates a copy of the
  headers and modifies them
* New function prepare(), sets Connection, Transfer-Encoding,
  Content-Length based on the body attributes and caller options.
  Callers can use prepare() to have the fields set automatically,
  or they can set the fields manually.
* Use write for operator<<
* Tests for serialization
2016-05-05 11:22:12 -04:00

153 lines
3.0 KiB
C++

//
// 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)
//
#ifndef BEAST_HTTP_PARSER_HPP
#define BEAST_HTTP_PARSER_HPP
#include <beast/http/basic_parser.hpp>
#include <beast/http/error.hpp>
#include <beast/http/message.hpp>
#include <boost/optional.hpp>
#include <functional>
#include <string>
#include <type_traits>
#include <utility>
namespace beast {
namespace http {
namespace detail {
struct parser_request
{
std::string method_;
std::string uri_;
};
struct parser_response
{
std::string reason_;
};
} // detail
template<bool isRequest, class Body, class Headers>
class parser
: public basic_parser<isRequest,
parser<isRequest, Body, Headers>>
, private std::conditional<isRequest,
detail::parser_request, detail::parser_response>::type
{
using message_type =
message<isRequest, Body, Headers>;
std::string field_;
std::string value_;
message_type m_;
typename message_type::body_type::reader r_;
public:
parser(parser&&) = default;
parser()
: r_(m_)
{
}
message_type
release()
{
return std::move(m_);
}
private:
friend class basic_parser<isRequest, parser>;
void flush()
{
if(! value_.empty())
{
rfc2616::trim_right_in_place(value_);
// VFALCO could std::move
m_.headers.insert(field_, value_);
field_.clear();
value_.clear();
}
}
void on_method(boost::string_ref const& s, error_code&)
{
this->method_.append(s.data(), s.size());
}
void on_uri(boost::string_ref const& s, error_code&)
{
this->uri_.append(s.data(), s.size());
}
void on_reason(boost::string_ref const& s, error_code&)
{
this->reason_.append(s.data(), s.size());
}
void on_field(boost::string_ref const& s, error_code&)
{
flush();
field_.append(s.data(), s.size());
}
void on_value(boost::string_ref const& s, error_code&)
{
value_.append(s.data(), s.size());
}
void set(std::true_type)
{
m_.method = std::move(this->method_);
m_.url = std::move(this->uri_);
}
void set(std::false_type)
{
m_.status = this->status_code();
m_.reason = std::move(this->reason_);
}
int on_headers(error_code&)
{
flush();
m_.version = 10 * this->http_major() + this->http_minor();
return 0;
}
void on_request(error_code& ec)
{
set(std::integral_constant<
bool, isRequest>{});
}
void on_response(error_code& ec)
{
set(std::integral_constant<
bool, isRequest>{});
}
void on_body(boost::string_ref const& s, error_code& ec)
{
r_.write(s.data(), s.size(), ec);
}
void on_complete(error_code&)
{
}
};
} // http
} // beast
#endif