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,231 @@
/*
This file is part of libhttpserver
Copyright (C) 2011-2019 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
*/
#if defined(_WIN32) && ! defined(__CYGWIN__)
#define _WINDOWS
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x600
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#endif
#include <curl/curl.h>
#include "httpserver.hpp"
#include "littletest.hpp"
#define MY_OPAQUE "11733b200778ce33060f31c9af70a870ba96ddd4"
using namespace std;
using namespace httpserver;
size_t writefunc(void *ptr, size_t size, size_t nmemb, std::string *s)
{
s->append((char*) ptr, size*nmemb);
return size*nmemb;
}
class user_pass_resource : public httpserver::http_resource
{
public:
const shared_ptr<http_response> render_GET(const http_request& req)
{
if (req.get_user() != "myuser" || req.get_pass() != "mypass")
{
return shared_ptr<basic_auth_fail_response>(new basic_auth_fail_response("FAIL", "examplerealm"));
}
return shared_ptr<string_response>(new string_response(req.get_user() + " " + req.get_pass(), 200, "text/plain"));
}
};
class digest_resource : public httpserver::http_resource
{
public:
const shared_ptr<http_response> render_GET(const http_request& req)
{
if (req.get_digested_user() == "") {
return shared_ptr<digest_auth_fail_response>(new digest_auth_fail_response("FAIL", "examplerealm", MY_OPAQUE, true));
}
else
{
bool reload_nonce = false;
if(!req.check_digest_auth("examplerealm", "mypass", 300, reload_nonce))
{
return shared_ptr<digest_auth_fail_response>(new digest_auth_fail_response("FAIL", "examplerealm", MY_OPAQUE, reload_nonce));
}
}
return shared_ptr<string_response>(new string_response("SUCCESS", 200, "text/plain"));
}
};
LT_BEGIN_SUITE(authentication_suite)
void set_up()
{
}
void tear_down()
{
}
LT_END_SUITE(authentication_suite)
LT_BEGIN_AUTO_TEST(authentication_suite, base_auth)
webserver ws = create_webserver(8080);
user_pass_resource user_pass;
ws.register_resource("base", &user_pass);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_USERNAME, "myuser");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "mypass");
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "myuser mypass");
curl_easy_cleanup(curl);
ws.stop();
LT_END_AUTO_TEST(base_auth)
LT_BEGIN_AUTO_TEST(authentication_suite, base_auth_fail)
webserver ws = create_webserver(8080);
user_pass_resource user_pass;
ws.register_resource("base", &user_pass);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_USERNAME, "myuser");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "wrongpass");
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "FAIL");
curl_easy_cleanup(curl);
ws.stop();
LT_END_AUTO_TEST(base_auth_fail)
// do not run the digest auth tests on windows as curl
// appears to have problems with it.
// Will fix this separately
#ifndef _WINDOWS
LT_BEGIN_AUTO_TEST(authentication_suite, digest_auth)
webserver ws = create_webserver(8080)
.digest_auth_random("myrandom")
.nonce_nc_size(300);
digest_resource digest;
ws.register_resource("base", &digest);
ws.start(false);
#if defined(_WINDOWS)
curl_global_init(CURL_GLOBAL_WIN32 );
#else
curl_global_init(CURL_GLOBAL_ALL);
#endif
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
#if defined(_WINDOWS)
curl_easy_setopt(curl, CURLOPT_USERPWD, "examplerealm/myuser:mypass");
#else
curl_easy_setopt(curl, CURLOPT_USERPWD, "myuser:mypass");
#endif
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 150L);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 150L);
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "SUCCESS");
curl_easy_cleanup(curl);
ws.stop();
LT_END_AUTO_TEST(digest_auth)
LT_BEGIN_AUTO_TEST(authentication_suite, digest_auth_wrong_pass)
webserver ws = create_webserver(8080)
.digest_auth_random("myrandom")
.nonce_nc_size(300);
digest_resource digest;
ws.register_resource("base", &digest);
ws.start(false);
#if defined(_WINDOWS)
curl_global_init(CURL_GLOBAL_WIN32 );
#else
curl_global_init(CURL_GLOBAL_ALL);
#endif
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
#if defined(_WINDOWS)
curl_easy_setopt(curl, CURLOPT_USERPWD, "examplerealm/myuser:wrongpass");
#else
curl_easy_setopt(curl, CURLOPT_USERPWD, "myuser:wrongpass");
#endif
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 150L);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 150L);
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "FAIL");
curl_easy_cleanup(curl);
ws.stop();
LT_END_AUTO_TEST(digest_auth_wrong_pass)
#endif
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()

View File

