initial commit
Some checks failed
BlockStorage/repertory_osx/pipeline/head There was a failure building this commit
BlockStorage/repertory_windows/pipeline/head This commit looks good
BlockStorage/repertory_linux_builds/pipeline/head This commit looks good

This commit is contained in:
2022-03-05 00:30:50 -06:00
commit 3ff46723b8
626 changed files with 178600 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
#
# This file is part of libhttpserver
# Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
LDADD = $(top_builddir)/src/libhttpserver.la
AM_CPPFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/src/httpserver/
METASOURCES = AUTO
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 benchmark_select benchmark_threads benchmark_nodelay deferred_with_accumulator
hello_world_SOURCES = hello_world.cpp
service_SOURCES = service.cpp
minimal_hello_world_SOURCES = minimal_hello_world.cpp
custom_error_SOURCES = custom_error.cpp
allowing_disallowing_methods_SOURCES = allowing_disallowing_methods.cpp
handlers_SOURCES = handlers.cpp
hello_with_get_arg_SOURCES = hello_with_get_arg.cpp
setting_headers_SOURCES = setting_headers.cpp
custom_access_log_SOURCES = custom_access_log.cpp
basic_authentication_SOURCES = basic_authentication.cpp
digest_authentication_SOURCES = digest_authentication.cpp
minimal_https_SOURCES = minimal_https.cpp
minimal_file_response_SOURCES = minimal_file_response.cpp
minimal_deferred_SOURCES = minimal_deferred.cpp
deferred_with_accumulator_SOURCES = deferred_with_accumulator.cpp
url_registration_SOURCES = url_registration.cpp
minimal_ip_ban_SOURCES = minimal_ip_ban.cpp
benchmark_select_SOURCES = benchmark_select.cpp
benchmark_threads_SOURCES = benchmark_threads.cpp
benchmark_nodelay_SOURCES = benchmark_nodelay.cpp

View File

@@ -0,0 +1,58 @@
Example Programs
================
hello_world.cpp - a very simple example of using libhttpserver to
create a Rest server capable of receiving and processing
HTTP requests. The server will be listening on port
8080.
service.cpp - an example using more of the libhttpserver API.
This creates a Rest server capable of running with
HTTP or HTTPS (provided that libhttpserver and
libmicrohttpd have been compiled with SSL support.
The server can be configured via command line
arguments:
-p <port> - port number to listen on (default 8080)
-s - enable HTTPS
-k - server key filename (default "key.pem")
-c - server certificate filename (default "cert.pem")
Creating Certificates
=====================
Self-signed certificates can be created using OpenSSL using the
following steps:
$ openssl genrsa -des3 -passout pass:x -out server.pass.key 2048
$ openssl rsa -passin pass:x -in server.pass.key -out server.key
$ openssl req -new -key server.key -out server.csr
$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
On the last step when prompted for a challenge password it can be left
empty.
Thanks to https://devcenter.heroku.com/articles/ssl-certificate-self
for these instructions.
Keystore configuration
======================
If using a local client such as RestClient
(https://github.com/wiztools/rest-client) for testing the Rest server
then a keystore needs to be established. These commands should be
bundled with your Java installation.
$ keytool -noprompt -import -keystore /path/to/restclient.store -alias
restclient -file /path/to/server.crt
The keys in the store can be listed as follows:
$ keytool -list -v -keystore /path/to/restclient.store
The client can then be configured to use this keystore. Thanks to
http://rubensgomes.blogspot.com/2012/01/how-to-set-up-restclient-for-ssl.html
for instructions on configuring RestClient.

View File

@@ -0,0 +1,42 @@
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <httpserver.hpp>
using namespace httpserver;
class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request&) {
return std::shared_ptr<http_response>(new string_response("Hello, World!"));
}
};
int main(int argc, char** argv) {
webserver ws = create_webserver(8080);
hello_world_resource hwr;
hwr.disallow_all();
hwr.set_allowing("GET", true);
ws.register_resource("/hello", &hwr);
ws.start(true);
return 0;
}

View File

