Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/pistache/endpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ namespace Pistache::Http
* [2] https://en.wikipedia.org/wiki/CRIME
*/
void useSSL(const std::string& cert, const std::string& key,
bool use_compression = false, int (*cb_password)(char *, int, int, void *) = NULL);
bool use_compression = false, int (*cb_password)(char*, int, int, void*) = NULL);

/*!
* \brief Use SSL certificate authentication on this endpoint
Expand Down
18 changes: 17 additions & 1 deletion include/pistache/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ namespace Pistache
} // namespace Uri

// Remove when RequestBuilder will be out of namespace Experimental
namespace Experimental {class RequestBuilder; }
namespace Experimental
{
class RequestBuilder;
}

// 5. Request
class Request : public Message
Expand All @@ -192,6 +195,17 @@ namespace Pistache

const Uri::Query& query() const;

void putAttribute(std::string name, std::shared_ptr<void> data);
std::shared_ptr<void> getAttribute(const std::string& name) const;
std::shared_ptr<void> tryGetAttribute(const std::string& name) const;
void removeAttribute(const std::string& name);

template <class T>
std::shared_ptr<T> getAttributeAs(const std::string& name) const
{
return std::static_pointer_cast<T>(getAttribute(name));
}

/* @Investigate: this is disabled because of a lock in the shared_ptr /
weak_ptr implementation of libstdc++. Under contention, we experience a
performance drop of 5x with that lock
Expand Down Expand Up @@ -230,6 +244,8 @@ namespace Pistache
#endif
Address address_;
std::chrono::milliseconds timeout_ = std::chrono::milliseconds(0);

std::unordered_map<std::string, std::shared_ptr<void>> data_;
};

class Handler;
Expand Down
2 changes: 1 addition & 1 deletion include/pistache/listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ namespace Pistache::Tcp
void pinWorker(size_t worker, const CpuSet& set);

void setupSSL(const std::string& cert_path, const std::string& key_path,
bool use_compression, int (*cb_password)(char *, int, int, void *));
bool use_compression, int (*cb_password)(char*, int, int, void*));
void setupSSLAuth(const std::string& ca_file, const std::string& ca_path,
int (*cb)(int, void*));

Expand Down
4 changes: 2 additions & 2 deletions src/common/description.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ namespace Pistache::Rest
{
auto group = paths(name);
auto it = std::find_if(std::begin(group), std::end(group),
[&](const Path& p) { return p.method == method; });
[&](const Path& p) { return p.method == method; });
return it != std::end(group);
}

Expand All @@ -168,7 +168,7 @@ namespace Pistache::Rest
{
auto group = paths(name);
auto it = std::find_if(std::begin(group), std::end(group),
[&](const Path& p) { return p.method == method; });
[&](const Path& p) { return p.method == method; });

if (it != std::end(group))
{
Expand Down
42 changes: 42 additions & 0 deletions src/common/http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,48 @@ namespace Pistache::Http

const Uri::Query& Request::query() const { return query_; }

void Request::putAttribute(std::string name, std::shared_ptr<void> data)
{
auto it = data_.find(name);
if (it != std::end(data_))
{
throw std::runtime_error("The data already exists");
}

data_.insert(std::make_pair(std::move(name), std::move(data)));
}

std::shared_ptr<void> Request::getAttribute(const std::string& name) const
{
auto data = tryGetAttribute(name);
if (data == nullptr)
{
throw std::runtime_error("The data does not exist");
}

return data;
}

std::shared_ptr<void> Request::tryGetAttribute(const std::string& name) const
{
auto it = data_.find(name);
if (it == std::end(data_))
return nullptr;

return it->second;
}

void Request::removeAttribute(const std::string& name)
{
auto it = data_.find(name);
if (it == std::end(data_))
{
return;
}

data_.erase(it);
}

const Address& Request::address() const { return address_; }

std::chrono::milliseconds Request::timeout() const { return timeout_; }
Expand Down
2 changes: 1 addition & 1 deletion src/server/endpoint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ namespace Pistache::Http

void Endpoint::shutdown() { listener.shutdown(); }

void Endpoint::useSSL([[maybe_unused]] const std::string& cert, [[maybe_unused]] const std::string& key, [[maybe_unused]] bool use_compression, [[maybe_unused]] int (*pass_cb)(char *, int, int, void *))
void Endpoint::useSSL([[maybe_unused]] const std::string& cert, [[maybe_unused]] const std::string& key, [[maybe_unused]] bool use_compression, [[maybe_unused]] int (*pass_cb)(char*, int, int, void*))
{
#ifndef PISTACHE_USE_SSL
throw std::runtime_error("Pistache is not compiled with SSL support.");
Expand Down
4 changes: 2 additions & 2 deletions src/server/listener.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ namespace Pistache::Tcp
ssl::SSLCtxPtr ssl_create_context(const std::string& cert,
const std::string& key,
bool use_compression,
int (*cb)(char *, int, int, void *))
int (*cb)(char*, int, int, void*))
{
const SSL_METHOD* method = SSLv23_server_method();

Expand Down Expand Up @@ -587,7 +587,7 @@ namespace Pistache::Tcp
void Listener::setupSSL(const std::string& cert_path,
const std::string& key_path,
bool use_compression,
int (*cb_password)(char *, int, int, void *))
int (*cb_password)(char*, int, int, void*))
{
SSL_load_error_strings();
OpenSSL_add_ssl_algorithms();
Expand Down
3 changes: 1 addition & 2 deletions tests/https_server_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,7 @@ TEST(https_server_test, basic_tls_request_with_password_cert)
{
Http::Endpoint server(Address("localhost", Pistache::Port(0)));

const auto passwordCallback = [](char* buf, int size, int /*rwflag*/, void* /*u*/) -> int
{
const auto passwordCallback = [](char* buf, int size, int /*rwflag*/, void * /*u*/) -> int {
static constexpr const char* const password = "test";
std::strncpy(buf, password, size);
return static_cast<int>(std::strlen(password));
Expand Down
30 changes: 30 additions & 0 deletions tests/router_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,21 @@ TEST(router_test, test_bind_shared_ptr)
endpoint->shutdown();
}

class User
{

public:
User() = default;

auto& getUserName() const
{
return username;
}

private:
std::string username { "SomeUserName" };
};

class HandlerWithAuthMiddleware : public MyHandler
{
public:
Expand All @@ -307,6 +322,9 @@ class HandlerWithAuthMiddleware : public MyHandler
if (auth->getMethod() == Pistache::Http::Header::Authorization::Method::Basic)
{
auth_succ_count++;

auto user = std::make_shared<User>();
request.putAttribute("User", user);
return true;
}
else
Expand All @@ -321,6 +339,18 @@ class HandlerWithAuthMiddleware : public MyHandler
}
}