@ -0,0 +1,166 @@
/*
This file is part of libhttpserver
Copyright (C) 2011-2019 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 <curl/curl.h>
#include <map>
#include <string>
#include "httpserver.hpp"
#include "httpserver/http_utils.hpp"
#include "littletest.hpp"
using namespace httpserver;
using namespace std;
using namespace http;
size_t writefunc(void *ptr, size_t size, size_t nmemb, std::string *s)
{
s->append((char*) ptr, size*nmemb);
return size*nmemb;
}
class ok_resource : public http_resource
{
public:
const shared_ptr<http_response> render_GET(const http_request& req)
{
return shared_ptr<string_response>(new string_response("OK", 200, "text/plain"));
}
};
LT_BEGIN_SUITE(ban_system_suite)
void set_up()
{
}
void tear_down()
{
}
LT_END_SUITE(ban_system_suite)
LT_BEGIN_AUTO_TEST(ban_system_suite, accept_default_ban_blocks)
webserver ws = create_webserver(8080).default_policy(http_utils::ACCEPT);
ws.start(false);
ok_resource resource;
ws.register_resource("base", &resource);
curl_global_init(CURL_GLOBAL_ALL);
{
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
}
{
ws.ban_ip("127.0.0.1");
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
res = curl_easy_perform(curl);
LT_ASSERT_NEQ(res, 0);
curl_easy_cleanup(curl);
}
{
ws.unban_ip("127.0.0.1");
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
}
curl_global_cleanup();
ws.stop();
LT_END_AUTO_TEST(accept_default_ban_blocks)
LT_BEGIN_AUTO_TEST(ban_system_suite, reject_default_allow_passes)
webserver ws = create_webserver(8080).default_policy(http_utils::REJECT);
ws.start(false);
ok_resource resource;
ws.register_resource("base", &resource);
curl_global_init(CURL_GLOBAL_ALL);
{
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
res = curl_easy_perform(curl);
LT_ASSERT_NEQ(res, 0);
curl_easy_cleanup(curl);
}
{
ws.allow_ip("127.0.0.1");
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
}
{
ws.disallow_ip("127.0.0.1");
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
res = curl_easy_perform(curl);
LT_ASSERT_NEQ(res, 0);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
ws.stop();
LT_END_AUTO_TEST(reject_default_allow_passes)
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,165 @@
/*
This file is part of libhttpserver
Copyright (C) 2011-2019 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
*/
#if defined(_WIN32) && ! defined(__CYGWIN__)
#define _WINDOWS
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x600
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#endif
#include <curl/curl.h>
#include <signal.h>
#include <unistd.h>
#include "httpserver.hpp"
#include "littletest.hpp"
using namespace std;
using namespace httpserver;
size_t writefunc(void *ptr, size_t size, size_t nmemb, std::string *s)
{
s->append((char*) ptr, size*nmemb);
return size*nmemb;
}
static int counter = 0;
struct test_data
{
int value;
};
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();
}
}
ssize_t test_callback_with_data(std::shared_ptr<test_data> closure_data, char* buf, size_t max)
{
if (counter == 2)
{
return -1;
}
else
{
memset(buf, 0, max);
strcat(buf, ("test" + std::to_string(closure_data->value)).c_str());
closure_data->value = 84;
counter++;
return std::string(buf).size();
}
}
class deferred_resource : public http_resource
{
public:
const shared_ptr<http_response> render_GET(const http_request& req)
{
return shared_ptr<deferred_response<void>>(new deferred_response<void>(test_callback, nullptr, "cycle callback response"));
}
};
class deferred_resource_with_data : public http_resource
{
public:
const shared_ptr<http_response> render_GET(const http_request& req)
{
std::shared_ptr<test_data> internal_info(new test_data);
internal_info->value = 42;
return shared_ptr<deferred_response<test_data>>(new deferred_response<test_data>(test_callback_with_data, internal_info, "cycle callback response"));
}
};
LT_BEGIN_SUITE(deferred_suite)
webserver* ws;
void set_up()
{
ws = new webserver(create_webserver(8080));
ws->start(false);
}
void tear_down()
{
counter = 0;
ws->stop();
delete ws;
}
LT_END_SUITE(deferred_suite)
LT_BEGIN_AUTO_TEST(deferred_suite, deferred_response)
deferred_resource resource;
ws->register_resource("base", &resource);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "testtest");
curl_easy_cleanup(curl);
LT_END_AUTO_TEST(deferred_response)
LT_BEGIN_AUTO_TEST(deferred_suite, deferred_response_with_data)
deferred_resource_with_data resource;
ws->register_resource("base", &resource);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "test42test84");
curl_easy_cleanup(curl);
LT_END_AUTO_TEST(deferred_response_with_data)
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()

View File

