mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-12 01:53:07 +03:00
Add ESP8266WebServerTemplate<ServerType>::enableCORS(bool _ec) (#7388)
Add ESP8266WebServerTemplate<ServerType>::enableCORS(bool _ec) like ESP32 Arduino platform.
This commit is contained in:
@ -20,7 +20,6 @@
|
|||||||
Modified 8 May 2015 by Hristo Gochkov (proper post and file upload handling)
|
Modified 8 May 2015 by Hristo Gochkov (proper post and file upload handling)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <libb64/cencode.h>
|
#include <libb64/cencode.h>
|
||||||
#include "WiFiServer.h"
|
#include "WiFiServer.h"
|
||||||
@ -61,6 +60,7 @@ ESP8266WebServerTemplate<ServerType>::ESP8266WebServerTemplate(IPAddress addr, i
|
|||||||
, _currentHeaders(nullptr)
|
, _currentHeaders(nullptr)
|
||||||
, _contentLength(0)
|
, _contentLength(0)
|
||||||
, _chunked(false)
|
, _chunked(false)
|
||||||
|
, _corsEnabled(false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,6 +82,7 @@ ESP8266WebServerTemplate<ServerType>::ESP8266WebServerTemplate(int port)
|
|||||||
, _currentHeaders(nullptr)
|
, _currentHeaders(nullptr)
|
||||||
, _contentLength(0)
|
, _contentLength(0)
|
||||||
, _chunked(false)
|
, _chunked(false)
|
||||||
|
, _corsEnabled(false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,6 +99,10 @@ ESP8266WebServerTemplate<ServerType>::~ESP8266WebServerTemplate() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename ServerType>
|
||||||
|
void ESP8266WebServerTemplate<ServerType>::enableCORS(bool enable) {
|
||||||
|
_corsEnabled = enable;
|
||||||
|
}
|
||||||
template <typename ServerType>
|
template <typename ServerType>
|
||||||
void ESP8266WebServerTemplate<ServerType>::begin() {
|
void ESP8266WebServerTemplate<ServerType>::begin() {
|
||||||
close();
|
close();
|
||||||
@ -421,6 +426,9 @@ void ESP8266WebServerTemplate<ServerType>::_prepareHeader(String& response, int
|
|||||||
sendHeader(String(F("Accept-Ranges")),String(F("none")));
|
sendHeader(String(F("Accept-Ranges")),String(F("none")));
|
||||||
sendHeader(String(F("Transfer-Encoding")),String(F("chunked")));
|
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")));
|
sendHeader(String(F("Connection")), String(F("close")));
|
||||||
|
|
||||||
response += _responseHeaders;
|
response += _responseHeaders;
|
||||||
|
@ -99,6 +99,7 @@ public:
|
|||||||
void serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_header = NULL );
|
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 onNotFound(THandlerFunction fn); //called when handler is not assigned
|
||||||
void onFileUpload(THandlerFunction fn); //handle file uploads
|
void onFileUpload(THandlerFunction fn); //handle file uploads
|
||||||
|
void enableCORS(bool enable);
|
||||||
|
|
||||||
const String& uri() const { return _currentUri; }
|
const String& uri() const { return _currentUri; }
|
||||||
HTTPMethod method() const { return _currentMethod; }
|
HTTPMethod method() const { return _currentMethod; }
|
||||||
@ -244,11 +245,14 @@ protected:
|
|||||||
|
|
||||||
String _hostHeader;
|
String _hostHeader;
|
||||||
bool _chunked;
|
bool _chunked;
|
||||||
|
bool _corsEnabled;
|
||||||
|
|
||||||
String _snonce; // Store noance and opaque for future comparison
|
String _snonce; // Store noance and opaque for future comparison
|
||||||
String _sopaque;
|
String _sopaque;
|
||||||
String _srealm; // Store the Auth realm between Calls
|
String _srealm; // Store the Auth realm between Calls
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user