@@ -0,0 +1,46 @@
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <httpserver.hpp>
using namespace httpserver;
class user_pass_resource : public httpserver::http_resource
{
public:
const std::shared_ptr<http_response> render_GET(const http_request& req)
{
if (req.get_user() != "myuser" || req.get_pass() != "mypass")
{
return std::shared_ptr<basic_auth_fail_response>(new basic_auth_fail_response("FAIL", "test@example.com"));
}
return std::shared_ptr<string_response>(new string_response(req.get_user() + " " + req.get_pass(), 200, "text/plain"));
}
};
int main(int argc, char** argv) {
webserver ws = create_webserver(8080);
user_pass_resource hwr;
ws.register_resource("/hello", &hwr);
ws.start(true);
return 0;
}

View File

@@ -0,0 +1,42 @@
#include <cstdlib>
#include <memory>
#include <httpserver.hpp>
#define PATH "/plaintext"
#define BODY "Hello, World!"
using namespace httpserver;
class hello_world_resource : public http_resource {
public:
hello_world_resource(const std::shared_ptr<http_response>& resp):
resp(resp)
{
}
const std::shared_ptr<http_response> render(const http_request&) {
return resp;
}
private:
std::shared_ptr<http_response> resp;
};
int main(int argc, char** argv)
{
webserver ws = create_webserver(atoi(argv[1]))
.start_method(http::http_utils::INTERNAL_SELECT)
.tcp_nodelay()
.max_threads(atoi(argv[2]));
std::shared_ptr<http_response> hello = std::shared_ptr<http_response>(new string_response(BODY, 200));
hello->with_header("Server", "libhttpserver");
hello_world_resource hwr(hello);
ws.register_resource(PATH, &hwr, false);
ws.start(true);
return 0;
}

View File

@@ -0,0 +1,41 @@
#include <cstdlib>
#include <memory>
#include <httpserver.hpp>
#define PATH "/plaintext"
#define BODY "Hello, World!"
using namespace httpserver;
class hello_world_resource : public http_resource {
public:
hello_world_resource(const std::shared_ptr<http_response>& resp):
resp(resp)
{
}
const std::shared_ptr<http_response> render(const http_request&) {
return resp;
}
private:
std::shared_ptr<http_response> resp;
};
int main(int argc, char** argv)
{
webserver ws = create_webserver(atoi(argv[1]))
.start_method(http::http_utils::INTERNAL_SELECT)
.max_threads(atoi(argv[2]));
std::shared_ptr<http_response> hello = std::shared_ptr<http_response>(new string_response(BODY, 200));
hello->with_header("Server", "libhttpserver");
hello_world_resource hwr(hello);
ws.register_resource(PATH, &hwr, false);
ws.start(true);
return 0;
}

View File

@@ -0,0 +1,40 @@
#include <cstdlib>
#include <memory>
#include <httpserver.hpp>
#define PATH "/plaintext"
#define BODY "Hello, World!"
using namespace httpserver;
class hello_world_resource : public http_resource {
public:
hello_world_resource(const std::shared_ptr<http_response>& resp):
resp(resp)
{
}
const std::shared_ptr<http_response> render(const http_request&) {
return resp;
}
private:
std::shared_ptr<http_response> resp;
};
int main(int argc, char** argv)
{
webserver ws = create_webserver(atoi(argv[1]))
.start_method(http::http_utils::THREAD_PER_CONNECTION);
std::shared_ptr<http_response> hello = std::shared_ptr<http_response>(new string_response(BODY, 200));
hello->with_header("Server", "libhttpserver");
hello_world_resource hwr(hello);
ws.register_resource(PATH, &hwr, false);
ws.start(true);
return 0;
}

View File

