1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

Merge pull request #1530 from Links2004/master

improve http client
This commit is contained in:
Ivan Grokhotkov 2016-01-31 22:38:41 +03:00
commit 6c8ca2d363
3 changed files with 24 additions and 11 deletions

View File

@ -26,10 +26,11 @@
extern "C" {
#include <stdlib.h>
}
#include "esp8266_peri.h"
void randomSeed(unsigned long seed) {
if(seed != 0) {
srand(seed);
srand((seed ^ RANDOM_REG32));
}
}
@ -37,7 +38,7 @@ long random(long howbig) {
if(howbig == 0) {
return 0;
}
return rand() % howbig;
return (rand() ^ RANDOM_REG32) % howbig;
}
long random(long howsmall, long howbig) {

View File

@ -273,8 +273,6 @@ void HTTPClient::setTimeout(uint16_t timeout) {
}
}
/**
* use HTTP1.0
* @param timeout
@ -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", ....
@ -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)) {
@ -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);
@ -819,10 +825,12 @@ 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();
@ -830,6 +838,8 @@ int HTTPClient::handleHeaderResponse() {
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.")) {
@ -885,6 +895,9 @@ int HTTPClient::handleHeaderResponse() {
}
} else {
if((millis() - lastDataTime) > _tcpTimeout) {
return HTTPC_ERROR_READ_TIMEOUT;
}
delay(0);
}
}
@ -892,8 +905,6 @@ int HTTPClient::handleHeaderResponse() {
return HTTPC_ERROR_CONNECTION_LOST;
}
/**
* write one Data Block to Stream
* @param stream Stream *

View File

@ -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);