http_request_handler.cpp
Go to the documentation of this file.
2 
5 
6 #include <boost/bind.hpp>
7 #include <boost/enable_shared_from_this.hpp>
8 #include <boost/noncopyable.hpp>
9 #include <boost/regex.hpp>
10 #include <boost/shared_ptr.hpp>
11 
12 #if defined(_MSC_VER)
13 # include <BaseTsd.h>
14 using ssize_t = SSIZE_T;
15 #endif
16 
17 namespace async_web_server_cpp
18 {
19 
21  HttpServerRequestHandler default_handler)
22  : default_handler_(default_handler)
23 {
24 }
25 
27 {
28 public:
29  explicit PathMatcher(const std::string& path_regex_string)
30  : path_regex_(boost::regex(path_regex_string))
31  {
32  }
33 
34  bool operator()(const HttpRequest& request)
35  {
36  return regex_match(request.path, path_regex_);
37  }
38 
39 private:
40  const boost::regex path_regex_;
41 };
42 
44  const std::string& path_regex, HttpServerRequestHandler handler)
45 {
46  addHandler(PathMatcher(path_regex), handler);
47 }
48 
51 {
52  handlers_.push_back(std::make_pair(predicate, handler));
53 }
54 
56  const HttpRequest& request, boost::shared_ptr<HttpConnection> connection,
57  const char* begin, const char* end)
58 {
59  for (auto& handler : handlers_)
60  {
61  if (handler.first(request))
62  {
63  if (handler.second(request, connection, begin, end))
64  return true;
65  }
66  }
67  return default_handler_(request, connection, begin, end);
68 }
69 
71 typedef boost::shared_ptr<BodyCollectingConnection> BodyCollectingConnectionPtr;
72 typedef boost::weak_ptr<BodyCollectingConnection>
75  : public boost::enable_shared_from_this<BodyCollectingConnection>,
76  private boost::noncopyable
77 {
78 public:
80  const HttpRequest& request,
81  boost::shared_ptr<HttpConnection> connection)
82  : handler_(handler), request_(request), connection_(connection),
84  {
85  std::string length_str =
86  request_.get_header_value_or_default("Content-Length", "");
87  try
88  {
89  length_ = boost::lexical_cast<ssize_t>(length_str);
90  }
91  catch (const boost::bad_lexical_cast&)
92  {
93  length_ = -1; // indicate error
94  }
95  }
96 
98  const char* begin, const char* end)
99  {
100  _this->handle_read(begin, end);
101  }
102  void handle_read(const char* begin, const char* end)
103  {
104  if (length_ < 0)
105  {
107  connection_->write("No Content-Length header");
108  return;
109  }
110  std::string chunk(begin, end - begin);
111  body_stream_ << chunk;
112  received_length_ += chunk.length();
113  if (received_length_ >= static_cast<size_t>(length_))
114  {
116  body_stream_.str().substr(0, length_));
117  }
118  else
119  {
120  connection_->async_read(
122  shared_from_this(), _1, _2));
123  }
124  }
125 
126 private:
129  boost::shared_ptr<HttpConnection> connection_;
130  std::stringstream body_stream_;
131  ssize_t length_;
133 };
134 
136  : handler_(handler)
137 {
138 }
139 
141  const HttpRequest& request, boost::shared_ptr<HttpConnection> connection,
142  const char* begin, const char* end)
143 {
144  BodyCollectingConnectionPtr collecting_connection(
145  new BodyCollectingConnection(handler_, request, connection));
146  collecting_connection->handle_read(begin, end);
147  return true;
148 }
149 
150 } // namespace async_web_server_cpp
async_web_server_cpp::HttpRequestBodyCollector::HttpRequestBodyCollector
HttpRequestBodyCollector(Handler handler)
Definition: http_request_handler.cpp:135
async_web_server_cpp::BodyCollectingConnection::request_
const HttpRequest request_
Definition: http_request_handler.cpp:128
http_request_handler.hpp
async_web_server_cpp::HttpRequestHandlerGroup::operator()
bool operator()(const HttpRequest &request, boost::shared_ptr< HttpConnection > connection, const char *begin, const char *end)
Definition: http_request_handler.cpp:55
async_web_server_cpp::PathMatcher
Definition: http_request_handler.cpp:26
http_connection.hpp
async_web_server_cpp::HttpServerRequestHandler
boost::function< bool(const HttpRequest &, boost::shared_ptr< HttpConnection >, const char *begin, const char *end)> HttpServerRequestHandler
Definition: http_request_handler.hpp:12
async_web_server_cpp::BodyCollectingConnectionWeakPtr
boost::weak_ptr< BodyCollectingConnection > BodyCollectingConnectionWeakPtr
Definition: http_request_handler.cpp:73
async_web_server_cpp::HttpRequestHandlerGroup::HandlerPredicate
boost::function< bool(const HttpRequest &)> HandlerPredicate
Definition: http_request_handler.hpp:33
async_web_server_cpp::HttpRequestHandlerGroup::addHandler
void addHandler(HandlerPredicate predicate, HttpServerRequestHandler handler)
Definition: http_request_handler.cpp:49
async_web_server_cpp::BodyCollectingConnection::static_handle_read
static void static_handle_read(BodyCollectingConnectionPtr _this, const char *begin, const char *end)
Definition: http_request_handler.cpp:97
async_web_server_cpp
Definition: http_connection.hpp:14
async_web_server_cpp::BodyCollectingConnection::body_stream_
std::stringstream body_stream_
Definition: http_request_handler.cpp:130
async_web_server_cpp::HttpRequestHandlerGroup::HttpRequestHandlerGroup
HttpRequestHandlerGroup(HttpServerRequestHandler default_handler)
Definition: http_request_handler.cpp:20
async_web_server_cpp::BodyCollectingConnection::handle_read
void handle_read(const char *begin, const char *end)
Definition: http_request_handler.cpp:102
async_web_server_cpp::BodyCollectingConnection::handler_
HttpRequestBodyCollector::Handler handler_
Definition: http_request_handler.cpp:127
async_web_server_cpp::HttpRequest::path
std::string path
Definition: http_request.hpp:26
async_web_server_cpp::HttpRequest
Definition: http_request.hpp:18
async_web_server_cpp::BodyCollectingConnection::BodyCollectingConnection
BodyCollectingConnection(HttpRequestBodyCollector::Handler handler, const HttpRequest &request, boost::shared_ptr< HttpConnection > connection)
Definition: http_request_handler.cpp:79
async_web_server_cpp::BodyCollectingConnection
Definition: http_request_handler.cpp:74
async_web_server_cpp::HttpRequest::get_header_value_or_default
std::string get_header_value_or_default(const std::string &name, const std::string &default_value) const
Definition: http_request.cpp:62
async_web_server_cpp::HttpRequestHandlerGroup::handlers_
std::vector< std::pair< HandlerPredicate, HttpServerRequestHandler > > handlers_
Definition: http_request_handler.hpp:50
async_web_server_cpp::HttpRequestHandlerGroup::default_handler_
HttpServerRequestHandler default_handler_
Definition: http_request_handler.hpp:48
async_web_server_cpp::PathMatcher::PathMatcher
PathMatcher(const std::string &path_regex_string)
Definition: http_request_handler.cpp:29
async_web_server_cpp::HttpReply::builder
static ReplyBuilder builder(status_type status)
Definition: http_reply.cpp:447
async_web_server_cpp::HttpRequestBodyCollector::handler_
Handler handler_
Definition: http_request_handler.hpp:68
async_web_server_cpp::ReplyBuilder::write
void write(HttpConnectionPtr connection)
Definition: http_reply.cpp:475
http_reply.hpp
async_web_server_cpp::BodyCollectingConnection::length_
ssize_t length_
Definition: http_request_handler.cpp:131
async_web_server_cpp::HttpRequestHandlerGroup::addHandlerForPath
void addHandlerForPath(const std::string &path_regex, HttpServerRequestHandler handler)
Definition: http_request_handler.cpp:43
async_web_server_cpp::BodyCollectingConnection::connection_
boost::shared_ptr< HttpConnection > connection_
Definition: http_request_handler.cpp:129
async_web_server_cpp::HttpRequestBodyCollector::Handler
boost::function< void(const HttpRequest &, boost::shared_ptr< HttpConnection >, const std::string &body)> Handler
Definition: http_request_handler.hpp:59
async_web_server_cpp::BodyCollectingConnection::received_length_
size_t received_length_
Definition: http_request_handler.cpp:132
async_web_server_cpp::PathMatcher::operator()
bool operator()(const HttpRequest &request)
Definition: http_request_handler.cpp:34
async_web_server_cpp::HttpReply::bad_request
@ bad_request
Definition: http_reply.hpp:35
async_web_server_cpp::HttpRequestBodyCollector::operator()
bool operator()(const HttpRequest &request, boost::shared_ptr< HttpConnection > connection, const char *begin, const char *end)
Definition: http_request_handler.cpp:140
async_web_server_cpp::PathMatcher::path_regex_
const boost::regex path_regex_
Definition: http_request_handler.cpp:40
async_web_server_cpp::BodyCollectingConnectionPtr
boost::shared_ptr< BodyCollectingConnection > BodyCollectingConnectionPtr
Definition: http_request_handler.cpp:70


async_web_server_cpp
Author(s): Mitchell Wills , Russel Toris
autogenerated on Tue Mar 1 2022 23:49:06