Skip to content

Commit 6c39432

Browse files
authored
Merge pull request arduino-libraries#2 from sandeepmistry/master
More updates
2 parents 4ab54b0 + 8f42a68 commit 6c39432

File tree

10 files changed

+225
-139
lines changed

10 files changed

+225
-139
lines changed

HttpClient.cpp

Lines changed: 135 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,18 @@ void HttpClient::beginRequest()
5959
iState = eRequestStarted;
6060
}
6161

62-
int HttpClient::startRequest(const char* aURLPath, const char* aHttpMethod)
62+
int HttpClient::startRequest(const char* aURLPath, const char* aHttpMethod,
63+
const char* aContentType, int aContentLength, const byte aBody[])
6364
{
64-
tHttpState initialState = iState;
65-
66-
if (!iConnectionClose)
65+
if (iState == eReadingBody)
6766
{
6867
flushClientRx();
6968

7069
resetState();
7170
}
7271

72+
tHttpState initialState = iState;
73+
7374
if ((eIdle != iState) && (eRequestStarted != iState))
7475
{
7576
return HTTP_ERROR_API;
@@ -104,12 +105,33 @@ int HttpClient::startRequest(const char* aURLPath, const char* aHttpMethod)
104105

105106
// Now we're connected, send the first part of the request
106107
int ret = sendInitialHeaders(aURLPath, aHttpMethod);
107-
if ((initialState == eIdle) && (HTTP_SUCCESS == ret))
108+
109+
if (HTTP_SUCCESS == ret)
108110
{
109-
// This was a simple version of the API, so terminate the headers now
110-
finishHeaders();
111+
if (aContentType)
112+
{
113+
sendHeader(HTTP_HEADER_CONTENT_TYPE, aContentType);
114+
}
115+
116+
if (aContentLength > 0)
117+
{
118+
sendHeader(HTTP_HEADER_CONTENT_LENGTH, aContentLength);
119+
}
120+
121+
bool hasBody = (aBody && aContentLength > 0);
122+
123+
if (initialState == eIdle || hasBody)
124+
{
125+
// This was a simple version of the API, so terminate the headers now
126+
finishHeaders();
127+
}
128+
// else we'll call it in endRequest or in the first call to print, etc.
129+
130+
if (hasBody)
131+
{
132+
write(aBody, aContentLength);
133+
}
111134
}
112-
// else we'll call it in endRequest or in the first call to print, etc.
113135

114136
return ret;
115137
}
@@ -231,12 +253,9 @@ void HttpClient::finishHeaders()
231253

232254
void HttpClient::flushClientRx()
233255
{
234-
if (iClient->connected())
256+
while (iClient->available())
235257
{
236-
while (iClient->available())
237-
{
238-
iClient->read();
239-
}
258+
iClient->read();
240259
}
241260
}
242261

@@ -250,6 +269,91 @@ void HttpClient::endRequest()
250269
// else the end of headers has already been sent, so nothing to do here
251270
}
252271

272+
int HttpClient::get(const char* aURLPath)
273+
{
274+
return startRequest(aURLPath, HTTP_METHOD_GET);
275+
}
276+
277+
int HttpClient::get(const String& aURLPath)
278+
{
279+
return get(aURLPath.c_str());
280+
}
281+
282+
int HttpClient::post(const char* aURLPath)
283+
{
284+
return startRequest(aURLPath, HTTP_METHOD_POST);
285+
}
286+
287+
int HttpClient::post(const String& aURLPath)
288+
{
289+
return post(aURLPath.c_str());
290+
}
291+
292+
int HttpClient::post(const char* aURLPath, const char* aContentType, const char* aBody)
293+
{
294+
return post(aURLPath, aContentType, strlen(aBody), (const byte*)aBody);
295+
}
296+
297+
int HttpClient::post(const String& aURLPath, const String& aContentType, const String& aBody)
298+
{
299+
return post(aURLPath.c_str(), aContentType.c_str(), aBody.length(), (const byte*)aBody.c_str());
300+
}
301+
302+
int HttpClient::post(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[])
303+
{
304+
return startRequest(aURLPath, HTTP_METHOD_POST, aContentType, aContentLength, aBody);
305+
}
306+
307+
int HttpClient::put(const char* aURLPath)
308+
{
309+
return startRequest(aURLPath, HTTP_METHOD_PUT);
310+
}
311+
312+
int HttpClient::put(const String& aURLPath)
313+
{
314+
return put(aURLPath.c_str());
315+
}
316+
317+
int HttpClient::put(const char* aURLPath, const char* aContentType, const char* aBody)
318+
{
319+
return put(aURLPath, aContentType, strlen(aBody), (const byte*)aBody);
320+
}
321+
322+
int HttpClient::put(const String& aURLPath, const String& aContentType, const String& aBody)
323+
{
324+
return put(aURLPath.c_str(), aContentType.c_str(), aBody.length(), (const byte*)aBody.c_str());
325+
}
326+
327+
int HttpClient::put(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[])
328+
{
329+
return startRequest(aURLPath, HTTP_METHOD_PUT, aContentType, aContentLength, aBody);
330+
}
331+
332+
int HttpClient::del(const char* aURLPath)
333+
{
334+
return startRequest(aURLPath, HTTP_METHOD_DELETE);
335+
}
336+
337+
int HttpClient::del(const String& aURLPath)
338+
{
339+
return del(aURLPath.c_str());
340+
}
341+
342+
int HttpClient::del(const char* aURLPath, const char* aContentType, const char* aBody)
343+
{
344+
return del(aURLPath, aContentType, strlen(aBody), (const byte*)aBody);
345+
}
346+
347+
int HttpClient::del(const String& aURLPath, const String& aContentType, const String& aBody)
348+
{
349+
return del(aURLPath.c_str(), aContentType.c_str(), aBody.length(), (const byte*)aBody.c_str());
350+
}
351+
352+
int HttpClient::del(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[])
353+
{
354+
return startRequest(aURLPath, HTTP_METHOD_DELETE, aContentType, aContentLength, aBody);
355+
}
356+
253357
int HttpClient::responseStatusCode()
254358
{
255359
if (iState < eRequestSent)
@@ -407,6 +511,24 @@ int HttpClient::contentLength()
407511
return iContentLength;
408512
}
409513