@ -0,0 +1,75 @@
/*
This file is part of libhttpserver
Copyright (C) 2011-2019 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 <curl/curl.h>
#include <map>
#include <string>
#include "httpserver.hpp"
#include "littletest.hpp"
using namespace httpserver;
using namespace std;
class ok_resource : public http_resource
{
public:
const shared_ptr<http_response> render_GET(const http_request& req)
{
return shared_ptr<string_response>(new string_response("OK", 200, "text/plain"));
}
};
LT_BEGIN_SUITE(threaded_suite)
webserver* ws;
void set_up()
{
ws = new webserver(create_webserver(8080).tcp_nodelay());
ws->start(false);
}
void tear_down()
{
ws->stop();
delete ws;
}
LT_END_SUITE(threaded_suite)
LT_BEGIN_AUTO_TEST(threaded_suite, base)
ok_resource resource;
ws->register_resource("base", &resource);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL* curl;
CURLcode res;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
curl_easy_cleanup(curl);
LT_END_AUTO_TEST(base)
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()

View File

@ -0,0 +1,75 @@
/*
This file is part of libhttpserver
Copyright (C) 2011-2019 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 <curl/curl.h>
#include <map>
#include <string>
#include "httpserver.hpp"
#include "littletest.hpp"
using namespace httpserver;
using namespace std;
class ok_resource : public http_resource
{
public:
const shared_ptr<http_response> render_GET(const http_request& req)
{
return shared_ptr<string_response>(new string_response("OK", 200, "text/plain"));
}
};
LT_BEGIN_SUITE(threaded_suite)
webserver* ws;
void set_up()
{
ws = new webserver(create_webserver(8080).start_method(http::http_utils::INTERNAL_SELECT).max_threads(5));
ws->start(false);
}
void tear_down()
{
ws->stop();
delete ws;
}
LT_END_SUITE(threaded_suite)
LT_BEGIN_AUTO_TEST(threaded_suite, base)
ok_resource resource;
ws->register_resource("base", &resource);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL* curl;
CURLcode res;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
curl_easy_cleanup(curl);
LT_END_AUTO_TEST(base)
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()

View File

@ -0,0 +1,607 @@
/*
This file is part of libhttpserver
Copyright (C) 2011-2019 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
*/
#if defined(_WIN32) && ! defined(__CYGWIN__)
#define _WINDOWS
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x600
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#endif
#include <curl/curl.h>
#include <pthread.h>
#include <unistd.h>
#include "httpserver.hpp"
#include "littletest.hpp"
using namespace std;
using namespace httpserver;
size_t writefunc(void *ptr, size_t size, size_t nmemb, std::string *s)
{
s->append((char*) ptr, size*nmemb);
return size*nmemb;
}
class ok_resource : public http_resource
{
public:
const shared_ptr<http_response> render_GET(const http_request& req)
{
return shared_ptr<string_response>(new string_response("OK", 200, "text/plain"));
}
};
const shared_ptr<http_response> not_found_custom(const http_request& req)
{
return shared_ptr<string_response>(new string_response("Not found custom", 404, "text/plain"));
}
const shared_ptr<http_response> not_allowed_custom(const http_request& req)
{
return shared_ptr<string_response>(new string_response("Not allowed custom", 405, "text/plain"));
}
LT_BEGIN_SUITE(ws_start_stop_suite)
void set_up()
{
}
void tear_down()
{
}
LT_END_SUITE(ws_start_stop_suite)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, start_stop)
{
webserver ws = create_webserver(8080);
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
ws.stop();
}
{
webserver ws = create_webserver(8080).start_method(http::http_utils::INTERNAL_SELECT);
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
ws.stop();
}
{
webserver ws = create_webserver(8080).start_method(http::http_utils::THREAD_PER_CONNECTION);
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
ws.stop();
}
LT_END_AUTO_TEST(start_stop)
#if defined(IPV6_TESTS_ENABLED)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ipv6)
{
webserver ws = create_webserver(8080).use_ipv6();
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
ws.stop();
}
LT_END_AUTO_TEST(ipv6)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, dual_stack)
{
webserver ws = create_webserver(8080).use_dual_stack();
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
ws.stop();
}
LT_END_AUTO_TEST(dual_stack)
#endif
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, sweet_kill)
webserver ws = create_webserver(8080);
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
{
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
}
ws.sweet_kill();
{
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 7);
curl_easy_cleanup(curl);
}
LT_END_AUTO_TEST(sweet_kill)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, disable_options)
webserver ws = create_webserver(8080)
.no_ssl()
.no_ipv6()
.no_debug()
.no_pedantic()
.no_basic_auth()
.no_digest_auth()
.no_deferred()
.no_regex_checking()
.no_ban_system()
.no_post_process();
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
ws.stop();
LT_END_AUTO_TEST(disable_options)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, enable_options)
webserver ws = create_webserver(8080)
.debug()
.pedantic()
.deferred()
.regex_checking()
.ban_system()
.post_process();
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
ws.stop();
LT_END_AUTO_TEST(enable_options)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, custom_socket)
#ifndef DARWIN
int fd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = htons(8181);
bind(fd, (struct sockaddr*) &address, sizeof(address));
listen(fd, 10000);
webserver ws = create_webserver(-1).bind_socket(fd); //whatever port here doesn't matter
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8181/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
ws.stop();
#endif
LT_END_AUTO_TEST(custom_socket)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, single_resource)
webserver ws = create_webserver(8080).single_resource();
ok_resource ok;
ws.register_resource("/", &ok, true);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/any/url/works");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
ws.stop();
LT_END_AUTO_TEST(single_resource)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, single_resource_not_default_resource)
webserver ws = create_webserver(8080).single_resource();
ok_resource ok;
LT_CHECK_THROW(ws.register_resource("/other", &ok, true));
LT_CHECK_THROW(ws.register_resource("/", &ok, false));
ws.start(false);
ws.stop();
LT_END_AUTO_TEST(single_resource_not_default_resource)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, thread_per_connection_fails_with_max_threads)
{
webserver ws = create_webserver(8080)
.start_method(http::http_utils::THREAD_PER_CONNECTION)
.max_threads(5);
LT_CHECK_THROW(ws.start(false));
}
LT_END_AUTO_TEST(thread_per_connection_fails_with_max_threads)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, thread_per_connection_fails_with_max_threads_stack_size)
{
webserver ws = create_webserver(8080)
.start_method(http::http_utils::THREAD_PER_CONNECTION)
.max_thread_stack_size(4*1024*1024);
LT_CHECK_THROW(ws.start(false));
}
LT_END_AUTO_TEST(thread_per_connection_fails_with_max_threads_stack_size)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, tuning_options)
webserver ws = create_webserver(8080)
.max_connections(10)
.max_threads(10)
.memory_limit(10000)
.per_IP_connection_limit(10)
.max_thread_stack_size(4*1024*1024)
.nonce_nc_size(10);
;
ok_resource ok;
ws.register_resource("base", &ok);
LT_CHECK_NOTHROW(ws.start(false));
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
ws.stop();
LT_END_AUTO_TEST(tuning_options)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ssl_base)
webserver ws = create_webserver(8080)
.use_ssl()
.https_mem_key("key.pem")
.https_mem_cert("cert.pem");
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); // avoid verifying ssl
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); // avoid verifying ssl
curl_easy_setopt(curl, CURLOPT_URL, "https://localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
curl_global_cleanup();
ws.stop();
LT_END_AUTO_TEST(ssl_base)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ssl_with_protocol_priorities)
webserver ws = create_webserver(8080)
.use_ssl()
.https_mem_key("key.pem")
.https_mem_cert("cert.pem")
.https_priorities("NONE:+VERS-TLS1.0:+AES-128-CBC:+SHA1:+RSA:+COMP-NULL");
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); // avoid verifying ssl
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); // avoid verifying ssl
curl_easy_setopt(curl, CURLOPT_URL, "https://localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
ws.stop();
LT_END_AUTO_TEST(ssl_with_protocol_priorities)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, ssl_with_trust)
webserver ws = create_webserver(8080)
.use_ssl()
.https_mem_key("key.pem")
.https_mem_cert("cert.pem")
.https_mem_trust("test_root_ca.pem");
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); // avoid verifying ssl
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); // avoid verifying ssl
curl_easy_setopt(curl, CURLOPT_URL, "https://localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
ws.stop();
LT_END_AUTO_TEST(ssl_with_trust)
void* start_ws_blocking(void* par)
{
webserver* ws = (webserver*) par;
ok_resource ok;
ws->register_resource("base", &ok);
ws->start(true);
return 0x0;
}
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, blocking_server)
webserver ws = create_webserver(8080);
pthread_t tid;
pthread_create(&tid, NULL, start_ws_blocking, (void *) &ws);
sleep(1);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
ws.stop();
char* b;
pthread_join(tid,(void**) &b);
free(b);
LT_END_AUTO_TEST(blocking_server)
LT_BEGIN_AUTO_TEST(ws_start_stop_suite, custom_error_resources)
webserver ws = create_webserver(8080)
.not_found_resource(not_found_custom)
.method_not_allowed_resource(not_allowed_custom);
ok_resource ok;
ws.register_resource("base", &ok);
ws.start(false);
{
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "OK");
curl_easy_cleanup(curl);
}
{
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/not_registered");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "Not found custom");
long http_code = 0;
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
LT_ASSERT_EQ(http_code, 404);
curl_easy_cleanup(curl);
}
{
ok.set_allowing("PUT", false);
curl_global_init(CURL_GLOBAL_ALL);
std::string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "Not allowed custom");
long http_code = 0;
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
LT_ASSERT_EQ(http_code, 405);
curl_easy_cleanup(curl);
}
ws.stop();
LT_END_AUTO_TEST(custom_error_resources)
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()