-
Notifications
You must be signed in to change notification settings - Fork 1
/
A6Http.cpp
76 lines (70 loc) · 1.62 KB
/
A6Http.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
#include <Arduino.h>
#include "A6Services.h"
#include "A6http.h"
A6HTTP::A6HTTP(A6GPRS& a6gprs)
{
_a6gprs = &a6gprs;
}
bool A6HTTP::get(char server[], char path[])
{
// avoid sprintf if possible, send as an array of strings
char *strings[5]; // send in 5 parts
strings[0] = "GET ";
strings[1] = path;
strings[2] = " HTTP/1.1\r\nUser-Agent Arduino\r\nAccept: text/plain\r\nHost: ";
strings[3] = server;
strings[4] = "\r\n\r\n";
return _a6gprs->sendToServer(strings,5);
}
bool A6HTTP::get(char server[])
{
return get(server,"/");
}
bool A6HTTP::post(char server[],char path[],char parms[])
{
char *strings[8]; // send in 5 parts
char buff[8];
sprintf(buff,"%u",strlen(parms));
strings[0] = "POST ";
strings[1] = path;
strings[2] = " HTTP/1.1\r\nUser-Agent Arduino\r\nAccept: text/plain\r\nHost: ";
strings[3] = server;
strings[4] = "\r\nContent-Length: ";
strings[5] = buff;
strings[6] = "\r\n\r\n";
strings[7] = parms;
return _a6gprs->sendToServer(strings,8);
}
void A6HTTP::Parse(byte *rawData,unsigned length)
{
if (length == 1)
return; // single trailing LF
// rawData[length] = 0;
if (strncmp((char *)rawData,"+CGREG",6) == 0)
{
}
else
OnDataReceived(rawData,length);
}
unsigned A6HTTP::urlencode(char src[], char tgt[])
{
unsigned offset = 0;
// const char rfc3986[] = "/?#[]@";
const char rfc3986[] = " !*'();:@&=+$,/?#[]";
// const char rfc3986[] = " !*'();:@+$,/?#[]"; // without & =
while (*src)
{
if (strchr(rfc3986,*src))
{
sprintf(&tgt[offset],"%%%02X",*src);
offset += 3;
}
else
{
tgt[offset++] = *src;
}
src++;
}
tgt[offset] = 0;
return offset;
}