514+
String HttpClient::responseBody()
515+
{
516+
int bodyLength = contentLength();
517+
String response;
518+
519+
if (bodyLength > 0)
520+
{
521+
response.reserve(bodyLength);
522+
}
523+
524+
while (available())
525+
{
526+
response += (char)read();
527+
}
528+
529+
return response;
530+
}
531+
410532
bool HttpClient::endOfBodyReached()
411533
{
412534
if (endOfHeadersReached() && (contentLength() != kNoContentLengthHeader))

HttpClient.h

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ static const int HTTP_ERROR_INVALID_RESPONSE =-4;
3131
#define HTTP_METHOD_PUT "PUT"
3232
#define HTTP_METHOD_DELETE "DELETE"
3333
#define HTTP_HEADER_CONTENT_LENGTH "Content-Length"
34+
#define HTTP_HEADER_CONTENT_TYPE "Content-Type"
3435
#define HTTP_HEADER_CONNECTION "Connection"
3536
#define HTTP_HEADER_USER_AGENT "User-Agent"
3637

@@ -64,39 +65,77 @@ class HttpClient : public Client
6465
@param aURLPath Url to request
6566
@return 0 if successful, else error
6667
*/
67-
int get(const char* aURLPath)
68-
{ return startRequest(aURLPath, HTTP_METHOD_GET); }
69-
70-
int get(const String& aURLPath)
71-
{ return get(aURLPath.c_str()); }
68+
int get(const char* aURLPath);
69+
int get(const String& aURLPath);
7270

7371
/** Connect to the server and start to send a POST request.
7472
@param aURLPath Url to request
7573
@return 0 if successful, else error
7674
*/
77-
int post(const char* aURLPath)
78-
{ return startRequest(aURLPath, HTTP_METHOD_POST); }
75+
int post(const char* aURLPath);
76+
int post(const String& aURLPath);
7977

80-
int post(const String& aURLPath)
81-
{ return post(aURLPath.c_str()); }
78+
/** Connect to the server and send a POST request
79+
with body and content type
80+
@param aURLPath Url to request
81+
@param aContentType Content type of request body
82+
@param aBody Body of the request
83+
@return 0 if successful, else error
84+
*/
85+
int post(const char* aURLPath, const char* aContentType, const char* aBody);
86+
int post(const String& aURLPath, const String& aContentType, const String& aBody);
87+
int post(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[]);
8288

8389
/** Connect to the server and start to send a PUT request.
8490
@param aURLPath Url to request
8591
@return 0 if successful, else error
8692
*/
87-
int put(const char* aURLPath)
88-
{ return startRequest(aURLPath, HTTP_METHOD_PUT); }
93+
int put(const char* aURLPath);
94+
int put(const String& aURLPath);
8995

90-
int put(const String& aURLPath)
91-
{ return put(aURLPath.c_str()); }
96+
/** Connect to the server and send a PUT request
97+
with body and content type
98+
@param aURLPath Url to request
99+
@param aContentType Content type of request body
100+
@param aBody Body of the request
101+
@return 0 if successful, else error
102+
*/
103+
int put(const char* aURLPath, const char* aContentType, const char* aBody);
104+
int put(const String& aURLPath, const String& aContentType, const String& aBody);
105+
int put(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[]);
92106

93-
/** Connect to the server and start to send the request.
107+
/** Connect to the server and start to send a DELETE request.
94108
@param aURLPath Url to request
95-
@param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc.
109+
@return 0 if successful, else error
110+
*/
111+
int del(const char* aURLPath);
112+
int del(const String& aURLPath);
113+
114+
/** Connect to the server and send a DELETE request
115+
with body and content type
116+
@param aURLPath Url to request
117+
@param aContentType Content type of request body
118+
@param aBody Body of the request
119+
@return 0 if successful, else error
120+
*/
121+
int del(const char* aURLPath, const char* aContentType, const char* aBody);
122+
int del(const String& aURLPath, const String& aContentType, const String& aBody);
123+
int del(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[]);
124+
125+
/** Connect to the server and start to send the request.
126+
If a body is provided, the entire request (including headers and body) will be sent
127+
@param aURLPath Url to request
128+
@param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc.
129+
@param aContentType Content type of request body (optional)
130+
@param aContentLength Length of request body (optional)
131+
@param aBody Body of request (optional)
96132
@return 0 if successful, else error
97133
*/
98134
int startRequest(const char* aURLPath,
99-
const char* aHttpMethod);
135+
const char* aHttpMethod,
136+
const char* aContentType = NULL,
137+
int aContentLength = -1,
138+
const byte aBody[] = NULL);
100139

