Skip to content

Commit 41f1b72

Browse files
committed
Created additional examples
1 parent 54072bf commit 41f1b72

File tree

7 files changed

+268
-1
lines changed

7 files changed

+268
-1
lines changed

examples/Makefile.am

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@
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
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
2323

2424
hello_world_SOURCES = hello_world.cpp
2525
service_SOURCES = service.cpp
2626
minimal_hello_world_SOURCES = minimal_hello_world.cpp
2727
custom_error_SOURCES = custom_error.cpp
28+
allowing_disallowing_methods_SOURCES = allowing_disallowing_methods.cpp
29+
handlers_SOURCES = handlers.cpp
30+
hello_with_get_arg_SOURCES = hello_with_get_arg.cpp
31+
setting_headers_SOURCES = setting_headers.cpp
32+
custom_access_log_SOURCES = custom_access_log.cpp
33+
basic_authentication_SOURCES = basic_authentication.cpp
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
35+
hello_world_resource hwr;
36+
hwr.disallow_all();
37+
hwr.set_allowing("GET", true);
38+
ws.register_resource("/hello", &hwr);
39+
ws.start(true);
40+
41+
return 0;
42+
}

examples/basic_authentication.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 user_pass_resource : public httpserver::http_resource
26+
{
27+
public:
28+
const std::shared_ptr<http_response> render_GET(const http_request& req)
29+
{
30+
if (req.get_user() != "myuser" || req.get_pass() != "mypass")
31+
{
32+
return std::shared_ptr<basic_auth_fail_response>(new basic_auth_fail_response("FAIL", "test@example.com"));
33+
}
34+
return std::shared_ptr<string_response>(new string_response(req.get_user() + " " + req.get_pass(), 200, "text/plain"));
35+
}
36+
};
37+
38+
int main(int argc, char** argv) {
39+
webserver ws = create_webserver(8080);
40+
41+
user_pass_resource hwr;
42+
ws.register_resource("/hello", &hwr);
43+
ws.start(true);
44+
45+
return 0;
46+
}

examples/custom_access_log.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
#include <iostream>
23+
24+
using namespace httpserver;
25+
26+
void custom_access_log(const std::string& url) {
27+
std::cout << "ACCESSING: " << url << std::endl;
28+
}
29+
30+
class hello_world_resource : public http_resource {
31+
public:
32+
const std::shared_ptr<http_response> render(const http_request&) {
33+
return std::shared_ptr<http_response>(new string_response("Hello, World!"));
34+
}
35+
};
36+
37+
int main(int argc, char** argv) {
38+
webserver ws = create_webserver(8080)
39+
.log_access(custom_access_log);
40+
41+
hello_world_resource hwr;
42+
ws.register_resource("/hello", &hwr);
43+
ws.start(true);
44+
45+
return 0;
46+
}

examples/handlers.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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_GET(const http_request&) {
28+
return std::shared_ptr<http_response>(new string_response("GET: Hello, World!"));
29+
}
30+
31+
const std::shared_ptr<http_response> render(const http_request&) {
32+
return std::shared_ptr<http_response>(new string_response("OTHER: Hello, World!"));
33+
}
34+
};
35+
36+
int main(int argc, char** argv) {
37+
webserver ws = create_webserver(8080);
38+
39+
hello_world_resource hwr;
40+
ws.register_resource("/hello", &hwr);
41+
ws.start(true);
42+
43+
return 0;
44+
}
45+

examples/hello_with_get_arg.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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& req) {
28+
return std::shared_ptr<http_response>(new string_response("Hello: " + req.get_arg("name")));
29+
}
30+
};
31+
32+
int main(int argc, char** argv) {
33+
webserver ws = create_webserver(8080);
34+
35+
hello_world_resource hwr;
36+
ws.register_resource("/hello", &hwr);
37+
ws.start(true);
38+
39+
return 0;
40+
}

examples/setting_headers.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
std::shared_ptr<http_response> response = std::shared_ptr<http_response>(new string_response("Hello, World!"));
29+
response->with_header("MyHeader", "MyValue");
30+
return response;
31+
}
32+
};
33+
34+
int main(int argc, char** argv) {
35+
webserver ws = create_webserver(8080);
36+
37+
hello_world_resource hwr;
38+
ws.register_resource("/hello", &hwr);
39+
ws.start(true);
40+
41+
return 0;
42+
}

0 commit comments

Comments
 (0)