@@ -0,0 +1,17 @@
-----BEGIN CERTIFICATE-----
MIICpjCCAZCgAwIBAgIESEPtjjALBgkqhkiG9w0BAQUwADAeFw0wODA2MDIxMjU0
MzhaFw0wOTA2MDIxMjU0NDZaMAAwggEfMAsGCSqGSIb3DQEBAQOCAQ4AMIIBCQKC
AQC03TyUvK5HmUAirRp067taIEO4bibh5nqolUoUdo/LeblMQV+qnrv/RNAMTx5X
fNLZ45/kbM9geF8qY0vsPyQvP4jumzK0LOJYuIwmHaUm9vbXnYieILiwCuTgjaud
3VkZDoQ9fteIo+6we9UTpVqZpxpbLulBMh/VsvX0cPJ1VFC7rT59o9hAUlFf9jX/
GmKdYI79MtgVx0OPBjmmSD6kicBBfmfgkO7bIGwlRtsIyMznxbHu6VuoX/eVxrTv
rmCwgEXLWRZ6ru8MQl5YfqeGXXRVwMeXU961KefbuvmEPccgCxm8FZ1C1cnDHFXh
siSgAzMBjC/b6KVhNQ4KnUdZAgMBAAGjLzAtMAwGA1UdEwEB/wQCMAAwHQYDVR0O
BBYEFJcUvpjvE5fF/yzUshkWDpdYiQh/MAsGCSqGSIb3DQEBBQOCAQEARP7eKSB2
RNd6XjEjK0SrxtoTnxS3nw9sfcS7/qD1+XHdObtDFqGNSjGYFB3Gpx8fpQhCXdoN
8QUs3/5ZVa5yjZMQewWBgz8kNbnbH40F2y81MHITxxCe1Y+qqHWwVaYLsiOTqj2/
0S3QjEJ9tvklmg7JX09HC4m5QRYfWBeQLD1u8ZjA1Sf1xJriomFVyRLI2VPO2bNe
JDMXWuP+8kMC7gEvUnJ7A92Y2yrhu3QI3bjPk8uSpHea19Q77tul1UVBJ5g+zpH3
OsF5p0MyaVf09GTzcLds5nE/osTdXGUyHJapWReVmPm3Zn6gqYlnzD99z+DPIgIV
RhZvQx74NQnS6g==
-----END CERTIFICATE-----

View File

@@ -0,0 +1,47 @@
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <iostream>
#include <httpserver.hpp>
using namespace httpserver;
void custom_access_log(const std::string& url) {
std::cout << "ACCESSING: " << url << std::endl;
}
class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request&) {
return std::shared_ptr<http_response>(new string_response("Hello, World!"));
}
};
int main(int argc, char** argv) {
webserver ws = create_webserver(8080)
.log_access(custom_access_log);
hello_world_resource hwr;
ws.register_resource("/hello", &hwr);
ws.start(true);
return 0;
}

View File

@@ -0,0 +1,54 @@
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <httpserver.hpp>
using namespace httpserver;
const std::shared_ptr<http_response> not_found_custom(const http_request& req)
{
return std::shared_ptr<string_response>(new string_response("Not found custom", 404, "text/plain"));
}
const std::shared_ptr<http_response> not_allowed_custom(const http_request& req)
{
return std::shared_ptr<string_response>(new string_response("Not allowed custom", 405, "text/plain"));
}
class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request&) {
return std::shared_ptr<http_response>(new string_response("Hello, World!"));
}
};
int main(int argc, char** argv) {
webserver ws = create_webserver(8080)
.not_found_resource(not_found_custom)
.method_not_allowed_resource(not_allowed_custom);
hello_world_resource hwr;
hwr.disallow_all();
hwr.set_allowing("GET", true);
ws.register_resource("/hello", &hwr);
ws.start(true);
return 0;
}

View File

@@ -0,0 +1,74 @@
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <atomic>
#include <chrono>
#include <thread>
#include <httpserver.hpp>
using namespace httpserver;
std::atomic<int> counter;
ssize_t test_callback (std::shared_ptr<std::atomic<int> > closure_data, char* buf, size_t max) {
int reqid;
if (closure_data == nullptr) {
reqid = -1;
} else {
reqid = *closure_data;
}
// only first 5 connections can be established
if (reqid >= 5) {
return -1;
} else {
// respond corresponding request IDs to the clients
std::string str = "";
str += std::to_string(reqid) + " ";
memset(buf, 0, max);
std::copy(str.begin(), str.end(), buf);
// keep sending reqid
// sleep(1); ==> adapted for C++11 on non-*Nix systems
std::this_thread::sleep_for(std::chrono::seconds(1));
return (ssize_t)max;
}
}
class deferred_resource : public http_resource {
public:
const std::shared_ptr<http_response> render_GET(const http_request& req) {
std::shared_ptr<std::atomic<int> > closure_data(new std::atomic<int>(counter++));
return std::shared_ptr<deferred_response<std::atomic<int> > >(new deferred_response<std::atomic<int> >(test_callback, closure_data, "cycle callback response"));
}
};
int main(int argc, char** argv) {
webserver ws = create_webserver(8080);
deferred_resource hwr;
ws.register_resource("/hello", &hwr);
ws.start(true);
return 0;
}

