Skip to content

Commit b465716

Browse files
author
Chris Love
committed
Updated example programs and Makefiles
1 parent 91f3a42 commit b465716

File tree

6 files changed

+252
-5
lines changed

6 files changed

+252
-5
lines changed

Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ LIBTOOL_DEPS = @LIBTOOL_DEPS@
2424
AUTOMAKE_OPTIONS = foreign 1.4
2525
ACLOCAL_AMFLAGS = -I m4
2626

27-
SUBDIRS = src test
28-
DIST_SUBDIRS = src test
27+
SUBDIRS = src test examples
28+
DIST_SUBDIRS = src test examples
2929
EXTRA_DIST = libhttpserver.pc.in debian/changelog.in debian/control.in debian/copyright.in debian/rules.in debian/libhttpserver-dev.install.in debian/libhttpserver.install.in redhat/libhttpserver.SPEC.in $(DX_CONFIG)
3030

3131
MOSTLYCLEANFILES = $(DX_CLEANFILES) redhat/SOURCES/*

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ AC_OUTPUT(
176176
doc/Makefile
177177
src/Makefile
178178
test/Makefile
179+
examples/Makefile
179180
debian/changelog
180181
debian/copyright
181182
debian/control

examples/Makefile.am

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# This file is part of libhttpserver
3+
# Copyright (C) 2011, 2012, 2013, 2014 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 USA
18+
19+
LDADD = $(top_builddir)/src/libhttpserver.la
20+
AM_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/src/httpserver/
21+
METASOURCES = AUTO
22+
noinst_PROGRAMS = hello_world service
23+
24+
hello_world_SOURCES = hello_world.cpp
25+
service_SOURCES = service.cpp
26+

examples/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Example Programs
2+
================
3+
4+
hello_world.cpp - a very simple example of using libhttpserver to
5+
create a Rest server capable of receiving and processing
6+
HTTP requests. The server will be listening on port
7+
8080.
8+
9+
10+
service.cpp - an example using more of the libhttpserver API.
11+
This creates a Rest server capable of running with
12+
HTTP or HTTPS (provided that libhttpserver and
13+
libmicrohttpd have been compiled with SSL support.
14+
15+
The server can be configured via command line
16+
arguments:
17+
18+
-p <port> - port number to listen on (default 8080)
19+
-s - enable HTTPS
20+
-k - server key filename (default "key.pem")
21+
-c - server certificate filename (default "cert.pem")
22+
23+
Creating Certificates
24+
=====================
25+
Self-signed certificates can be created using OpenSSL using the
26+
following steps:
27+
28+
$ openssl genrsa -des3 -passout pass:x -out serer.pass.key 2048
29+
$ openssl rsa -passin pass:x -in server.pass.key -out server.key
30+
$ openssl req -new -key server.key -out server.csr
31+
$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
32+
33+
On the last step when prompted for a challenge password it can be left
34+
empty.
35+
36+
Thanks to https://devcenter.heroku.com/articles/ssl-certificate-self
37+
for these instructions.
38+
39+
Keystore configuration
40+
======================
41+
If using a local client such as RestClient for testing the Rest server
42+
then a keystore needs to be established. These commands should be
43+
bundled with your Java installation.
44+
45+
$ keytool -noprompt -import -keystore /path/to/restclient.store -alias
46+
restclient -file /path/to/server.crt
47+
48+
The keys in the store can be listed as follows:
49+
50+
$ keytool -list -v -keystore /path/to/restclient.store
51+
52+
The client can then be configured to use this keystore. Thanks to
53+
http://rubensgomes.blogspot.com/2012/01/how-to-set-up-restclient-for-ssl.html
54+
for instructions on configuring RestClient.
55+
56+
57+

examples/readme

Lines changed: 0 additions & 3 deletions
This file was deleted.

examples/service.cpp

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#include <httpserver.hpp>
2+
#include <iostream>
3+
#include <unistd.h>
4+
#include <cstdio>
5+
6+
using namespace httpserver;
7+
8+
class service_resource: public http_resource<service_resource> {
9+
public:
10+
service_resource();
11+
12+
~service_resource();
13+
14+
void render_GET(const http_request &req, http_response** res);
15+
16+
void render_PUT(const http_request &req, http_response** res);
17+
18+
void render_POST(const http_request &req, http_response** res);
19+
20+
void render(const http_request &req, http_response** res);
21+
22+
void render_HEAD(const http_request &req, http_response** res);
23+
24+
void render_OPTIONS(const http_request &req, http_response** res);
25+
26+
void render_CONNECT(const http_request &req, http_response** res);
27+
28+
void render_DELETE(const http_request &req, http_response** res);
29+
30+
private:
31+
32+
33+
};
34+
35+
service_resource::service_resource()
36+
{}
37+
38+
service_resource::~service_resource()
39+
{}
40+
41+
void
42+
service_resource::render_GET(const http_request &req, http_response** res)
43+
{
44+
std::cout << "service_resource::render_GET()" << std::endl;
45+
46+
*res = new http_response(http_response_builder("GET response", 200).string_response());
47+
}
48+
49+
50+
void
51+
service_resource::render_PUT(const http_request &req, http_response** res)
52+
{
53+
std::cout << "service_resource::render_PUT()" << std::endl;
54+
55+
*res = new http_response(http_response_builder("PUT response", 200).string_response());
56+
}
57+
58+
59+
void
60+
service_resource::render_POST(const http_request &req, http_response** res)
61+
{
62+
std::cout << "service_resource::render_POST()" << std::endl;
63+
64+
*res = new http_response(http_response_builder("POST response", 200).string_response());
65+
}
66+
void
67+
service_resource::render(const http_request &req, http_response** res)
68+
{
69+
std::cout << "service_resource::render()" << std::endl;
70+
71+
*res = new http_response(http_response_builder("generic response", 200).string_response());
72+
}
73+
74+
75+
void
76+
service_resource::render_HEAD(const http_request &req, http_response** res)
77+
{
78+
std::cout << "service_resource::render_HEAD()" << std::endl;
79+
80+
*res = new http_response(http_response_builder("HEAD response", 200).string_response());
81+
}
82+
83+
void
84+
service_resource::render_OPTIONS(const http_request &req, http_response** res)
85+
{
86+
std::cout << "service_resource::render_OPTIONS()" << std::endl;
87+
88+
*res = new http_response(http_response_builder("OPTIONS response", 200).string_response());
89+
}
90+
91+
void
92+
service_resource::render_CONNECT(const http_request &req, http_response** res)
93+
{
94+
std::cout << "service_resource::render_CONNECT()" << std::endl;
95+
96+
*res = new http_response(http_response_builder("CONNECT response", 200).string_response());
97+
}
98+
99+
void
100+
service_resource::render_DELETE(const http_request &req, http_response** res)
101+
{
102+
std::cout << "service_resource::render_DELETE()" << std::endl;
103+
104+
*res = new http_response(http_response_builder("DELETE response", 200).string_response());
105+
}
106+
107+
int main(int argc, char **argv)
108+
{
109+
uint16_t port=8090;
110+
int c;
111+
const char *key="key.pem";
112+
const char *cert="cert.pem";
113+
bool secure=false;
114+
115+
while ((c = getopt(argc,argv,"p:k:c:s")) != EOF) {
116+
switch (c) {
117+
case 'p':
118+
port=strtoul(optarg,NULL,10);
119+
break;
120+
case 'k':
121+
key = optarg;
122+
break;
123+
case 'c':
124+
cert=optarg;
125+
break;
126+
case 's':
127+
secure=true;
128+
break;
129+
default:
130+
break;
131+
}
132+
}
133+
134+
std::cout << "Using port " << port << std::endl;
135+
if (secure) {
136+
std::cout << "Key: " << key << " Certificate: " << cert
137+
<< std::endl;
138+
}
139+
140+
//
141+
// Use builder to define webserver configuration options
142+
//
143+
create_webserver cw = create_webserver(port).max_threads(5);
144+
145+
if (secure) {
146+
cw.use_ssl().https_mem_key(key).https_mem_cert(cert);
147+
}
148+
149+
//
150+
// Create webserver using the configured options
151+
//
152+
webserver ws = cw;
153+
154+
//
155+
// Create and register service resource available at /service
156+
//
157+
service_resource res;
158+
ws.register_resource("/service",&res,true);
159+
160+
//
161+
// Start and block the webserver
162+
//
163+
ws.start(true);
164+
165+
return 0;
166+
}

0 commit comments

Comments
 (0)