Skip to content

Commit b7a59c8

Browse files
Generalize send_header into its own method
Reduces duplication Also enables users to only send the header using this API and to pass the client to a function that prints to a stream
1 parent 1c5cef0 commit b7a59c8

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

src/internals/StreamHttpReply.cpp

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,37 @@ ArduinoHttpServer::AbstractStreamHttpReply::AbstractStreamHttpReply(Stream& stre
1818

1919
}
2020

21-
//------------------------------------------------------------------------------
22-
//! \brief Send this reply / print this reply to stream.
23-
//! \todo: Accept char* also for data coming directly from flash.
24-
void ArduinoHttpServer::AbstractStreamHttpReply::send(const String& data, const String& title)
21+
void ArduinoHttpServer::AbstractStreamHttpReply::sendHeader(size_t size)
2522
{
2623
// Read away remaining bytes.
2724
while(getStream().read()>=0);
2825

29-
DEBUG_ARDUINO_HTTP_SERVER_PRINT("Printing Reply ... ");
30-
3126
getStream().print( AHS_F("HTTP/1.1 ") );
3227
getStream().print( getCode() + " ");
33-
getStream().print( title + "\r\n" );
3428
getStream().print( AHS_F("Connection: close\r\n") );
35-
getStream().print( AHS_F("Content-Length: ") ); getStream().print( data.length()); getStream().print( AHS_F("\r\n") );
29+
if (size > 0)
30+
{
31+
getStream().print( AHS_F("Content-Length: ") ); getStream().print(size); getStream().print( AHS_F("\r\n") );
32+
}
3633
getStream().print( AHS_F("Content-Type: ") ); getStream().print( m_contentType ); getStream().print( AHS_F("\r\n") );
3734
getStream().print( AHS_F("\r\n") );
38-
getStream().print( data ); getStream().print( AHS_F("\r\n") );
35+
}
3936

37+
//------------------------------------------------------------------------------
38+
//! \brief Send this reply / print this reply to stream.
39+
//! \todo: Accept char* also for data coming directly from flash.
40+
void ArduinoHttpServer::AbstractStreamHttpReply::send(const String& data, const String& title)
41+
{
42+
DEBUG_ARDUINO_HTTP_SERVER_PRINT("Printing Reply ... ");
43+
AbstractStreamHttpReply::sendHeader(data.length());
44+
getStream().print( data ); getStream().print( AHS_F("\r\n") );
4045
DEBUG_ARDUINO_HTTP_SERVER_PRINTLN("done.");
4146
}
4247

4348
void ArduinoHttpServer::AbstractStreamHttpReply::send(const uint8_t* buf, const size_t size) {
44-
// Read away remaining bytes.
45-
while(getStream().read()>=0);
46-
4749
DEBUG_ARDUINO_HTTP_SERVER_PRINT("Printing Reply ... ");
48-
49-
getStream().print( AHS_F("HTTP/1.1 ") );
50-
getStream().print( getCode() + " ");
51-
getStream().print( AHS_F("Connection: close\r\n") );
52-
getStream().print( AHS_F("Content-Length: ") ); getStream().print(size); getStream().print( AHS_F("\r\n") );
53-
getStream().print( AHS_F("Content-Type: ") ); getStream().print( m_contentType ); getStream().print( AHS_F("\r\n") );
54-
getStream().print( AHS_F("\r\n") );
50+
AbstractStreamHttpReply::sendHeader(size);
5551
getStream().write(buf, size);
56-
5752
DEBUG_ARDUINO_HTTP_SERVER_PRINTLN("done.");
5853
}
5954

src/internals/StreamHttpReply.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class AbstractStreamHttpReply
2525
{
2626

2727
public:
28+
virtual void sendHeader(size_t size);
2829
virtual void send(const String& data, const String& title);
2930
virtual void send(const uint8_t* buf, const size_t size);
3031

0 commit comments

Comments
 (0)