This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RestClient.cpp
139 lines (127 loc) · 3.29 KB
/
RestClient.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
#include "RestClient.h"
RestClient::RestClient(String _host, int _port, String _protocol, String _basePath)
{
host = _host;
port = _port;
basePath = _basePath;
setProtocol(_protocol);
num_headers = 0;
if (contentType == NULL)
{
contentType = "application/x-www-form-urlencoded"; // default
}
}
void RestClient::setHeader(String header)
{
headers[num_headers] = header;
num_headers++;
}
void RestClient::setContentType(String contentTypeValue)
{
contentType = contentTypeValue;
}
void RestClient::setProtocol(String protocol)
{
ssl = protocol == "https";
}
// The mother- generic request method.
//
int RestClient::request(String method, String path, const char *body, String *response)
{
if (ssl)
{
client = new BearSSL::WiFiClientSecure();
static_cast<BearSSL::WiFiClientSecure *>(client)->setInsecure();
}
else
{
client = new WiFiClient();
}
if (!client->connect(host, port))
{
return 0;
}
String request = method + " " + basePath + path + " HTTP/1.1\r\n";
for (int i = 0; i < num_headers; i++)
{
request += String(headers[i]) + "\r\n";
}
request += "Host: " + String(host) + ":" + String(port) + "\r\n";
request += "Connection: close\r\n";
if (body != NULL)
{
char contentLength[30];
sprintf(contentLength, "Content-Length: %d\r\n", strlen(body));
request += String(contentLength);
request += "Content-Type: " + String(contentType) + "\r\n";
}
request += "\r\n";
if (body != NULL)
{
request += String(body);
request += "\r\n\r\n";
}
client->print(request.c_str());
int statusCode = _readResponse(response);
//cleanup
num_headers = 0;
client->stop();
delete client;
client = NULL;
return statusCode;
}
int RestClient::_readResponse(String *response)
{
// an http request ends with a blank line
boolean currentLineIsBlank = true;
boolean httpBody = false;
boolean inStatus = false;
char statusCode[4];
int i = 0;
int code = 0;
while (client->connected())
{
if (client->available())
{
char c = client->read();
if (c == ' ' && !inStatus)
{
inStatus = true;
}
if (inStatus && i < 3 && c != ' ')
{
statusCode[i] = c;
i++;
}
if (i == 3)
{
statusCode[i] = '\0';
code = atoi(statusCode);
}
if (httpBody)
{
//only write response if its not null
if (response != NULL)
response->concat(c);
}
else
{
if (c == '\n' && currentLineIsBlank)
{
httpBody = true;
}
if (c == '\n')
{
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r')
{
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
}
return code;
}