Skip to content

Commit accfd62

Browse files
authored
Merge pull request #21 from MarcelRobitaille/support-binary-files
Add support for sending binary files
2 parents 398039e + c950fd1 commit accfd62

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

src/internals/StreamHttpReply.cpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,44 @@ ArduinoHttpServer::AbstractStreamHttpReply::AbstractStreamHttpReply(Stream& stre
1818

1919
}
2020

21+
void ArduinoHttpServer::AbstractStreamHttpReply::sendHeader(
22+
size_t size, const String& title) {
23+
// Read away remaining bytes.
24+
while (getStream().read() >= 0) {
25+
}
26+
27+
getStream().print(AHS_F("HTTP/1.1 "));
28+
getStream().print(getCode() + " ");
29+
getStream().print(title + "\r\n");
30+
getStream().print(AHS_F("Connection: close\r\n"));
31+
if (size > 0) {
32+
getStream().print(AHS_F("Content-Length: "));
33+
getStream().print(size);
34+
getStream().print(AHS_F("\r\n"));
35+
}
36+
getStream().print(AHS_F("Content-Type: "));
37+
getStream().print(m_contentType);
38+
getStream().print(AHS_F("\r\n"));
39+
getStream().print(AHS_F("\r\n"));
40+
}
41+
2142
//------------------------------------------------------------------------------
2243
//! \brief Send this reply / print this reply to stream.
2344
//! \todo: Accept char* also for data coming directly from flash.
2445
void ArduinoHttpServer::AbstractStreamHttpReply::send(const String& data, const String& title)
2546
{
26-
// Read away remaining bytes.
27-
while(getStream().read()>=0);
28-
2947
DEBUG_ARDUINO_HTTP_SERVER_PRINT("Printing Reply ... ");
30-
31-
getStream().print( AHS_F("HTTP/1.1 ") );
32-
getStream().print( getCode() + " ");
33-
getStream().print( title + "\r\n" );
34-
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") );
36-
getStream().print( AHS_F("Content-Type: ") ); getStream().print( m_contentType ); getStream().print( AHS_F("\r\n") );
37-
getStream().print( AHS_F("\r\n") );
48+
AbstractStreamHttpReply::sendHeader(data.length(), title);
3849
getStream().print( data ); getStream().print( AHS_F("\r\n") );
50+
DEBUG_ARDUINO_HTTP_SERVER_PRINTLN("done.");
51+
}
3952

53+
void ArduinoHttpServer::AbstractStreamHttpReply::send(const uint8_t* buf,
54+
const size_t size,
55+
const String& title) {
56+
DEBUG_ARDUINO_HTTP_SERVER_PRINT("Printing Reply ... ");
57+
AbstractStreamHttpReply::sendHeader(size, title);
58+
getStream().write(buf, size);
4059
DEBUG_ARDUINO_HTTP_SERVER_PRINTLN("done.");
4160
}
4261

src/internals/StreamHttpReply.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ class AbstractStreamHttpReply
2525
{
2626

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

3032
protected:
3133
AbstractStreamHttpReply(Stream& stream, const String& contentType, const String& code);
@@ -81,6 +83,8 @@ class StreamHttpReply: public AbstractStreamHttpReply
8183
public:
8284
StreamHttpReply(Stream& stream, const String& contentType);
8385
virtual void send(const String& data, const bool gzipencoded=false) { AbstractStreamHttpReply::send(data, "OK"); };
86+
virtual void send(const uint8_t* buf, const size_t size, const String& title="OK") { AbstractStreamHttpReply::send(buf, size, title); };
87+
virtual void sendHeader(size_t size, const String& title="OK") { AbstractStreamHttpReply::sendHeader(size, title); }
8488
};
8589

8690

0 commit comments

Comments
 (0)