Skip to content

Commit 464b9f2

Browse files
committed
improve error handling
add httpClient::sendRequest ( universal request send )
1 parent 3b24638 commit 464b9f2

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
lines changed

libraries/ESP8266httpClient/src/ESP8266httpClient.cpp

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,7 @@ bool httpClient::connected() {
8585
* @return http code
8686
*/
8787
int httpClient::GET() {
88-
89-
bool status;
90-
status = connect();
91-
if(status) {
92-
status = sendHeader("GET");
93-
}
94-
95-
if(status) {
96-
return handleHeaderResponse();
97-
}
98-
99-
return 0;
88+
return sendRequest("GET");
10089
}
10190

10291
/**
@@ -106,29 +95,46 @@ int httpClient::GET() {
10695
* @return http code
10796
*/
10897
int httpClient::POST(uint8_t * payload, size_t size) {
98+
return sendRequest("POST", payload, size);
99+
}
100+
101+
int httpClient::POST(String payload) {
102+
return POST((uint8_t *) payload.c_str(), payload.length());
103+
}
104+
105+
/**
106+
* sendRequest
107+
* @param type const char * "GET", "POST", ....
108+
* @param payload uint8_t * data for the message body if null not send
109+
* @param size size_t size for the message body if 0 not send
110+
* @return -1 if no info or > 0 when Content-Length is set by server
111+
*/
112+
int httpClient::sendRequest(const char * type, uint8_t * payload, size_t size) {
113+
// connect ro server
114+
if(!connect()) {
115+
return HTTPC_ERROR_CONNECTION_REFUSED;
116+
}
109117

110-
bool status;
111-
status = connect();
112-
if(status) {
118+
if(payload && size > 0) {
113119
addHeader("Content-Length", String(size));
114-
status = sendHeader("POST");
115120
}
116121

117-
if(status) {
118-
status = _tcp->write(&payload[0], size);
122+
// send Header
123+
if(!sendHeader(type)) {
124+
return HTTPC_ERROR_SEND_HEADER_FAILD;
119125
}
120126

121-
if(status) {
122-
return handleHeaderResponse();
127+
// send Payload if needed
128+
if(payload && size > 0) {
129+
if(_tcp->write(&payload[0], size) != size) {
130+
return HTTPC_ERROR_SEND_PAYLOAD_FAILD;
131+
}
123132
}
124-
return 0;
125-
}
126133

127-
int httpClient::POST(String payload) {
128-
return POST((uint8_t *) payload.c_str(), payload.length());
134+
// handle Server Response (Header)
135+
return handleHeaderResponse();
129136
}
130137

131-
132138
/**
133139
* size of message body / payload
134140
* @return -1 if no info or > 0 when Content-Length is set by server
@@ -145,6 +151,9 @@ WiFiClient & httpClient::getStream(void) {
145151
if(connected()) {
146152
return *_tcp;
147153
}
154+
155+
DEBUG_HTTPCLIENT("[HTTP-Client] no stream to return!?\n");
156+
148157
// todo return error?
149158
}
150159

@@ -210,8 +219,6 @@ bool httpClient::hasHeader(const char* name) {
210219
return false;
211220
}
212221

213-
214-
215222
/**
216223
* init TCP connection and handle ssl verify if needed
217224
* @return true if connection is ok
@@ -282,7 +289,7 @@ bool httpClient::sendHeader(const char * type) {
282289
int httpClient::handleHeaderResponse() {
283290

284291
if(!connected()) {
285-
return false;
292+
return HTTPC_ERROR_NOT_CONNECTED;
286293
}
287294

288295
while(connected()) {
@@ -291,7 +298,7 @@ int httpClient::handleHeaderResponse() {
291298
String headerLine = _tcp->readStringUntil('\n');
292299
headerLine.trim(); // remove \r
293300

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

296303
if(headerLine.startsWith("HTTP/1.")) {
297304
_returnCode = headerLine.substring(9, headerLine.indexOf(' ', 9)).toInt();
@@ -306,16 +313,16 @@ int httpClient::handleHeaderResponse() {
306313
for(size_t i = 0; i < _headerKeysCount; i++) {
307314
if(_currentHeaders[i].key == headerName) {
308315
_currentHeaders[i].value = headerValue;
309-
return true;
316+
break;
310317
}
311318
}
312319

313320
}
314321

315322
if(headerLine == "") {
316-
DEBUG_HTTPCLIENT("[HTTP][handleHeaderResponse] code: '%s'\n", String(_returnCode).c_str());
323+
DEBUG_HTTPCLIENT("[HTTP-Client][handleHeaderResponse] code: '%s'\n", String(_returnCode).c_str());
317324
if(_size) {
318-
DEBUG_HTTPCLIENT("[HTTP][handleHeaderResponse] size: '%s'\n", String(_size).c_str());
325+
DEBUG_HTTPCLIENT("[HTTP-Client][handleHeaderResponse] size: '%s'\n", String(_size).c_str());
319326
}
320327
return _returnCode;
321328
}
@@ -324,4 +331,6 @@ int httpClient::handleHeaderResponse() {
324331
delay(0);
325332
}
326333
}
334+
335+
return HTTPC_ERROR_CONNECTION_LOST;
327336
}

libraries/ESP8266httpClient/src/ESP8266httpClient.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@
3333

3434
#define HTTPCLIENT_TCP_TIMEOUT (1000)
3535

36+
/// HTTP client errors
37+
#define HTTPC_ERROR_CONNECTION_REFUSED (-1)
38+
#define HTTPC_ERROR_SEND_HEADER_FAILD (-2)
39+
#define HTTPC_ERROR_SEND_PAYLOAD_FAILD (-3)
40+
#define HTTPC_ERROR_NOT_CONNECTED (-4)
41+
#define HTTPC_ERROR_CONNECTION_LOST (-5)
42+
43+
3644
class httpClient {
3745
public:
3846
httpClient();
@@ -47,6 +55,7 @@ class httpClient {
4755
int GET();
4856
int POST(uint8_t * payload, size_t size);
4957
int POST(String payload);
58+
int sendRequest(const char * type, uint8_t * payload = NULL, size_t size = 0);
5059

5160
void addHeader(const String& name, const String& value, bool first = false);
5261

0 commit comments

Comments
 (0)