Skip to content

Commit

Permalink
Fixed bug with the new path-based Response where the headers were not
Browse files Browse the repository at this point in the history
being populated.
Moved the examples to source files instead of headers. There's no need
for them to be headers since each Module registers itself.
  • Loading branch information
parnham committed Jan 12, 2024
1 parent b7dfcfc commit 9add214
Show file tree
Hide file tree
Showing 18 changed files with 80 additions and 154 deletions.
2 changes: 1 addition & 1 deletion include/niven/Cookie.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace niven
Cookie(std::string name, std::string value, const DateTime &expires) : name(name), value(value), expires(expires) {}

// Build the cookie string.
std::string Build();
std::string Build() const;

// Enable the Secure flag.
Cookie &Secure();
Expand Down
81 changes: 0 additions & 81 deletions include/niven/Dependencies.h

This file was deleted.

1 change: 0 additions & 1 deletion include/niven/Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <niven/Configuration.h>
#include <niven/Module.h>
#include <niven/Http.h>
#include <niven/Dependencies.h>
#include <niven/Pipeline.h>
#include <niven/routing/Router.h>
#include <microhttpd.h>
Expand Down
51 changes: 28 additions & 23 deletions src/examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,39 @@
#include <mutex>
#include <condition_variable>

#include <emergent/redis/RedisMultiplexer.hpp>

#include "examples/Simple.hpp"
#include "examples/Entities.hpp"
#include "examples/Dependencies.hpp"
#include "examples/Pipeline.hpp"
#include "examples/Cookies.hpp"
#include "examples/Headers.hpp"
#include "examples/Query.hpp"
#include "examples/RouteCaptures.hpp"
#include "examples/Hashing.hpp"
#include "examples/Files.hpp"
// #include "examples/Simple.hpp"
// #include "examples/Entities.hpp"
// #include "examples/Dependencies.hpp"
// #include "examples/Pipeline.hpp"
// #include "examples/Cookies.hpp"
// #include "examples/Headers.hpp"
// #include "examples/Query.hpp"
// #include "examples/RouteCaptures.hpp"
// #include "examples/Hashing.hpp"
// #include "examples/Files.hpp"

using namespace std;
using namespace emg;
using namespace niven;
// using namespace std;
// using namespace emg;
// using namespace niven;


bool run = true;
condition_variable condition;
std::condition_variable condition;

