mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-06 05:21:22 +03:00
Merge pull request #753 from martinayotte/esp8266
add ESP8266WebServer::sendContent_P and ESP8266WebServer::send_P with contentLength
This commit is contained in:
commit
e0da5a284f
@ -280,6 +280,8 @@ uint32_t EspClass::getFlashChipSizeByChipId(void) {
|
|||||||
return (2_MB);
|
return (2_MB);
|
||||||
case 0x1440EF: // W25Q80
|
case 0x1440EF: // W25Q80
|
||||||
return (1_MB);
|
return (1_MB);
|
||||||
|
case 0x1340EF: // W25Q40
|
||||||
|
return (512_kB);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -159,11 +159,22 @@ void ESP8266WebServer::send_P(int code, PGM_P content_type, PGM_P content) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String header;
|
String header;
|
||||||
_prepareHeader(header, code, String(FPSTR(content_type)).c_str(), contentLength);
|
char type[64];
|
||||||
|
memccpy_P((void*)type, (PGM_VOID_P)content_type, 0, sizeof(type));
|
||||||
|
_prepareHeader(header, code, (const char* )type, contentLength);
|
||||||
sendContent(header);
|
sendContent(header);
|
||||||
sendContent_P(content);
|
sendContent_P(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ESP8266WebServer::send_P(int code, PGM_P content_type, PGM_P content, size_t contentLength) {
|
||||||
|
String header;
|
||||||
|
char type[64];
|
||||||
|
memccpy_P((void*)type, (PGM_VOID_P)content_type, 0, sizeof(type));
|
||||||
|
_prepareHeader(header, code, (const char* )type, contentLength);
|
||||||
|
sendContent(header);
|
||||||
|
sendContent_P(content, contentLength);
|
||||||
|
}
|
||||||
|
|
||||||
void ESP8266WebServer::send(int code, char* content_type, const String& content) {
|
void ESP8266WebServer::send(int code, char* content_type, const String& content) {
|
||||||
send(code, (const char*)content_type, content);
|
send(code, (const char*)content_type, content);
|
||||||
}
|
}
|
||||||
@ -216,6 +227,26 @@ void ESP8266WebServer::sendContent_P(PGM_P content) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ESP8266WebServer::sendContent_P(PGM_P content, size_t size) {
|
||||||
|
char contentUnit[HTTP_DOWNLOAD_UNIT_SIZE + 1];
|
||||||
|
contentUnit[HTTP_DOWNLOAD_UNIT_SIZE] = '\0';
|
||||||
|
size_t remaining_size = size;
|
||||||
|
|
||||||
|
while (content != NULL && remaining_size > 0) {
|
||||||
|
size_t contentUnitLen = HTTP_DOWNLOAD_UNIT_SIZE;
|
||||||
|
|
||||||
|
if (remaining_size < HTTP_DOWNLOAD_UNIT_SIZE) contentUnitLen = remaining_size;
|
||||||
|
// due to the memcpy signature, lots of casts are needed
|
||||||
|
memcpy_P((void*)contentUnit, (PGM_VOID_P)content, contentUnitLen);
|
||||||
|
|
||||||
|
content += contentUnitLen;
|
||||||
|
remaining_size -= contentUnitLen;
|
||||||
|
|
||||||
|
// write is so overloaded, had to use the cast to get it pick the right one
|
||||||
|
_currentClient.write((const char*)contentUnit, contentUnitLen);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
String ESP8266WebServer::arg(const char* name) {
|
String ESP8266WebServer::arg(const char* name) {
|
||||||
for (int i = 0; i < _currentArgCount; ++i) {
|
for (int i = 0; i < _currentArgCount; ++i) {
|
||||||
if (_currentArgs[i].key == name)
|
if (_currentArgs[i].key == name)
|
||||||
|
@ -90,11 +90,13 @@ public:
|
|||||||
void send(int code, char* content_type, const String& content);
|
void send(int code, char* content_type, const String& content);
|
||||||
void send(int code, const String& content_type, const String& content);
|
void send(int code, const String& content_type, const String& content);
|
||||||
void send_P(int code, PGM_P content_type, PGM_P content);
|
void send_P(int code, PGM_P content_type, PGM_P content);
|
||||||
|
void send_P(int code, PGM_P content_type, PGM_P content, size_t contentLength);
|
||||||
|
|
||||||
void setContentLength(size_t contentLength) { _contentLength = contentLength; }
|
void setContentLength(size_t contentLength) { _contentLength = contentLength; }
|
||||||
void sendHeader(const String& name, const String& value, bool first = false);
|
void sendHeader(const String& name, const String& value, bool first = false);
|
||||||
void sendContent(const String& content);
|
void sendContent(const String& content);
|
||||||
void sendContent_P(PGM_P content);
|
void sendContent_P(PGM_P content);
|
||||||
|
void sendContent_P(PGM_P content, size_t size);
|
||||||
|
|
||||||
template<typename T> size_t streamFile(T &file, const String& contentType){
|
template<typename T> size_t streamFile(T &file, const String& contentType){
|
||||||
setContentLength(file.size());
|
setContentLength(file.size());
|
||||||
|
@ -186,24 +186,19 @@ size_t WiFiClient::write_P(PGM_P buf, size_t size)
|
|||||||
|
|
||||||
char chunkUnit[WIFICLIENT_MAX_PACKET_SIZE + 1];
|
char chunkUnit[WIFICLIENT_MAX_PACKET_SIZE + 1];
|
||||||
chunkUnit[WIFICLIENT_MAX_PACKET_SIZE] = '\0';
|
chunkUnit[WIFICLIENT_MAX_PACKET_SIZE] = '\0';
|
||||||
while (buf != NULL)
|
size_t remaining_size = size;
|
||||||
{
|
|
||||||
size_t chunkUnitLen;
|
while (buf != NULL && remaining_size > 0) {
|
||||||
PGM_P chunkNext;
|
size_t chunkUnitLen = WIFICLIENT_MAX_PACKET_SIZE;
|
||||||
chunkNext = (PGM_P)memccpy_P((void*)chunkUnit, (PGM_VOID_P)buf, 0, WIFICLIENT_MAX_PACKET_SIZE);
|
|
||||||
if (chunkNext == NULL)
|
if (remaining_size < WIFICLIENT_MAX_PACKET_SIZE) chunkUnitLen = remaining_size;
|
||||||
{
|
// due to the memcpy signature, lots of casts are needed
|
||||||
// no terminator, more data available
|
memcpy_P((void*)chunkUnit, (PGM_VOID_P)buf, chunkUnitLen);
|
||||||
buf += WIFICLIENT_MAX_PACKET_SIZE;
|
|
||||||
chunkUnitLen = WIFICLIENT_MAX_PACKET_SIZE;
|
buf += chunkUnitLen;
|
||||||
}
|
remaining_size -= chunkUnitLen;
|
||||||
else
|
|
||||||
{
|
// write is so overloaded, had to use the cast to get it pick the right one
|
||||||
// reached terminator
|
|
||||||
chunkUnitLen = chunkNext - buf;
|
|
||||||
buf = NULL;
|
|
||||||
}
|
|
||||||
if (size < WIFICLIENT_MAX_PACKET_SIZE) chunkUnitLen = size;
|
|
||||||
_client->write((const char*)chunkUnit, chunkUnitLen);
|
_client->write((const char*)chunkUnit, chunkUnitLen);
|
||||||
}
|
}
|
||||||
return size;
|
return size;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user