Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions HTTPClient.cpp
100644 → 100755
Original file line number Diff line number Diff line change
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 @@ -400,7 +408,22 @@ int
HTTPClient::skipHeader(FILE* stream)
{
//skip over the header
fscanf_P(stream, PSTR("HTTP/1.1 %i"), &lastReturnCode);
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, else it is NULL

char inByte = '\0';
char lastByte = '\0';
while (!(inByte == '\n' && lastByte == '\n'))
Expand All @@ -427,10 +450,7 @@ HTTPClient::closeStream(FILE* stream)
if (stream != NULL)
{
http_stream_udata* udata = (http_stream_udata*) fdev_get_udata(stream);
if (udata->client->connected())
{
udata->client->stop();
}
udata->client->stop();
free(udata);
fclose(stream);
}
Expand Down
9 changes: 8 additions & 1 deletion HTTPClient.h
100644 → 100755
Original file line number Diff line number Diff line change
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