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
This commit is contained in:
Victor Aprea 2019-04-22 14:52:39 -04:00 committed by Me No Dev
parent f8eebb5c39
commit 672e4faa92
2 changed files with 22 additions and 5 deletions

View File

@ -55,6 +55,7 @@ WebServer::WebServer(IPAddress addr, int port)
, _currentHeaders(nullptr) , _currentHeaders(nullptr)
, _contentLength(0) , _contentLength(0)
, _chunked(false) , _chunked(false)
, _corsEnabled(false)
{ {
} }
@ -75,6 +76,7 @@ WebServer::WebServer(int port)
, _currentHeaders(nullptr) , _currentHeaders(nullptr)
, _contentLength(0) , _contentLength(0)
, _chunked(false) , _chunked(false)
, _corsEnabled(false)
{ {
} }
@ -104,7 +106,7 @@ void WebServer::begin(uint16_t port) {
String WebServer::_extractParam(String& authReq,const String& param,const char delimit){ String WebServer::_extractParam(String& authReq,const String& param,const char delimit){
int _begin = authReq.indexOf(param); int _begin = authReq.indexOf(param);
if (_begin == -1) if (_begin == -1)
return ""; return "";
return authReq.substring(_begin+param.length(),authReq.indexOf(delimit,_begin+param.length())); return authReq.substring(_begin+param.length(),authReq.indexOf(delimit,_begin+param.length()));
} }
@ -366,6 +368,14 @@ void WebServer::setContentLength(const size_t contentLength) {
_contentLength = 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) { void WebServer::_prepareHeader(String& response, int code, const char* content_type, size_t contentLength) {
response = String(F("HTTP/1.")) + String(_currentVersion) + ' '; response = String(F("HTTP/1.")) + String(_currentVersion) + ' ';
response += String(code); 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("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(FPSTR("Access-Control-Allow-Origin")), String("*"));
}
sendHeader(String(F("Connection")), String(F("close"))); sendHeader(String(F("Connection")), String(F("close")));
response += _responseHeaders; response += _responseHeaders;
@ -494,7 +507,7 @@ void WebServer::_streamFileCore(const size_t fileSize, const String & fileName,
send(200, contentType, ""); send(200, contentType, "");
} }
String WebServer::pathArg(unsigned int i) { String WebServer::pathArg(unsigned int i) {
if (_currentHandler != nullptr) if (_currentHandler != nullptr)
return _currentHandler->pathArg(i); return _currentHandler->pathArg(i);
return ""; return "";

View File

@ -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);
void send_P(int code, PGM_P content_type, PGM_P content, size_t contentLength); 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 setContentLength(const size_t contentLength);
void sendHeader(const String& name, const String& value, bool first = false); void sendHeader(const String& name, const String& value, bool first = false);
void sendContent(const String& content); void sendContent(const String& content);
@ -130,12 +133,12 @@ public:
static String urlDecode(const String& text); static String urlDecode(const String& text);
template<typename T> template<typename T>
size_t streamFile(T &file, const String& contentType) { size_t streamFile(T &file, const String& contentType) {
_streamFileCore(file.size(), file.name(), contentType); _streamFileCore(file.size(), file.name(), contentType);
return _currentClient.write(file); return _currentClient.write(file);
} }
protected: protected:
virtual size_t _currentClientWrite(const char* b, size_t l) { return _currentClient.write( b, l ); } virtual size_t _currentClientWrite(const char* b, size_t l) { return _currentClient.write( b, l ); }
virtual size_t _currentClientWrite_P(PGM_P b, size_t l) { return _currentClient.write_P( b, l ); } virtual size_t _currentClientWrite_P(PGM_P b, size_t l) { return _currentClient.write_P( b, l ); }
@ -151,7 +154,7 @@ protected:
int _uploadReadByte(WiFiClient& client); int _uploadReadByte(WiFiClient& client);
void _prepareHeader(String& response, int code, const char* content_type, size_t contentLength); void _prepareHeader(String& response, int code, const char* content_type, size_t contentLength);
bool _collectHeader(const char* headerName, const char* headerValue); bool _collectHeader(const char* headerName, const char* headerValue);
void _streamFileCore(const size_t fileSize, const String & fileName, const String & contentType); void _streamFileCore(const size_t fileSize, const String & fileName, const String & contentType);
String _getRandomHexString(); String _getRandomHexString();
@ -163,6 +166,7 @@ protected:
String value; String value;
}; };
boolean _corsEnabled;
WiFiServer _server; WiFiServer _server;
WiFiClient _currentClient; WiFiClient _currentClient;