@@ -23,16 +23,23 @@ static const int HTTP_ERROR_TIMED_OUT =-3;
2323// server?
2424static 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+
2637class HttpClient : public Client
2738{
2839public:
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