-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a80b31e
Showing
11 changed files
with
577 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
Copyright (c) 2016 Imre Horvath. All rights reserved. | ||
This library is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU Lesser General Public | ||
License as published by the Free Software Foundation; either | ||
version 2.1 of the License, or (at your option) any later version. | ||
This library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public | ||
License along with this library; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#include "BridgeHttpClient.h" | ||
|
||
void BridgeHttpClient::get(const char *url) { | ||
request("GET", url, NULL); | ||
} | ||
|
||
void BridgeHttpClient::post(const char *url, const char *data) { | ||
request("POST", url, data); | ||
} | ||
|
||
void BridgeHttpClient::put(const char *url, const char *data) { | ||
request("PUT", url, data); | ||
} | ||
|
||
void BridgeHttpClient::del(const char *url) { | ||
request("DELETE", url, NULL); | ||
} | ||
|
||
unsigned int BridgeHttpClient::getResponseCode() { | ||
Process p; | ||
p.runShellCommand("head -n 1 " + tempFileName + " | cut -d' ' -f2"); | ||
return p.parseInt(); | ||
} | ||
|
||
String BridgeHttpClient::getResponseHeaders() { | ||
String responseHeaders; | ||
|
||
Process p; | ||
p.runShellCommand("tail -n +2 " + tempFileName); | ||
|
||
while (p.available() > 0) { | ||
char c = p.read(); | ||
responseHeaders += c; | ||
} | ||
|
||
responseHeaders.trim(); | ||
return responseHeaders; | ||
} | ||
|
||
void BridgeHttpClient::getAsync(const char *url) { | ||
request("GET", url, NULL, true); | ||
} | ||
|
||
void BridgeHttpClient::postAsync(const char *url, const char *data) { | ||
request("POST", url, data, true); | ||
} | ||
|
||
void BridgeHttpClient::putAsync(const char *url, const char *data) { | ||
request("PUT", url, data, true); | ||
} | ||
|
||
void BridgeHttpClient::delAsync(const char *url) { | ||
request("DELETE", url, NULL, true); | ||
} | ||
|
||
int BridgeHttpClient::addHeader(const char *header) { | ||
if (headerIndex < HEADERCNT) { | ||
headers[headerIndex++] = header; | ||
return 0; | ||
} | ||
return -1; | ||
} | ||
|
||
void BridgeHttpClient::basicAuth(const char *user, const char *passwd) { | ||
this->user = user; | ||
this->passwd = passwd; | ||
} | ||
|
||
String BridgeHttpClient::getResponseHeaderValue(const String& header) { | ||
if (cachedRespHeaders.length() == 0) { | ||
cachedRespHeaders = getResponseHeaders(); | ||
} | ||
int startOfValue = cachedRespHeaders.indexOf(':', cachedRespHeaders.indexOf(header)) + 1; | ||
String respValue = cachedRespHeaders.substring(startOfValue, | ||
cachedRespHeaders.indexOf('\n', startOfValue)); | ||
respValue.trim(); | ||
return respValue; | ||
} | ||
|
||
void BridgeHttpClient::request(const char *verb, const char *url, const char *data, bool async) { | ||
Process p; | ||
p.runShellCommand("mktemp"); | ||
tempFileName = p.readStringUntil('\n'); | ||
|
||
clearCachedRespHeaders(); | ||
|
||
begin("curl"); | ||
addParameter("-D"); | ||
addParameter(tempFileName); | ||
|
||
if (verb != "GET") { | ||
addParameter("-X"); | ||
addParameter(verb); | ||
} | ||
|
||
if (isInsecureEnabled) { | ||
addParameter("-k"); | ||
} | ||
|
||
if (user && passwd) { | ||
String auth; | ||
auth += user; | ||
auth += ":"; | ||
auth += passwd; | ||
|
||
addParameter("-u"); | ||
addParameter(auth); | ||
} | ||
|
||
for (int i = 0; i < headerIndex; i++) { | ||
addParameter("-H"); | ||
addParameter(headers[i]); | ||
} | ||
|
||
if (data != NULL) { | ||
addParameter("-d"); | ||
addParameter(data); | ||
} | ||
|
||
addParameter(url); | ||
|
||
if (async) { | ||
runAsynchronously(); | ||
} else { | ||
(void) run(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
Copyright (c) 2016 Imre Horvath. All rights reserved. | ||
This library is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU Lesser General Public | ||
License as published by the Free Software Foundation; either | ||
version 2.1 of the License, or (at your option) any later version. | ||
This library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public | ||
License along with this library; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#ifndef _BRIDGE_HTTP_CLIENT_H_ | ||
#define _BRIDGE_HTTP_CLIENT_H_ | ||
|
||
#include <Process.h> | ||
|
||
class BridgeHttpClient : public Process { | ||
|
||
public: | ||
BridgeHttpClient() : isInsecureEnabled(false), | ||
user(NULL), passwd(NULL), | ||
headerIndex(0) {} | ||
|
||
void get(const char *url); | ||
void post(const char *url, const char *data); | ||
void put(const char *url, const char *data); | ||
void del(const char *url); | ||
|
||
/** | ||
* Call this method after the request has finished, | ||
* to get the HTTP response code from the server. | ||
* | ||
* Returns the response code eg. 200 | ||
*/ | ||
unsigned int getResponseCode(); | ||
|
||
/** | ||
* Call this method after the request has finished, | ||
* to get the response headers received from the server. | ||
* | ||
* Returns a String object with all the response headers included. | ||
*/ | ||
String getResponseHeaders(); | ||
|
||
// unsigned int exitValue(); // Method in superclass to retrieve the exit status | ||
// boolean running(); // Method in superclass which can be used to check if the async request is still running or has finished. | ||
|
||
void getAsync(const char *url); | ||
void postAsync(const char *url, const char *data); | ||
void putAsync(const char *url, const char *data); | ||
void delAsync(const char *url); | ||
|
||
/** | ||
* Call this method before issuing the requests, | ||
* to allows "insecure" SSL. | ||
* | ||
* Useful in the following case for example: | ||
* Certificate cannot be authenticated with known CA certificates. | ||
*/ | ||
void enableInsecure() { isInsecureEnabled = true; } | ||
|
||
/** | ||
* Call this method before issuing the request, | ||
* to add an extra header to the request. | ||
* | ||
* Returns 0 if the header fits into the array of headers. -1 otherwise. | ||
*/ | ||
int addHeader(const char *header); | ||
|
||
/** | ||
* Call this method before issuing the request, | ||
* to include basic authorization into the request. | ||
*/ | ||
void basicAuth(const char *user, const char *passwd); | ||
|
||
/** | ||
* Call this method between the different request calls on the same client, | ||
* to clear/setup the request headers for the next call. | ||
*/ | ||
void clearHeaders() { headerIndex = 0; } | ||
|
||
/** | ||
* Call this method between the different request calls on the same client, | ||
* to clear the previously set basic authorization for the subsequent call. | ||
*/ | ||
void clearAuth() { user = passwd = NULL; } | ||
|
||
/** | ||
* Call this method after the request has finished, | ||
* to get a particular response header value. | ||
* | ||
* Returns a String object representing the response header value | ||
*/ | ||
String getResponseHeaderValue(const String& header); | ||
|
||
private: | ||
String tempFileName; | ||
bool isInsecureEnabled; | ||
const char *user; | ||
const char *passwd; | ||
static const int HEADERCNT = 10; // We handle max. 10 extra request headers | ||
const char *headers[HEADERCNT]; | ||
int headerIndex; | ||
String cachedRespHeaders; | ||
|
||
void request(const char *verb, const char *url, const char *data, bool async=false); | ||
void clearCachedRespHeaders() { cachedRespHeaders = ""; } | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Copyright (c) 2016 Imre Horvath. All rights reserved. | ||
|
||
This library is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU Lesser General Public | ||
License as published by the Free Software Foundation; either | ||
version 2.1 of the License, or (at your option) any later version. | ||
|
||
This library is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
Lesser General Public License for more details. | ||
|
||
You should have received a copy of the GNU Lesser General Public | ||
License along with this library; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
A practical and easy to use generic HTTP client library for the Yun | ||
=================================================================== | ||
|
||
Features | ||
-------- | ||
|
||
* GET/POST/PUT/DELETE | ||
* Both sync and async requests | ||
* You can access the HTTP response code and headers, not just the body | ||
* Basic authorization | ||
* Multiple extra request headers | ||
* Extraction of a particular response header value | ||
* Designed for efficiency and easy usage | ||
* Built on top of the Bridge library | ||
* Uses cURL on the Linux side | ||
|
||
API | ||
--- | ||
|
||
```c++ | ||
void get(const char *url); | ||
void post(const char *url, const char *data); | ||
void put(const char *url, const char *data); | ||
void del(const char *url); | ||
|
||
/** | ||
* Call this method after the request has finished, | ||
* to get the HTTP response code from the server. | ||
* | ||
* Returns the response code eg. 200 | ||
*/ | ||
unsigned int getResponseCode(); | ||
|
||
/** | ||
* Call this method after the request has finished, | ||
* to get the response headers received from the server. | ||
* | ||
* Returns a String object with all the response headers included. | ||
*/ | ||
String getResponseHeaders(); | ||
|
||
// unsigned int exitValue(); // Method in superclass to retrieve the exit status | ||
// boolean running(); // Method in superclass which can be used to check if the async request is still running or has finished. | ||
|
||
void getAsync(const char *url); | ||
void postAsync(const char *url, const char *data); | ||
void putAsync(const char *url, const char *data); | ||
void delAsync(const char *url); | ||
|
||
/** | ||
* Call this method before issuing the requests, | ||
* to allows "insecure" SSL. | ||
* | ||
* Useful in the following case for example: | ||
* Certificate cannot be authenticated with known CA certificates. | ||
*/ | ||
void enableInsecure() { isInsecureEnabled = true; } | ||
|
||
/** | ||
* Call this method before issuing the request, | ||
* to add an extra header to the request. | ||
* | ||
* Returns 0 if the header fits into the array of headers. -1 otherwise. | ||
*/ | ||
int addHeader(const char *header); | ||
|
||
/** | ||
* Call this method before issuing the request, | ||
* to include basic authorization into the request. | ||
*/ | ||
void basicAuth(const char *user, const char *passwd); | ||
|
||
/** | ||
* Call this method between the different request calls on the same client, | ||
* to clear/setup the request headers for the next call. | ||
*/ | ||
void clearHeaders() { headerIndex = 0; } | ||
|
||
/** | ||
* Call this method between the different request calls on the same client, | ||
* to clear the previously set basic authorization for the subsequent call. | ||
*/ | ||
void clearAuth() { user = passwd = NULL; } | ||
|
||
/** | ||
* Call this method after the request has finished, | ||
* to get a particular response header value. | ||
* | ||
* Returns a String object representing the response header value | ||
*/ | ||
String getResponseHeaderValue(const String& header); | ||
``` | ||
TODO | ||
---- | ||
* Add proxy support | ||
* Provide more examples |
Oops, something went wrong.