Skip to content

Commit

Permalink
separate out cors constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Jadit19 committed Oct 10, 2024
1 parent a752213 commit 6e6ea57
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
31 changes: 31 additions & 0 deletions include/expresso/constants/cors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <set>
#include <string>

namespace expresso {

namespace constants {

namespace cors {

const std::set<std::string> headers = {"accept",
"access-control-allow-credentials",
"access-control-allow-headers",
"access-control-allow-methods",
"access-control-allow-origin",
"access-control-expose-headers",
"access-control-max-age",
"authorization",
"content-type",
"origin",
"user-agent",
"x-requested-with"};

const std::string forbidden = "Forbidden";

} // namespace cors

} // namespace constants

} // namespace expresso
3 changes: 0 additions & 3 deletions include/expresso/middleware/cors.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ class Cors : public Middleware {

bool use(expresso::core::Request &req,
expresso::core::Response &res) override;

static const std::string FORBIDDEN;
static const std::set<std::string> HEADERS;
};

} // namespace middleware
Expand Down
28 changes: 4 additions & 24 deletions src/middleware/cors.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
#include <expresso/constants/cors.h>
#include <expresso/middleware/cors.h>

namespace expresso::middleware {

const std::set<std::string> Cors::HEADERS = {
"accept",
"access-control-allow-credentials",
"access-control-allow-headers",
"access-control-allow-methods",
"access-control-allow-origin",
"access-control-expose-headers",
"access-control-max-age",
"authorization",
"content-type",
"origin",
"user-agent",
"x-requested-with",
};

const std::string Cors::FORBIDDEN = "Forbidden";

} // namespace expresso::middleware

expresso::middleware::Cors::Cors()
: credentials(false), allowAllOrigins(false), allowedHeaders("") {
for (std::string _header : this->HEADERS) {
for (std::string _header : expresso::constants::cors::headers) {
this->headers.insert(_header);
}

Expand Down Expand Up @@ -87,7 +67,7 @@ bool expresso::middleware::Cors::use(expresso::core::Request &req,
if (requestOrigin == "") {
res.set("access-control-allow-origin", "null");
res.status(expresso::enums::STATUS_CODE::FORBIDDEN)
.send(expresso::middleware::Cors::FORBIDDEN);
.send(expresso::constants::cors::forbidden);
return false;
}

Expand All @@ -103,7 +83,7 @@ bool expresso::middleware::Cors::use(expresso::core::Request &req,
if (!isOriginPresent) {
res.set("access-control-allow-origin", "null");
res.status(expresso::enums::STATUS_CODE::FORBIDDEN)
.send(expresso::middleware::Cors::FORBIDDEN);
.send(expresso::constants::cors::forbidden);
return false;
}

Expand Down

0 comments on commit 6e6ea57

Please sign in to comment.