-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathurl_fetcher.h
162 lines (124 loc) · 2.66 KB
/
url_fetcher.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
#ifndef URL_FETCHER_H
#define URL_FETCHER_H
/*
* This is a simple class which can be used to make HTTP or HTTPS fetches.
*
* Usage is as simple as:
*
* UrlFetcher foo( "http://steve.fi/robots.txt" );
*
* String body = foo.body();
* String headers = foo.headers();
*
* If you wish you can set a custom user-agent, via:
*
* foo.setAgent( "moi.kissa/3.14" );
*
*/
class UrlFetcher
{
public:
/*
* Constructor.
*/
UrlFetcher(const char *url);
/*
* Destructor
*/
~UrlFetcher();
/*
* Return the headers of the remote URL.
*
* We only fetch it once regardless of how many times it is invoked.
*/
String headers();
/*
* Return the body-contents of the remote URL.
*
* We only fetch it once regardless of how many times it is invoked.
*/
String body();
/*
* Return the HTTP status-code of our fetch.
*/
int code();
/*
* Return the complete status-line of the remote server
*/
char *status();
/*
* Get the user-agent, if one hasn't been set it will be created
* based upon the MAC-address of the board.
*/
const char *getAgent();
/*
* Set the user-agent to the specified string.
*/
void setAgent(const char *userAgent);
private:
/*
* Get the host-part of the URL.
*/
char *getHost();
/*
* Get the path of the URL.
*/
char *getPath();
/*
* Is the URL SSL-based?
*/
bool is_secure();
/*
* Return the port we'll connect to, 80 for HTTP, 443 for HTTPS.
*/
int port();
/*
* Parse URL into "host" + "path"
*/
void parse();
/*
* Perform the fetch of the remote URL, recording
* the response-headers and the body.
*/
void fetch();
/*
* A copy of the URL we were constructed with.
*/
char *m_url;
/*
* The hostname extracted from the URL.
*/
char m_host[128] = { '\0' };
/*
* The path extracted from the URL.
*/
char m_path[128] = { '\0' };
/*
* The user-agent to use for fetches.
*/
char *m_user_agent = NULL;
/*
* The client we use for fetching.
*
* NOTE: This might be the derived class `WiFiClientSecure`
*
*/
WiFiClient *m_client;
/*
* Have we fetched the URL already?
*/
bool m_fetched = false;
/*
* The headers returned from the remote HTTP-fetch.
*/
String m_headers;
/*
* The status-line.
*/
char *m_status = NULL;
/*
* The body returned from the remote HTTP-fetch.
*/
String m_body;
};
#endif /* URL_FETCHER_H */