Skip to content

Commit 53cc49f

Browse files
author
Adrian McEwen
committed
Reworked to trim down the code size of sketches using HttpClient
1 parent fa50bfd commit 53cc49f

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

HttpClient.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,10 @@
77
#ifdef PROXY_ENABLED // currently disabled as introduces dependency on Dns.h in Ethernet
88
#include <Dns.h>
99
#endif
10-
#include <string.h>
11-
#include <ctype.h>
1210

1311
// Initialize constants
14-
const char* HttpClient::kUserAgent = "Arduino/2.0";
15-
const char* HttpClient::kGet = "GET";
16-
const char* HttpClient::kPost = "POST";
17-
const char* HttpClient::kPut = "PUT";
18-
const char* HttpClient::kDelete = "DELETE";
19-
const char* HttpClient::kContentLengthPrefix = "Content-Length: ";
12+
const char* HttpClient::kUserAgent = "Arduino/2.1";
13+
const char* HttpClient::kContentLengthPrefix = HTTP_HEADER_CONTENT_LENGTH ": ";
2014

2115
#ifdef PROXY_ENABLED // currently disabled as introduces dependency on Dns.h in Ethernet
2216
HttpClient::HttpClient(Client& aClient, const char* aProxy, uint16_t aProxyPort)
@@ -70,6 +64,7 @@ int HttpClient::startRequest(const char* aServerName, uint16_t aServerPort, cons
7064
return HTTP_ERROR_API;
7165
}
7266

