SOIL C++
C++ Unified Device Interface
Server.cpp
Go to the documentation of this file.
1#include "Server.h"
2
3using namespace std;
4using namespace web;
5using namespace utility;
6using namespace http;
7using namespace web::http::experimental::listener;
8
9HTTP::Server::Server(std::string url) : listener(utility::conversions::to_string_t(url))
10{
11 listener.support(std::bind(&Server::handle, this, std::placeholders::_1));
12}
13
14
16{
17}
18
19void HTTP::Server::handle(http_request message)
20{
21 for (auto i = resources.begin(); i != resources.end(); i++)
22 {
23 std::smatch match;
24 std::string line = utility::conversions::to_utf8string(message.relative_uri().to_string());
25 std::regex regex(i->first);
26 if (std::regex_search(line, match, regex))
27 {
28 Response response = i->second->handle(message, match);
29 message.reply(response);
30 return;
31 }
32 }
33 Response response = default_resource.handle(message);
34 message.reply(response);
35
36
37}
38
39void HTTP::Server::add(std::string path, std::shared_ptr<Resource> resource)
40{
41 resources.push_back(std::make_pair(path, resource));
42}
43
44void HTTP::Server::remove(std::string path)
45{
46 for (auto i = resources.begin(); i != resources.end(); i++)
47 {
48 if (i->first == path)
49 {
50 resources.erase(i);
51 break;
52 }
53 }
54}
void remove(std::string path)
Register resource.
Definition: Server.cpp:44
~Server()
Destructor.
Definition: Server.cpp:15
Server(std::string url)
Constructor.
Definition: Server.cpp:9
void add(std::string path, std::shared_ptr< Resource > resource)
Register resource.
Definition: Server.cpp:39
web::http::http_response Response
HTTP Response.
Definition: Types.h:18