1
0
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:
Markus Sattler 2015-11-27 13:01:04 +01:00
parent 20e238a4ca
commit ce5720d161
2 changed files with 87 additions and 15 deletions

View File

@ -202,7 +202,6 @@ bool httpClient::connected() {
return false;
}
/**
* try to reuse the connection to the server
* keep-alive
@ -220,7 +219,6 @@ void httpClient::setUserAgent(const char * userAgent) {
_userAgent = userAgent;
}
/**
* send a GET request
* @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
*/
int httpClient::sendRequest(const char * type, uint8_t * payload, size_t size) {
// connect ro server
// connect to server
if(!connect()) {
return HTTPC_ERROR_CONNECTION_REFUSED;
}
@ -276,6 +274,77 @@ int httpClient::sendRequest(const char * type, uint8_t * payload, size_t size) {
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
* @return -1 if no info or > 0 when Content-Length is set by server
@ -385,7 +454,6 @@ String httpClient::getString(void) {
return sstring;
}
/**
* adds Header to the request
* @param name
@ -394,6 +462,8 @@ String httpClient::getString(void) {
*/
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;
headerLine += ": ";
headerLine += value;
@ -406,6 +476,8 @@ void httpClient::addHeader(const String& name, const String& value, bool first)
}
}
}
void httpClient::collectHeaders(const char* headerKeys[], const size_t headerKeysCount) {
_headerKeysCount = headerKeysCount;
if(_currentHeaders)
@ -459,7 +531,6 @@ bool httpClient::connect(void) {
return true;
}
if(_https) {
DEBUG_HTTPCLIENT("[HTTP-Client] connect https...\n");
if(_tcps) {

View File

@ -66,6 +66,7 @@ class httpClient {
int POST(uint8_t * payload, size_t size);
int POST(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);
void addHeader(const String& name, const String& value, bool first = false);