void handle(
const Pistache::Rest::Request& request,
Pistache::Http::ResponseWriter response)
{
ASSERT_TRUE(request.tryGetAttribute("User"));

auto user = request.getAttributeAs<User>("User");
ASSERT_EQ(user->getUserName(), "SomeUserName");

MyHandler::handle(request, std::move(response));
}

int getAuthCount() { return auth_count; }
int getSuccAuthCount() { return auth_succ_count; }

Expand Down
7 changes: 4 additions & 3 deletions tests/streaming_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ namespace
// from
// https://stackoverflow.com/questions/6624667/can-i-use-libcurls-curlopt-writefunction-with-a-c11-lambda-expression#14720398
auto curl_callback = +[](void* ptr, size_t size, size_t nmemb,
void* userdata) -> size_t {
void* userdata) -> size_t {
auto* chunks = static_cast<Chunks*>(userdata);
chunks->emplace_back(static_cast<char*>(ptr), size * nmemb);
return size * nmemb;
Expand Down Expand Up @@ -266,7 +266,8 @@ TEST_F(StreamingTests, ChunkedStream)
EXPECT_EQ(chunks[2], "!");
}

class ClientDisconnectHandler : public Http::Handler {
class ClientDisconnectHandler : public Http::Handler
{
public:
HTTP_PROTOTYPE(ClientDisconnectHandler)

Expand Down Expand Up @@ -303,7 +304,7 @@ TEST(StreamingTest, ClientDisconnect)
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

CURLM* curlm = curl_multi_init();
CURLM* curlm = curl_multi_init();
int still_running = 1;
curl_multi_add_handle(curlm, curl);

Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.3.20220529
0.0.3.20220803