[136] | 1 | /*
|
---|
| 2 | Copyright (c) 2013-2014 Arduino LLC. All right reserved.
|
---|
| 3 |
|
---|
| 4 | This library is free software; you can redistribute it and/or
|
---|
| 5 | modify it under the terms of the GNU Lesser General Public
|
---|
| 6 | License as published by the Free Software Foundation; either
|
---|
| 7 | version 2.1 of the License, or (at your option) any later version.
|
---|
| 8 |
|
---|
| 9 | This library is distributed in the hope that it will be useful,
|
---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 12 | Lesser General Public License for more details.
|
---|
| 13 |
|
---|
| 14 | You should have received a copy of the GNU Lesser General Public
|
---|
| 15 | License along with this library; if not, write to the Free Software
|
---|
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
| 17 | */
|
---|
| 18 |
|
---|
| 19 | #include "HttpClient.h"
|
---|
| 20 |
|
---|
| 21 | HttpClient::HttpClient() :
|
---|
| 22 | insecure(false) {
|
---|
| 23 | // Empty
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | unsigned int HttpClient::get(String &url) {
|
---|
| 27 | begin("curl");
|
---|
| 28 | if (insecure) {
|
---|
| 29 | addParameter("-k");
|
---|
| 30 | }
|
---|
| 31 | addHeader();
|
---|
| 32 | addParameter(url);
|
---|
| 33 | return run();
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | unsigned int HttpClient::get(const char *url) {
|
---|
| 37 | begin("curl");
|
---|
| 38 | if (insecure) {
|
---|
| 39 | addParameter("-k");
|
---|
| 40 | }
|
---|
| 41 | addHeader();
|
---|
| 42 | addParameter(url);
|
---|
| 43 | return run();
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | void HttpClient::getAsynchronously(String &url) {
|
---|
| 47 | begin("curl");
|
---|
| 48 | if (insecure) {
|
---|
| 49 | addParameter("-k");
|
---|
| 50 | }
|
---|
| 51 | addHeader();
|
---|
| 52 | addParameter(url);
|
---|
| 53 | runAsynchronously();
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | void HttpClient::getAsynchronously(const char *url) {
|
---|
| 57 | begin("curl");
|
---|
| 58 | if (insecure) {
|
---|
| 59 | addParameter("-k");
|
---|
| 60 | }
|
---|
| 61 | addHeader();
|
---|
| 62 | addParameter(url);
|
---|
| 63 | runAsynchronously();
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | unsigned int HttpClient::post(String &url, String &data) {
|
---|
| 67 | return post(url.c_str(), data.c_str());
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | unsigned int HttpClient::post(const char *url, const char *data) {
|
---|
| 71 | begin("curl");
|
---|
| 72 | if (insecure) {
|
---|
| 73 | addParameter("-k");
|
---|
| 74 | }
|
---|
| 75 | addParameter("--request");
|
---|
| 76 | addParameter("POST");
|
---|
| 77 | addParameter("--data");
|
---|
| 78 | addParameter(data);
|
---|
| 79 | addHeader();
|
---|
| 80 | addParameter(url);
|
---|
| 81 | return run();
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | void HttpClient::postAsynchronously(String &url, String &data) {
|
---|
| 85 | postAsynchronously(url.c_str(), data.c_str());
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | void HttpClient::postAsynchronously(const char *url, const char *data) {
|
---|
| 89 | begin("curl");
|
---|
| 90 | if (insecure) {
|
---|
| 91 | addParameter("-k");
|
---|
| 92 | }
|
---|
| 93 | addParameter("--request");
|
---|
| 94 | addParameter("POST");
|
---|
| 95 | addParameter("--data");
|
---|
| 96 | addParameter(data);
|
---|
| 97 | addHeader();
|
---|
| 98 | addParameter(url);
|
---|
| 99 | runAsynchronously();
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | boolean HttpClient::ready() {
|
---|
| 103 | return !running();
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | unsigned int HttpClient::getResult() {
|
---|
| 107 | return exitValue();
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | void HttpClient::noCheckSSL() {
|
---|
| 111 | insecure = true;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | void HttpClient::checkSSL() {
|
---|
| 115 | insecure = false;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | void HttpClient::setHeader(String &header) {
|
---|
| 119 | this->header = header;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | void HttpClient::setHeader(const char * header) {
|
---|
| 123 | this->header = String(header);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | void HttpClient::addHeader() {
|
---|
| 127 | if (header.length() > 0) {
|
---|
| 128 | addParameter("--header");
|
---|
| 129 | addParameter(header);
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|