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> 14 using ssize_t = SSIZE_T;
22 : default_handler_(default_handler)
30 : path_regex_(boost::regex(path_regex_string))
36 return regex_match(request.
path, path_regex_);
52 handlers_.push_back(std::make_pair(predicate, handler));
56 const HttpRequest& request, boost::shared_ptr<HttpConnection> connection,
57 const char* begin,
const char* end)
61 if (handler.first(request))
63 if (handler.second(request, connection, begin, end))
72 typedef boost::weak_ptr<BodyCollectingConnection>
75 :
public boost::enable_shared_from_this<BodyCollectingConnection>,
76 private boost::noncopyable
81 boost::shared_ptr<HttpConnection> connection)
82 : handler_(handler), request_(request), connection_(connection),
85 std::string length_str =
86 request_.get_header_value_or_default(
"Content-Length",
"");
89 length_ = boost::lexical_cast<ssize_t>(length_str);
91 catch (
const boost::bad_lexical_cast&)
98 const char* begin,
const char* end)
100 _this->handle_read(begin, end);
107 connection_->write(
"No Content-Length header");
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_))
115 handler_(request_, connection_,
116 body_stream_.str().substr(0, length_));
120 connection_->async_read(
122 shared_from_this(), _1, _2));
141 const HttpRequest& request, boost::shared_ptr<HttpConnection> connection,
142 const char* begin,
const char* end)
144 BodyCollectingConnectionPtr collecting_connection(
146 collecting_connection->handle_read(begin, end);
void addHandler(HandlerPredicate predicate, HttpServerRequestHandler handler)
void handle_read(const char *begin, const char *end)
boost::function< bool(const HttpRequest &)> HandlerPredicate
bool operator()(const HttpRequest &request, boost::shared_ptr< HttpConnection > connection, const char *begin, const char *end)
const boost::regex path_regex_
static ReplyBuilder builder(status_type status)
HttpServerRequestHandler default_handler_
boost::shared_ptr< BodyCollectingConnection > BodyCollectingConnectionPtr
boost::function< void(const HttpRequest &, boost::shared_ptr< HttpConnection >, const std::string &body)> Handler
boost::weak_ptr< BodyCollectingConnection > BodyCollectingConnectionWeakPtr
const HttpRequest request_
bool operator()(const HttpRequest &request, boost::shared_ptr< HttpConnection > connection, const char *begin, const char *end)
static void static_handle_read(BodyCollectingConnectionPtr _this, const char *begin, const char *end)
void write(HttpConnectionPtr connection)
std::vector< std::pair< HandlerPredicate, HttpServerRequestHandler > > handlers_
HttpRequestHandlerGroup(HttpServerRequestHandler default_handler)
boost::function< bool(const HttpRequest &, boost::shared_ptr< HttpConnection >, const char *begin, const char *end)> HttpServerRequestHandler
boost::shared_ptr< HttpConnection > connection_
bool operator()(const HttpRequest &request)
PathMatcher(const std::string &path_regex_string)
std::stringstream body_stream_
HttpRequestBodyCollector::Handler handler_
BodyCollectingConnection(HttpRequestBodyCollector::Handler handler, const HttpRequest &request, boost::shared_ptr< HttpConnection > connection)
HttpRequestBodyCollector(Handler handler)
void addHandlerForPath(const std::string &path_regex, HttpServerRequestHandler handler)