Skip to content

Commit 7a6aebf

Browse files
committed
Additional examples.
url_registration and ban_system
1 parent 4a183cd commit 7a6aebf

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed

examples/Makefile.am

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
LDADD = $(top_builddir)/src/libhttpserver.la
2020
AM_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/src/httpserver/
2121
METASOURCES = AUTO
22-
noinst_PROGRAMS = hello_world service minimal_hello_world custom_error allowing_disallowing_methods handlers hello_with_get_arg setting_headers custom_access_log basic_authentication digest_authentication minimal_https minimal_file_response minimal_deferred
22+
noinst_PROGRAMS = hello_world service minimal_hello_world custom_error allowing_disallowing_methods handlers hello_with_get_arg setting_headers custom_access_log basic_authentication digest_authentication minimal_https minimal_file_response minimal_deferred url_registration minimal_ip_ban
2323

2424
hello_world_SOURCES = hello_world.cpp
2525
service_SOURCES = service.cpp
@@ -35,3 +35,5 @@ digest_authentication_SOURCES = digest_authentication.cpp
3535
minimal_https_SOURCES = minimal_https.cpp
3636
minimal_file_response_SOURCES = minimal_file_response.cpp
3737
minimal_deferred_SOURCES = minimal_deferred.cpp
38+
url_registration_SOURCES = url_registration.cpp
39+
minimal_ip_ban_SOURCES = minimal_ip_ban.cpp

examples/minimal_ip_ban.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
This file is part of libhttpserver
3+
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
USA
19+
*/
20+
21+
#include <httpserver.hpp>
22+
23+
using namespace httpserver;
24+
25+
class hello_world_resource : public http_resource {
26+
public:
27+
const std::shared_ptr<http_response> render(const http_request&) {
28+
return std::shared_ptr<http_response>(new string_response("Hello, World!"));
29+
}
30+
};
31+
32+
int main(int argc, char** argv) {
33+
webserver ws = create_webserver(8080)
34+
.default_policy(http::http_utils::REJECT);
35+
36+
ws.allow_ip("127.0.0.1");
37+
38+
hello_world_resource hwr;
39+
ws.register_resource("/hello", &hwr);
40+
ws.start(true);
41+
42+
return 0;
43+
}

examples/url_registration.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
This file is part of libhttpserver
3+
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18+
USA
19+
*/
20+
21+
#include <httpserver.hpp>
22+
23+
using namespace httpserver;
24+
25+
class hello_world_resource : public http_resource {
26+
public:
27+
const std::shared_ptr<http_response> render(const http_request&) {
28+
return std::shared_ptr<http_response>(new string_response("Hello, World!"));
29+
}
30+
};
31+
32+
class handling_multiple_resource : public http_resource {
33+
public:
34+
const std::shared_ptr<http_response> render(const http_request& req) {
35+
return std::shared_ptr<http_response>(new string_response("Your URL: " + req.get_path()));
36+
}
37+
};
38+
39+
class url_args_resource : public http_resource {
40+
public:
41+
const std::shared_ptr<http_response> render(const http_request& req) {
42+
return std::shared_ptr<http_response>(new string_response("ARGS: " + req.get_arg("arg1") + " and " + req.get_arg("arg2")));
43+
}
44+
};
45+
46+
int main(int argc, char** argv) {
47+
webserver ws = create_webserver(8080);
48+
49+
hello_world_resource hwr;
50+
ws.register_resource("/hello", &hwr);
51+
52+
handling_multiple_resource hmr;
53+
ws.register_resource("/family", &hmr, true);
54+
ws.register_resource("/with_regex_[0-9]+", &hmr);
55+
56+
url_args_resource uar;
57+
ws.register_resource("/url/with/{arg1}/and/{arg2}", &uar);
58+
ws.register_resource("/url/with/parametric/args/{arg1|[0-9]+}/and/{arg2|[A-Z]+}", &uar);
59+
60+
ws.start(true);
61+
62+
return 0;
63+
}

0 commit comments

Comments
 (0)