Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validations on making httpRequests #28

Merged
merged 3 commits into from
Aug 24, 2024
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
1 change: 1 addition & 0 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Personally, I don't encourange using namespaces, but, I left it here just so
// that the code could be more readable ¯\_(ツ)_/¯
using namespace expresso::core;
using namespace expresso::enums;
using namespace expresso::middleware;

// Global variable, just for fun :)
Expand Down
6 changes: 3 additions & 3 deletions include/expresso/core/response.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <zippuccino/zipper.h>

#include <expresso/core/cookie.h>
#include <expresso/core/status_code.h>
#include <expresso/enums/status_code.h>

namespace expresso {

Expand All @@ -21,7 +21,7 @@ class Response {
bool hasEnded;

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

std::string message;
std::vector<Cookie *> cookies;
Expand All @@ -41,7 +41,7 @@ class Response {
void setCookie(Cookie *cookie);
std::string get(std::string headerName);

Response &status(expresso::core::STATUS_CODE code);
Response &status(expresso::enums::STATUS_CODE code);
Response &send(std::string response);
Response &json(std::string response);
Response &json(json::object response);
Expand Down
1 change: 1 addition & 0 deletions include/expresso/core/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <nexus/pool.h>

#include <expresso/core/router.h>
#include <expresso/enums/verbs.h>
#include <expresso/version.h>

namespace expresso {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace expresso {

namespace core {
namespace enums {
Jadit19 marked this conversation as resolved.
Show resolved Hide resolved

enum STATUS_CODE {
// Informational responses
Expand Down Expand Up @@ -105,6 +105,6 @@ enum STATUS_CODE {
NETWORK_CONNECT_TIMEOUT_ERROR = 599
};

} // namespace core
} // namespace enums

} // namespace expresso
15 changes: 15 additions & 0 deletions include/expresso/enums/verbs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <set>
#include <string>

namespace expresso {

namespace enums {

static const std::set<std::string> VERBS = {"GET", "POST", "PUT", "PATCH",
"DELETE", "OPTIONS", "HEAD"};

} // namespace enums

} // namespace expresso
6 changes: 3 additions & 3 deletions src/core/response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ std::map<std::string, std::set<std::string>> Response::MIME_TYPES = {

expresso::core::Response::Response(int clientSocket)
: hasEnded(false), socket(clientSocket),
statusCode(expresso::core::STATUS_CODE::OK), message("") {
statusCode(expresso::enums::STATUS_CODE::OK), message("") {
return;
}

Expand Down Expand Up @@ -57,7 +57,7 @@ std::string expresso::core::Response::get(std::string headerName) {
}

expresso::core::Response &
expresso::core::Response::status(expresso::core::STATUS_CODE code) {
expresso::core::Response::status(expresso::enums::STATUS_CODE code) {
this->statusCode = code;

return *this;
Expand Down Expand Up @@ -168,7 +168,7 @@ void expresso::core::Response::sendFiles(const std::set<std::string> &paths,
}

void expresso::core::Response::sendNotFound() {
this->status(expresso::core::STATUS_CODE::NOT_FOUND)
this->status(expresso::enums::STATUS_CODE::NOT_FOUND)
.send(expresso::core::Response::NOT_FOUND)
.end();

Expand Down
14 changes: 13 additions & 1 deletion src/core/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void expresso::core::Server::handleConnection(int clientSocket) {
logger::error(
e.what(),
"void expresso::core::Server::handleConnection(int clientSocket)");
res->status(STATUS_CODE::BAD_REQUEST).send("Bad Request");
res->status(expresso::enums::STATUS_CODE::BAD_REQUEST).send("Bad Request");
}

return;
Expand All @@ -125,8 +125,20 @@ expresso::core::Server::makeRequest(std::string &request) noexcept(false) {

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

req.path = parts[1];
req.httpVersion = parts[2];
if (req.httpVersion.substr(0, 5) != "HTTP/") {
logger::error("Invalid HTTP version: " + req.httpVersion,
"expresso::core::Server::makeRequest(std::string &request) "
"noexcept(false)");
}

req.tempPath = req.path.substr(1, req.path.size());

// Headers
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/cors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ bool expresso::middleware::Cors::use(expresso::core::Request &req,

if (requestOrigin == "") {
res.set("Access-Control-Allow-Origin", "null");
res.status(expresso::core::STATUS_CODE::FORBIDDEN)
res.status(expresso::enums::STATUS_CODE::FORBIDDEN)
.send(expresso::middleware::Cors::FORBIDDEN);
return false;
}
Expand All @@ -101,7 +101,7 @@ bool expresso::middleware::Cors::use(expresso::core::Request &req,

if (!isOriginPresent) {
res.set("Access-Control-Allow-Origin", "null");
res.status(expresso::core::STATUS_CODE::FORBIDDEN)
res.status(expresso::enums::STATUS_CODE::FORBIDDEN)
.send(expresso::middleware::Cors::FORBIDDEN);
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/static_serve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bool expresso::middleware::StaticServe::use(expresso::core::Request &req,

res.send(this->getFolderHTML(req, tempPath));
res.set("Content-Type", "text/html");
res.status(expresso::core::STATUS_CODE::OK);
res.status(expresso::enums::STATUS_CODE::OK);
return false;
}

Expand Down
Loading