67+
#ifdef PROXY_ENABLED
7368
if (iProxyPort)
7469
{
7570
if (!iClient->connect(iProxyAddress, iProxyPort) > 0)
@@ -81,6 +76,7 @@ int HttpClient::startRequest(const char* aServerName, uint16_t aServerPort, cons
8176
}
8277
}
8378
else
79+
#endif
8480
{
8581
if (!iClient->connect(aServerName, aServerPort) > 0)
8682
{
@@ -111,6 +107,7 @@ int HttpClient::startRequest(const IPAddress& aServerAddress, const char* aServe
111107
return HTTP_ERROR_API;
112108
}
113109

110+
#ifdef PROXY_ENABLED
114111
if (iProxyPort)
115112
{
116113
if (!iClient->connect(iProxyAddress, iProxyPort) > 0)
@@ -122,6 +119,7 @@ int HttpClient::startRequest(const IPAddress& aServerAddress, const char* aServe
122119
}
123120
}
124121
else
122+
#endif
125123
{
126124
if (!iClient->connect(aServerAddress, aServerPort) > 0)
127125
{
@@ -152,6 +150,7 @@ int HttpClient::sendInitialHeaders(const char* aServerName, IPAddress aServerIP,
152150
// Send the HTTP command, i.e. "GET /somepath/ HTTP/1.0"
153151
iClient->print(aHttpMethod);
154152
iClient->print(" ");
153+
#ifdef PROXY_ENABLED
155154
if (iProxyPort)
156155
{
157156
// We're going through a proxy, send a full URL
@@ -172,6 +171,7 @@ int HttpClient::sendInitialHeaders(const char* aServerName, IPAddress aServerIP,
172171
iClient->print(aPort);
173172
}
174173
}
174+
#endif
175175
iClient->print(aURLPath);
176176
iClient->println(" HTTP/1.1");
177177
// The host header, if required
@@ -187,14 +187,13 @@ int HttpClient::sendInitialHeaders(const char* aServerName, IPAddress aServerIP,
187187
iClient->println();
188188
}
189189
// And user-agent string
190-
iClient->print("User-Agent: ");
191190
if (aUserAgent)
192191
{
193-
iClient->println(aUserAgent);
192+
sendHeader(HTTP_HEADER_USER_AGENT, aUserAgent);
194193
}
195194
else
196195
{
197-
iClient->println(kUserAgent);
196+
sendHeader(HTTP_HEADER_USER_AGENT, kUserAgent);
198197
}
199198

200199
// Everything has gone well

HttpClient.h

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,23 @@ static const int HTTP_ERROR_TIMED_OUT =-3;
2323
// server?
2424
static const int HTTP_ERROR_INVALID_RESPONSE =-4;
2525

26+
// Define some of the common methods and headers here
27+
// That lets other code reuse them without having to declare another copy
28+
// of them, so saves code space and RAM
29+
#define HTTP_METHOD_GET "GET"
30+
#define HTTP_METHOD_POST "POST"
31+
#define HTTP_METHOD_PUT "PUT"
32+
#define HTTP_METHOD_DELETE "DELETE"
33+
#define HTTP_HEADER_CONTENT_LENGTH "Content-Length"
34+
#define HTTP_HEADER_CONNECTION "Connection"
35+
#define HTTP_HEADER_USER_AGENT "User-Agent"
36+
2637
class HttpClient : public Client
2738
{
2839
public:
2940
static const int kNoContentLengthHeader =-1;
3041
static const int kHttpPort =80;
3142
static const char* kUserAgent;
32-
static const char* kGet;
33-
static const char* kPost;
34-
static const char* kPut;
35-
static const char* kDelete;
3643

3744
// FIXME Write longer API request, using port and user-agent, example
3845
// FIXME Update tempToPachube example to calculate Content-Length correctly
@@ -66,7 +73,7 @@ class HttpClient : public Client
6673
*/
6774
int get(const char* aServerName, uint16_t aServerPort, const char* aURLPath,
6875
const char* aUserAgent =NULL)
69-
{ return startRequest(aServerName, aServerPort, aURLPath, kGet, aUserAgent); }
76+
{ return startRequest(aServerName, aServerPort, aURLPath, HTTP_METHOD_GET, aUserAgent); }
7077

7178
/** Connect to the server and start to send a GET request.
7279
@param aServerName Name of the server being connected to. If NULL, the
@@ -77,7 +84,7 @@ class HttpClient : public Client
7784
@return 0 if successful, else error
7885
*/
7986
int get(const char* aServerName, const char* aURLPath, const char* aUserAgent =NULL)
80-
{ return startRequest(aServerName, kHttpPort, aURLPath, kGet, aUserAgent); }
87+
{ return startRequest(aServerName, kHttpPort, aURLPath, HTTP_METHOD_GET, aUserAgent); }
8188

8289
/** Connect to the server and start to send a GET request. This version connects
8390
doesn't perform a DNS lookup and just connects to the given IP address.
@@ -95,7 +102,7 @@ class HttpClient : public Client
95102
uint16_t aServerPort,
96103
const char* aURLPath,
97104
const char* aUserAgent =NULL)
98-
{ return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, kGet, aUserAgent); }
105+
{ return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, HTTP_METHOD_GET, aUserAgent); }
99106

100107
/** Connect to the server and start to send a GET request. This version connects
101108
doesn't perform a DNS lookup and just connects to the given IP address.
@@ -111,7 +118,7 @@ class HttpClient : public Client
111118
const char* aServerName,
112119
const char* aURLPath,
113120
const char* aUserAgent =NULL)
114-
{ return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, kGet, aUserAgent); }
121+
{ return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, HTTP_METHOD_GET, aUserAgent); }
115122

116123
/** Connect to the server and start to send a POST request.
117124
@param aServerName Name of the server being connected to. If NULL, the
@@ -126,7 +133,7 @@ class HttpClient : public Client
126133
uint16_t aServerPort,
127134
const char* aURLPath,
128135
const char* aUserAgent =NULL)
129-
{ return startRequest(aServerName, aServerPort, aURLPath, kPost, aUserAgent); }
136+
{ return startRequest(aServerName, aServerPort, aURLPath, HTTP_METHOD_POST, aUserAgent); }
130137

131138
/** Connect to the server and start to send a POST request.
132139
@param aServerName Name of the server being connected to. If NULL, the
@@ -139,7 +146,7 @@ class HttpClient : public Client
139146
int post(const char* aServerName,
140147
const char* aURLPath,
141148
const char* aUserAgent =NULL)
142-
{ return startRequest(aServerName, kHttpPort, aURLPath, kPost, aUserAgent); }
149+
{ return startRequest(aServerName, kHttpPort, aURLPath, HTTP_METHOD_POST, aUserAgent); }
143150

144151
/** Connect to the server and start to send a POST request. This version connects
145152
doesn't perform a DNS lookup and just connects to the given IP address.
@@ -157,7 +164,7 @@ class HttpClient : public Client
157164
uint16_t aServerPort,
158165
const char* aURLPath,
159166
const char* aUserAgent =NULL)
160-
{ return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, kPost, aUserAgent); }
167+
{ return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, HTTP_METHOD_POST, aUserAgent); }
161168

162169
/** Connect to the server and start to send a POST request. This version connects
163170
doesn't perform a DNS lookup and just connects to the given IP address.
@@ -173,7 +180,7 @@ class HttpClient : public Client
173180
const char* aServerName,
174181
const char* aURLPath,
175182
const char* aUserAgent =NULL)
176-
{ return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, kPost, aUserAgent); }
183+
{ return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, HTTP_METHOD_POST, aUserAgent); }
177184

178185
/** Connect to the server and start to send a PUT request.
179186
@param aServerName Name of the server being connected to. If NULL, the
@@ -188,7 +195,7 @@ class HttpClient : public Client
188195
uint16_t aServerPort,
189196
const char* aURLPath,
190197
const char* aUserAgent =NULL)
191-
{ return startRequest(aServerName, aServerPort, aURLPath, kPut, aUserAgent); }
198+
{ return startRequest(aServerName, aServerPort, aURLPath, HTTP_METHOD_PUT, aUserAgent); }
192199

193200
/** Connect to the server and start to send a PUT request.
194201
@param aServerName Name of the server being connected to. If NULL, the
@@ -201,7 +208,7 @@ class HttpClient : public Client
201208
int put(const char* aServerName,
202209
const char* aURLPath,
203210
const char* aUserAgent =NULL)
204-
{ return startRequest(aServerName, kHttpPort, aURLPath, kPut, aUserAgent); }
211+
{ return startRequest(aServerName, kHttpPort, aURLPath, HTTP_METHOD_PUT, aUserAgent); }
205212

206213
/** Connect to the server and start to send a PUT request. This version connects
207214
doesn't perform a DNS lookup and just connects to the given IP address.
@@ -219,7 +226,7 @@ class HttpClient : public Client
219226
uint16_t aServerPort,
220227
const char* aURLPath,
221228
const char* aUserAgent =NULL)
222-
{ return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, kPut, aUserAgent); }
229+
{ return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, HTTP_METHOD_PUT, aUserAgent); }
223230

224231
/** Connect to the server and start to send a PUT request. This version connects
225232
doesn't perform a DNS lookup and just connects to the given IP address.
@@ -235,7 +242,7 @@ class HttpClient : public Client
235242
const char* aServerName,
236243
const char* aURLPath,
237244
const char* aUserAgent =NULL)
238-
{ return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, kPut, aUserAgent); }
245+
{ return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, HTTP_METHOD_PUT, aUserAgent); }
239246

240247
/** Connect to the server and start to send the request.
241248
@param aServerName Name of the server being connected to.

0 commit comments

Comments
 (0)