Skip to content

Commit ce5720d

Browse files
committed
httpClient - allow using Stream as payload
httpClient - addHeader - not allow set of Header handled by code
1 parent 20e238a commit ce5720d

File tree

2 files changed

+87
-15
lines changed

2 files changed

+87
-15
lines changed

libraries/ESP8266httpClient/src/ESP8266httpClient.cpp

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ httpClient::~httpClient() {
7979
* @param httpsFingerprint const char *
8080
*/
8181
void httpClient::begin(const char *url, const char * httpsFingerprint) {
82-
begin(String(url), String(httpsFingerprint));
82+
begin(String(url), String(httpsFingerprint));
8383
}
8484

8585
/**
@@ -113,7 +113,7 @@ void httpClient::begin(String url, String httpsFingerprint) {
113113
_host = url.substring(0, index); // hostname
114114
url.remove(0, (index + 1)); // remove hostname + :
115115

116-
index = url.indexOf('/');
116+
index = url.indexOf('/');
117117
_port = url.substring(0, index).toInt(); // get port
118118
url.remove(0, index); // remove port
119119
hasPort = true;
@@ -202,7 +202,6 @@ bool httpClient::connected() {
202202
return false;
203203
}
204204

205-
206205
/**
207206
* try to reuse the connection to the server
208207
* keep-alive
@@ -220,7 +219,6 @@ void httpClient::setUserAgent(const char * userAgent) {
220219
_userAgent = userAgent;
221220
}
222221

223-
224222
/**
225223
* send a GET request
226224
* @return http code
@@ -251,7 +249,7 @@ int httpClient::POST(String payload) {
251249
* @return -1 if no info or > 0 when Content-Length is set by server
252250
*/
253251
int httpClient::sendRequest(const char * type, uint8_t * payload, size_t size) {
254-
// connect ro server
252+
// connect to server
255253
if(!connect()) {
256254
return HTTPC_ERROR_CONNECTION_REFUSED;
257255
}
@@ -276,6 +274,77 @@ int httpClient::sendRequest(const char * type, uint8_t * payload, size_t size) {
276274
return handleHeaderResponse();
277275
}
278276

277+
/**
278+
* sendRequest
279+
* @param type const char * "GET", "POST", ....
280+
* @param stream Stream * data stream for the message body
281+
* @param size size_t size for the message body if 0 not Content-Length is send
282+
* @return -1 if no info or > 0 when Content-Length is set by server
283+
*/
284+
int httpClient::sendRequest(const char * type, Stream * stream, size_t size) {
285+
286+
if(!stream) {
287+
return HTTPC_ERROR_NO_STREAM;
288+
}
289+
290+
// connect to server
291+
if(!connect()) {
292+
return HTTPC_ERROR_CONNECTION_REFUSED;
293+
}
294+
295+
if(size > 0) {
296+
addHeader("Content-Length", String(size));
297+
}
298+
299+
// send Header
300+
if(!sendHeader(type)) {
301+
return HTTPC_ERROR_SEND_HEADER_FAILED;
302+
}
303+
304+
// create buffer for read
305+
uint8_t buff[1460] = { 0 };
306+
307+
int len = size;
308+
int bytesWritten = 0;
309+
310+
if(len == 0) {
311+
len = -1;
312+
}
313+
314+
// read all data from stream and send it to server
315+
while(connected() && stream->available() && (len > 0 || len == -1)) {
316+
317+
// get available data size
318+
size_t s = stream->available();
319+
320+
if(s) {
321+
int c = stream->readBytes(buff, ((s > sizeof(buff)) ? sizeof(buff) : s));
322+
323+
// write it to Stream
324+
bytesWritten += _tcp->write((const uint8_t *)buff, c);
325+
326+
if(len > 0) {
327+
len -= c;
328+
}
329+
330+
delay(0);
331+
} else {
332+
delay(1);
333+
}
334+
}
335+
336+
if(size && (int)size != bytesWritten) {
337+
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n", bytesWritten, _size);
338+
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!");
339+
return HTTPC_ERROR_SEND_PAYLOAD_FAILED;
340+
} else {
341+
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten);
342+
}
343+
344+
// handle Server Response (Header)
345+
return handleHeaderResponse();
346+
}
347+
279348
/**
280349
* size of message body / payload
281350
* @return -1 if no info or > 0 when Content-Length is set by server
@@ -385,7 +454,6 @@ String httpClient::getString(void) {
385454
return sstring;
386455
}
387456

388-
389457
/**
390458
* adds Header to the request
391459
* @param name
@@ -394,16 +462,20 @@ String httpClient::getString(void) {
394462
*/
395463
void httpClient::addHeader(const String& name, const String& value, bool first) {
396464

397-
String headerLine = name;
398-
headerLine += ": ";
399-
headerLine += value;
400-
headerLine += "\r\n";
465+
// not allow set of Header handled by code
466+
if(!name.equalsIgnoreCase("Connection") && !name.equalsIgnoreCase("User-Agent") && !name.equalsIgnoreCase("Host")) {
467+
String headerLine = name;
468+
headerLine += ": ";
469+
headerLine += value;
470+
headerLine += "\r\n";
401471

402-
if(first) {
403-
_Headers = headerLine + _Headers;
404-
} else {
405-
_Headers += headerLine;
472+
if(first) {
473+
_Headers = headerLine + _Headers;
474+
} else {
475+
_Headers += headerLine;
476+
}
406477
}
478+
407479
}
408480

409481
void httpClient::collectHeaders(const char* headerKeys[], const size_t headerKeysCount) {
@@ -459,7 +531,6 @@ bool httpClient::connect(void) {
459531
return true;
460532
}
461533

462-
463534
if(_https) {
464535
DEBUG_HTTPCLIENT("[HTTP-Client] connect https...\n");
465536
if(_tcps) {

libraries/ESP8266httpClient/src/ESP8266httpClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class httpClient {
6666
int POST(uint8_t * payload, size_t size);
6767
int POST(String payload);
6868
int sendRequest(const char * type, uint8_t * payload = NULL, size_t size = 0);
69+
int sendRequest(const char * type, Stream * stream, size_t size = 0);
6970

7071
void addHeader(const String& name, const String& value, bool first = false);
7172

0 commit comments

Comments
 (0)