int main(int argc, char **argv)
{
// Enable logging to stdout
Log::Initialise({ make_unique<logger::Console>() });
Log::Verbosity("info");
emg::Log::Initialise({ std::make_unique<emg::logger::Console>() });
emg::Log::Verbosity("info");

// Catch the interrupt/quit signals and handle appropriately
signal(SIGINT, [](int) { run = false; condition.notify_one(); });
signal(SIGQUIT, [](int) { run = false; condition.notify_one(); });


NivenHost host;
niven::NivenHost host;

// Configure the host to listen on port 8090
host.Listen(8090);
Expand All @@ -50,22 +51,23 @@ int main(int argc, char **argv)
// Refer to https://github.com/emergent-design/libemergent/ for more
// information about the C++ Redis wrappers. This Redis connection is
// used in the "include/examples/Dependencies.hpp" example.
host.Register<Redis>(make_shared<RedisMultiplexer>());
host.Register<emg::redis::Redis>(std::make_shared<emg::redis::RedisMultiplexer>());

// Resolve the singleton and invoke the Initialise function which will
// connect to redis using the default parameters (127.0.0.1:6379).
host.Resolve<Redis>()->Initialise();
host.Resolve<emg::redis::Redis>()->Initialise();


// Start the host running. This function will return true if successfully
// started and since the host has its own threading we will use a condition
// variable to wait for an interrupt or quit signal from the user.
if (host.Run())
{
cout << "Listening on port 8090" << endl;
std::cout << "Listening on port 8090\n";

std::mutex m;
std::unique_lock lock(m);

mutex m;
unique_lock<mutex> lock(m);
while (run)
{
condition.wait(lock);
Expand All @@ -74,7 +76,10 @@ int main(int argc, char **argv)
// Tell the host to stop listening.
host.Stop();
}
else cout << "Failed to initialise host" << endl;
else
{
std::cout << "Failed to initialise host\n";
}

return 0;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/examples/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ solution "examples"
kind "ConsoleApp"
targetdir "bin"
links { "niven", "hiredis", "pthread", "stdc++fs" }
files { "example.cpp" }
files { "example.cpp", "modules/**.cpp" }

project "ssl"
kind "ConsoleApp"
targetdir "bin"
links { "niven", "pthread" }
files { "ssl.cpp" }
files { "ssl.cpp", "modules/Simple.cpp" }
32 changes: 17 additions & 15 deletions src/examples/ssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
// #include <bits/signum.h>
#include <condition_variable>

#include "examples/Simple.hpp"


using namespace std;
using namespace emg;
using namespace niven;
// #include "examples/Simple.hpp"


auto KEY = R"ssl(
Expand Down Expand Up @@ -55,20 +50,20 @@ auto CERTIFICATE = R"ssl(


bool run = true;
condition_variable condition;
std::condition_variable condition;

int main(int argc, char **argv)
{
// Enable logging to stdout
Log::Initialise({ make_unique<logger::Console>() });
Log::Verbosity("info");
emg::Log::Initialise({ std::make_unique<emg::logger::Console>() });
emg::Log::Verbosity("info");

// Catch the interrupt/quit signals and handle appropriately
signal(SIGINT, [](int) { run = false; condition.notify_one(); });
signal(SIGQUIT, [](int) { run = false; condition.notify_one(); });


NivenHost host;
niven::NivenHost host;

// Listen on port 8090 and enable SSL using an embedded key and certificate.
host.Listen(8090).EnableSSL(KEY, CERTIFICATE);
Expand All @@ -82,15 +77,22 @@ int main(int argc, char **argv)
// variable to wait for an interrupt or quit signal from the user.
if (host.Run())
{
cout << "Listening on port 8090" << endl;
std::cout << "Listening on port 8090\n";

std::mutex m;
std::unique_lock lock(m);

mutex m;
unique_lock<mutex> lock(m);
while (run) condition.wait(lock);
while (run)
{
condition.wait(lock);
}

host.Stop();
}
else cout << "Failed to initialise host" << endl;
else
{
std::cout << "Failed to initialise host\n";
}

return 0;
}
2 changes: 1 addition & 1 deletion src/niven/Cookie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using namespace emergent;
namespace niven
{
// Build the cookie string.
string Cookie::Build()
string Cookie::Build() const
{
auto result = String::format("%s=%s; Path=%s", this->name, this->value, this->path);

Expand Down
61 changes: 31 additions & 30 deletions src/niven/Response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,39 +63,40 @@ namespace niven

int Response::Send(MHD_Connection *connection)
{
if (!this->path.empty())
MHD_Response *response = nullptr;

if (this->path.empty())
{
if (auto file = fopen(this->path.c_str(), "rb"))
{
auto response = MHD_create_response_from_callback(
fs::file_size(this->path),
32 * 1024,
[](void *file, uint64_t pos, char *buffer, size_t max) {
fseek((FILE *)file, pos, SEEK_SET);
return (ssize_t)fread(buffer, 1, max, (FILE *)file);
},
file,
[](void *file) { fclose((FILE *)file); }
);

MHD_add_response_header(response, "Server", "niven/0.1");
MHD_add_response_header(response, "Keep-Alive", "timeout=60");

int result = MHD_queue_response(connection, (int)this->status, response);

MHD_destroy_response(response);

return result;
}

this->status = Http::NotFound;
// auto response = MHD_create_response_from_buffer(this->data.size(), (void *)this->data.data(), MHD_RESPMEM_PERSISTENT);
response = MHD_create_response_from_buffer(this->data.size(), (void *)this->data.data(), MHD_RESPMEM_MUST_COPY);
}
else if (auto file = fopen(this->path.c_str(), "rb"))
{
response = MHD_create_response_from_callback(
fs::file_size(this->path),
32 * 1024,
[](void *file, uint64_t pos, char *buffer, size_t max) {
fseek((FILE *)file, pos, SEEK_SET);
return (ssize_t)fread(buffer, 1, max, (FILE *)file);
},
file,
[](void *file) { fclose((FILE *)file); }
);
}
else
{
response = MHD_create_response_from_buffer(0, nullptr, MHD_RESPMEM_MUST_COPY);
this->status = Http::NotFound;
}

// auto response = MHD_create_response_from_buffer(this->data.size(), (void *)this->data.data(), MHD_RESPMEM_PERSISTENT);
auto response = MHD_create_response_from_buffer(this->data.size(), (void *)this->data.data(), MHD_RESPMEM_MUST_COPY);

for (auto &h : this->headers) MHD_add_response_header(response, h.first.c_str(), h.second.c_str());
for (auto &c : this->cookies) MHD_add_response_header(response, "Set-Cookie", c.Build().c_str());
for (const auto &[name, value] : this->headers)
{
MHD_add_response_header(response, name.c_str(), value.c_str());
}
for (const auto &c : this->cookies)
{
MHD_add_response_header(response, "Set-Cookie", c.Build().c_str());
}

MHD_add_response_header(response, "Server", "niven/0.1");
MHD_add_response_header(response, "Keep-Alive", "timeout=60");
Expand Down

0 comments on commit 9add214

Please sign in to comment.