View File

@@ -0,0 +1,51 @@
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <httpserver.hpp>
#define MY_OPAQUE "11733b200778ce33060f31c9af70a870ba96ddd4"
using namespace httpserver;
class digest_resource : public httpserver::http_resource {
public:
const std::shared_ptr<http_response> render_GET(const http_request& req) {
if (req.get_digested_user() == "") {
return std::shared_ptr<digest_auth_fail_response>(new digest_auth_fail_response("FAIL", "test@example.com", MY_OPAQUE, true));
}
else {
bool reload_nonce = false;
if(!req.check_digest_auth("test@example.com", "mypass", 300, reload_nonce)) {
return std::shared_ptr<digest_auth_fail_response>(new digest_auth_fail_response("FAIL", "test@example.com", MY_OPAQUE, reload_nonce));
}
}
return std::shared_ptr<string_response>(new string_response("SUCCESS", 200, "text/plain"));
}
};
int main(int argc, char** argv) {
webserver ws = create_webserver(8080);
digest_resource hwr;
ws.register_resource("/hello", &hwr);
ws.start(true);
return 0;
}

View File

@@ -0,0 +1,45 @@
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <httpserver.hpp>
using namespace httpserver;
class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render_GET(const http_request&) {
return std::shared_ptr<http_response>(new string_response("GET: Hello, World!"));
}
const std::shared_ptr<http_response> render(const http_request&) {
return std::shared_ptr<http_response>(new string_response("OTHER: Hello, World!"));
}
};
int main(int argc, char** argv) {
webserver ws = create_webserver(8080);
hello_world_resource hwr;
ws.register_resource("/hello", &hwr);
ws.start(true);
return 0;
}

View File

@@ -0,0 +1,40 @@
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <httpserver.hpp>
using namespace httpserver;
class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request& req) {
return std::shared_ptr<http_response>(new string_response("Hello: " + req.get_arg("name")));
}
};
int main(int argc, char** argv) {
webserver ws = create_webserver(8080);
hello_world_resource hwr;
ws.register_resource("/hello", &hwr);
ws.start(true);
return 0;
}

View File

@@ -0,0 +1,67 @@
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <iostream>
#include <httpserver.hpp>
using namespace httpserver;
class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request&);
void set_some_data(const std::string &s) {data = s;}
std::string data;
};
//using the render method you are able to catch each type of request you receive
const std::shared_ptr<http_response> hello_world_resource::render(const http_request& req)
{
//it is possible to store data inside the resource object that can be altered
//through the requests
std::cout << "Data was: " << data << std::endl;
std::string datapar = req.get_arg("data");
set_some_data(datapar == "" ? "no data passed!!!" : datapar);
std::cout << "Now data is:" << data << std::endl;
//it is possible to send a response initializing an http_string_response
//that reads the content to send in response from a string.
return std::shared_ptr<http_response>(new string_response("Hello World!!!", 200));
}
int main()
{
//it is possible to create a webserver passing a great number of parameters.
//In this case we are just passing the port and the number of thread running.
webserver ws = create_webserver(8080).start_method(http::http_utils::INTERNAL_SELECT).max_threads(5);
hello_world_resource hwr;
//this way we are registering the hello_world_resource to answer for the endpoint
//"/hello". The requested method is called (if the request is a GET we call the render_GET
//method. In case that the specific render method is not implemented, the generic "render"
//method is called.
ws.register_resource("/hello", &hwr, true);
//This way we are putting the created webserver in listen. We pass true in order to have
//a blocking call; if we want the call to be non-blocking we can just pass false to the
//method.
ws.start(true);
return 0;
}

View File

