Skip to content
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

Commit 0b84d76

Browse files
committed
Implement send_P and sendContent_P
1 parent b4ed9d1 commit 0b84d76

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/EthernetWebServer_STM32-impl.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,39 @@ void EthernetWebServer::send(int code, const String& content_type, const String&
481481
send(code, (const char*)content_type.c_str(), content);
482482
}
483483

484+
void EthernetWebServer::send_P(int code, char* content_type, PGM_P content)
485+
{
486+
size_t contentLength = 0;
487+
488+
if (content != NULL)
489+
{
490+
contentLength = strlen_P(content);
491+
}
492+
493+
send_P(code, content_type, content, contentLength);
494+
}
495+
496+
void EthernetWebServer::send_P(int code, char* content_type, PGM_P content, size_t contentLength)
497+
{
498+
String header;
499+
char type[64];
500+
501+
memccpy((void*)type, content_type, 0, sizeof(type));
502+
_prepareHeader(header, code, (const char* )type, contentLength);
503+
504+
ET_LOGDEBUG1(F("EthernetWebServer::send_P: len = "), contentLength);
505+
ET_LOGDEBUG1(F("content = "), content);
506+
ET_LOGDEBUG1(F("EthernetWebServer::send_P: hdrlen = "), header.length());
507+
ET_LOGDEBUG1(F("header = "), header);
508+
509+
_currentClient.write((const uint8_t *) header.c_str(), header.length());
510+
511+
if (contentLength)
512+
{
513+
sendContent_P(content, contentLength);
514+
}
515+
}
516+
484517
void EthernetWebServer::sendContent(const String& content)
485518
{
486519
const char * footer = "\r\n";
@@ -534,6 +567,47 @@ void EthernetWebServer::sendContent(const String& content, size_t size)
534567
}
535568
}
536569

570+
void EthernetWebServer::sendContent_P(PGM_P content)
571+
{
572+
sendContent_P(content, strlen_P(content));
573+
}
574+
575+
void EthernetWebServer::sendContent_P(PGM_P content, size_t size)
576+
{
577+
const char * footer = "\r\n";
578+
579+
if (_chunked)
580+
{
581+
char * chunkSize = (char *) malloc(11);
582+
583+
if (chunkSize)
584+
{
585+
sprintf(chunkSize, "%x%s", size, footer);
586+
_currentClient.write(chunkSize, strlen(chunkSize));
587+
free(chunkSize);
588+
}
589+
}
590+
591+
uint8_t buffer[PROGMEM_BUFFLEN];
592+
uint16_t count = size / PROGMEM_BUFFLEN;
593+
uint16_t remainder = size % PROGMEM_BUFFLEN;
594+
uint16_t i = 0;
595+
596+
for (i = 0; i < count; i++) {
597+
/* code */
598+
memcpy_P(buffer, &content[i*PROGMEM_BUFFLEN], PROGMEM_BUFFLEN);
599+
_currentClient.write(buffer, PROGMEM_BUFFLEN);
600+
}
601+
602+
memcpy_P(buffer, &content[i*PROGMEM_BUFFLEN], remainder);
603+
_currentClient.write(buffer, remainder);
604+
605+
if (_chunked)
606+
{
607+
_currentClient.write(footer, 2);
608+
}
609+
}
610+
537611
String EthernetWebServer::arg(String name)
538612
{
539613
for (int i = 0; i < _currentArgCount; ++i)

src/EthernetWebServer_STM32.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ enum HTTPAuthMethod
9999
#define HTTP_UPLOAD_BUFLEN 4096 //2048
100100
#endif
101101

102+
#if !defined(PROGMEM_BUFFLEN)
103+
#define PROGMEM_BUFFLEN 4096 //4096
104+
#endif
105+
102106
#define HTTP_MAX_DATA_WAIT 3000 //ms to wait for the client to send the request
103107
#define HTTP_MAX_POST_WAIT 3000 //ms to wait for POST data to arrive
104108
#define HTTP_MAX_SEND_WAIT 5000 //ms to wait for data chunk to be ACKed
@@ -200,6 +204,9 @@ class EthernetWebServer
200204
//KH
201205
void send(int code, char* content_type, const String& content, size_t contentLength);
202206

207+
void send_P(int code, char* content_type, PGM_P content)
208+
void send_P(int code, char* content_type, PGM_P content, size_t contentLength)
209+
203210
void setContentLength(size_t contentLength);
204211
void sendHeader(const String& name, const String& value, bool first = false);
205212
void sendContent(const String& content);

0 commit comments

Comments
 (0)