test_web_server.cpp
Go to the documentation of this file.
5 
6 #include <ros/package.h>
7 #include <signal.h>
8 
9 using namespace async_web_server_cpp;
10 
11 volatile bool should_shutdown = false;
12 void sig_handler(int signo)
13 {
14  should_shutdown = true;
15 }
16 
19  const std::string& body)
20 {
22  connection->write(body);
23 }
24 
27  const char* begin, const char* end)
28 {
30  connection->write(request.path);
31  return true;
32 }
33 
36  const char* begin, const char* end)
37 {
39  for (std::map<std::string, std::string>::const_iterator itr =
40  request.query_params.begin();
41  itr != request.query_params.end(); ++itr)
42  {
43  connection->write(itr->first + "=" + itr->second + "\n");
44  }
45  return true;
46 }
47 
49 {
50 public:
51  WebsocketEchoer(WebsocketConnectionPtr websocket) : websocket_(websocket) {}
52  void operator()(const WebsocketMessage& message)
53  {
54  websocket_->sendMessage(message);
55  }
56 
57 private:
59 };
62 {
63  return WebsocketEchoer(websocket);
64 }
65 
66 int main(int argc, char** argv)
67 {
68  if (signal(SIGINT, sig_handler) == SIG_ERR)
69  {
70  return 1;
71  }
72 
73  HttpRequestHandlerGroup handler_group(
75 
76  handler_group.addHandlerForPath("/response/ok",
78  handler_group.addHandlerForPath("/response/created",
80  handler_group.addHandlerForPath(
81  "/response/accepted", HttpReply::stock_reply(HttpReply::accepted));
82  handler_group.addHandlerForPath(
83  "/response/forbidden", HttpReply::stock_reply(HttpReply::forbidden));
84  handler_group.addHandlerForPath(
85  "/response/not_found", HttpReply::stock_reply(HttpReply::not_found));
86  handler_group.addHandlerForPath(
87  "/response/internal_server_error",
89 
90  handler_group.addHandlerForPath(
91  "/a_static_response",
92  HttpReply::static_reply(HttpReply::ok, "text/example", "A RESPONSE"));
93  handler_group.addHandlerForPath("/http_body_echo",
95  handler_group.addHandlerForPath("/http_path_echo.*", http_path_echo);
96  handler_group.addHandlerForPath("/http_query_echo", http_query_echo);
97 
98  handler_group.addHandlerForPath(
99  "/websocket_echo", WebsocketHttpRequestHandler(websocket_echo));
100 
101  handler_group.addHandlerForPath(
102  "/test_files/.+",
104  HttpReply::ok, "/test_files/",
105  ros::package::getPath("async_web_server_cpp") + "/test", false));
106  handler_group.addHandlerForPath(
107  "/test_files_with_dir/.+",
109  HttpReply::ok, "/test_files_with_dir/",
110  ros::package::getPath("async_web_server_cpp") + "/test", true));
111  handler_group.addHandlerForPath(
112  "/test_file",
113  HttpReply::from_file(HttpReply::ok, "text/html",
114  ros::package::getPath("async_web_server_cpp")
115  + "/test/test.html"));
116 
117  HttpServer server("0.0.0.0", "9849", handler_group, 1);
118 
119  server.run();
120 
121  while (!should_shutdown)
122  {
123  sleep(1);
124  }
125 
126  return (0);
127 }
boost::function< void(const WebsocketMessage &message)> MessageHandler
static HttpServerRequestHandler static_reply(status_type status, const std::string &content_type, const std::string &content, const std::vector< HttpHeader > &additional_headers=std::vector< HttpHeader >())
Definition: http_reply.cpp:416
static ReplyBuilder builder(status_type status)
Definition: http_reply.cpp:447
static HttpServerRequestHandler from_file(HttpReply::status_type status, const std::string &content_type, const std::string &filename, const std::vector< HttpHeader > &additional_headers=std::vector< HttpHeader >())
Definition: http_reply.cpp:237
int main(int argc, char **argv)
WebsocketConnection::MessageHandler websocket_echo(const HttpRequest &request, WebsocketConnectionPtr websocket)
std::map< std::string, std::string > query_params
WebsocketConnectionPtr websocket_
boost::shared_ptr< WebsocketConnection > WebsocketConnectionPtr
static HttpServerRequestHandler from_filesystem(HttpReply::status_type status, const std::string &path_root, const std::string &filesystem_root, bool list_directories, const std::vector< HttpHeader > &additional_headers=std::vector< HttpHeader >())
Definition: http_reply.cpp:314
void sig_handler(int signo)
boost::shared_ptr< HttpConnection > HttpConnectionPtr
void write(HttpConnectionPtr connection)
Definition: http_reply.cpp:475
volatile bool should_shutdown
static HttpServerRequestHandler stock_reply(status_type status)
Definition: http_reply.cpp:410
static void http_body_echo(const async_web_server_cpp::HttpRequest &request, async_web_server_cpp::HttpConnectionPtr connection, const std::string &body)
void operator()(const WebsocketMessage &message)
static bool http_path_echo(const async_web_server_cpp::HttpRequest &request, async_web_server_cpp::HttpConnectionPtr connection, const char *begin, const char *end)
WebsocketEchoer(WebsocketConnectionPtr websocket)
static bool http_query_echo(const async_web_server_cpp::HttpRequest &request, async_web_server_cpp::HttpConnectionPtr connection, const char *begin, const char *end)
void addHandlerForPath(const std::string &path_regex, HttpServerRequestHandler handler)


async_web_server_cpp
Author(s): Mitchell Wills , Russel Toris
autogenerated on Mon Feb 28 2022 21:54:08