mirror of
https://github.com/arduino-libraries/ArduinoHttpClient.git
synced 2025-04-19 21:22:15 +03:00
Reworked to trim down the code size of sketches using HttpClient
This commit is contained in:
parent
fa50bfdbdb
commit
53cc49f1c8
@ -7,16 +7,10 @@
|
|||||||
#ifdef PROXY_ENABLED // currently disabled as introduces dependency on Dns.h in Ethernet
|
#ifdef PROXY_ENABLED // currently disabled as introduces dependency on Dns.h in Ethernet
|
||||||
#include <Dns.h>
|
#include <Dns.h>
|
||||||
#endif
|
#endif
|
||||||
#include <string.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
|
|
||||||
// Initialize constants
|
// Initialize constants
|
||||||
const char* HttpClient::kUserAgent = "Arduino/2.0";
|
const char* HttpClient::kUserAgent = "Arduino/2.1";
|
||||||
const char* HttpClient::kGet = "GET";
|
const char* HttpClient::kContentLengthPrefix = HTTP_HEADER_CONTENT_LENGTH ": ";
|
||||||
const char* HttpClient::kPost = "POST";
|
|
||||||
const char* HttpClient::kPut = "PUT";
|
|
||||||
const char* HttpClient::kDelete = "DELETE";
|
|
||||||
const char* HttpClient::kContentLengthPrefix = "Content-Length: ";
|
|
||||||
|
|
||||||
#ifdef PROXY_ENABLED // currently disabled as introduces dependency on Dns.h in Ethernet
|
#ifdef PROXY_ENABLED // currently disabled as introduces dependency on Dns.h in Ethernet
|
||||||
HttpClient::HttpClient(Client& aClient, const char* aProxy, uint16_t aProxyPort)
|
HttpClient::HttpClient(Client& aClient, const char* aProxy, uint16_t aProxyPort)
|
||||||
@ -70,6 +64,7 @@ int HttpClient::startRequest(const char* aServerName, uint16_t aServerPort, cons
|
|||||||
return HTTP_ERROR_API;
|
return HTTP_ERROR_API;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef PROXY_ENABLED
|
||||||
if (iProxyPort)
|
if (iProxyPort)
|
||||||
{
|
{
|
||||||
if (!iClient->connect(iProxyAddress, iProxyPort) > 0)
|
if (!iClient->connect(iProxyAddress, iProxyPort) > 0)
|
||||||
@ -81,6 +76,7 @@ int HttpClient::startRequest(const char* aServerName, uint16_t aServerPort, cons
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
#endif
|
||||||
{
|
{
|
||||||
if (!iClient->connect(aServerName, aServerPort) > 0)
|
if (!iClient->connect(aServerName, aServerPort) > 0)
|
||||||
{
|
{
|
||||||
@ -111,6 +107,7 @@ int HttpClient::startRequest(const IPAddress& aServerAddress, const char* aServe
|
|||||||
return HTTP_ERROR_API;
|
return HTTP_ERROR_API;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef PROXY_ENABLED
|
||||||
if (iProxyPort)
|
if (iProxyPort)
|
||||||
{
|
{
|
||||||
if (!iClient->connect(iProxyAddress, iProxyPort) > 0)
|
if (!iClient->connect(iProxyAddress, iProxyPort) > 0)
|
||||||
@ -122,6 +119,7 @@ int HttpClient::startRequest(const IPAddress& aServerAddress, const char* aServe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
#endif
|
||||||
{
|
{
|
||||||
if (!iClient->connect(aServerAddress, aServerPort) > 0)
|
if (!iClient->connect(aServerAddress, aServerPort) > 0)
|
||||||
{
|
{
|
||||||
@ -152,6 +150,7 @@ int HttpClient::sendInitialHeaders(const char* aServerName, IPAddress aServerIP,
|
|||||||
// Send the HTTP command, i.e. "GET /somepath/ HTTP/1.0"
|
// Send the HTTP command, i.e. "GET /somepath/ HTTP/1.0"
|
||||||
iClient->print(aHttpMethod);
|
iClient->print(aHttpMethod);
|
||||||
iClient->print(" ");
|
iClient->print(" ");
|
||||||
|
#ifdef PROXY_ENABLED
|
||||||
if (iProxyPort)
|
if (iProxyPort)
|
||||||
{
|
{
|
||||||
// We're going through a proxy, send a full URL
|
// We're going through a proxy, send a full URL
|
||||||
@ -172,6 +171,7 @@ int HttpClient::sendInitialHeaders(const char* aServerName, IPAddress aServerIP,
|
|||||||
iClient->print(aPort);
|
iClient->print(aPort);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
iClient->print(aURLPath);
|
iClient->print(aURLPath);
|
||||||
iClient->println(" HTTP/1.1");
|
iClient->println(" HTTP/1.1");
|
||||||
// The host header, if required
|
// The host header, if required
|
||||||
@ -187,14 +187,13 @@ int HttpClient::sendInitialHeaders(const char* aServerName, IPAddress aServerIP,
|
|||||||
iClient->println();
|
iClient->println();
|
||||||
}
|
}
|
||||||
// And user-agent string
|
// And user-agent string
|
||||||
iClient->print("User-Agent: ");
|
|
||||||
if (aUserAgent)
|
if (aUserAgent)
|
||||||
{
|
{
|
||||||
iClient->println(aUserAgent);
|
sendHeader(HTTP_HEADER_USER_AGENT, aUserAgent);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
iClient->println(kUserAgent);
|
sendHeader(HTTP_HEADER_USER_AGENT, kUserAgent);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Everything has gone well
|
// Everything has gone well
|
||||||
|
39
HttpClient.h
39
HttpClient.h
@ -23,16 +23,23 @@ static const int HTTP_ERROR_TIMED_OUT =-3;
|
|||||||
// server?
|
// server?
|
||||||
static const int HTTP_ERROR_INVALID_RESPONSE =-4;
|
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
|
class HttpClient : public Client
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static const int kNoContentLengthHeader =-1;
|
static const int kNoContentLengthHeader =-1;
|
||||||
static const int kHttpPort =80;
|
static const int kHttpPort =80;
|
||||||
static const char* kUserAgent;
|
static const char* kUserAgent;
|
||||||
static const char* kGet;
|
|
||||||
static const char* kPost;
|
|
||||||
static const char* kPut;
|
|
||||||
static const char* kDelete;
|
|
||||||
|
|
||||||
// FIXME Write longer API request, using port and user-agent, example
|
// FIXME Write longer API request, using port and user-agent, example
|
||||||
// FIXME Update tempToPachube example to calculate Content-Length correctly
|
// FIXME Update tempToPachube example to calculate Content-Length correctly
|
||||||
@ -66,7 +73,7 @@ public:
|
|||||||
*/
|
*/
|
||||||
int get(const char* aServerName, uint16_t aServerPort, const char* aURLPath,
|
int get(const char* aServerName, uint16_t aServerPort, const char* aURLPath,
|
||||||
const char* aUserAgent =NULL)
|
const char* aUserAgent =NULL)
|
||||||
{ return startRequest(aServerName, aServerPort, aURLPath, kGet, aUserAgent); }
|
{ return startRequest(aServerName, aServerPort, aURLPath, HTTP_METHOD_GET, aUserAgent); }
|
||||||
|
|
||||||
/** Connect to the server and start to send a GET request.
|
/** Connect to the server and start to send a GET request.
|
||||||
@param aServerName Name of the server being connected to. If NULL, the
|
@param aServerName Name of the server being connected to. If NULL, the
|
||||||
@ -77,7 +84,7 @@ public:
|
|||||||
@return 0 if successful, else error
|
@return 0 if successful, else error
|
||||||
*/
|
*/
|
||||||
int get(const char* aServerName, const char* aURLPath, const char* aUserAgent =NULL)
|
int get(const char* aServerName, const char* aURLPath, const char* aUserAgent =NULL)
|
||||||
{ return startRequest(aServerName, kHttpPort, aURLPath, kGet, aUserAgent); }
|
{ return startRequest(aServerName, kHttpPort, aURLPath, HTTP_METHOD_GET, aUserAgent); }
|
||||||
|
|
||||||
/** Connect to the server and start to send a GET request. This version connects
|
/** 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.
|
doesn't perform a DNS lookup and just connects to the given IP address.
|
||||||
@ -95,7 +102,7 @@ public:
|
|||||||
uint16_t aServerPort,
|
uint16_t aServerPort,
|
||||||
const char* aURLPath,
|
const char* aURLPath,
|
||||||
const char* aUserAgent =NULL)
|
const char* aUserAgent =NULL)
|
||||||
{ return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, kGet, aUserAgent); }
|
{ return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, HTTP_METHOD_GET, aUserAgent); }
|
||||||
|
|
||||||
/** Connect to the server and start to send a GET request. This version connects
|
/** 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.
|
doesn't perform a DNS lookup and just connects to the given IP address.
|
||||||
@ -111,7 +118,7 @@ public:
|
|||||||
const char* aServerName,
|
const char* aServerName,
|
||||||
const char* aURLPath,
|
const char* aURLPath,
|
||||||
const char* aUserAgent =NULL)
|
const char* aUserAgent =NULL)
|
||||||
{ return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, kGet, aUserAgent); }
|
{ return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, HTTP_METHOD_GET, aUserAgent); }
|
||||||
|
|
||||||
/** Connect to the server and start to send a POST request.
|
/** Connect to the server and start to send a POST request.
|
||||||
@param aServerName Name of the server being connected to. If NULL, the
|
@param aServerName Name of the server being connected to. If NULL, the
|
||||||
@ -126,7 +133,7 @@ public:
|
|||||||
uint16_t aServerPort,
|
uint16_t aServerPort,
|
||||||
const char* aURLPath,
|
const char* aURLPath,
|
||||||
const char* aUserAgent =NULL)
|
const char* aUserAgent =NULL)
|
||||||
{ return startRequest(aServerName, aServerPort, aURLPath, kPost, aUserAgent); }
|
{ return startRequest(aServerName, aServerPort, aURLPath, HTTP_METHOD_POST, aUserAgent); }
|
||||||
|
|
||||||
/** Connect to the server and start to send a POST request.
|
/** Connect to the server and start to send a POST request.
|
||||||
@param aServerName Name of the server being connected to. If NULL, the
|
@param aServerName Name of the server being connected to. If NULL, the
|
||||||
@ -139,7 +146,7 @@ public:
|
|||||||
int post(const char* aServerName,
|
int post(const char* aServerName,
|
||||||
const char* aURLPath,
|
const char* aURLPath,
|
||||||
const char* aUserAgent =NULL)
|
const char* aUserAgent =NULL)
|
||||||
{ return startRequest(aServerName, kHttpPort, aURLPath, kPost, aUserAgent); }
|
{ return startRequest(aServerName, kHttpPort, aURLPath, HTTP_METHOD_POST, aUserAgent); }
|
||||||
|
|
||||||
/** Connect to the server and start to send a POST request. This version connects
|
/** 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.
|
doesn't perform a DNS lookup and just connects to the given IP address.
|
||||||
@ -157,7 +164,7 @@ public:
|
|||||||
uint16_t aServerPort,
|
uint16_t aServerPort,
|
||||||
const char* aURLPath,
|
const char* aURLPath,
|
||||||
const char* aUserAgent =NULL)
|
const char* aUserAgent =NULL)
|
||||||
{ return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, kPost, aUserAgent); }
|
{ return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, HTTP_METHOD_POST, aUserAgent); }
|
||||||
|
|
||||||
/** Connect to the server and start to send a POST request. This version connects
|
/** 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.
|
doesn't perform a DNS lookup and just connects to the given IP address.
|
||||||
@ -173,7 +180,7 @@ public:
|
|||||||
const char* aServerName,
|
const char* aServerName,
|
||||||
const char* aURLPath,
|
const char* aURLPath,
|
||||||
const char* aUserAgent =NULL)
|
const char* aUserAgent =NULL)
|
||||||
{ return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, kPost, aUserAgent); }
|
{ return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, HTTP_METHOD_POST, aUserAgent); }
|
||||||
|
|
||||||
/** Connect to the server and start to send a PUT request.
|
/** Connect to the server and start to send a PUT request.
|
||||||
@param aServerName Name of the server being connected to. If NULL, the
|
@param aServerName Name of the server being connected to. If NULL, the
|
||||||
@ -188,7 +195,7 @@ public:
|
|||||||
uint16_t aServerPort,
|
uint16_t aServerPort,
|
||||||
const char* aURLPath,
|
const char* aURLPath,
|
||||||
const char* aUserAgent =NULL)
|
const char* aUserAgent =NULL)
|
||||||
{ return startRequest(aServerName, aServerPort, aURLPath, kPut, aUserAgent); }
|
{ return startRequest(aServerName, aServerPort, aURLPath, HTTP_METHOD_PUT, aUserAgent); }
|
||||||
|
|
||||||
/** Connect to the server and start to send a PUT request.
|
/** Connect to the server and start to send a PUT request.
|
||||||
@param aServerName Name of the server being connected to. If NULL, the
|
@param aServerName Name of the server being connected to. If NULL, the
|
||||||
@ -201,7 +208,7 @@ public:
|
|||||||
int put(const char* aServerName,
|
int put(const char* aServerName,
|
||||||
const char* aURLPath,
|
const char* aURLPath,
|
||||||
const char* aUserAgent =NULL)
|
const char* aUserAgent =NULL)
|
||||||
{ return startRequest(aServerName, kHttpPort, aURLPath, kPut, aUserAgent); }
|
{ return startRequest(aServerName, kHttpPort, aURLPath, HTTP_METHOD_PUT, aUserAgent); }
|
||||||
|
|
||||||
/** Connect to the server and start to send a PUT request. This version connects
|
/** 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.
|
doesn't perform a DNS lookup and just connects to the given IP address.
|
||||||
@ -219,7 +226,7 @@ public:
|
|||||||
uint16_t aServerPort,
|
uint16_t aServerPort,
|
||||||
const char* aURLPath,
|
const char* aURLPath,
|
||||||
const char* aUserAgent =NULL)
|
const char* aUserAgent =NULL)
|
||||||
{ return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, kPut, aUserAgent); }
|
{ return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, HTTP_METHOD_PUT, aUserAgent); }
|
||||||
|
|
||||||
/** Connect to the server and start to send a PUT request. This version connects
|
/** 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.
|
doesn't perform a DNS lookup and just connects to the given IP address.
|
||||||
@ -235,7 +242,7 @@ public:
|
|||||||
const char* aServerName,
|
const char* aServerName,
|
||||||
const char* aURLPath,
|
const char* aURLPath,
|
||||||
const char* aUserAgent =NULL)
|
const char* aUserAgent =NULL)
|
||||||
{ return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, kPut, aUserAgent); }
|
{ return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, HTTP_METHOD_PUT, aUserAgent); }
|
||||||
|
|
||||||
/** Connect to the server and start to send the request.
|
/** Connect to the server and start to send the request.
|
||||||
@param aServerName Name of the server being connected to.
|
@param aServerName Name of the server being connected to.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user