@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAtN08lLyuR5lAIq0adOu7WiBDuG4m4eZ6qJVKFHaPy3m5TEFf
qp67/0TQDE8eV3zS2eOf5GzPYHhfKmNL7D8kLz+I7psytCziWLiMJh2lJvb2152I
niC4sArk4I2rnd1ZGQ6EPX7XiKPusHvVE6VamacaWy7pQTIf1bL19HDydVRQu60+
faPYQFJRX/Y1/xpinWCO/TLYFcdDjwY5pkg+pInAQX5n4JDu2yBsJUbbCMjM58Wx
7ulbqF/3lca0765gsIBFy1kWeq7vDEJeWH6nhl10VcDHl1PetSnn27r5hD3HIAsZ
vBWdQtXJwxxV4bIkoAMzAYwv2+ilYTUOCp1HWQIDAQABAoIBAArOQv3R7gmqDspj
lDaTFOz0C4e70QfjGMX0sWnakYnDGn6DU19iv3GnX1S072ejtgc9kcJ4e8VUO79R
EmqpdRR7k8dJr3RTUCyjzf/C+qiCzcmhCFYGN3KRHA6MeEnkvRuBogX4i5EG1k5l
/5t+YBTZBnqXKWlzQLKoUAiMLPg0eRWh+6q7H4N7kdWWBmTpako7TEqpIwuEnPGx
u3EPuTR+LN6lF55WBePbCHccUHUQaXuav18NuDkcJmCiMArK9SKb+h0RqLD6oMI/
dKD6n8cZXeMBkK+C8U/K0sN2hFHACsu30b9XfdnljgP9v+BP8GhnB0nCB6tNBCPo
32srOwECgYEAxWh3iBT4lWqL6bZavVbnhmvtif4nHv2t2/hOs/CAq8iLAw0oWGZc
+JEZTUDMvFRlulr0kcaWra+4fN3OmJnjeuFXZq52lfMgXBIKBmoSaZpIh2aDY1Rd
RbEse7nQl9hTEPmYspiXLGtnAXW7HuWqVfFFP3ya8rUS3t4d07Hig8ECgYEA6ou6
OHiBRTbtDqLIv8NghARc/AqwNWgEc9PelCPe5bdCOLBEyFjqKiT2MttnSSUc2Zob
XhYkHC6zN1Mlq30N0e3Q61YK9LxMdU1vsluXxNq2rfK1Scb1oOlOOtlbV3zA3VRF
hV3t1nOA9tFmUrwZi0CUMWJE/zbPAyhwWotKyZkCgYEAh0kFicPdbABdrCglXVae
SnfSjVwYkVuGd5Ze0WADvjYsVkYBHTvhgRNnRJMg+/vWz3Sf4Ps4rgUbqK8Vc20b
AU5G6H6tlCvPRGm0ZxrwTWDHTcuKRVs+pJE8C/qWoklE/AAhjluWVoGwUMbPGuiH
6Gf1bgHF6oj/Sq7rv/VLZ8ECgYBeq7ml05YyLuJutuwa4yzQ/MXfghzv4aVyb0F3
QCdXR6o2IYgR6jnSewrZKlA9aPqFJrwHNR6sNXlnSmt5Fcf/RWO/qgJQGLUv3+rG
7kuLTNDR05azSdiZc7J89ID3Bkb+z2YkV+6JUiPq/Ei1+nDBEXb/m+/HqALU/nyj
P3gXeQKBgBusb8Rbd+KgxSA0hwY6aoRTPRt8LNvXdsB9vRcKKHUFQvxUWiUSS+L9
/Qu1sJbrUquKOHqksV5wCnWnAKyJNJlhHuBToqQTgKXjuNmVdYSe631saiI7PHyC
eRJ6DxULPxABytJrYCRrNqmXi5TCiqR2mtfalEMOPxz8rUU8dYyx
-----END RSA PRIVATE KEY-----

View File

@@ -0,0 +1,55 @@
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <httpserver.hpp>
using namespace httpserver;
static int counter = 0;
ssize_t test_callback (std::shared_ptr<void> closure_data, char* buf, size_t max) {
if (counter == 2) {
return -1;
}
else {
memset(buf, 0, max);
strcat(buf, " test ");
counter++;
return std::string(buf).size();
}
}
class deferred_resource : public http_resource {
public:
const std::shared_ptr<http_response> render_GET(const http_request& req) {
return std::shared_ptr<deferred_response<void> >(new deferred_response<void>(test_callback, nullptr, "cycle callback response"));
}
};
int main(int argc, char** argv) {
webserver ws = create_webserver(8080);
deferred_resource hwr;
ws.register_resource("/hello", &hwr);
ws.start(true);
return 0;
}

View File

