Skip to content

Commit

Permalink
Add ESP8266WebServerTemplate<ServerType>::enableCORS(bool _ec) (#7388)
Browse files Browse the repository at this point in the history
Add ESP8266WebServerTemplate<ServerType>::enableCORS(bool _ec) like ESP32 Arduino platform.
  • Loading branch information
Mjxkill authored Jun 21, 2020
1 parent 786a654 commit 7298691
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 9 additions & 1 deletion libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
Modified 8 May 2015 by Hristo Gochkov (proper post and file upload handling)
*/


#include <Arduino.h>
#include <libb64/cencode.h>
#include "WiFiServer.h"
Expand Down Expand Up @@ -61,6 +60,7 @@ ESP8266WebServerTemplate<ServerType>::ESP8266WebServerTemplate(IPAddress addr, i
, _currentHeaders(nullptr)
, _contentLength(0)
, _chunked(false)
, _corsEnabled(false)
{
}

Expand All @@ -82,6 +82,7 @@ ESP8266WebServerTemplate<ServerType>::ESP8266WebServerTemplate(int port)
, _currentHeaders(nullptr)
, _contentLength(0)
, _chunked(false)
, _corsEnabled(false)
{
}

Expand All @@ -98,6 +99,10 @@ ESP8266WebServerTemplate<ServerType>::~ESP8266WebServerTemplate() {
}
}

template <typename ServerType>
void ESP8266WebServerTemplate<ServerType>::enableCORS(bool enable) {
_corsEnabled = enable;
}
template <typename ServerType>
void ESP8266WebServerTemplate<ServerType>::begin() {
close();
Expand Down Expand Up @@ -421,6 +426,9 @@ void ESP8266WebServerTemplate<ServerType>::_prepareHeader(String& response, int
sendHeader(String(F("Accept-Ranges")),String(F("none")));
sendHeader(String(F("Transfer-Encoding")),String(F("chunked")));
}
if (_corsEnabled) {
sendHeader(String(F("Access-Control-Allow-Origin")), String("*"));
}
sendHeader(String(F("Connection")), String(F("close")));

response += _responseHeaders;
Expand Down
4 changes: 4 additions & 0 deletions libraries/ESP8266WebServer/src/ESP8266WebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class ESP8266WebServerTemplate
void serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_header = NULL );
void onNotFound(THandlerFunction fn); //called when handler is not assigned
void onFileUpload(THandlerFunction fn); //handle file uploads
void enableCORS(bool enable);

const String& uri() const { return _currentUri; }
HTTPMethod method() const { return _currentMethod; }
Expand Down Expand Up @@ -244,11 +245,14 @@ class ESP8266WebServerTemplate

String _hostHeader;
bool _chunked;
bool _corsEnabled;

String _snonce; // Store noance and opaque for future comparison
String _sopaque;
String _srealm; // Store the Auth realm between Calls



};


Expand Down

0 comments on commit 7298691

Please sign in to comment.