Skip to content

improve http client #1530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 31, 2016
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
5 changes: 3 additions & 2 deletions cores/esp8266/WMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@
extern "C" {
#include <stdlib.h>
}
#include "esp8266_peri.h"

void randomSeed(unsigned long seed) {
if(seed != 0) {
srand(seed);
srand((seed ^ RANDOM_REG32));
}
}

long random(long howbig) {
if(howbig == 0) {
return 0;
}
return rand() % howbig;
return (rand() ^ RANDOM_REG32) % howbig;
}

long random(long howsmall, long howbig) {
Expand Down
29 changes: 20 additions & 9 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ void HTTPClient::setAuthorization(const char * auth) {

/**
* set the timeout for the TCP connection
* @param timeout unsigned int
* @param timeout unsigned int
*/
void HTTPClient::setTimeout(uint16_t timeout) {
_tcpTimeout = timeout;
Expand All @@ -273,14 +273,12 @@ void HTTPClient::setTimeout(uint16_t timeout) {
}
}



/**
* use HTTP1.0
* @param timeout
*/
void HTTPClient::useHTTP10(bool useHTTP10) {
_useHTTP10 = useHTTP10;
_useHTTP10 = useHTTP10;
}

/**
Expand All @@ -305,6 +303,16 @@ int HTTPClient::POST(String payload) {
return POST((uint8_t *) payload.c_str(), payload.length());
}

/**
* sendRequest
* @param type const char * "GET", "POST", ....
* @param payload String data for the message body
* @return
*/
int HTTPClient::sendRequest(const char * type, String payload) {
return sendRequest(type, (uint8_t *) payload.c_str(), payload.length());
}

/**
* sendRequest
* @param type const char * "GET", "POST", ....
Expand Down Expand Up @@ -382,7 +390,6 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
// create buffer for read
uint8_t * buff = (uint8_t *) malloc(buff_size);


if(buff) {
// read all data from stream and send it to server
while(connected() && (stream->available() > -1) && (len > 0 || len == -1)) {
Expand Down Expand Up @@ -461,8 +468,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
free(buff);

if(size && (int) size != bytesWritten) {
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n", bytesWritten, size);
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!");
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n", bytesWritten, size); DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!");
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
} else {
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten);
Expand Down Expand Up @@ -819,17 +825,21 @@ int HTTPClient::handleHeaderResponse() {
if(!connected()) {
return HTTPC_ERROR_NOT_CONNECTED;
}

String transferEncoding;
_returnCode = -1;
_size = -1;
_transferEncoding = HTTPC_TE_IDENTITY;
unsigned long lastDataTime = millis();

while(connected()) {
size_t len = _tcp->available();
if(len > 0) {
String headerLine = _tcp->readStringUntil('\n');
headerLine.trim(); // remove \r

lastDataTime = millis();

DEBUG_HTTPCLIENT("[HTTP-Client][handleHeaderResponse] RX: '%s'\n", headerLine.c_str());

if(headerLine.startsWith("HTTP/1.")) {
Expand Down Expand Up @@ -885,15 +895,16 @@ int HTTPClient::handleHeaderResponse() {
}

} else {
if((millis() - lastDataTime) > _tcpTimeout) {
return HTTPC_ERROR_READ_TIMEOUT;
}
delay(0);
}
}

return HTTPC_ERROR_CONNECTION_LOST;
}



/**
* write one Data Block to Stream
* @param stream Stream *
Expand Down
1 change: 1 addition & 0 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class HTTPClient {
int GET();
int POST(uint8_t * payload, size_t size);
int POST(String payload);
int sendRequest(const char * type, String payload);
int sendRequest(const char * type, uint8_t * payload = NULL, size_t size = 0);
int sendRequest(const char * type, Stream * stream, size_t size = 0);

Expand Down