Skip to content

Commit

Permalink
Fixed CORS on regular requests. Added OPTIONS endpoint support (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
hitecSmartHome authored Sep 18, 2024
1 parent 437c83f commit 4906302
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/PsychicHttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ class PsychicHttpServer
virtual ~PsychicHttpServer();

// what methods to support
std::list<http_method> supported_methods = {HTTP_GET, HTTP_POST, HTTP_DELETE, HTTP_HEAD, HTTP_PUT};
std::list<http_method> supported_methods = {
HTTP_GET,
HTTP_POST,
HTTP_DELETE,
HTTP_HEAD,
HTTP_PUT,
HTTP_OPTIONS
};

// esp-idf specific stuff
httpd_handle_t server;
Expand Down
2 changes: 1 addition & 1 deletion src/PsychicHttpsServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ class PsychicHttpsServer : public PsychicHttpServer
#endif // PsychicHttpsServer_h

#else
#error ESP-IDF https server support not enabled.
#warning ESP-IDF https server support not enabled.
#endif // CONFIG_ESP_HTTPS_SERVER_ENABLE
10 changes: 5 additions & 5 deletions src/PsychicMiddlewares.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ class CorsMiddleware : public PsychicMiddleware

esp_err_t run(PsychicRequest* request, PsychicResponse* response, PsychicMiddlewareCallback next) override
{
response->addHeader("Access-Control-Allow-Origin", _origin.c_str());
response->addHeader("Access-Control-Allow-Methods", _methods.c_str());
response->addHeader("Access-Control-Allow-Headers", _headers.c_str());
response->addHeader("Access-Control-Allow-Credentials", _credentials ? "true" : "false");
response->addHeader("Access-Control-Max-Age", String(_maxAge).c_str());
if (request->method() == HTTP_OPTIONS && request->hasHeader("Origin")) {
response->addHeader("Access-Control-Allow-Origin", _origin.c_str());
response->addHeader("Access-Control-Allow-Methods", _methods.c_str());
response->addHeader("Access-Control-Allow-Headers", _headers.c_str());
response->addHeader("Access-Control-Allow-Credentials", _credentials ? "true" : "false");
response->addHeader("Access-Control-Max-Age", String(_maxAge).c_str());
return response->send(200);
}
return next(request, response);
Expand Down

0 comments on commit 4906302

Please sign in to comment.