@@ -50,6 +50,7 @@ ESP8266WebServer::ESP8266WebServer(IPAddress addr, int port)
50
50
, _headerKeysCount(0 )
51
51
, _currentHeaders(0 )
52
52
, _contentLength(0 )
53
+ , _chunked(false )
53
54
{
54
55
}
55
56
@@ -65,6 +66,7 @@ ESP8266WebServer::ESP8266WebServer(int port)
65
66
, _headerKeysCount(0 )
66
67
, _currentHeaders(0 )
67
68
, _contentLength(0 )
69
+ , _chunked(false )
68
70
{
69
71
}
70
72
@@ -257,13 +259,15 @@ void ESP8266WebServer::_prepareHeader(String& response, int code, const char* co
257
259
if (!content_type)
258
260
content_type = " text/html" ;
259
261
262
+ _chunked = false ;
260
263
sendHeader (" Content-Type" , content_type, true );
261
264
if (_contentLength == CONTENT_LENGTH_NOT_SET) {
262
265
sendHeader (" Content-Length" , String (contentLength));
263
266
} else if (_contentLength != CONTENT_LENGTH_UNKNOWN) {
264
267
sendHeader (" Content-Length" , String (_contentLength));
265
268
} else if (_contentLength == CONTENT_LENGTH_UNKNOWN && _currentVersion){ // HTTP/1.1 or above client
266
269
// let's do chunked
270
+ _chunked = true ;
267
271
sendHeader (" Accept-Ranges" ," none" );
268
272
sendHeader (" Transfer-Encoding" ," chunked" );
269
273
}
@@ -276,8 +280,9 @@ void ESP8266WebServer::_prepareHeader(String& response, int code, const char* co
276
280
277
281
void ESP8266WebServer::send (int code, const char * content_type, const String& content) {
278
282
String header;
279
- if (content.length () == 0 && _contentLength == CONTENT_LENGTH_NOT_SET)
280
- _contentLength = CONTENT_LENGTH_UNKNOWN;
283
+ // Can we asume the following?
284
+ // if(code == 200 && content.length() == 0 && _contentLength == CONTENT_LENGTH_NOT_SET)
285
+ // _contentLength = CONTENT_LENGTH_UNKNOWN;
281
286
_prepareHeader (header, code, content_type, content.length ());
282
287
_currentClient.write (header.c_str (), header.length ());
283
288
if (content.length ())
@@ -319,7 +324,7 @@ void ESP8266WebServer::send(int code, const String& content_type, const String&
319
324
void ESP8266WebServer::sendContent (const String& content) {
320
325
const char * footer = " \r\n " ;
321
326
size_t len = content.length ();
322
- if (_contentLength == CONTENT_LENGTH_UNKNOWN && _currentVersion ) {
327
+ if (_chunked ) {
323
328
char * chunkSize = (char *)malloc (11 );
324
329
if (chunkSize){
325
330
sprintf (chunkSize, " %x%s" , len, footer);
@@ -328,7 +333,7 @@ void ESP8266WebServer::sendContent(const String& content) {
328
333
}
329
334
}
330
335
_currentClient.write (content.c_str (), len);
331
- if (_contentLength == CONTENT_LENGTH_UNKNOWN && _currentVersion ){
336
+ if (_chunked ){
332
337
_currentClient.write (footer, 2 );
333
338
}
334
339
}
@@ -339,7 +344,7 @@ void ESP8266WebServer::sendContent_P(PGM_P content) {
339
344
340
345
void ESP8266WebServer::sendContent_P (PGM_P content, size_t size) {
341
346
const char * footer = " \r\n " ;
342
- if (_contentLength == CONTENT_LENGTH_UNKNOWN && _currentVersion ) {
347
+ if (_chunked ) {
343
348
char * chunkSize = (char *)malloc (11 );
344
349
if (chunkSize){
345
350
sprintf (chunkSize, " %x%s" , size, footer);
@@ -348,7 +353,7 @@ void ESP8266WebServer::sendContent_P(PGM_P content, size_t size) {
348
353
}
349
354
}
350
355
_currentClient.write_P (content, size);
351
- if (_contentLength == CONTENT_LENGTH_UNKNOWN && _currentVersion ){
356
+ if (_chunked ){
352
357
_currentClient.write (footer, 2 );
353
358
}
354
359
}
0 commit comments