-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathHTTP.cpp
363 lines (310 loc) · 12.9 KB
/
HTTP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "HTTP.h"
#include "URC.h"
/* HTTP Class */
HTTP::HTTP(DTE &dte, IP &ip) {
this->dte = &dte;
this->ip = &ip;
this->initialized = false;
this->httpStatus = (struct HttpStatus){"GET", 0, 0};
this->serverResponseDataLength = 0;
}
unsigned char HTTP::methodIndex(const char method[]) {
if (strcmp_P(method, (const char *)F("GET")) == 0) return 0;
if (strcmp_P(method, (const char *)F("POST")) == 0) return 1;
if (strcmp_P(method, (const char *)F("HEAD")) == 0) return 2;
return 0;
}
unsigned char HTTP::methodIndex(const __FlashStringHelper *method) {
char buffer[strlen_P((const char *)method) + 1];
strcpy_P(buffer, (const char *)method);
return methodIndex(buffer);
}
bool HTTP::atInitializeHttpService(void) {
const __FlashStringHelper *command = F("AT+HTTPINIT\r");
dte->clearReceivedBuffer();
if (!dte->ATCommand(command)) return false;
if (!dte->ATResponseOk()) return false;
initialized = true;
return true;
}
bool HTTP::atTerminateHttpService(void) {
const __FlashStringHelper *command = F("AT+HTTPTERM\r");
initialized = false;
dte->clearReceivedBuffer();
if (!dte->ATCommand(command)) return false;
if (!dte->ATResponseOk()) return false;
return true;
}
bool HTTP::atSetHttpParametersValue(const char paramTag[], const char paramValue[], const char userdataDelimiter[]) {
const __FlashStringHelper *command = F("AT+HTTPPARA=\"%s\",\"%s\"\r");
if (strlen(userdataDelimiter) > 0) command = F("AT+HTTPPARA=\"%s\",\"%s\",\"%s\"\r");
char buffer[22 + strlen(paramTag) + strlen(paramValue) + strlen(userdataDelimiter)]; // "AT+HTTPPARA=\"{paramTag}\",\"{paramValue}\",\"{userdataDelimiter}\"\r"
if (strlen(userdataDelimiter) == 0)
sprintf_P(buffer, (const char *)command, paramTag, paramValue);
else
sprintf_P(buffer, (const char *)command, paramTag, paramValue, userdataDelimiter);
dte->clearReceivedBuffer();
if (!dte->ATCommand(buffer)) return false;
if (!dte->ATResponseOk()) return false;
return true;
}
bool HTTP::atSetHttpParametersValue(const __FlashStringHelper *paramTag, const char paramValue[], const char userdataDelimiter[]) {
char buffer[strlen_P((const char *)paramTag) + 1];
strcpy_P(buffer, (const char *)paramTag);
return atSetHttpParametersValue(buffer, paramValue, userdataDelimiter);
}
bool HTTP::atSetHttpParametersValue(const char paramTag[], const __FlashStringHelper *paramValue, const char userdataDelimiter[]) {
char buffer[strlen_P((const char *)paramValue) + 1];
strcpy_P(buffer, (const char *)paramValue);
return atSetHttpParametersValue(paramTag, buffer, userdataDelimiter);
}
bool HTTP::atSetHttpParametersValue(const __FlashStringHelper *paramTag, const __FlashStringHelper *paramValue, const char userdataDelimiter[]) {
char buffer[strlen_P((const char *)paramValue) + 1];
strcpy_P(buffer, (const char *)paramValue);
return atSetHttpParametersValue(paramTag, buffer, userdataDelimiter);
}
bool HTTP::atSetHttpParametersValue(const char paramTag[], const char paramValue[], const __FlashStringHelper *userdataDelimiter) {
char buffer[strlen_P((const char *)userdataDelimiter) + 1];
strcpy_P(buffer, (const char *)userdataDelimiter);
return atSetHttpParametersValue(paramTag, paramValue, buffer);
}
bool HTTP::atSetHttpParametersValue(const __FlashStringHelper *paramTag, const char paramValue[], const __FlashStringHelper *userdataDelimiter) {
char buffer[strlen_P((const char *)userdataDelimiter) + 1];
strcpy_P(buffer, (const char *)userdataDelimiter);
return atSetHttpParametersValue(paramTag, paramValue, buffer);
}
bool HTTP::atSetHttpParametersValue(const char paramTag[], const __FlashStringHelper *paramValue, const __FlashStringHelper *userdataDelimiter) {
char buffer[strlen_P((const char *)userdataDelimiter) + 1];
strcpy_P(buffer, (const char *)userdataDelimiter);
return atSetHttpParametersValue(paramTag, paramValue, buffer);
}
bool HTTP::atSetHttpParametersValue(const __FlashStringHelper *paramTag, const __FlashStringHelper *paramValue, const __FlashStringHelper *userdataDelimiter) {
char buffer[strlen_P((const char *)userdataDelimiter) + 1];
strcpy_P(buffer, (const char *)userdataDelimiter);
return atSetHttpParametersValue(paramTag, paramValue, buffer);
}
bool HTTP::atInputHttpData(const char data[], unsigned int timeout) {
const __FlashStringHelper *command = F("AT+HTTPDATA=%u,%d\r");
const __FlashStringHelper *response = F("DOWNLOAD");
char buffer[15 + 4 + 5]; // "AT+HTTPDATA={strlen(data)},{timeout}}\r"
sprintf_P(buffer, (const char *)command, strlen(data), timeout);
dte->clearReceivedBuffer();
if (!dte->ATCommand(buffer)) return false;
if (!dte->ATResponseEqual(response)) return false;
size_t i = 0;
unsigned long t = millis();
while (i < strlen(data)) {
i += dte->write(data);
if (millis() - t > timeout) return false;
}
if (!dte->ATResponseOk(timeout + 500)) return false;
return true;
}
bool HTTP::atInputHttpData(const __FlashStringHelper *data, unsigned int timeout) {
char buffer[strlen_P((const char *)data) + 1];
strcpy_P(buffer, (const char *)data);
return atInputHttpData(buffer, timeout);
}
bool HTTP::atHttpMethodAction(unsigned char method) {
const __FlashStringHelper *command = F("AT+HTTPACTION=%d\r");
char buffer[17]; // "AT+HTTPACTION=X\r"
sprintf_P(buffer, (const char *)command, method);
Urc.httpAction.updated = false;
Urc.httpAction.method = method;
Urc.httpAction.dataLength = 0;
Urc.httpAction.statusCode = 0;
dte->clearReceivedBuffer();
if (!dte->ATCommand(buffer)) return false;
if (!dte->ATResponseOk(1000)) return false;
return true;
}
bool HTTP::atReadHttpServerResponse(char dataReceived[], unsigned long startAddress, unsigned long byteSize) {
char buffer[15 + 7 + 7]; // "AT+HTTPREAD=XXXXXXX,XXXXXXX\r"
unsigned long serverResponseDataLength = 0;
if (byteSize > 0) {
const __FlashStringHelper *command = F("AT+HTTPREAD=%ld,%ld\r");
sprintf_P(buffer, (const char *)command, startAddress, byteSize);
} else
return false;
const __FlashStringHelper *response = F("+HTTPREAD:");
dte->clearReceivedBuffer();
if (!dte->ATCommand(buffer)) return false;
if (dte->ATResponse()) {
if (dte->isResponseContain("ERROR")) return false;
if (dte->isResponseOk()) {
this->serverResponseDataLength = 0;
strcpy(dataReceived, "");
return true;
}
if (!dte->isResponseContain(response)) {
Urc.unsolicitedResultCode(dte->getResponse());
return false;
}
}
char *pointer = strstr_P(dte->getResponse(), (const char *)response) + strlen_P((const char *)response);
char *str = strtok(pointer, "\r\n");
serverResponseDataLength = atoi(str);
this->serverResponseDataLength = serverResponseDataLength;
if (byteSize > serverResponseDataLength)
byteSize = serverResponseDataLength;
if (byteSize > 0)
dte->readBytes(dataReceived, byteSize);
dataReceived[byteSize] = '\0';
if (!dte->ATResponseOk()) return false;
return true;
}
bool HTTP::atReadHttpStatus(void) {
const __FlashStringHelper *command = F("AT+HTTPSTATUS?\r");
const __FlashStringHelper *response = F("+HTTPSTATUS: ");
struct HttpStatus httpStatus;
dte->clearReceivedBuffer();
if (!dte->ATCommand(command)) return false;
if (!dte->ATResponseContain(response)) return false;
char *pointer = strstr_P(dte->getResponse(), (const char *)response) + strlen_P((const char *)response);
char *str = strtok(pointer, ",");
for (unsigned char i = 0; i < 4 && str != NULL; i++) {
if (i == 0) strcpy(httpStatus.mode, str);
if (i == 1) httpStatus.status = str[0] - '0';
if (i == 2) httpStatus.finish = atol(str);
if (i == 3) httpStatus.remain = atol(str);
str = strtok(NULL, ",");
}
if (!dte->ATResponseOk()) return false;
this->httpStatus = httpStatus;
return true;
}
bool HTTP::atSslHttp(void) {
const __FlashStringHelper *command = F("AT+HTTPSSL\r");
dte->clearReceivedBuffer();
if (!dte->ATCommand(command)) return false;
if (!dte->ATResponseOk()) return false;
sslEnabled = true;
return true;
}
bool HTTP::atSslHttp(bool enable) {
const __FlashStringHelper *command = F("AT+HTTPSSL=%d\r");
char buffer[14]; // "AT+HTTPSSL=X\r"
sprintf_P(buffer, (const char *)command, enable ? 1 : 0);
dte->clearReceivedBuffer();
if (!dte->ATCommand(buffer)) return false;
if (!dte->ATResponseOk()) return false;
sslEnabled = enable;
return true;
}
struct HttpStatus HTTP::getStatus(void) {
atReadHttpStatus();
return httpStatus;
}
bool HTTP::initialize(unsigned int timeout, unsigned char cid) {
if (ip->getConnectionStatus(cid).status == 1) {
if (initialized) atTerminateHttpService();
if (!initialized) {
if (!atInitializeHttpService()) return false;
char buffer[5];
if (timeout < 30) timeout = 30;
if (!atSetHttpParametersValue(F("TIMEOUT"), itoa(timeout, buffer, 10))) return false;
if (!atSetHttpParametersValue(F("CONTENT"), "APPLICATION/X-WWW-FORM-URLENCODED")) return false;
if (!atSetHttpParametersValue(F("REDIR"), "1")) return false;
}
} else {
if (initialized) atTerminateHttpService();
}
return initialized;
}
bool HTTP::setUserAgent(const char userAgent[]) {
if (!initialized) return false;
atSetHttpParametersValue(F("UA"), userAgent);
return true;
}
bool HTTP::setUserAgent(const __FlashStringHelper *userAgent) {
char buffer[strlen_P((const char *)userAgent) + 1];
strcpy_P(buffer, (const char *)userAgent);
return setUserAgent(buffer);
}
bool HTTP::setHeaders(const char headers[], const char userdataDelimiter[]) {
if (!initialized) return false;
atSetHttpParametersValue(F("USERDATA"), headers, userdataDelimiter);
return true;
}
bool HTTP::setHeaders(const __FlashStringHelper *headers, const char userdataDelimiter[]) {
char buffer[strlen_P((const char *)headers) + 1];
strcpy_P(buffer, (const char *)headers);
return setHeaders(buffer, userdataDelimiter);
}
bool HTTP::setHeaders(const char headers[], const __FlashStringHelper *userdataDelimiter) {
char buffer[strlen_P((const char *)userdataDelimiter) + 1];
strcpy_P(buffer, (const char *)userdataDelimiter);
return setHeaders(headers, buffer);
}
bool HTTP::setHeaders(const __FlashStringHelper *headers, const __FlashStringHelper *userdataDelimiter) {
char buffer[strlen_P((const char *)userdataDelimiter) + 1];
strcpy_P(buffer, (const char *)userdataDelimiter);
return setHeaders(headers, buffer);
}
bool HTTP::action(const char method[], const char url[], const char data[]) {
if (!initialized) return false;
if (!getStatus().status == 0) return false;
if (strstr_P(url, (const char *)F("https://")) != NULL) {
atSslHttp(true);
} else {
atSslHttp(false);
}
if (!atSetHttpParametersValue(F("URL"), url)) return false;
size_t i = 0;
do
{
if ((methodIndex(method) == 1) && strlen(data) > 0) {
if (!atInputHttpData(data)) return false;
}
if (!atHttpMethodAction(methodIndex(method))) return false;
} while (Urc.httpAction.updated && Urc.httpAction.statusCode == 603 && i++ == 0);
return true;
}
bool HTTP::action(const __FlashStringHelper *method, const char url[], const char data[]) {
char buffer[strlen_P((const char *)method) + 1];
strcpy_P(buffer, (const char *)method);
return action(buffer, url, data);
}
bool HTTP::action(const char method[], const __FlashStringHelper *url, const char data[]) {
char buffer[strlen_P((const char *)url) + 1];
strcpy_P(buffer, (const char *)url);
return action(method, buffer, data);
}
bool HTTP::action(const __FlashStringHelper *method, const __FlashStringHelper *url, const char data[]) {
char buffer[strlen_P((const char *)url) + 1];
strcpy_P(buffer, (const char *)url);
return action(method, buffer, data);
}
bool HTTP::action(const char method[], const char url[], const __FlashStringHelper *data) {
char buffer[strlen_P((const char *)data) + 1];
strcpy_P(buffer, (const char *)data);
return action(method, url, buffer);
}
bool HTTP::action(const __FlashStringHelper *method, const char url[], const __FlashStringHelper *data) {
char buffer[strlen_P((const char *)data) + 1];
strcpy_P(buffer, (const char *)data);
return action(method, url, buffer);
}
bool HTTP::action(const char method[], const __FlashStringHelper *url, const __FlashStringHelper *data) {
char buffer[strlen_P((const char *)data) + 1];
strcpy_P(buffer, (const char *)data);
return action(method, url, buffer);
}
bool HTTP::action(const __FlashStringHelper *method, const __FlashStringHelper *url, const __FlashStringHelper *data) {
char buffer[strlen_P((const char *)data) + 1];
strcpy_P(buffer, (const char *)data);
return action(method, url, buffer);
}
bool HTTP::readDataReceived(char buffer[], unsigned long length, unsigned long startAddress) {
if (!initialized) return false;
if (!getStatus().status == 0) return false;
if (Urc.httpAction.statusCode != 0) {
if (!Urc.httpAction.updated) return false;
}
if (!atReadHttpServerResponse(buffer, startAddress, length)) return false;
return true;
}
bool HTTP::terminate(void) {
if (initialized) atTerminateHttpService();
return !initialized;
}