@@ -0,0 +1,42 @@
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <httpserver.hpp>
using namespace httpserver;
class file_response_resource : public http_resource
{
public:
const std::shared_ptr<http_response> render_GET(const http_request& req)
{
return std::shared_ptr<file_response>(new file_response("test_content", 200, "text/plain"));
}
};
int main(int argc, char** argv) {
webserver ws = create_webserver(8080);
file_response_resource hwr;
ws.register_resource("/hello", &hwr);
ws.start(true);
return 0;
}

View File

@@ -0,0 +1,40 @@
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <httpserver.hpp>
using namespace httpserver;
class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request&) {
return std::shared_ptr<http_response>(new string_response("Hello, World!"));
}
};
int main(int argc, char** argv) {
webserver ws = create_webserver(8080);
hello_world_resource hwr;
ws.register_resource("/hello", &hwr);
ws.start(true);
return 0;
}

View File

@@ -0,0 +1,43 @@
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <httpserver.hpp>
using namespace httpserver;
class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request&) {
return std::shared_ptr<http_response>(new string_response("Hello, World!"));
}
};
int main(int argc, char** argv) {
webserver ws = create_webserver(8080)
.use_ssl()
.https_mem_key("key.pem")
.https_mem_cert("cert.pem");
hello_world_resource hwr;
ws.register_resource("/hello", &hwr);
ws.start(true);
return 0;
}

View File

@@ -0,0 +1,43 @@
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <httpserver.hpp>
using namespace httpserver;
class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request&) {
return std::shared_ptr<http_response>(new string_response("Hello, World!"));
}
};
int main(int argc, char** argv) {
webserver ws = create_webserver(8080)
.default_policy(http::http_utils::REJECT);
ws.allow_ip("127.0.0.1");
hello_world_resource hwr;
ws.register_resource("/hello", &hwr);
ws.start(true);
return 0;
}

View File