101140
/** Send an additional header line. This can only be called in between the
102141
calls to startRequest and finishRequest.
@@ -207,6 +246,13 @@ class HttpClient : public Client
207246
*/
208247
int contentLength();
209248

249+
/** Return the response body as a String
250+
Also skips response headers if they have not been read already
251+
MUST be called after responseStatusCode()
252+
@return response body of request as a String
253+
*/
254+
String responseBody();
255+
210256
/** Enables connection keep-alive mode
211257
*/
212258
void connectionKeepAlive();

examples/DweetGet/DweetGet.ino

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ WiFiClient wifi;
3333
HttpClient client = HttpClient(wifi, serverAddress, port);
3434
int status = WL_IDLE_STATUS;
3535
int statusCode = 0;
36-
int contentLength = 0;
3736
String response;
3837

3938
void setup() {
@@ -63,27 +62,13 @@ void loop() {
6362

6463
// send the GET request
6564
Serial.println("making GET request");
66-
client.beginRequest();
6765
client.get(path);
68-
client.endRequest();
6966

70-
// read the status code of the response
67+
// read the status code and body of the response
7168
statusCode = client.responseStatusCode();
69+
response = client.responseBody();
7270
Serial.print("Status code: ");
7371
Serial.println(statusCode);
74-
75-
// read the content length of the response
76-
contentLength = client.contentLength();
77-
Serial.print("Content Length: ");
78-
Serial.println(contentLength);
79-
80-
// read the response body
81-
response = "";
82-
response.reserve(contentLength);
83-
while (client.available()) {
84-
response += (char)client.read();
85-
}
86-
8772
Serial.print("Response: ");
8873
Serial.println(response);
8974

examples/DweetPost/DweetPost.ino

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ WiFiClient wifi;
2727
HttpClient client = HttpClient(wifi, serverAddress, port);
2828
int status = WL_IDLE_STATUS;
2929
int statusCode = 0;
30-
int contentLength = 0;
3130
String response;
3231

3332
void setup() {
@@ -56,6 +55,8 @@ void loop() {
5655
String dweetName = "scandalous-cheese-hoarder";
5756
String path = "/dweet/for/" + dweetName;
5857

58+
String contentType = "application/json";
59+
5960
// assemble the body of the POST message:
6061
int sensorValue = analogRead(A0);
6162
String postData = "{\"sensorValue\":\"";
@@ -65,23 +66,11 @@ void loop() {
6566
Serial.println("making POST request");
6667

6768
// send the POST request
68-
client.beginRequest();
69-
client.post(path);
70-
client.sendHeader("Content-Type", "application/json");
71-
client.sendHeader("Content-Length", postData.length());
72-
client.endRequest();
73-
client.write((const byte*)postData.c_str(), postData.length());
74-
75-
// read the status code and content length of the response
76-
statusCode = client.responseStatusCode();
77-
contentLength = client.contentLength();
69+
client.post(path, contentType, postData);
7870

79-
// read the response body
80-
response = "";
81-
response.reserve(contentLength);
82-
while (client.available()) {
83-
response += (char)client.read();
84-
}
71+
// read the status code and body of the response
72+
statusCode = client.responseStatusCode();
73+
response = client.responseBody();
8574

8675
Serial.print("Status code: ");
8776
Serial.println(statusCode);

0 commit comments

Comments
 (0)