Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/internals/StreamHttpReply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

#include "ArduinoHttpServerDebug.h"

ArduinoHttpServer::Header::Header(const char* header): header(header)
{
next = nullptr;
}
ArduinoHttpServer::Header::~Header()
{
delete next;
free(this);
}

//------------------------------------------------------------------------------
// Class Definition
//------------------------------------------------------------------------------
Expand All @@ -15,7 +25,24 @@ ArduinoHttpServer::AbstractStreamHttpReply::AbstractStreamHttpReply(Stream& stre
m_contentType(contentType),
m_code(code)
{
headers_head = nullptr;
headers_tail = nullptr;
}

ArduinoHttpServer::AbstractStreamHttpReply::~AbstractStreamHttpReply()
{
delete headers_head;
}

void ArduinoHttpServer::AbstractStreamHttpReply::addHeader(const char* header) {
Header* h = new Header(header);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can understand you want this feature, but I want to steer clear from dynamic memory allocation for this library.
I am trying to move everything to FixString instead of using new/delete (I even rather would use smart pointers instead of new/delete (and even the C-style free)).

If you want to use a linked list I would advise not embedding it inside a class named Header, but instead make a container type class that can hold Header classes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I completely understand not wanting dynamic memory allocation. What do you think about using an #ifdef to let the user switch between this implementation and something like #19, with the default being static?

if (headers_tail == nullptr) {
headers_tail = h;
headers_head = h;
} else {
headers_tail->next = h;
headers_tail = h;
}
}

//------------------------------------------------------------------------------
Expand All @@ -34,6 +61,12 @@ 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") );
Header* h = headers_head;
while (h != nullptr) {
getStream().print(AHS_F(h->header));
getStream().print(AHS_F("\r\n"));
h = h->next;
}
getStream().print( AHS_F("\r\n") );
getStream().print( data ); getStream().print( AHS_F("\r\n") );

Expand Down
13 changes: 13 additions & 0 deletions src/internals/StreamHttpReply.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
namespace ArduinoHttpServer
{

class Header
{
public:
const char* header;
Header* next;
Header(const char* header);
~Header();
};

//------------------------------------------------------------------------------
// Class Declaration
//------------------------------------------------------------------------------
Expand All @@ -26,6 +35,8 @@ class AbstractStreamHttpReply

public:
virtual void send(const String& data, const String& title);
virtual void addHeader(const char* header);
~AbstractStreamHttpReply();

protected:
AbstractStreamHttpReply(Stream& stream, const String& contentType, const String& code);
Expand All @@ -42,6 +53,8 @@ class AbstractStreamHttpReply
String m_contentType; //!< Needs to be overridden to default when required. Therefore not const.
const String m_code;

Header* headers_head;
Header* headers_tail;
};

//------------------------------------------------------------------------------
Expand Down