Go to the documentation of this file.00001 #include <boost/regex.hpp>
00002 #include "async_web_server_cpp/http_request_handler.hpp"
00003
00004 namespace async_web_server_cpp
00005 {
00006
00007 HttpRequestHandlerGroup::HttpRequestHandlerGroup(HttpServerRequestHandler default_handler)
00008 : default_handler_(default_handler)
00009 {
00010 }
00011
00012 class PathMatcher
00013 {
00014 public:
00015 explicit PathMatcher(const std::string &path_regex_string)
00016 : path_regex_(boost::regex(path_regex_string))
00017 {
00018 }
00019
00020 bool operator()(const HttpRequest &request)
00021 {
00022 return regex_match(request.path, path_regex_);
00023 }
00024
00025 private:
00026 const boost::regex path_regex_;
00027 };
00028
00029 void HttpRequestHandlerGroup::addHandlerForPath(const std::string &path_regex, HttpServerRequestHandler handler)
00030 {
00031 addHandler(PathMatcher(path_regex), handler);
00032 }
00033
00034 void HttpRequestHandlerGroup::addHandler(HandlerPredicate predicate, HttpServerRequestHandler handler)
00035 {
00036 handlers_.push_back(std::make_pair(predicate, handler));
00037 }
00038
00039
00040 void HttpRequestHandlerGroup::operator()(const HttpRequest &request, boost::shared_ptr<HttpConnection> connection, const char* begin, const char* end)
00041 {
00042 for (int i = 0; i < handlers_.size(); ++i)
00043 {
00044 std::pair<HandlerPredicate, HttpServerRequestHandler> &handler = handlers_[i];
00045 if (handler.first(request))
00046 {
00047 handler.second(request, connection, begin, end);
00048 return;
00049 }
00050 }
00051 default_handler_(request, connection, begin, end);
00052 }
00053
00054 }