Skip to content

Commit 8b052df

Browse files
committed
Additional examples
Digest authentication. Deferred response. File response. HTTPS.
1 parent 41f1b72 commit 8b052df

File tree

6 files changed

+199
-1
lines changed

6 files changed

+199
-1
lines changed

configure.ac

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,9 @@ AC_CONFIG_LINKS([test/test_content:test/test_content])
332332
AC_CONFIG_LINKS([test/cert.pem:test/cert.pem])
333333
AC_CONFIG_LINKS([test/key.pem:test/key.pem])
334334
AC_CONFIG_LINKS([test/test_root_ca.pem:test/test_root_ca.pem])
335+
AC_CONFIG_LINKS([examples/cert.pem:examples/cert.pem])
336+
AC_CONFIG_LINKS([examples/key.pem:examples/key.pem])
337+
AC_CONFIG_LINKS([examples/test_content:examples/test_content])
335338

336339
AC_OUTPUT(
337340
libhttpserver.pc

examples/Makefile.am

Lines changed: 5 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
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
2323

2424
hello_world_SOURCES = hello_world.cpp
2525
service_SOURCES = service.cpp
@@ -31,3 +31,7 @@ hello_with_get_arg_SOURCES = hello_with_get_arg.cpp
3131
setting_headers_SOURCES = setting_headers.cpp
3232
custom_access_log_SOURCES = custom_access_log.cpp
3333
basic_authentication_SOURCES = basic_authentication.cpp
34+
digest_authentication_SOURCES = digest_authentication.cpp
35+
minimal_https_SOURCES = minimal_https.cpp
36+
minimal_file_response_SOURCES = minimal_file_response.cpp
37+
minimal_deferred_SOURCES = minimal_deferred.cpp

examples/digest_authentication.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
#define MY_OPAQUE "11733b200778ce33060f31c9af70a870ba96ddd4"
24+
25+
using namespace httpserver;
26+
27+
class digest_resource : public httpserver::http_resource {
28+
public:
29+
const std::shared_ptr<http_response> render_GET(const http_request& req) {
30+
if (req.get_digested_user() == "") {
31+
return std::shared_ptr<digest_auth_fail_response>(new digest_auth_fail_response("FAIL", "test@example.com", MY_OPAQUE, true));
32+
}
33+
else {
34+
bool reload_nonce = false;
35+
if(!req.check_digest_auth("test@example.com", "mypass", 300, reload_nonce)) {
36+
return std::shared_ptr<digest_auth_fail_response>(new digest_auth_fail_response("FAIL", "test@example.com", MY_OPAQUE, reload_nonce));
37+
}
38+
}
39+
return std::shared_ptr<string_response>(new string_response("SUCCESS", 200, "text/plain"));
40+
}
41+
};
42+
43+
int main(int argc, char** argv) {
44+
webserver ws = create_webserver(8080);
45+
46+
digest_resource hwr;
47+
ws.register_resource("/hello", &hwr);
48+
ws.start(true);
49+
50+
return 0;
51+
}

examples/minimal_deferred.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
static int counter = 0;
26+
27+
ssize_t test_callback (char* buf, size_t max) {
28+
if (counter == 2) {
29+
return -1;
30+
}
31+
else {
32+
memset(buf, 0, max);
33+
strcat(buf, " test ");
34+
counter++;
35+
return std::string(buf).size();
36+
}
37+
}
38+
39+
class deferred_resource : public http_resource {
40+
public:
41+
const std::shared_ptr<http_response> render_GET(const http_request& req) {
42+
return std::shared_ptr<deferred_response>(new deferred_response(test_callback, "cycle callback response"));
43+
}
44+
};
45+
46+
int main(int argc, char** argv) {
47+
webserver ws = create_webserver(8080);
48+
49+
deferred_resource hwr;
50+
ws.register_resource("/hello", &hwr);
51+
ws.start(true);
52+
53+
return 0;
54+
}
55+

examples/minimal_file_response.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 file_response_resource : public http_resource
26+
{
27+
public:
28+
const std::shared_ptr<http_response> render_GET(const http_request& req)
29+
{
30+
return std::shared_ptr<file_response>(new file_response("test_content", 200, "text/plain"));
31+
}
32+
};
33+
34+
int main(int argc, char** argv) {
35+
webserver ws = create_webserver(8080);
36+
37+
file_response_resource hwr;
38+
ws.register_resource("/hello", &hwr);
39+
ws.start(true);
40+
41+
return 0;
42+
}

examples/minimal_https.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+
.use_ssl()
35+
.https_mem_key("key.pem")
36+
.https_mem_cert("cert.pem");
37+
38+
hello_world_resource hwr;
39+
ws.register_resource("/hello", &hwr);
40+
ws.start(true);
41+
42+
return 0;
43+
}

0 commit comments

Comments
 (0)