Renamed the sendHTTPHeader method to sendHTTPResponse because it is clearer, this method is also not static anymore, added new methods to the WEBServer api to allow user defined http headers.

This commit is contained in:
anschrammh 2022-10-17 23:54:17 +02:00
parent 52a0cb7963
commit c9ead83204

View File

@ -48,20 +48,46 @@ class WEBServer : public TCPServer<T>, public HttpConstants
free(_WWWDir);
}
boolean addApiRoutine(const char *uri, boolean (*apiRoutine)(HttpRequestData&, WiFiClient*, void*), void *pData, HttpRequestMethod HRM = UNDEFINED)
using ApiRoutineCallback = boolean (*)(WEBServer<T>*, HttpRequestData&, WiFiClient*, void*);
boolean addApiRoutine(const char *uri, ApiRoutineCallback apiRoutine, void *pData, HttpRequestMethod HRM = UNDEFINED)
{
return _apiDictionary.add(uri, new ApiRoutine({apiRoutine, pData, HRM}));
}
void clearApiRoutine() { _apiDictionary.clear(); };
void clearApiRoutine(void) { _apiDictionary.clear(); };
boolean removeApiRoutine(const char* uri)
boolean removeApiRoutine(const char *uri)
{
return _apiDictionary.remove(uri);
}
//Helper function used for the webApi
static void sendHTTPHeader(WiFiClient *client, const char *contentType, const size_t contentLength = 0, HttpVersion version = HttpVersion::HTTP_1_1, HTTP_CODE HTTPCode = HTTP_CODE::HTTP_CODE_OK)
unsigned int apiRoutineCount(void) const
{
return _apiDictionary.count();
}
boolean addHttpHeader(const char *name, const char *value)
{
return _httpHeadersDictionary.add(name, DictionaryHelper::StringEntity(value));
}
void clearHttpHeaders(void)
{
_httpHeadersDictionary.clear();
}
boolean removeHttpHeader(const char *name)
{
return _httpHeadersDictionary.remove(name);
}
unsigned int httpHeadersCount(void) const
{
return _httpHeadersDictionary.count();
}
void sendHTTPResponse(WiFiClient *client, const char *contentType, const size_t contentLength = 0, HttpVersion version = HttpVersion::HTTP_1_1, HTTP_CODE HTTPCode = HTTP_CODE::HTTP_CODE_OK)
{
if(!client) return;
(void)HTTPCode;
@ -73,7 +99,12 @@ class WEBServer : public TCPServer<T>, public HttpConstants
client->printf("\r\nContent-Length: %d", contentLength);
}
client->print("\r\nAccess-Control-Allow-Origin: *");
//We here send user defined HTTP headers :)
for(unsigned int i(0); i < _httpHeadersDictionary.count(); i++)
{
client->printf("\r\n%s: %s", _httpHeadersDictionary.getParameter(i), _httpHeadersDictionary.getAt(i) ? _httpHeadersDictionary.getAt(i)->getString() : "");
}
//client->print("\r\nAccess-Control-Allow-Origin: *");
client->print("\r\n\r\n");
}
@ -102,6 +133,8 @@ class WEBServer : public TCPServer<T>, public HttpConstants
{
return _WWWDir;
}
protected:
private:
virtual T* createNewClient(WiFiClient wc)
@ -652,11 +685,11 @@ class WEBServer : public TCPServer<T>, public HttpConstants
if(ref->HRM == UNDEFINED)
{
return (*(ref->apiRoutine))(client->_httpRequestData, &(client->_client), ref->pData);
return (*(ref->apiRoutine))(this, client->_httpRequestData, &(client->_client), ref->pData);
}
else if(ref->HRM == client->_httpRequestData.HRM)
{
return (*(ref->apiRoutine))(client->_httpRequestData, &(client->_client), ref->pData);
return (*(ref->apiRoutine))(this, client->_httpRequestData, &(client->_client), ref->pData);
}
else
return false;
@ -844,7 +877,7 @@ class WEBServer : public TCPServer<T>, public HttpConstants
void sendDirectoryListing(T *client, File& pageToSend)
{
sendHTTPHeader(&client->_client, HttpConstants::httpMIMETypeToString(HttpConstants::TEXT_HTML));
sendHTTPResponse(&client->_client, HttpConstants::httpMIMETypeToString(HttpConstants::TEXT_HTML));
client->_client.printf_P(PSTR( "<!DOCTYPE HTML>\r\n\
<html>\r\n\
<head>\r\n\
@ -1037,12 +1070,13 @@ class WEBServer : public TCPServer<T>, public HttpConstants
struct ApiRoutine
{
boolean (*apiRoutine)(HttpRequestData&, WiFiClient*, void*);
ApiRoutineCallback apiRoutine;
void *pData;
HttpRequestMethod HRM;
};
Dictionary<ApiRoutine> _apiDictionary;
Dictionary<DictionaryHelper::StringEntity> _httpHeadersDictionary;
SDClass *_sdClass;
char *_WWWDir = nullptr; //Website root folder
};