diff --git a/src/internals/StreamHttpReply.cpp b/src/internals/StreamHttpReply.cpp index cfbaaa9..1b13aa0 100644 --- a/src/internals/StreamHttpReply.cpp +++ b/src/internals/StreamHttpReply.cpp @@ -18,6 +18,15 @@ ArduinoHttpServer::AbstractStreamHttpReply::AbstractStreamHttpReply(Stream& stre } +void ArduinoHttpServer::AbstractStreamHttpReply::addHeader(const String& name, const String &value) { + if (m_headerCount == MAX_HEADERS) return; + + m_headerNames[m_headerCount] = name; + m_headerValues[m_headerCount] = value; + + m_headerCount++; +} + //------------------------------------------------------------------------------ //! \brief Send this reply / print this reply to stream. //! \todo: Accept char* also for data coming directly from flash. @@ -34,13 +43,16 @@ void ArduinoHttpServer::AbstractStreamHttpReply::send(const String& data, const getStream().print( AHS_F("Connection: close\r\n") ); getStream().print( AHS_F("Content-Length: ") ); getStream().print( data.length()); getStream().print( AHS_F("\r\n") ); getStream().print( AHS_F("Content-Type: ") ); getStream().print( m_contentType ); getStream().print( AHS_F("\r\n") ); + for(int i = 0; i < m_headerCount; i++) { + getStream().print( m_headerNames[i] ); getStream().print(": "); getStream().print( m_headerValues[i] ); getStream().print( AHS_F("\r\n") ); + } + getStream().print( AHS_F("\r\n") ); getStream().print( data ); getStream().print( AHS_F("\r\n") ); DEBUG_ARDUINO_HTTP_SERVER_PRINTLN("done."); } - Stream& ArduinoHttpServer::AbstractStreamHttpReply::getStream() { return m_stream; diff --git a/src/internals/StreamHttpReply.hpp b/src/internals/StreamHttpReply.hpp index 11cbdb7..e9f6c17 100644 --- a/src/internals/StreamHttpReply.hpp +++ b/src/internals/StreamHttpReply.hpp @@ -11,9 +11,10 @@ #define __ArduinoHttpServer__StreamHttpReply__ #include - #include "ArduinoHttpServerDebug.h" +#define MAX_HEADERS 3 + namespace ArduinoHttpServer { @@ -25,6 +26,7 @@ class AbstractStreamHttpReply { public: + void addHeader(const String& name, const String &value); virtual void send(const String& data, const String& title); protected: @@ -37,7 +39,9 @@ class AbstractStreamHttpReply constexpr static const char* CONTENT_TYPE_APPLICATION_JSON PROGMEM = "application/json"; private: - + String m_headerNames[MAX_HEADERS]; + String m_headerValues[MAX_HEADERS]; + int m_headerCount = 0; Stream& m_stream; String m_contentType; //!< Needs to be overridden to default when required. Therefore not const. const String m_code;