Skip to content

Commit

Permalink
Checks and delay before reading header infos
Browse files Browse the repository at this point in the history
- Added some checks that stream variable is not NULL before using it
(just to make sure)
- To avoid header-read errors that happened sometimes implement a check
for available data with a maximum timeout of 1000ms (configurable via
defines in header file). With my default settings we wait 20 ms and
then check again if at least 17 characters are available to read (which
should be a normal HTTP-header first line). This is done at max 50
times before the read is tried
  • Loading branch information
Apollon77 committed Nov 6, 2015
1 parent 5a2f1b5 commit ab4a026
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
21 changes: 20 additions & 1 deletion HTTPClient.cpp
Expand Up @@ -203,6 +203,10 @@ char
HTTPClient::sendUriAndHeaders(FILE* stream, char* hostName, const char* requestType, char* uri,
http_client_parameter parameters[], http_client_parameter headers[])
{
if (stream == NULL)
{
return 0;
}
fprintf_P(stream, requestType, uri);
fprintf_P(stream, PSTR(" "), uri);
//encode but use reserved characters
Expand Down Expand Up @@ -256,6 +260,10 @@ HTTPClient::sendUriAndHeaders(FILE* stream, char* hostName, const char* requestT
char
HTTPClient::sendContentPayload(FILE* stream, char* data)
{
if (stream == NULL)
{
return 0;
}
//calculate the content length
int content_length = 0;
if (data != NULL)
Expand Down Expand Up @@ -402,9 +410,20 @@ HTTPClient::skipHeader(FILE* stream)
//skip over the header
int httpReturnCode;
lastReturnCode = NULL;

if (stream == NULL) return NULL;
http_stream_udata* udata = (http_stream_udata*) fdev_get_udata(stream);
HTTPClient* client = udata->client;

int delayReadHeaderCount=0;
// check if at least the first HTTP-response available, up to 1000ms=1s
while ((client->available()<17) && (delayReadHeaderCount++<HTTPCLIENT_HEADER_READ_DELAY_MAXCOUNT))
delay(HTTPCLIENT_HEADER_READ_DELAY_ONCE);

int res=fscanf_P(stream, PSTR("HTTP/1.1 %i"), &httpReturnCode);
if (res!=1) return NULL;
lastReturnCode = httpReturnCode; // only set class variable when successfully matched
lastReturnCode = httpReturnCode; // only set class variable when successfully matched, else it is NULL

char inByte = '\0';
char lastByte = '\0';
while (!(inByte == '\n' && lastByte == '\n'))
Expand Down
9 changes: 8 additions & 1 deletion HTTPClient.h
Expand Up @@ -26,7 +26,14 @@
#define HTTPCLIENT_H_

#include <inttypes.h>
#include "EthernetClient.h"
#include <EthernetClient.h>

/* With this we try a delay of 20 ms before checking again if content is available
* and we try this up to 50 times.
* So max: 20x50ms=1000ms=1s
*/
#define HTTPCLIENT_HEADER_READ_DELAY_ONCE 20
#define HTTPCLIENT_HEADER_READ_DELAY_MAXCOUNT 50

/* This struct is used to pass parameters as URI paramters and additional HTTP headers.
* normally you pass this as a array. The last entry must have the NULL-Pointer as name.
Expand Down

0 comments on commit ab4a026

Please sign in to comment.