Commit 672e4faa authored by Victor Aprea's avatar Victor Aprea Committed by Me No Dev

Add optional support for CORS headers (#2688)

* add support for CORS headers

* remove accidental function impl

* rename setCORS to enableCORS, and add aliased function enableCrossOrigin
parent f8eebb5c
......@@ -55,6 +55,7 @@ WebServer::WebServer(IPAddress addr, int port)
, _currentHeaders(nullptr)
, _contentLength(0)
, _chunked(false)
, _corsEnabled(false)
{
}
......@@ -75,6 +76,7 @@ WebServer::WebServer(int port)
, _currentHeaders(nullptr)
, _contentLength(0)
, _chunked(false)
, _corsEnabled(false)
{
}
......@@ -366,6 +368,14 @@ void WebServer::setContentLength(const size_t contentLength) {
_contentLength = contentLength;
}
void WebServer::enableCORS(boolean value) {
_corsEnabled = value;
}
void WebServer::enableCrossOrigin(boolean value) {
enableCORS(value);
}
void WebServer::_prepareHeader(String& response, int code, const char* content_type, size_t contentLength) {
response = String(F("HTTP/1.")) + String(_currentVersion) + ' ';
response += String(code);
......@@ -388,6 +398,9 @@ void WebServer::_prepareHeader(String& response, int code, const char* content_t
sendHeader(String(F("Accept-Ranges")),String(F("none")));
sendHeader(String(F("Transfer-Encoding")),String(F("chunked")));
}
if (_corsEnabled) {
sendHeader(String(FPSTR("Access-Control-Allow-Origin")), String("*"));
}
sendHeader(String(F("Connection")), String(F("close")));
response += _responseHeaders;
......
......@@ -122,6 +122,9 @@ public:
void send_P(int code, PGM_P content_type, PGM_P content);
void send_P(int code, PGM_P content_type, PGM_P content, size_t contentLength);
void enableCORS(boolean value = true);
void enableCrossOrigin(boolean value = true);
void setContentLength(const size_t contentLength);
void sendHeader(const String& name, const String& value, bool first = false);
void sendContent(const String& content);
......@@ -163,6 +166,7 @@ protected:
String value;
};
boolean _corsEnabled;
WiFiServer _server;
WiFiClient _currentClient;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment