Repository navigation
Expand file tree
/
Copy pathHTTPClient.cpp
More file actions
332 lines (312 loc) · 8.76 KB
/
Copy pathHTTPClient.cpp
File metadata and controls
332 lines (312 loc) · 8.76 KB
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
/*
* HTTPClient
* HTTPClient.c
*
* http://interactive-matter.org/
*
* This file is part of HTTPClient.
*
* HTTPClient is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HTTPClient is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with HTTPClient. If not, see <http://www.gnu.org/licenses/>.
*
* Created on: 18.09.2010
* Author: marcus
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <avr/pgmspace.h>
#include <HardwareSerial.h>
#include "HTTPClient.h"
//some HTTP helpers
char
sendUriAndHeaders(FILE* stream, char* hostName, char* uri, uri_parameter parameters[], char* headers);
char
sendContentPayload(FILE* stream, char* data);
char
skipHeader(FILE* stream);
//a struct to store the uriEncoder & the handle to the http client
typedef struct
{
HTTPClient* client;
uint8_t encode;
#define URI_ENCODE _BV(0)
#define URI_ENCODE_RESERVED _BV(1)
} http_stream_udata;
//we need this probably more than once o it is defined here
#define URI_ALLOWED(byte) ((byte>='A' && byte<='Z') || (byte>='a' && byte<='z') || (byte>='0' && byte<='9') || byte == '-' || byte == '_' || byte == '.' || byte == '~')
#define URI_RESERVED(byte) (byte == '!' || byte == '*' || byte == '\'' || byte == '(' || byte == ')' || byte == ';' || byte == ':' || byte == '&' || byte == '=' || byte == '+' || byte == '$' || byte == ',' || byte == '/' || byte == '?' || byte == '#' || byte == '[' || byte == ']')
HTTPClient::HTTPClient(char*host, uint8_t* ip, uint16_t port) :
Client(ip, port)
{
this->hostName = host;
}
FILE*
HTTPClient::getURI(char* uri)
{
return getURI(uri, NULL, NULL);
}
FILE*
HTTPClient::getURI(char* uri, uri_parameter parameters[])
{
return getURI(uri, parameters, NULL);
}
FILE*
HTTPClient::getURI(char* uri, uri_parameter parameters[], char* headers)
{
FILE* result = openClientFile();
//the request and the default headers
fprintf_P(result, PSTR("GET "));
sendUriAndHeaders(result, this->hostName, uri, parameters, headers);
//ok header finished
fprintf_P(result, PSTR("\n"));
skipHeader(result);
return result;
}
FILE*
HTTPClient::postURI(char* uri, char* data)
{
return postURI(uri, NULL, data, NULL);
}
FILE*
HTTPClient::postURI(char* uri, uri_parameter parameters[], char* data)
{
return postURI(uri, parameters, data, NULL);
}
FILE*
HTTPClient::postURI(char* uri, uri_parameter parameters[], char* data, char* headers)
{
FILE* result = openClientFile();
fprintf_P(result, PSTR("POST "));
sendUriAndHeaders(result, this->hostName, uri, parameters, headers);
sendContentPayload(result, data);
skipHeader(result);
return result;
}
FILE*
HTTPClient::putURI(char* uri, char* data)
{
return postURI(uri, NULL, data, NULL);
}
FILE*
HTTPClient::putURI(char* uri, uri_parameter parameters[], char* data)
{
return postURI(uri, parameters, data, NULL);
}
FILE*
HTTPClient::putURI(char* uri, uri_parameter parameters[], char* data, char* headers)
{
FILE* result = openClientFile();
fprintf_P(result, PSTR("PUT "));
sendUriAndHeaders(result, this->hostName, uri, parameters, headers);
sendContentPayload(result, data);
skipHeader(result);
return result;
}
void HTTPClient::setEncoding(FILE* stream, char encode, char encodeReserved) {
http_stream_udata* udata = (http_stream_udata*) fdev_get_udata(stream);
if (encode==0) {
udata->encode=0;
} else {
if (encodeReserved) {
udata->encode = URI_ENCODE | URI_ENCODE_RESERVED;
} else {
udata->encode = URI_ENCODE;
}
}
}
FILE*
HTTPClient::openClientFile()
{
FILE* result = fdevopen(clientWrite, clientRead);
if (result == NULL)
{
return NULL;
}
http_stream_udata* udata = (http_stream_udata*) malloc(
sizeof(http_stream_udata));
fdev_set_udata(result,udata);
udata->client = this;
udata->encode = 0;
connect();
return result;
}
char
sendUriAndHeaders(FILE* stream, char* hostName, char* uri, uri_parameter parameters[], char* headers)
{
//encode but use reserved characters
HTTPClient::setEncoding(stream, 1, 0);
fprintf_P(stream, PSTR("%s"), uri);
if (parameters!=NULL) {
fprintf_P(stream, PSTR("?"));
char parameter_number = 0;
uri_parameter* parameter = ¶meters[0];
while (parameter->name!=NULL) {
if (parameter_number>0) {
fprintf_P(stream, PSTR("&"));
}
HTTPClient::setEncoding(stream, 1, 1);
fprintf_P(stream, PSTR("%s"), parameter->name);
HTTPClient::setEncoding(stream, 1, 0);
fprintf_P(stream, PSTR("="));
HTTPClient::setEncoding(stream, 1, 1);
if (parameter->value!=NULL) {
fprintf_P(stream, PSTR("%s"), parameter->value);
}
HTTPClient::setEncoding(stream, 1, 0);
parameter_number++;
parameter = ¶meters[parameter_number];
}
}
HTTPClient::setEncoding(stream, 0, 0);
fprintf_P(stream, PSTR(" HTTP/1.1\nHost: %s\nAccept: */*\n"), hostName);
//is there an additional header?
if (headers != NULL)
{
fprintf(stream, PSTR("%s"), headers);
}
return 0;
}
char
sendContentPayload(FILE* stream, char* data)
{
//calculate the content length
int content_length = 0;
Serial.print("Payload: ");
Serial.println(data);
if (data != NULL)
{
content_length = strlen(data);
}
fprintf_P(stream, PSTR("Content-Length: %i"), content_length);
//ok finished header
fprintf_P(stream, PSTR("\n"));
//now sending data
if (content_length > 0)
{
fprintf_P(stream, PSTR("%s\n"), data);
}
fprintf_P(stream, PSTR("\n"));
return 0;
}
int
HTTPClient::clientWrite(char byte, FILE* stream)
{
http_stream_udata* udata = (http_stream_udata*) fdev_get_udata(stream);
HTTPClient* client = udata->client;
if (udata->encode==0) {
client->write(byte);
Serial.print(byte);
} else {
if (URI_ALLOWED(byte) || ((URI_RESERVED(byte) && (udata->encode & URI_ENCODE_RESERVED)==0)))
{
client->write(byte);
Serial.print(byte);
}
else
{
char encoded[4] =
{ 0, 0, 0 };
sprintf(encoded, "%%%2x", byte);
for (char i = 0; i < 4; i++)
{
client->write(encoded[i]);
Serial.print(encoded[i]);
}
}
}
return 0;
}
int
HTTPClient::clientRead(FILE* stream)
{
http_stream_udata* udata = (http_stream_udata*) fdev_get_udata(stream);
HTTPClient* client = udata->client;
//block until we got a byte
while (client->available() == 0)
{
if (client->connected()==0) {
return EOF;
}
};
int result = client->read();
if (result == EOF)
{
return EOF;
}
//as long as we do not read encoded or it is no % everything is ok
else if (udata->encode==0 || result != '%') {
return result;
} else {
//block until we got the needed bytes
while (client->available() >= 2)
{
if (client->connected()==0) {
return EOF;
}
};
char return_value = 0;
for (char i = 0; i < 2; i++)
{
result = client->read();
if (result == EOF)
{
return EOF;
}
else if (result >= 'A' && result <= 'Z')
{
return_value += (1 - i) * 16 * (result - 'A');
}
else if (result >= 'a' && result <= 'z')
{
return_value += (1 - i) * 16 * (result - 'a');
}
else if (result >= '0' && result <= '9')
{
return_value += (1 - i) * 16 * (result - '0');
}
}
return return_value;
}
}
char
skipHeader(FILE* stream)
{
//skip over the header
static int inByte = 0;
static int lastByte = 0;
while (!(inByte == '\n' && lastByte == '\n'))
{
//by that we ignore \r
if (inByte != '\r')
{
lastByte = inByte;
}
inByte = fgetc(stream);
Serial.print((char) inByte);
if (inByte == EOF)
{
//hmm, an error occured - lets just end this
HTTPClient::closeStream(stream);
return NULL;
}
}
return 0;
}
void
HTTPClient::closeStream(FILE* stream)
{
http_stream_udata* udata = (http_stream_udata*) fdev_get_udata(stream);
udata->client->stop();
free(udata);
fclose(stream);
}