Skip to content

Commit 77d7aa3

Browse files
committed
Merge pull request #1 from arduino-libraries/Stringtest
Merging new branch from tigoe
2 parents 62632fe + 2873cfa commit 77d7aa3

File tree

19 files changed

+574
-267
lines changed

19 files changed

+574
-267
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
examples/*/config.h

RestClient.cpp

+67-100
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "Client.h"
22
#include "RestClient.h"
33

4+
//#define HTTP_DEBUG true
45
#ifdef HTTP_DEBUG
56
#define HTTP_DEBUG_PRINT(string) (Serial.print(string))
67
#endif
@@ -16,169 +17,131 @@ RestClient::RestClient(Client& netClient, const char* _host) {
1617
num_headers = 0;
1718
contentType = "x-www-form-urlencoded"; // default
1819
this->client = &netClient;
20+
this->responseBody = "";
21+
this->timeout = 1000; // default. TODO: add a setter function
1922
}
2023

2124
RestClient::RestClient(Client& netClient, const char* _host, int _port) {
2225
host = _host;
2326
port = _port;
2427
num_headers = 0;
25-
contentType = "x-www-form-urlencoded"; // default
28+
contentType = "application/x-www-form-urlencoded"; // default
2629
this->client = &netClient;
2730
}
2831

29-
30-
31-
3232
// 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, "");
4035
}
4136

4237
// 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);
5040
}
5141

5242
// 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);
6045
}
6146

6247
// 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, "");
7050
}
7151

7252
// 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);
8055
}
8156

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){
8858
headers[num_headers] = header;
8959
num_headers++;
9060
}
9161

92-
void RestClient::setContentType(const char* contentTypeValue){
62+
void RestClient::setContentType(String contentTypeValue){
9363
contentType = contentTypeValue;
9464
}
9565

9666
// The mother- generic request method.
9767
//
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){
10069

10170
HTTP_DEBUG_PRINT("HTTP: connect\n");
10271

10372
if(client->connect(host, port)){
10473
HTTP_DEBUG_PRINT("HTTP: connected\n");
10574
HTTP_DEBUG_PRINT("REQUEST: \n");
10675
// 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";
11181
for(int i=0; i<num_headers; i++){
112-
write(headers[i]);
113-
write("\r\n");
82+
requestString += headers[i];
83+
requestString += "\n";
11484
}
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;
12899
}
100+
requestString += "\n\n";
101+
client->print(requestString);
129102

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);
140106

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");
144110

145111
//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+
}
152120
return statusCode;
153121
}else{
154122
HTTP_DEBUG_PRINT("HTTP Connection failed\n");
155123
return 0;
156124
}
157125
}
158126

159-
int RestClient::readResponse(String* response) {
160-
127+
int RestClient::getResponse() {
128+
this->requestStart = millis();
161129
// an http request ends with a blank line
162130
boolean currentLineIsBlank = true;
163131
boolean httpBody = false;
164132
boolean inStatus = false;
133+
// clear response string:
134+
this->responseBody = "";
165135

166136
char statusCode[4];
167137
int i = 0;
168138
int code = 0;
169139

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-
176140
HTTP_DEBUG_PRINT("HTTP: RESPONSE: \n");
177141
while (client->connected()) {
178-
HTTP_DEBUG_PRINT(".");
179-
142+
// HTTP_DEBUG_PRINT(".");
180143
if (client->available()) {
181-
HTTP_DEBUG_PRINT(",");
144+
// HTTP_DEBUG_PRINT(",");
182145

183146
char c = client->read();
184147
HTTP_DEBUG_PRINT(c);
@@ -197,8 +160,8 @@ int RestClient::readResponse(String* response) {
197160
}
198161

199162
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;
202165
}
203166
else
204167
{
@@ -215,9 +178,13 @@ int RestClient::readResponse(String* response) {
215178
currentLineIsBlank = false;
216179
}
217180
}
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;
218185
}
219186
}
220187

221-
HTTP_DEBUG_PRINT("HTTP: return readResponse3\n");
188+
HTTP_DEBUG_PRINT("HTTP: return getResponse3\n");
222189
return code;
223190
}

RestClient.h

+15-24
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,35 @@ class RestClient {
99
RestClient(Client& netClient, const char* _host, int _port);
1010

1111
//Generic HTTP Request
12-
int request(const char* method, const char* path,
13-
const char* body, String* response);
12+
int request(const char* method, String path, String body);
1413
// Set a Request Header
15-
void setHeader(const char*);
14+
void setHeader(String header);
1615
// Set Content-Type Header
17-
void setContentType(const char*);
16+
void setContentType(String contentTypeValue);
1817

1918
// GET path
20-
int get(const char*);
21-
// GET path and response
22-
int get(const char*, String*);
23-
19+
int get(String path);
2420
// POST path and body
25-
int post(const char* path, const char* body);
26-
// POST path and body and response
27-
int post(const char* path, const char* body, String*);
21+
int post(String path, String body);
2822

2923
// PUT path and body
30-
int put(const char* path, const char* body);
31-
// PUT path and body and response
32-
int put(const char* path, const char* body, String*);
24+
int put(String path, String body);
3325

3426
// DELETE path
35-
int del(const char*);
27+
int del(String path);
3628
// DELETE path and body
37-
int del(const char*, const char*);
38-
// DELETE path and response
39-
int del(const char*, String*);
40-
// DELETE path and body and response
41-
int del(const char*, const char*, String*);
29+
int del(String path, String body);
30+
String readResponse() {return responseBody;};
4231

4332
private:
4433
Client* client;
45-
int readResponse(String*);
46-
void write(const char*);
34+
int getResponse();
4735
const char* host;
4836
int port;
4937
int num_headers;
50-
const char* headers[10];
51-
const char* contentType;
38+
String headers[10];
39+
String contentType;
40+
String responseBody;
41+
int timeout;
42+
long requestStart;
5243
};

0 commit comments

Comments
 (0)