@@ -85,18 +85,7 @@ bool httpClient::connected() {
85
85
* @return http code
86
86
*/
87
87
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" );
100
89
}
101
90
102
91
/* *
@@ -106,29 +95,46 @@ int httpClient::GET() {
106
95
* @return http code
107
96
*/
108
97
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
+ }
109
117
110
- bool status;
111
- status = connect ();
112
- if (status) {
118
+ if (payload && size > 0 ) {
113
119
addHeader (" Content-Length" , String (size));
114
- status = sendHeader (" POST" );
115
120
}
116
121
117
- if (status) {
118
- status = _tcp->write (&payload[0 ], size);
122
+ // send Header
123
+ if (!sendHeader (type)) {
124
+ return HTTPC_ERROR_SEND_HEADER_FAILD;
119
125
}
120
126
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
+ }
123
132
}
124
- return 0 ;
125
- }
126
133
127
- int httpClient::POST (String payload) {
128
- return POST (( uint8_t *) payload. c_str (), payload. length () );
134
+ // handle Server Response (Header)
135
+ return handleHeaderResponse ( );
129
136
}
130
137
131
-
132
138
/* *
133
139
* size of message body / payload
134
140
* @return -1 if no info or > 0 when Content-Length is set by server
@@ -145,6 +151,9 @@ WiFiClient & httpClient::getStream(void) {
145
151
if (connected ()) {
146
152
return *_tcp;
147
153
}
154
+
155
+ DEBUG_HTTPCLIENT (" [HTTP-Client] no stream to return!?\n " );
156
+
148
157
// todo return error?
149
158
}
150
159
@@ -210,8 +219,6 @@ bool httpClient::hasHeader(const char* name) {
210
219
return false ;
211
220
}
212
221
213
-
214
-
215
222
/* *
216
223
* init TCP connection and handle ssl verify if needed
217
224
* @return true if connection is ok
@@ -282,7 +289,7 @@ bool httpClient::sendHeader(const char * type) {
282
289
int httpClient::handleHeaderResponse () {
283
290
284
291
if (!connected ()) {
285
- return false ;
292
+ return HTTPC_ERROR_NOT_CONNECTED ;
286
293
}
287
294
288
295
while (connected ()) {
@@ -291,7 +298,7 @@ int httpClient::handleHeaderResponse() {
291
298
String headerLine = _tcp->readStringUntil (' \n ' );
292
299
headerLine.trim (); // remove \r
293
300
294
- DEBUG_HTTPCLIENT (" [HTTP][handleHeaderResponse] RX: '%s'\n " , headerLine.c_str ());
301
+ DEBUG_HTTPCLIENT (" [HTTP-Client ][handleHeaderResponse] RX: '%s'\n " , headerLine.c_str ());
295
302
296
303
if (headerLine.startsWith (" HTTP/1." )) {
297
304
_returnCode = headerLine.substring (9 , headerLine.indexOf (' ' , 9 )).toInt ();
@@ -306,16 +313,16 @@ int httpClient::handleHeaderResponse() {
306
313
for (size_t i = 0 ; i < _headerKeysCount; i++) {
307
314
if (_currentHeaders[i].key == headerName) {
308
315
_currentHeaders[i].value = headerValue;
309
- return true ;
316
+ break ;
310
317
}
311
318
}
312
319
313
320
}
314
321
315
322
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 ());
317
324
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 ());
319
326
}
320
327
return _returnCode;
321
328
}
@@ -324,4 +331,6 @@ int httpClient::handleHeaderResponse() {
324
331
delay (0 );
325
332
}
326
333
}
334
+
335
+ return HTTPC_ERROR_CONNECTION_LOST;
327
336
}
0 commit comments