| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent fa50bfd commit 53cc49f
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,16 +7,10 @@ | |||
| 7 | 7 | #ifdef PROXY_ENABLED // currently disabled as introduces dependency on Dns.h in Ethernet | |
| 8 | 8 | #include <Dns.h> | |
| 9 | 9 | #endif | |
| 10 | - #include <string.h> | ||
| 11 | - #include <ctype.h> | ||
| 12 | 10 | ||
| 13 | 11 | // 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 ": "; | ||
| 20 | 14 | ||
| 21 | 15 | #ifdef PROXY_ENABLED // currently disabled as introduces dependency on Dns.h in Ethernet | |
| 22 | 16 | HttpClient::HttpClient(Client& aClient, const char* aProxy, uint16_t aProxyPort) | |
@@ -70,6 +64,7 @@ int HttpClient::startRequest(const char* aServerName, uint16_t aServerPort, cons | |||
| 70 | 64 | return HTTP_ERROR_API; | |
| 71 | 65 | } | |
| 72 | 66 | ||
| 67 | + #ifdef PROXY_ENABLED | ||
| 73 | 68 | if (iProxyPort) | |
| 74 | 69 | { | |
| 75 | 70 | if (!iClient->connect(iProxyAddress, iProxyPort) > 0) | |
@@ -81,6 +76,7 @@ int HttpClient::startRequest(const char* aServerName, uint16_t aServerPort, cons | |||
| 81 | 76 | } | |
| 82 | 77 | } | |
| 83 | 78 | else | |
| 79 | + #endif | ||
| 84 | 80 | { | |
| 85 | 81 | if (!iClient->connect(aServerName, aServerPort) > 0) | |
| 86 | 82 | { | |
@@ -111,6 +107,7 @@ int HttpClient::startRequest(const IPAddress& aServerAddress, const char* aServe | |||
| 111 | 107 | return HTTP_ERROR_API; | |
| 112 | 108 | } | |
| 113 | 109 | ||
| 110 | + #ifdef PROXY_ENABLED | ||
| 114 | 111 | if (iProxyPort) | |
| 115 | 112 | { | |
| 116 | 113 | if (!iClient->connect(iProxyAddress, iProxyPort) > 0) | |
@@ -122,6 +119,7 @@ int HttpClient::startRequest(const IPAddress& aServerAddress, const char* aServe | |||
| 122 | 119 | } | |
| 123 | 120 | } | |
| 124 | 121 | else | |
| 122 | + #endif | ||
| 125 | 123 | { | |
| 126 | 124 | if (!iClient->connect(aServerAddress, aServerPort) > 0) | |
| 127 | 125 | { | |
@@ -152,6 +150,7 @@ int HttpClient::sendInitialHeaders(const char* aServerName, IPAddress aServerIP, | |||
| 152 | 150 | // Send the HTTP command, i.e. "GET /somepath/ HTTP/1.0" | |
| 153 | 151 | iClient->print(aHttpMethod); | |
| 154 | 152 | iClient->print(" "); | |
| 153 | + #ifdef PROXY_ENABLED | ||
| 155 | 154 | if (iProxyPort) | |
| 156 | 155 | { | |
| 157 | 156 | // We're going through a proxy, send a full URL | |
@@ -172,6 +171,7 @@ int HttpClient::sendInitialHeaders(const char* aServerName, IPAddress aServerIP, | |||
| 172 | 171 | iClient->print(aPort); | |
| 173 | 172 | } | |
| 174 | 173 | } | |
| 174 | + #endif | ||
| 175 | 175 | iClient->print(aURLPath); | |
| 176 | 176 | iClient->println(" HTTP/1.1"); | |
| 177 | 177 | // The host header, if required | |
@@ -187,14 +187,13 @@ int HttpClient::sendInitialHeaders(const char* aServerName, IPAddress aServerIP, | |||
| 187 | 187 | iClient->println(); | |
| 188 | 188 | } | |
| 189 | 189 | // And user-agent string | |
| 190 | - iClient->print("User-Agent: "); | ||
| 191 | 190 | if (aUserAgent) | |
| 192 | 191 | { | |
| 193 | - iClient->println(aUserAgent); | ||
| 192 | + sendHeader(HTTP_HEADER_USER_AGENT, aUserAgent); | ||
| 194 | 193 | } | |
| 195 | 194 | else | |
| 196 | 195 | { | |
| 197 | - iClient->println(kUserAgent); | ||
| 196 | + sendHeader(HTTP_HEADER_USER_AGENT, kUserAgent); | ||
| 198 | 197 | } | |
| 199 | 198 | ||
| 200 | 199 | // Everything has gone well | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,16 +23,23 @@ static const int HTTP_ERROR_TIMED_OUT =-3; | |||
| 23 | 23 | // server? | |
| 24 | 24 | static const int HTTP_ERROR_INVALID_RESPONSE =-4; | |
| 25 | 25 | ||
| 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 | + | ||
| 26 | 37 | class HttpClient : public Client | |
| 27 | 38 | { | |
| 28 | 39 | public: | |
| 29 | 40 | static const int kNoContentLengthHeader =-1; | |
| 30 | 41 | static const int kHttpPort =80; | |
| 31 | 42 | static const char* kUserAgent; | |
| 32 | - static const char* kGet; | ||
| 33 | - static const char* kPost; | ||
| 34 | - static const char* kPut; | ||
| 35 | - static const char* kDelete; | ||
| 36 | 43 | ||
| 37 | 44 | // FIXME Write longer API request, using port and user-agent, example | |
| 38 | 45 | // FIXME Update tempToPachube example to calculate Content-Length correctly | |
@@ -66,7 +73,7 @@ class HttpClient : public Client | |||
| 66 | 73 | */ | |
| 67 | 74 | int get(const char* aServerName, uint16_t aServerPort, const char* aURLPath, | |
| 68 | 75 | const char* aUserAgent =NULL) | |
| 69 | - { return startRequest(aServerName, aServerPort, aURLPath, kGet, aUserAgent); } | ||
| 76 | + { return startRequest(aServerName, aServerPort, aURLPath, HTTP_METHOD_GET, aUserAgent); } | ||
| 70 | 77 | ||
| 71 | 78 | /** Connect to the server and start to send a GET request. | |
| 72 | 79 | @param aServerName Name of the server being connected to. If NULL, the | |
@@ -77,7 +84,7 @@ class HttpClient : public Client | |||
| 77 | 84 | @return 0 if successful, else error | |
| 78 | 85 | */ | |
| 79 | 86 | 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); } | ||
| 81 | 88 | ||
| 82 | 89 | /** Connect to the server and start to send a GET request. This version connects | |
| 83 | 90 | doesn't perform a DNS lookup and just connects to the given IP address. | |
@@ -95,7 +102,7 @@ class HttpClient : public Client | |||
| 95 | 102 | uint16_t aServerPort, | |
| 96 | 103 | const char* aURLPath, | |
| 97 | 104 | const char* aUserAgent =NULL) | |
| 98 | - { return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, kGet, aUserAgent); } | ||
| 105 | + { return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, HTTP_METHOD_GET, aUserAgent); } | ||
| 99 | 106 | ||
| 100 | 107 | /** Connect to the server and start to send a GET request. This version connects | |
| 101 | 108 | doesn't perform a DNS lookup and just connects to the given IP address. | |
@@ -111,7 +118,7 @@ class HttpClient : public Client | |||
| 111 | 118 | const char* aServerName, | |
| 112 | 119 | const char* aURLPath, | |
| 113 | 120 | const char* aUserAgent =NULL) | |
| 114 | - { return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, kGet, aUserAgent); } | ||
| 121 | + { return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, HTTP_METHOD_GET, aUserAgent); } | ||
| 115 | 122 | ||
| 116 | 123 | /** Connect to the server and start to send a POST request. | |
| 117 | 124 | @param aServerName Name of the server being connected to. If NULL, the | |
@@ -126,7 +133,7 @@ class HttpClient : public Client | |||
| 126 | 133 | uint16_t aServerPort, | |
| 127 | 134 | const char* aURLPath, | |
| 128 | 135 | const char* aUserAgent =NULL) | |
| 129 | - { return startRequest(aServerName, aServerPort, aURLPath, kPost, aUserAgent); } | ||
| 136 | + { return startRequest(aServerName, aServerPort, aURLPath, HTTP_METHOD_POST, aUserAgent); } | ||
| 130 | 137 | ||
| 131 | 138 | /** Connect to the server and start to send a POST request. | |
| 132 | 139 | @param aServerName Name of the server being connected to. If NULL, the | |
@@ -139,7 +146,7 @@ class HttpClient : public Client | |||
| 139 | 146 | int post(const char* aServerName, | |
| 140 | 147 | const char* aURLPath, | |
| 141 | 148 | const char* aUserAgent =NULL) | |
| 142 | - { return startRequest(aServerName, kHttpPort, aURLPath, kPost, aUserAgent); } | ||
| 149 | + { return startRequest(aServerName, kHttpPort, aURLPath, HTTP_METHOD_POST, aUserAgent); } | ||
| 143 | 150 | ||
| 144 | 151 | /** Connect to the server and start to send a POST request. This version connects | |
| 145 | 152 | doesn't perform a DNS lookup and just connects to the given IP address. | |
@@ -157,7 +164,7 @@ class HttpClient : public Client | |||
| 157 | 164 | uint16_t aServerPort, | |
| 158 | 165 | const char* aURLPath, | |
| 159 | 166 | const char* aUserAgent =NULL) | |
| 160 | - { return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, kPost, aUserAgent); } | ||
| 167 | + { return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, HTTP_METHOD_POST, aUserAgent); } | ||
| 161 | 168 | ||
| 162 | 169 | /** Connect to the server and start to send a POST request. This version connects | |
| 163 | 170 | doesn't perform a DNS lookup and just connects to the given IP address. | |
@@ -173,7 +180,7 @@ class HttpClient : public Client | |||
| 173 | 180 | const char* aServerName, | |
| 174 | 181 | const char* aURLPath, | |
| 175 | 182 | const char* aUserAgent =NULL) | |
| 176 | - { return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, kPost, aUserAgent); } | ||
| 183 | + { return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, HTTP_METHOD_POST, aUserAgent); } | ||
| 177 | 184 | ||
| 178 | 185 | /** Connect to the server and start to send a PUT request. | |
| 179 | 186 | @param aServerName Name of the server being connected to. If NULL, the | |
@@ -188,7 +195,7 @@ class HttpClient : public Client | |||
| 188 | 195 | uint16_t aServerPort, | |
| 189 | 196 | const char* aURLPath, | |
| 190 | 197 | const char* aUserAgent =NULL) | |
| 191 | - { return startRequest(aServerName, aServerPort, aURLPath, kPut, aUserAgent); } | ||
| 198 | + { return startRequest(aServerName, aServerPort, aURLPath, HTTP_METHOD_PUT, aUserAgent); } | ||
| 192 | 199 | ||
| 193 | 200 | /** Connect to the server and start to send a PUT request. | |
| 194 | 201 | @param aServerName Name of the server being connected to. If NULL, the | |
@@ -201,7 +208,7 @@ class HttpClient : public Client | |||
| 201 | 208 | int put(const char* aServerName, | |
| 202 | 209 | const char* aURLPath, | |
| 203 | 210 | const char* aUserAgent =NULL) | |
| 204 | - { return startRequest(aServerName, kHttpPort, aURLPath, kPut, aUserAgent); } | ||
| 211 | + { return startRequest(aServerName, kHttpPort, aURLPath, HTTP_METHOD_PUT, aUserAgent); } | ||
| 205 | 212 | ||
| 206 | 213 | /** Connect to the server and start to send a PUT request. This version connects | |
| 207 | 214 | doesn't perform a DNS lookup and just connects to the given IP address. | |
@@ -219,7 +226,7 @@ class HttpClient : public Client | |||
| 219 | 226 | uint16_t aServerPort, | |
| 220 | 227 | const char* aURLPath, | |
| 221 | 228 | const char* aUserAgent =NULL) | |
| 222 | - { return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, kPut, aUserAgent); } | ||
| 229 | + { return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, HTTP_METHOD_PUT, aUserAgent); } | ||
| 223 | 230 | ||
| 224 | 231 | /** Connect to the server and start to send a PUT request. This version connects | |
| 225 | 232 | doesn't perform a DNS lookup and just connects to the given IP address. | |
@@ -235,7 +242,7 @@ class HttpClient : public Client | |||
| 235 | 242 | const char* aServerName, | |
| 236 | 243 | const char* aURLPath, | |
| 237 | 244 | const char* aUserAgent =NULL) | |
| 238 | - { return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, kPut, aUserAgent); } | ||
| 245 | + { return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, HTTP_METHOD_PUT, aUserAgent); } | ||
| 239 | 246 | ||
| 240 | 247 | /** Connect to the server and start to send the request. | |
| 241 | 248 | @param aServerName Name of the server being connected to. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments