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
21HttpClient::HttpClient() :
22 insecure(false) {
23 // Empty
24}
25
26unsigned 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
36unsigned 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
46void HttpClient::getAsynchronously(String &url) {
47 begin("curl");
48 if (insecure) {
49 addParameter("-k");
50 }
51 addHeader();
52 addParameter(url);
53 runAsynchronously();
54}
55
56void HttpClient::getAsynchronously(const char *url) {
57 begin("curl");
58 if (insecure) {
59 addParameter("-k");
60 }
61 addHeader();
62 addParameter(url);
63 runAsynchronously();
64}
65
66unsigned int HttpClient::post(String &url, String &data) {
67 return post(url.c_str(), data.c_str());
68}
69
70unsigned 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
84void HttpClient::postAsynchronously(String &url, String &data) {
85 postAsynchronously(url.c_str(), data.c_str());
86}
87
88void 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
102unsigned int HttpClient::patch(String &url, String &data) {
103 return patch(url.c_str(), data.c_str());
104}
105
106unsigned int HttpClient::patch(const char *url, const char *data) {
107 begin("curl");
108 if (insecure) {
109 addParameter("-k");
110 }
111 addParameter("--request");
112 addParameter("PATCH");
113 addParameter("--data");
114 addParameter(data);
115 addHeader();
116 addParameter(url);
117 return run();
118}
119
120void HttpClient::patchAsynchronously(String &url, String &data) {
121 patchAsynchronously(url.c_str(), data.c_str());
122}
123
124void HttpClient::patchAsynchronously(const char *url, const char *data) {
125 begin("curl");
126 if (insecure) {
127 addParameter("-k");
128 }
129 addParameter("--request");
130 addParameter("PATCH");
131 addParameter("--data");
132 addParameter(data);
133 addHeader();
134 addParameter(url);
135 runAsynchronously();
136}
137
138unsigned int HttpClient::put(String &url, String &data) {
139 return put(url.c_str(), data.c_str());
140}
141
142unsigned int HttpClient::put(const char *url, const char *data) {
143 begin("curl");
144 if (insecure) {
145 addParameter("-k");
146 }
147 addParameter("--request");
148 addParameter("PUT");
149 addParameter("--data");
150 addParameter(data);
151 addHeader();
152 addParameter(url);
153 return run();
154}
155
156void HttpClient::putAsynchronously(String &url, String &data) {
157 putAsynchronously(url.c_str(), data.c_str());
158}
159
160void HttpClient::putAsynchronously(const char *url, const char *data) {
161 begin("curl");
162 if (insecure) {
163 addParameter("-k");
164 }
165 addParameter("--request");
166 addParameter("PUT");
167 addParameter("--data");
168 addParameter(data);
169 addHeader();
170 addParameter(url);
171 runAsynchronously();
172}
173
174boolean HttpClient::ready() {
175 return !running();
176}
177
178unsigned int HttpClient::getResult() {
179 return exitValue();
180}
181
182void HttpClient::noCheckSSL() {
183 insecure = true;
184}
185
186void HttpClient::checkSSL() {
187 insecure = false;
188}
189
190void HttpClient::setHeader(String &header) {
191 this->header = header;
192}
193
194void HttpClient::setHeader(const char * header) {
195 this->header = String(header);
196}
197
198void HttpClient::addHeader() {
199 if (header.length() > 0) {
200 addParameter("--header");
201 addParameter(header);
202 }
203}
204