-
Notifications
You must be signed in to change notification settings - Fork 316
/
HttpClient.h
449 lines (409 loc) · 20.5 KB
/
HttpClient.h
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Class to simplify HTTP fetching on Arduino
// (c) Copyright MCQN Ltd. 2010-2012
// Released under Apache License, version 2.0
#ifndef HttpClient_h
#define HttpClient_h
#include <Arduino.h>
#include <IPAddress.h>
#include "Client.h"
static const int HTTP_SUCCESS =0;
// The end of the headers has been reached. This consumes the '\n'
// Could not connect to the server
static const int HTTP_ERROR_CONNECTION_FAILED =-1;
// This call was made when the HttpClient class wasn't expecting it
// to be called. Usually indicates your code is using the class
// incorrectly
static const int HTTP_ERROR_API =-2;
// Spent too long waiting for a reply
static const int HTTP_ERROR_TIMED_OUT =-3;
// The response from the server is invalid, is it definitely an HTTP
// server?
static const int HTTP_ERROR_INVALID_RESPONSE =-4;
// Define some of the common methods and headers here
// That lets other code reuse them without having to declare another copy
// of them, so saves code space and RAM
#define HTTP_METHOD_GET "GET"
#define HTTP_METHOD_POST "POST"
#define HTTP_METHOD_PUT "PUT"
#define HTTP_METHOD_DELETE "DELETE"
#define HTTP_HEADER_CONTENT_LENGTH "Content-Length"
#define HTTP_HEADER_CONNECTION "Connection"
#define HTTP_HEADER_USER_AGENT "User-Agent"
class HttpClient : public Client
{
public:
static const int kNoContentLengthHeader =-1;
static const int kHttpPort =80;
static const char* kUserAgent;
// FIXME Write longer API request, using port and user-agent, example
// FIXME Update tempToPachube example to calculate Content-Length correctly
#ifdef PROXY_ENABLED // currently disabled as introduces dependency on Dns.h in Ethernet
HttpClient(Client& aClient, const char* aProxy =NULL, uint16_t aProxyPort =0);
#else
HttpClient(Client& aClient);
#endif
/** Start a more complex request.
Use this when you need to send additional headers in the request,
but you will also need to call endRequest() when you are finished.
*/
void beginRequest();
/** End a more complex request.
Use this when you need to have sent additional headers in the request,
but you will also need to call beginRequest() at the start.
*/
void endRequest();
/** Connect to the server and start to send a GET request.
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aServerPort Port to connect to on the server
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int get(const char* aServerName, uint16_t aServerPort, const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerName, aServerPort, aURLPath, HTTP_METHOD_GET, aUserAgent); }
/** Connect to the server and start to send a GET request.
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int get(const char* aServerName, const char* aURLPath, const char* aUserAgent =NULL)
{ return startRequest(aServerName, kHttpPort, aURLPath, HTTP_METHOD_GET, aUserAgent); }
/** Connect to the server and start to send a GET request. This version connects
doesn't perform a DNS lookup and just connects to the given IP address.
@param aServerAddress IP address of the server to connect to
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aServerPort Port to connect to on the server
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int get(const IPAddress& aServerAddress,
const char* aServerName,
uint16_t aServerPort,
const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, HTTP_METHOD_GET, aUserAgent); }
/** Connect to the server and start to send a GET request. This version connects
doesn't perform a DNS lookup and just connects to the given IP address.
@param aServerAddress IP address of the server to connect to
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int get(const IPAddress& aServerAddress,
const char* aServerName,
const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, HTTP_METHOD_GET, aUserAgent); }
/** Connect to the server and start to send a POST request.
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aServerPort Port to connect to on the server
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int post(const char* aServerName,
uint16_t aServerPort,
const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerName, aServerPort, aURLPath, HTTP_METHOD_POST, aUserAgent); }
/** Connect to the server and start to send a POST request.
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int post(const char* aServerName,
const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerName, kHttpPort, aURLPath, HTTP_METHOD_POST, aUserAgent); }
/** Connect to the server and start to send a POST request. This version connects
doesn't perform a DNS lookup and just connects to the given IP address.
@param aServerAddress IP address of the server to connect to
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aServerPort Port to connect to on the server
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int post(const IPAddress& aServerAddress,
const char* aServerName,
uint16_t aServerPort,
const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, HTTP_METHOD_POST, aUserAgent); }
/** Connect to the server and start to send a POST request. This version connects
doesn't perform a DNS lookup and just connects to the given IP address.
@param aServerAddress IP address of the server to connect to
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int post(const IPAddress& aServerAddress,
const char* aServerName,
const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, HTTP_METHOD_POST, aUserAgent); }
/** Connect to the server and start to send a PUT request.
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aServerPort Port to connect to on the server
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int put(const char* aServerName,
uint16_t aServerPort,
const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerName, aServerPort, aURLPath, HTTP_METHOD_PUT, aUserAgent); }
/** Connect to the server and start to send a PUT request.
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int put(const char* aServerName,
const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerName, kHttpPort, aURLPath, HTTP_METHOD_PUT, aUserAgent); }
/** Connect to the server and start to send a PUT request. This version connects
doesn't perform a DNS lookup and just connects to the given IP address.
@param aServerAddress IP address of the server to connect to
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aServerPort Port to connect to on the server
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int put(const IPAddress& aServerAddress,
const char* aServerName,
uint16_t aServerPort,
const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, HTTP_METHOD_PUT, aUserAgent); }
/** Connect to the server and start to send a PUT request. This version connects
doesn't perform a DNS lookup and just connects to the given IP address.
@param aServerAddress IP address of the server to connect to
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int put(const IPAddress& aServerAddress,
const char* aServerName,
const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, HTTP_METHOD_PUT, aUserAgent); }
/** Connect to the server and start to send the request.
@param aServerName Name of the server being connected to.
@param aServerPort Port to connect to on the server
@param aURLPath Url to request
@param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc.
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int startRequest(const char* aServerName,
uint16_t aServerPort,
const char* aURLPath,
const char* aHttpMethod,
const char* aUserAgent);
/** Connect to the server and start to send the request.
@param aServerAddress IP address of the server to connect to.
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aServerPort Port to connect to on the server
@param aURLPath Url to request
@param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc.
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int startRequest(const IPAddress& aServerAddress,
const char* aServerName,
uint16_t aServerPort,
const char* aURLPath,
const char* aHttpMethod,
const char* aUserAgent);
/** Send an additional header line. This can only be called in between the
calls to startRequest and finishRequest.
@param aHeader Header line to send, in its entirety (but without the
trailing CRLF. E.g. "Authorization: Basic YQDDCAIGES"
*/
void sendHeader(const char* aHeader);
/** Send an additional header line. This is an alternate form of
sendHeader() which takes the header name and content as separate strings.
The call will add the ": " to separate the header, so for example, to
send a XXXXXX header call sendHeader("XXXXX", "Something")
@param aHeaderName Type of header being sent
@param aHeaderValue Value for that header
*/
void sendHeader(const char* aHeaderName, const char* aHeaderValue);
/** Send an additional header line. This is an alternate form of
sendHeader() which takes the header name and content separately but where
the value is provided as an integer.
The call will add the ": " to separate the header, so for example, to
send a XXXXXX header call sendHeader("XXXXX", 123)
@param aHeaderName Type of header being sent
@param aHeaderValue Value for that header
*/
void sendHeader(const char* aHeaderName, const int aHeaderValue);
/** Send a basic authentication header. This will encode the given username
and password, and send them in suitable header line for doing Basic
Authentication.
@param aUser Username for the authorization
@param aPassword Password for the user aUser
*/
void sendBasicAuth(const char* aUser, const char* aPassword);
/** Finish sending the HTTP request. This basically just sends the blank
line to signify the end of the request
*/
void finishRequest();
/** Get the HTTP status code contained in the response.
For example, 200 for successful request, 404 for file not found, etc.
*/
int responseStatusCode();
/** Read the next character of the response headers.
This functions in the same way as read() but to be used when reading
through the headers. Check whether or not the end of the headers has
been reached by calling endOfHeadersReached(), although after that point
this will still return data as read() would, but slightly less efficiently
@return The next character of the response headers
*/
int readHeader();
/** Skip any response headers to get to the body.
Use this if you don't want to do any special processing of the headers
returned in the response. You can also use it after you've found all of
the headers you're interested in, and just want to get on with processing
the body.
@return HTTP_SUCCESS if successful, else an error code
*/
int skipResponseHeaders();
/** Test whether all of the response headers have been consumed.
@return true if we are now processing the response body, else false
*/
bool endOfHeadersReached() { return (iState == eReadingBody); };
/** Test whether the end of the body has been reached.
Only works if the Content-Length header was returned by the server
@return true if we are now at the end of the body, else false
*/
bool endOfBodyReached();
virtual bool endOfStream() { return endOfBodyReached(); };
virtual bool completed() { return endOfBodyReached(); };
/** Return the length of the body.
@return Length of the body, in bytes, or kNoContentLengthHeader if no
Content-Length header was returned by the server
*/
int contentLength() { return iContentLength; };
// Inherited from Print
// Note: 1st call to these indicates the user is sending the body, so if need
// Note: be we should finish the header first
virtual size_t write(uint8_t aByte) { if (iState < eRequestSent) { finishHeaders(); }; return iClient-> write(aByte); };
virtual size_t write(const uint8_t *aBuffer, size_t aSize) { if (iState < eRequestSent) { finishHeaders(); }; return iClient->write(aBuffer, aSize); };
// Inherited from Stream
virtual int available() { return iClient->available(); };
/** Read the next byte from the server.
@return Byte read or -1 if there are no bytes available.
*/
virtual int read();
virtual int read(uint8_t *buf, size_t size);
virtual int peek() { return iClient->peek(); };
virtual void flush() { return iClient->flush(); };
// Inherited from Client
virtual int connect(IPAddress ip, uint16_t port) { return iClient->connect(ip, port); };
virtual int connect(const char *host, uint16_t port) { return iClient->connect(host, port); };
virtual void stop();
virtual uint8_t connected() { return iClient->connected(); };
virtual operator bool() { return bool(iClient); };
virtual uint32_t httpResponseTimeout() { return iHttpResponseTimeout; };
virtual void setHttpResponseTimeout(uint32_t timeout) { iHttpResponseTimeout = timeout; };
protected:
/** Reset internal state data back to the "just initialised" state
*/
void resetState();
/** Send the first part of the request and the initial headers.
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aServerIP IP address of the server (only used if we're going through a
proxy and aServerName is NULL
@param aServerPort Port of the server being connected to.
@param aURLPath Url to request
@param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc.
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int sendInitialHeaders(const char* aServerName,
IPAddress aServerIP,
uint16_t aPort,
const char* aURLPath,
const char* aHttpMethod,
const char* aUserAgent);
/* Let the server know that we've reached the end of the headers
*/
void finishHeaders();
// Number of milliseconds that we wait each time there isn't any data
// available to be read (during status code and header processing)
static const int kHttpWaitForDataDelay = 1000;
// Number of milliseconds that we'll wait in total without receiveing any
// data before returning HTTP_ERROR_TIMED_OUT (during status code and header
// processing)
static const int kHttpResponseTimeout = 30*1000;
static const char* kContentLengthPrefix;
typedef enum {
eIdle,
eRequestStarted,
eRequestSent,
eReadingStatusCode,
eStatusCodeRead,
eReadingContentLength,
eSkipToEndOfHeader,
eLineStartingCRFound,
eReadingBody
} tHttpState;
// Ethernet client we're using
Client* iClient;
// Current state of the finite-state-machine
tHttpState iState;
// Stores the status code for the response, once known
int iStatusCode;
// Stores the value of the Content-Length header, if present
int iContentLength;
// How many bytes of the response body have been read by the user
int iBodyLengthConsumed;
// How far through a Content-Length header prefix we are
const char* iContentLengthPtr;
// Address of the proxy to use, if we're using one
IPAddress iProxyAddress;
uint16_t iProxyPort;
uint32_t iHttpResponseTimeout;
};
#endif