1
1
#include " Client.h"
2
2
#include " RestClient.h"
3
3
4
+ // #define HTTP_DEBUG true
4
5
#ifdef HTTP_DEBUG
5
6
#define HTTP_DEBUG_PRINT (string ) (Serial.print(string))
6
7
#endif
@@ -16,169 +17,131 @@ RestClient::RestClient(Client& netClient, const char* _host) {
16
17
num_headers = 0 ;
17
18
contentType = " x-www-form-urlencoded" ; // default
18
19
this ->client = &netClient;
20
+ this ->responseBody = " " ;
21
+ this ->timeout = 1000 ; // default. TODO: add a setter function
19
22
}
20
23
21
24
RestClient::RestClient (Client& netClient, const char * _host, int _port) {
22
25
host = _host;
23
26
port = _port;
24
27
num_headers = 0 ;
25
- contentType = " x-www-form-urlencoded" ; // default
28
+ contentType = " application/ x-www-form-urlencoded" ; // default
26
29
this ->client = &netClient;
27
30
}
28
31
29
-
30
-
31
-
32
32
// GET path
33
- int RestClient::get (const char * path){
34
- return request (" GET" , path, NULL , NULL );
35
- }
36
-
37
- // GET path with response
38
- int RestClient::get (const char * path, String* response){
39
- return request (" GET" , path, NULL , response);
33
+ int RestClient::get (String path){
34
+ return request (" GET" , path, " " );
40
35
}
41
36
42
37
// POST path and body
43
- int RestClient::post (const char * path, const char * body){
44
- return request (" POST" , path, body, NULL );
45
- }
46
-
47
- // POST path and body with response
48
- int RestClient::post (const char * path, const char * body, String* response){
49
- return request (" POST" , path, body, response);
38
+ int RestClient::post (String path, String body){
39
+ return request (" POST" , path, body);
50
40
}
51
41
52
42
// PUT path and body
53
- int RestClient::put (const char * path, const char * body){
54
- return request (" PUT" , path, body, NULL );
55
- }
56
-
57
- // PUT path and body with response
58
- int RestClient::put (const char * path, const char * body, String* response){
59
- return request (" PUT" , path, body, response);
43
+ int RestClient::put (String path, String body){
44
+ return request (" PUT" , path, body);
60
45
}
61
46
62
47
// DELETE path
63
- int RestClient::del (const char * path){
64
- return request (" DELETE" , path, NULL , NULL );
65
- }
66
-
67
- // DELETE path and response
68
- int RestClient::del (const char * path, String* response){
69
- return request (" DELETE" , path, NULL , response);
48
+ int RestClient::del (String path){
49
+ return request (" DELETE" , path, " " );
70
50
}
71
51
72
52
// DELETE path and body
73
- int RestClient::del (const char * path, const char * body ){
74
- return request (" DELETE" , path, body, NULL );
75
- }
76
-
77
- // DELETE path and body with response
78
- int RestClient::del (const char * path, const char * body, String* response){
79
- return request (" DELETE" , path, body, response);
53
+ int RestClient::del (String path, String body ){
54
+ return request (" DELETE" , path, body);
80
55
}
81
56
82
- void RestClient::write (const char * string){
83
- HTTP_DEBUG_PRINT (string);
84
- client->print (string);
85
- }
86
-
87
- void RestClient::setHeader (const char * header){
57
+ void RestClient::setHeader (String header){
88
58
headers[num_headers] = header;
89
59
num_headers++;
90
60
}
91
61
92
- void RestClient::setContentType (const char * contentTypeValue){
62
+ void RestClient::setContentType (String contentTypeValue){
93
63
contentType = contentTypeValue;
94
64
}
95
65
96
66
// The mother- generic request method.
97
67
//
98
- int RestClient::request (const char * method, const char * path,
99
- const char * body, String* response){
68
+ int RestClient::request (const char * method, String path, String body){
100
69
101
70
HTTP_DEBUG_PRINT (" HTTP: connect\n " );
102
71
103
72
if (client->connect (host, port)){
104
73
HTTP_DEBUG_PRINT (" HTTP: connected\n " );
105
74
HTTP_DEBUG_PRINT (" REQUEST: \n " );
106
75
// Make a HTTP request line:
107
- write (method);
108
- write (" " );
109
- write (path);
110
- write (" HTTP/1.1\r\n " );
76
+ String requestString = method;
77
+ requestString += " " ;
78
+ requestString += path;
79
+ requestString += " HTTP/1.1" ;
80
+ requestString += " \n " ;
111
81
for (int i=0 ; i<num_headers; i++){
112
- write ( headers[i]) ;
113
- write ( " \r\n " ) ;
82
+ requestString += headers[i];
83
+ requestString += " \n " ;
114
84
}
115
- write (" Host: " );
116
- write (host);
117
- write (" \r\n " );
118
- write (" Connection: close\r\n " );
119
-
120
- if (body != NULL ){
121
- char contentLength[30 ];
122
- sprintf (contentLength, " Content-Length: %d\r\n " , strlen (body));
123
- write (contentLength);
124
-
125
- write (" Content-Type: " );
126
- write (contentType);
127
- write (" \r\n " );
85
+ requestString += " Host: " ;
86
+ requestString += host;
87
+ requestString += " \n " ;
88
+ requestString += " Connection: close" ;
89
+ requestString += " \n " ;
90
+ // TODO: deal with binary content
91
+ if (body != " " ){
92
+ requestString += " Content-Length: " ;
93
+ requestString += body.length ();
94
+ requestString += " \n " ;
95
+ requestString += " Content-Type: " ;
96
+ requestString += contentType;
97
+ requestString += " \n\n " ;
98
+ requestString += body;
128
99
}
100
+ requestString += " \n\n " ;
101
+ client->print (requestString);
129
102
130
- write (" \r\n " );
131
-
132
- if (body != NULL ){
133
- write (body);
134
- write (" \r\n " );
135
- write (" \r\n " );
136
- }
137
-
138
- // make sure you write all those bytes.
139
- delay (100 );
103
+ // make sure you've sent all bytes. Ugly hack.
104
+ // TODO: check output buffer instead?
105
+ delay (50 );
140
106
141
- HTTP_DEBUG_PRINT (" HTTP: call readResponse \n " );
142
- int statusCode = readResponse (response );
143
- HTTP_DEBUG_PRINT (" HTTP: return readResponse \n " );
107
+ HTTP_DEBUG_PRINT (" HTTP: call getResponse \n " );
108
+ int statusCode = getResponse ( );
109
+ HTTP_DEBUG_PRINT (" HTTP: return getResponse \n " );
144
110
145
111
// cleanup
146
- HTTP_DEBUG_PRINT (" HTTP: stop client\n " );
147
- num_headers = 0 ;
148
- client->stop ();
149
- delay (50 );
150
- HTTP_DEBUG_PRINT (" HTTP: client stopped\n " );
151
-
112
+ // only stop if server disconnected. Otherwise you can
113
+ // get in an infinite loop in getResponse:
114
+ if (!client->connected ()) {
115
+ HTTP_DEBUG_PRINT (" HTTP: stop client\n " );
116
+ num_headers = 0 ;
117
+ // client->stop();
118
+ HTTP_DEBUG_PRINT (" HTTP: client stopped\n " );
119
+ }
152
120
return statusCode;
153
121
}else {
154
122
HTTP_DEBUG_PRINT (" HTTP Connection failed\n " );
155
123
return 0 ;
156
124
}
157
125
}
158
126
159
- int RestClient::readResponse (String* response ) {
160
-
127
+ int RestClient::getResponse ( ) {
128
+ this -> requestStart = millis ();
161
129
// an http request ends with a blank line
162
130
boolean currentLineIsBlank = true ;
163
131
boolean httpBody = false ;
164
132
boolean inStatus = false ;
133
+ // clear response string:
134
+ this ->responseBody = " " ;
165
135
166
136
char statusCode[4 ];
167
137
int i = 0 ;
168
138
int code = 0 ;
169
139
170
- if (response == NULL ){
171
- HTTP_DEBUG_PRINT (" HTTP: NULL RESPONSE POINTER: \n " );
172
- }else {
173
- HTTP_DEBUG_PRINT (" HTTP: NON-NULL RESPONSE POINTER: \n " );
174
- }
175
-
176
140
HTTP_DEBUG_PRINT (" HTTP: RESPONSE: \n " );
177
141
while (client->connected ()) {
178
- HTTP_DEBUG_PRINT (" ." );
179
-
142
+ // HTTP_DEBUG_PRINT(".");
180
143
if (client->available ()) {
181
- HTTP_DEBUG_PRINT (" ," );
144
+ // HTTP_DEBUG_PRINT(",");
182
145
183
146
char c = client->read ();
184
147
HTTP_DEBUG_PRINT (c);
@@ -197,8 +160,8 @@ int RestClient::readResponse(String* response) {
197
160
}
198
161
199
162
if (httpBody){
200
- // only write response if its not null
201
- if (response != NULL ) response-> concat (c) ;
163
+ // add this char tot the responseBody
164
+ this -> responseBody += c ;
202
165
}
203
166
else
204
167
{
@@ -215,9 +178,13 @@ int RestClient::readResponse(String* response) {
215
178
currentLineIsBlank = false ;
216
179
}
217
180
}
181
+ } else {
182
+ // there is a condition where client connects
183
+ // and available() always <= 0. So timeout is here to catch that:
184
+ if (millis () - this ->requestStart > this ->timeout ) return 0 ;
218
185
}
219
186
}
220
187
221
- HTTP_DEBUG_PRINT (" HTTP: return readResponse3 \n " );
188
+ HTTP_DEBUG_PRINT (" HTTP: return getResponse3 \n " );
222
189
return code;
223
190
}
0 commit comments