Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
[submodule "lib/zippuccino"]
path = lib/zippuccino
url = git@github.com:coding-cpp/zippuccino.git
[submodule "lib/mochios"]
path = lib/mochios
url = git@github.com:coding-cpp/mochios
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ target_include_directories(server
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lib/logger/include>
$<INSTALL_INTERFACE:lib/brewtils/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lib/brewtils/include>
$<INSTALL_INTERFACE:lib/brewtils/include>
$<INSTALL_INTERFACE:lib/zippuccino/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lib/zippuccino/include>
$<INSTALL_INTERFACE:lib/mochios/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lib/mochios/include>
)
Binary file modified assets/github/build.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/github/start.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ void about(Request &req, Response &res) {
response["submodules"][4]["location"] = "lib/zippuccino";
response["submodules"][4]["name"] = "Zippuccino";

json::object mochios;
mochios["repository"] = "https://github.com/coding-cpp/mochios";
mochios["work"] = "HTTP request library for C++";
mochios["location"] = "lib/mochios";
mochios["name"] = "Mochios";
response["submodules"].push_back(mochios);

res.status(STATUS_CODE::OK).json(response).end();
return;
}
Expand Down
4 changes: 2 additions & 2 deletions include/expresso/core/router.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class Router {
expresso::messages::Response &response);
std::map<std::string, void (*)(expresso::messages::Request &request,
expresso::messages::Response &response)> &
fetchMapFromMethod(expresso::enums::method method);
void addRoute(expresso::enums::method method, std::string path,
fetchMapFromMethod(mochios::enums::method method);
void addRoute(mochios::enums::method method, std::string path,
void (*handler)(expresso::messages::Request &request,
expresso::messages::Response &response));

Expand Down
26 changes: 0 additions & 26 deletions include/expresso/enums/method.h

This file was deleted.

21 changes: 3 additions & 18 deletions include/expresso/messages/cookie.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
#pragma once

#include <string>

#include <brewtils/url.h>
#include <mochios/messages/cookie.h>

namespace expresso {

namespace messages {

class Cookie {
class Cookie : public mochios::messages::Cookie {
public:
bool secure;
bool httpOnly;
bool partitioned;

std::string name;
std::string value;
std::string domain;
std::string path;
std::string expires;
std::string maxAge;
std::string sameSite;

Cookie();
Cookie(const std::string &data);
~Cookie();

std::string serialize();
};

} // namespace messages
Expand Down
17 changes: 6 additions & 11 deletions include/expresso/messages/request.h
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
#pragma once

#include <mochios/messages/request.h>

#include <expresso/messages/response.h>

namespace expresso {

namespace messages {

class Request {
class Request : public mochios::messages::Request {
private:
public:
Request();
Request(const std::string &path);
~Request();

bool xhr;
int contentLength;

std::string host;
std::string hostname;
expresso::enums::method method;
std::string path;
std::string httpVersion;

// tempPath used internally for referencing. DO NOT USE IT! Use `path`
// instead.
std::string tempPath;

json::object body;
std::vector<Cookie *> cookies;
std::map<std::string, std::string> headers;
std::map<std::string, std::string> params;
std::map<std::string, std::string> queries;
std::vector<expresso::messages::Cookie *> cookies;

expresso::messages::Response *res;

void print();
void print() override;
};

} // namespace messages
Expand Down
9 changes: 4 additions & 5 deletions include/expresso/messages/response.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,25 @@
#include <brewtils/string.h>
#include <brewtils/sys.h>
#include <json/object.h>
#include <mochios/enums/method.h>
#include <mochios/messages/response.h>
#include <zippuccino/zipper.h>

#include <expresso/enums/method.h>
#include <expresso/enums/status_code.h>
#include <expresso/messages/cookie.h>

namespace expresso {

namespace messages {

class Response {
class Response : public mochios::messages::Response {
private:
bool hasEnded;

int socket;
expresso::enums::STATUS_CODE statusCode;

std::string message;
std::vector<Cookie *> cookies;
std::map<std::string, std::string> headers;

void sendToClient();
void sendHeaders();
Expand All @@ -51,7 +50,7 @@ class Response {
void sendInvalidRange();

void end();
void print();
void print() override;
};

} // namespace messages
Expand Down
4 changes: 2 additions & 2 deletions include/expresso/middleware/cors.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class Cors : public Middleware {

std::set<std::string> origins;
std::set<std::string> headers;
std::set<expresso::enums::method> methods;
std::set<mochios::enums::method> methods;

public:
Cors();
~Cors();

void allowOrigin(std::string origin);
void allowMethod(std::string method);
void allowMethod(expresso::enums::method method);
void allowMethod(mochios::enums::method method);
void allowHeader(std::string header);
void allowCredentials(bool credentials);

Expand Down
1 change: 1 addition & 0 deletions lib/mochios
Submodule mochios added at 4fa51e
28 changes: 14 additions & 14 deletions src/core/router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,42 @@ expresso::core::Router::~Router() {
void expresso::core::Router::get(
std::string path, void (*handler)(expresso::messages::Request &request,
expresso::messages::Response &response)) {
this->addRoute(expresso::enums::method::GET, path, handler);
this->addRoute(mochios::enums::method::GET, path, handler);
return;
}

void expresso::core::Router::post(
std::string path, void (*handler)(expresso::messages::Request &request,
expresso::messages::Response &response)) {
this->addRoute(expresso::enums::method::POST, path, handler);
this->addRoute(mochios::enums::method::POST, path, handler);
return;
}

void expresso::core::Router::put(
std::string path, void (*handler)(expresso::messages::Request &request,
expresso::messages::Response &response)) {
this->addRoute(expresso::enums::method::PUT, path, handler);
this->addRoute(mochios::enums::method::PUT, path, handler);
return;
}

void expresso::core::Router::patch(
std::string path, void (*handler)(expresso::messages::Request &request,
expresso::messages::Response &response)) {
this->addRoute(expresso::enums::method::PATCH, path, handler);
this->addRoute(mochios::enums::method::PATCH, path, handler);
return;
}

void expresso::core::Router::del(
std::string path, void (*handler)(expresso::messages::Request &request,
expresso::messages::Response &response)) {
this->addRoute(expresso::enums::method::DELETE, path, handler);
this->addRoute(mochios::enums::method::DELETE, path, handler);
return;
}

void expresso::core::Router::options(
std::string path, void (*handler)(expresso::messages::Request &request,
expresso::messages::Response &response)) {
this->addRoute(expresso::enums::method::OPTIONS, path, handler);
this->addRoute(mochios::enums::method::OPTIONS, path, handler);
return;
}

Expand Down Expand Up @@ -157,20 +157,20 @@ bool expresso::core::Router::handleMiddlewares(
std::map<std::string,
void (*)(expresso::messages::Request &request,
expresso::messages::Response &response)> &
expresso::core::Router::fetchMapFromMethod(expresso::enums::method method) {
expresso::core::Router::fetchMapFromMethod(mochios::enums::method method) {
switch (method) {
case expresso::enums::method::GET:
case mochios::enums::method::GET:
return this->getMap;
case expresso::enums::method::POST:
case mochios::enums::method::POST:
return this->postMap;
case expresso::enums::method::PUT:
case mochios::enums::method::PUT:
return this->putMap;
case expresso::enums::method::PATCH:
case mochios::enums::method::PATCH:

return this->patchMap;
case expresso::enums::method::DELETE:
case mochios::enums::method::DELETE:
return this->deleteMap;
case expresso::enums::method::OPTIONS:
case mochios::enums::method::OPTIONS:
return this->optionsMap;
default:
logger::error("Invalid method: " + std::to_string(static_cast<int>(method)),
Expand All @@ -184,7 +184,7 @@ expresso::core::Router::fetchMapFromMethod(expresso::enums::method method) {
}

void expresso::core::Router::addRoute(
expresso::enums::method method, std::string path,
mochios::enums::method method, std::string path,
void (*handler)(expresso::messages::Request &request,
expresso::messages::Response &response)) {
if (path[0] != '/') {
Expand Down
18 changes: 9 additions & 9 deletions src/core/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ void expresso::core::Server::handleConnection(int clientSocket) {

charRequest.resize(totalBytesRead);
std::string request(charRequest.data());
expresso::messages::Response *res = new expresso::messages::Response(clientSocket);
expresso::messages::Response *res =
new expresso::messages::Response(clientSocket);

try {
expresso::messages::Request req = this->makeRequest(request);
Expand All @@ -124,24 +125,23 @@ void expresso::core::Server::handleConnection(int clientSocket) {

expresso::messages::Request
expresso::core::Server::makeRequest(std::string &request) noexcept(false) {
expresso::messages::Request req;
std::string line;
std::istringstream stream(request);
std::getline(stream, line);

std::vector<std::string> parts = brewtils::string::split(line, " ");
const std::string method = brewtils::string::upper(parts[0]);
std::set<std::string>::const_iterator methodIter =
expresso::enums::methods.find(method);
if (methodIter == expresso::enums::methods.end()) {
mochios::enums::methods.find(method);
if (methodIter == mochios::enums::methods.end()) {
logger::error("Unsupported HTTP method: " + method,
"expresso::core::Server::makeRequest(std::string &request) "
"noexcept(false)");
}

req.method = static_cast<expresso::enums::method>(
std::distance(expresso::enums::methods.begin(), methodIter) - 1);
req.path = parts[1];
expresso::messages::Request req(parts[1]);
req.method = static_cast<mochios::enums::method>(
std::distance(mochios::enums::methods.begin(), methodIter) - 1);
req.httpVersion = parts[2];
if (req.httpVersion.substr(0, 5) != "HTTP/") {
logger::error("Invalid HTTP version: " + req.httpVersion,
Expand Down Expand Up @@ -206,8 +206,8 @@ expresso::core::Server::makeRequest(std::string &request) noexcept(false) {
req.tempPath = req.tempPath.substr(0, req.tempPath.size() - 1);
}

if (req.method == expresso::enums::method::GET ||
req.method == expresso::enums::method::HEAD) {
if (req.method == mochios::enums::method::GET ||
req.method == mochios::enums::method::HEAD) {
return req;
}

Expand Down
36 changes: 0 additions & 36 deletions src/enums/method.cpp

This file was deleted.

Loading
Loading