test_web_server.cpp
Go to the documentation of this file.
00001 #include "async_web_server_cpp/http_server.hpp"
00002 #include "async_web_server_cpp/http_reply.hpp"
00003 #include "async_web_server_cpp/websocket_connection.hpp"
00004 #include "async_web_server_cpp/websocket_request_handler.hpp"
00005 #include <signal.h>
00006 #include <ros/package.h>
00007 
00008 using namespace async_web_server_cpp;
00009 
00010 volatile bool should_shutdown = false;
00011 void sig_handler(int signo) {
00012   should_shutdown = true;
00013 }
00014 
00015 static void http_body_echo(const async_web_server_cpp::HttpRequest &request,
00016                       async_web_server_cpp::HttpConnectionPtr connection, const std::string& body)
00017 {
00018   HttpReply::builder(HttpReply::ok).write(connection);
00019   connection->write(body);
00020 }
00021 
00022 static bool http_path_echo(const async_web_server_cpp::HttpRequest &request,
00023                       async_web_server_cpp::HttpConnectionPtr connection, const char* begin, const char* end)
00024 {
00025   HttpReply::builder(HttpReply::ok).write(connection);
00026   connection->write(request.path);
00027   return true;
00028 }
00029 
00030 static bool http_query_echo(const async_web_server_cpp::HttpRequest &request,
00031                       async_web_server_cpp::HttpConnectionPtr connection, const char* begin, const char* end)
00032 {
00033   HttpReply::builder(HttpReply::ok).write(connection);
00034   for(std::map<std::string,std::string>::const_iterator itr = request.query_params.begin();
00035       itr != request.query_params.end(); ++ itr) {
00036     connection->write(itr->first + "=" + itr->second + "\n");
00037   }
00038   return true;
00039 }
00040 
00041 class WebsocketEchoer {
00042 public:
00043   WebsocketEchoer(WebsocketConnectionPtr websocket) : websocket_(websocket) {}
00044   void operator()(const WebsocketMessage& message) {
00045     websocket_->sendMessage(message);
00046   }
00047 private:
00048   WebsocketConnectionPtr websocket_;
00049 };
00050 WebsocketConnection::MessageHandler websocket_echo(const HttpRequest& request, WebsocketConnectionPtr websocket)
00051 {
00052   return WebsocketEchoer(websocket);
00053 }
00054 
00055 
00056 
00057 int main(int argc, char **argv)
00058 {
00059   if (signal(SIGINT, sig_handler) == SIG_ERR) {
00060     return 1;
00061   }
00062 
00063   HttpRequestHandlerGroup handler_group(
00064     HttpReply::stock_reply(HttpReply::not_found));
00065 
00066   handler_group.addHandlerForPath("/response/ok", HttpReply::stock_reply(HttpReply::ok));
00067   handler_group.addHandlerForPath("/response/created", HttpReply::stock_reply(HttpReply::created));
00068   handler_group.addHandlerForPath("/response/accepted", HttpReply::stock_reply(HttpReply::accepted));
00069   handler_group.addHandlerForPath("/response/forbidden", HttpReply::stock_reply(HttpReply::forbidden));
00070   handler_group.addHandlerForPath("/response/not_found", HttpReply::stock_reply(HttpReply::not_found));
00071   handler_group.addHandlerForPath("/response/internal_server_error", HttpReply::stock_reply(HttpReply::internal_server_error));
00072 
00073   handler_group.addHandlerForPath("/a_static_response", HttpReply::static_reply(HttpReply::ok,
00074                                                                                 "text/example",
00075                                                                                 "A RESPONSE"));
00076   handler_group.addHandlerForPath("/http_body_echo", HttpRequestBodyCollector(http_body_echo));
00077   handler_group.addHandlerForPath("/http_path_echo.*", http_path_echo);
00078   handler_group.addHandlerForPath("/http_query_echo", http_query_echo);
00079 
00080   handler_group.addHandlerForPath("/websocket_echo", WebsocketHttpRequestHandler(websocket_echo));
00081 
00082   handler_group.addHandlerForPath("/test_files/.+", HttpReply::from_filesystem(HttpReply::ok,
00083                                                                                "/test_files/", ros::package::getPath("async_web_server_cpp") + "/test",
00084                                                                                 false));
00085   handler_group.addHandlerForPath("/test_files_with_dir/.+", HttpReply::from_filesystem(HttpReply::ok,
00086                                                                                         "/test_files_with_dir/", ros::package::getPath("async_web_server_cpp") + "/test",
00087                                                                                         true));
00088   handler_group.addHandlerForPath("/test_file", HttpReply::from_file(HttpReply::ok, "text/html",
00089                                                                      ros::package::getPath("async_web_server_cpp") + "/test/test.html"));
00090 
00091   HttpServer server("0.0.0.0", "9849", handler_group, 1);
00092 
00093   server.run();
00094 
00095   while(!should_shutdown) {
00096     sleep(1);
00097   }
00098 
00099   return (0);
00100 }


async_web_server_cpp
Author(s): Mitchell Wills
autogenerated on Fri Mar 4 2016 11:30:14