mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-06 05:21:22 +03:00
httpClient - allow using Stream as payload
httpClient - addHeader - not allow set of Header handled by code
This commit is contained in:
parent
20e238a4ca
commit
ce5720d161
@ -202,7 +202,6 @@ bool httpClient::connected() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* try to reuse the connection to the server
|
* try to reuse the connection to the server
|
||||||
* keep-alive
|
* keep-alive
|
||||||
@ -220,7 +219,6 @@ void httpClient::setUserAgent(const char * userAgent) {
|
|||||||
_userAgent = userAgent;
|
_userAgent = userAgent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* send a GET request
|
* send a GET request
|
||||||
* @return http code
|
* @return http code
|
||||||
@ -251,7 +249,7 @@ int httpClient::POST(String payload) {
|
|||||||
* @return -1 if no info or > 0 when Content-Length is set by server
|
* @return -1 if no info or > 0 when Content-Length is set by server
|
||||||
*/
|
*/
|
||||||
int httpClient::sendRequest(const char * type, uint8_t * payload, size_t size) {
|
int httpClient::sendRequest(const char * type, uint8_t * payload, size_t size) {
|
||||||
// connect ro server
|
// connect to server
|
||||||
if(!connect()) {
|
if(!connect()) {
|
||||||
return HTTPC_ERROR_CONNECTION_REFUSED;
|
return HTTPC_ERROR_CONNECTION_REFUSED;
|
||||||
}
|
}
|
||||||
@ -276,6 +274,77 @@ int httpClient::sendRequest(const char * type, uint8_t * payload, size_t size) {
|
|||||||
return handleHeaderResponse();
|
return handleHeaderResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sendRequest
|
||||||
|
* @param type const char * "GET", "POST", ....
|
||||||
|
* @param stream Stream * data stream for the message body
|
||||||
|
* @param size size_t size for the message body if 0 not Content-Length is send
|
||||||
|
* @return -1 if no info or > 0 when Content-Length is set by server
|
||||||
|
*/
|
||||||
|
int httpClient::sendRequest(const char * type, Stream * stream, size_t size) {
|
||||||
|
|
||||||
|
if(!stream) {
|
||||||
|
return HTTPC_ERROR_NO_STREAM;
|
||||||
|
}
|
||||||
|
|
||||||
|
// connect to server
|
||||||
|
if(!connect()) {
|
||||||
|
return HTTPC_ERROR_CONNECTION_REFUSED;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(size > 0) {
|
||||||
|
addHeader("Content-Length", String(size));
|
||||||
|
}
|
||||||
|
|
||||||
|
// send Header
|
||||||
|
if(!sendHeader(type)) {
|
||||||
|
return HTTPC_ERROR_SEND_HEADER_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
// create buffer for read
|
||||||
|
uint8_t buff[1460] = { 0 };
|
||||||
|
|
||||||
|
int len = size;
|
||||||
|
int bytesWritten = 0;
|
||||||
|
|
||||||
|
if(len == 0) {
|
||||||
|
len = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// read all data from stream and send it to server
|
||||||
|
while(connected() && stream->available() && (len > 0 || len == -1)) {
|
||||||
|
|
||||||
|
// get available data size
|
||||||
|
size_t s = stream->available();
|
||||||
|
|
||||||
|
if(s) {
|
||||||
|
int c = stream->readBytes(buff, ((s > sizeof(buff)) ? sizeof(buff) : s));
|
||||||
|
|
||||||
|
// write it to Stream
|
||||||
|
bytesWritten += _tcp->write((const uint8_t *)buff, c);
|
||||||
|
|
||||||
|
if(len > 0) {
|
||||||
|
len -= c;
|
||||||
|
}
|
||||||
|
|
||||||
|
delay(0);
|
||||||
|
} else {
|
||||||
|
delay(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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!");
|
||||||
|
return HTTPC_ERROR_SEND_PAYLOAD_FAILED;
|
||||||
|
} else {
|
||||||
|
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten);
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle Server Response (Header)
|
||||||
|
return handleHeaderResponse();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* size of message body / payload
|
* size of message body / payload
|
||||||
* @return -1 if no info or > 0 when Content-Length is set by server
|
* @return -1 if no info or > 0 when Content-Length is set by server
|
||||||
@ -385,7 +454,6 @@ String httpClient::getString(void) {
|
|||||||
return sstring;
|
return sstring;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* adds Header to the request
|
* adds Header to the request
|
||||||
* @param name
|
* @param name
|
||||||
@ -394,6 +462,8 @@ String httpClient::getString(void) {
|
|||||||
*/
|
*/
|
||||||
void httpClient::addHeader(const String& name, const String& value, bool first) {
|
void httpClient::addHeader(const String& name, const String& value, bool first) {
|
||||||
|
|
||||||
|
// not allow set of Header handled by code
|
||||||
|
if(!name.equalsIgnoreCase("Connection") && !name.equalsIgnoreCase("User-Agent") && !name.equalsIgnoreCase("Host")) {
|
||||||
String headerLine = name;
|
String headerLine = name;
|
||||||
headerLine += ": ";
|
headerLine += ": ";
|
||||||
headerLine += value;
|
headerLine += value;
|
||||||
@ -404,6 +474,8 @@ void httpClient::addHeader(const String& name, const String& value, bool first)
|
|||||||
} else {
|
} else {
|
||||||
_Headers += headerLine;
|
_Headers += headerLine;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void httpClient::collectHeaders(const char* headerKeys[], const size_t headerKeysCount) {
|
void httpClient::collectHeaders(const char* headerKeys[], const size_t headerKeysCount) {
|
||||||
@ -459,7 +531,6 @@ bool httpClient::connect(void) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(_https) {
|
if(_https) {
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client] connect https...\n");
|
DEBUG_HTTPCLIENT("[HTTP-Client] connect https...\n");
|
||||||
if(_tcps) {
|
if(_tcps) {
|
||||||
|
@ -66,6 +66,7 @@ class httpClient {
|
|||||||
int POST(uint8_t * payload, size_t size);
|
int POST(uint8_t * payload, size_t size);
|
||||||
int POST(String payload);
|
int POST(String payload);
|
||||||
int sendRequest(const char * type, uint8_t * payload = NULL, size_t size = 0);
|
int sendRequest(const char * type, uint8_t * payload = NULL, size_t size = 0);
|
||||||
|
int sendRequest(const char * type, Stream * stream, size_t size = 0);
|
||||||
|
|
||||||
void addHeader(const String& name, const String& value, bool first = false);
|
void addHeader(const String& name, const String& value, bool first = false);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user