Skip to content

More updates #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 135 additions & 13 deletions HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,18 @@ void HttpClient::beginRequest()
iState = eRequestStarted;
}

int HttpClient::startRequest(const char* aURLPath, const char* aHttpMethod)
int HttpClient::startRequest(const char* aURLPath, const char* aHttpMethod,
const char* aContentType, int aContentLength, const byte aBody[])
{
tHttpState initialState = iState;

if (!iConnectionClose)
if (iState == eReadingBody)
{
flushClientRx();

resetState();
}

tHttpState initialState = iState;

if ((eIdle != iState) && (eRequestStarted != iState))
{
return HTTP_ERROR_API;
Expand Down Expand Up @@ -104,12 +105,33 @@ int HttpClient::startRequest(const char* aURLPath, const char* aHttpMethod)

// Now we're connected, send the first part of the request
int ret = sendInitialHeaders(aURLPath, aHttpMethod);
if ((initialState == eIdle) && (HTTP_SUCCESS == ret))

if (HTTP_SUCCESS == ret)
{
// This was a simple version of the API, so terminate the headers now
finishHeaders();
if (aContentType)
{
sendHeader(HTTP_HEADER_CONTENT_TYPE, aContentType);
}

if (aContentLength > 0)
{
sendHeader(HTTP_HEADER_CONTENT_LENGTH, aContentLength);
}

bool hasBody = (aBody && aContentLength > 0);

if (initialState == eIdle || hasBody)
{
// This was a simple version of the API, so terminate the headers now
finishHeaders();
}
// else we'll call it in endRequest or in the first call to print, etc.

if (hasBody)
{
write(aBody, aContentLength);
}
}
// else we'll call it in endRequest or in the first call to print, etc.

return ret;
}
Expand Down Expand Up @@ -231,12 +253,9 @@ void HttpClient::finishHeaders()

void HttpClient::flushClientRx()
{
if (iClient->connected())
while (iClient->available())
{
while (iClient->available())
{
iClient->read();
}
iClient->read();
}
}

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

int HttpClient::get(const char* aURLPath)
{
return startRequest(aURLPath, HTTP_METHOD_GET);
}

int HttpClient::get(const String& aURLPath)
{
return get(aURLPath.c_str());
}

int HttpClient::post(const char* aURLPath)
{
return startRequest(aURLPath, HTTP_METHOD_POST);
}

int HttpClient::post(const String& aURLPath)
{
return post(aURLPath.c_str());
}

int HttpClient::post(const char* aURLPath, const char* aContentType, const char* aBody)
{
return post(aURLPath, aContentType, strlen(aBody), (const byte*)aBody);
}

int HttpClient::post(const String& aURLPath, const String& aContentType, const String& aBody)
{
return post(aURLPath.c_str(), aContentType.c_str(), aBody.length(), (const byte*)aBody.c_str());
}

int HttpClient::post(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[])
{
return startRequest(aURLPath, HTTP_METHOD_POST, aContentType, aContentLength, aBody);
}

int HttpClient::put(const char* aURLPath)
{
return startRequest(aURLPath, HTTP_METHOD_PUT);
}

int HttpClient::put(const String& aURLPath)
{
return put(aURLPath.c_str());
}

int HttpClient::put(const char* aURLPath, const char* aContentType, const char* aBody)
{
return put(aURLPath, aContentType, strlen(aBody), (const byte*)aBody);
}

int HttpClient::put(const String& aURLPath, const String& aContentType, const String& aBody)
{
return put(aURLPath.c_str(), aContentType.c_str(), aBody.length(), (const byte*)aBody.c_str());
}

int HttpClient::put(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[])
{
return startRequest(aURLPath, HTTP_METHOD_PUT, aContentType, aContentLength, aBody);
}

int HttpClient::del(const char* aURLPath)
{
return startRequest(aURLPath, HTTP_METHOD_DELETE);
}

int HttpClient::del(const String& aURLPath)
{
return del(aURLPath.c_str());
}

int HttpClient::del(const char* aURLPath, const char* aContentType, const char* aBody)
{
return del(aURLPath, aContentType, strlen(aBody), (const byte*)aBody);
}

int HttpClient::del(const String& aURLPath, const String& aContentType, const String& aBody)
{
return del(aURLPath.c_str(), aContentType.c_str(), aBody.length(), (const byte*)aBody.c_str());
}

int HttpClient::del(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[])
{
return startRequest(aURLPath, HTTP_METHOD_DELETE, aContentType, aContentLength, aBody);
}

int HttpClient::responseStatusCode()
{
if (iState < eRequestSent)
Expand Down Expand Up @@ -407,6 +511,24 @@ int HttpClient::contentLength()
return iContentLength;
}

String HttpClient::responseBody()
{
int bodyLength = contentLength();
String response;

if (bodyLength > 0)
{
response.reserve(bodyLength);
}

while (available())
{
response += (char)read();
}

return response;
}

bool HttpClient::endOfBodyReached()
{
if (endOfHeadersReached() && (contentLength() != kNoContentLengthHeader))
Expand Down
78 changes: 62 additions & 16 deletions HttpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static const int HTTP_ERROR_INVALID_RESPONSE =-4;
#define HTTP_METHOD_PUT "PUT"
#define HTTP_METHOD_DELETE "DELETE"
#define HTTP_HEADER_CONTENT_LENGTH "Content-Length"
#define HTTP_HEADER_CONTENT_TYPE "Content-Type"
#define HTTP_HEADER_CONNECTION "Connection"
#define HTTP_HEADER_USER_AGENT "User-Agent"

Expand Down Expand Up @@ -64,39 +65,77 @@ class HttpClient : public Client
@param aURLPath Url to request
@return 0 if successful, else error
*/
int get(const char* aURLPath)
{ return startRequest(aURLPath, HTTP_METHOD_GET); }

int get(const String& aURLPath)
{ return get(aURLPath.c_str()); }
int get(const char* aURLPath);
int get(const String& aURLPath);

/** Connect to the server and start to send a POST request.
@param aURLPath Url to request
@return 0 if successful, else error
*/
int post(const char* aURLPath)
{ return startRequest(aURLPath, HTTP_METHOD_POST); }
int post(const char* aURLPath);
int post(const String& aURLPath);

int post(const String& aURLPath)
{ return post(aURLPath.c_str()); }
/** Connect to the server and send a POST request
with body and content type
@param aURLPath Url to request
@param aContentType Content type of request body
@param aBody Body of the request
@return 0 if successful, else error
*/
int post(const char* aURLPath, const char* aContentType, const char* aBody);
int post(const String& aURLPath, const String& aContentType, const String& aBody);
int post(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[]);

/** Connect to the server and start to send a PUT request.
@param aURLPath Url to request
@return 0 if successful, else error
*/
int put(const char* aURLPath)
{ return startRequest(aURLPath, HTTP_METHOD_PUT); }
int put(const char* aURLPath);
int put(const String& aURLPath);

int put(const String& aURLPath)
{ return put(aURLPath.c_str()); }
/** Connect to the server and send a PUT request
with body and content type
@param aURLPath Url to request
@param aContentType Content type of request body
@param aBody Body of the request
@return 0 if successful, else error
*/
int put(const char* aURLPath, const char* aContentType, const char* aBody);
int put(const String& aURLPath, const String& aContentType, const String& aBody);
int put(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[]);

/** Connect to the server and start to send the request.
/** Connect to the server and start to send a DELETE request.
@param aURLPath Url to request
@param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc.
@return 0 if successful, else error
*/
int del(const char* aURLPath);
int del(const String& aURLPath);

/** Connect to the server and send a DELETE request
with body and content type
@param aURLPath Url to request
@param aContentType Content type of request body
@param aBody Body of the request
@return 0 if successful, else error
*/
int del(const char* aURLPath, const char* aContentType, const char* aBody);
int del(const String& aURLPath, const String& aContentType, const String& aBody);
int del(const char* aURLPath, const char* aContentType, int aContentLength, const byte aBody[]);

/** Connect to the server and start to send the request.
If a body is provided, the entire request (including headers and body) will be sent
@param aURLPath Url to request
@param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc.
@param aContentType Content type of request body (optional)
@param aContentLength Length of request body (optional)
@param aBody Body of request (optional)
@return 0 if successful, else error
*/
int startRequest(const char* aURLPath,
const char* aHttpMethod);
const char* aHttpMethod,
const char* aContentType = NULL,
int aContentLength = -1,
const byte aBody[] = NULL);

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

/** Return the response body as a String
Also skips response headers if they have not been read already
MUST be called after responseStatusCode()
@return response body of request as a String
*/
String responseBody();

/** Enables connection keep-alive mode
*/
void connectionKeepAlive();
Expand Down
19 changes: 2 additions & 17 deletions examples/DweetGet/DweetGet.ino
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;
int statusCode = 0;
int contentLength = 0;
String response;

void setup() {
Expand Down Expand Up @@ -63,27 +62,13 @@ void loop() {

// send the GET request
Serial.println("making GET request");
client.beginRequest();
client.get(path);
client.endRequest();

// read the status code of the response
// read the status code and body of the response
statusCode = client.responseStatusCode();
response = client.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);

// read the content length of the response
contentLength = client.contentLength();
Serial.print("Content Length: ");
Serial.println(contentLength);

// read the response body
response = "";
response.reserve(contentLength);
while (client.available()) {
response += (char)client.read();
}

Serial.print("Response: ");
Serial.println(response);

Expand Down
23 changes: 6 additions & 17 deletions examples/DweetPost/DweetPost.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;
int statusCode = 0;
int contentLength = 0;
String response;

void setup() {
Expand Down Expand Up @@ -56,6 +55,8 @@ void loop() {
String dweetName = "scandalous-cheese-hoarder";
String path = "/dweet/for/" + dweetName;

String contentType = "application/json";

// assemble the body of the POST message:
int sensorValue = analogRead(A0);
String postData = "{\"sensorValue\":\"";
Expand All @@ -65,23 +66,11 @@ void loop() {
Serial.println("making POST request");

// send the POST request
client.beginRequest();
client.post(path);
client.sendHeader("Content-Type", "application/json");
client.sendHeader("Content-Length", postData.length());
client.endRequest();
client.write((const byte*)postData.c_str(), postData.length());

// read the status code and content length of the response
statusCode = client.responseStatusCode();
contentLength = client.contentLength();
client.post(path, contentType, postData);

// read the response body
response = "";
response.reserve(contentLength);
while (client.available()) {
response += (char)client.read();
}
// read the status code and body of the response
statusCode = client.responseStatusCode();
response = client.responseBody();

Serial.print("Status code: ");
Serial.println(statusCode);
Expand Down
Loading