@@ -0,0 +1,242 @@
/*
This file is part of libhttpserver
Copyright (C) 2014 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <unistd.h>
#include <cstdio>
#include <iostream>
#include <httpserver.hpp>
using namespace httpserver;
bool verbose=false;
class service_resource: public http_resource {
public:
service_resource();
~service_resource();
const std::shared_ptr<http_response> render_GET(const http_request &req);
const std::shared_ptr<http_response> render_PUT(const http_request &req);
const std::shared_ptr<http_response> render_POST(const http_request &req);
const std::shared_ptr<http_response> render(const http_request &req);
const std::shared_ptr<http_response> render_HEAD(const http_request &req);
const std::shared_ptr<http_response> render_OPTIONS(const http_request &req);
const std::shared_ptr<http_response> render_CONNECT(const http_request &req);
const std::shared_ptr<http_response> render_DELETE(const http_request &req);
private:
};
service_resource::service_resource()
{}
service_resource::~service_resource()
{}
const std::shared_ptr<http_response>
service_resource::render_GET(const http_request &req)
{
std::cout << "service_resource::render_GET()" << std::endl;
if (verbose) std::cout << req;
string_response* res = new string_response("GET response", 200);
if (verbose) std::cout << *res;
return std::shared_ptr<http_response>(res);
}
const std::shared_ptr<http_response>
service_resource::render_PUT(const http_request &req)
{
std::cout << "service_resource::render_PUT()" << std::endl;
if (verbose) std::cout << req;
string_response* res = new string_response("PUT response", 200);
if (verbose) std::cout << *res;
return std::shared_ptr<http_response>(res);
}
const std::shared_ptr<http_response>
service_resource::render_POST(const http_request &req)
{
std::cout << "service_resource::render_POST()" << std::endl;
if (verbose) std::cout << req;
string_response* res = new string_response("POST response", 200);
if (verbose) std::cout << *res;
return std::shared_ptr<http_response>(res);
}
const std::shared_ptr<http_response>
service_resource::render(const http_request &req)
{
std::cout << "service_resource::render()" << std::endl;
if (verbose) std::cout << req;
string_response* res = new string_response("generic response", 200);
if (verbose) std::cout << *res;
return std::shared_ptr<http_response>(res);
}
const std::shared_ptr<http_response>
service_resource::render_HEAD(const http_request &req)
{
std::cout << "service_resource::render_HEAD()" << std::endl;
if (verbose) std::cout << req;
string_response* res = new string_response("HEAD response", 200);
if (verbose) std::cout << *res;
return std::shared_ptr<http_response>(res);
}
const std::shared_ptr<http_response>
service_resource::render_OPTIONS(const http_request &req)
{
std::cout << "service_resource::render_OPTIONS()" << std::endl;
if (verbose) std::cout << req;
string_response* res = new string_response("OPTIONS response", 200);
if (verbose) std::cout << *res;
return std::shared_ptr<http_response>(res);
}
const std::shared_ptr<http_response>
service_resource::render_CONNECT(const http_request &req)
{
std::cout << "service_resource::render_CONNECT()" << std::endl;
if (verbose) std::cout << req;
string_response* res = new string_response("CONNECT response", 200);
if (verbose) std::cout << *res;
return std::shared_ptr<http_response>(res);
}
const std::shared_ptr<http_response>
service_resource::render_DELETE(const http_request &req)
{
std::cout << "service_resource::render_DELETE()" << std::endl;
if (verbose) std::cout << req;
string_response* res = new string_response("DELETE response", 200);
if (verbose) std::cout << *res;
return std::shared_ptr<http_response>(res);
}
void usage()
{
std::cout << "Usage:" << std::endl
<< "service [-p <port>][-s [-k <keyFileName>][-c <certFileName>]][-v]" << std::endl;
}
int main(int argc, char **argv)
{
uint16_t port=8080;
int c;
const char *key="key.pem";
const char *cert="cert.pem";
bool secure=false;
while ((c = getopt(argc,argv,"p:k:c:sv?")) != EOF) {
switch (c) {
case 'p':
port=strtoul(optarg,NULL,10);
break;
case 'k':
key = optarg;
break;
case 'c':
cert=optarg;
break;
case 's':
secure=true;
break;
case 'v':
verbose=true;
break;
default:
usage();
exit(1);
break;
}
}
std::cout << "Using port " << port << std::endl;
if (secure) {
std::cout << "Key: " << key << " Certificate: " << cert
<< std::endl;
}
//
// Use builder to define webserver configuration options
//
create_webserver cw = create_webserver(port).max_threads(5);
if (secure) {
cw.use_ssl().https_mem_key(key).https_mem_cert(cert);
}
//
// Create webserver using the configured options
//
webserver ws = cw;
//
// Create and register service resource available at /service
//
service_resource res;
ws.register_resource("/service",&res,true);
//
// Start and block the webserver
//
ws.start(true);
return 0;
}

View File

@@ -0,0 +1,42 @@
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <httpserver.hpp>
using namespace httpserver;
class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request&) {
std::shared_ptr<http_response> response = std::shared_ptr<http_response>(new string_response("Hello, World!"));
response->with_header("MyHeader", "MyValue");
return response;
}
};
int main(int argc, char** argv) {
webserver ws = create_webserver(8080);
hello_world_resource hwr;
ws.register_resource("/hello", &hwr);
ws.start(true);
return 0;
}

View File

@@ -0,0 +1 @@
test content of file

View File

@@ -0,0 +1,63 @@
/*
This file is part of libhttpserver
Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#include <httpserver.hpp>
using namespace httpserver;
class hello_world_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request&) {
return std::shared_ptr<http_response>(new string_response("Hello, World!"));
}
};
class handling_multiple_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request& req) {
return std::shared_ptr<http_response>(new string_response("Your URL: " + req.get_path()));
}
};
class url_args_resource : public http_resource {
public:
const std::shared_ptr<http_response> render(const http_request& req) {
return std::shared_ptr<http_response>(new string_response("ARGS: " + req.get_arg("arg1") + " and " + req.get_arg("arg2")));
}
};
int main(int argc, char** argv) {
webserver ws = create_webserver(8080);
hello_world_resource hwr;
ws.register_resource("/hello", &hwr);
handling_multiple_resource hmr;
ws.register_resource("/family", &hmr, true);
ws.register_resource("/with_regex_[0-9]+", &hmr);
url_args_resource uar;
ws.register_resource("/url/with/{arg1}/and/{arg2}", &uar);
ws.register_resource("/url/with/parametric/args/{arg1|[0-9]+}/and/{arg2|[A-Z]+}", &uar);
ws.start(true);
return 0;
}