mirror of
https://github.com/arduino-libraries/ArduinoHttpClient.git
synced 2025-04-19 21:22:15 +03:00
Updated to use the new generic Client API (rather than the Ethernet-specific one) and added ability to connect via an HTTP proxy
This commit is contained in:
parent
a9a0821cff
commit
6e8d284c7b
101
HttpClient.cpp
101
HttpClient.cpp
@ -4,9 +4,9 @@
|
|||||||
|
|
||||||
#include "HttpClient.h"
|
#include "HttpClient.h"
|
||||||
#include <../b64/b64.h>
|
#include <../b64/b64.h>
|
||||||
|
#include <Dns.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include "wiring.h"
|
|
||||||
|
|
||||||
// Initialize constants
|
// Initialize constants
|
||||||
const char* HttpClient::kUserAgent = "Arduino/2.0";
|
const char* HttpClient::kUserAgent = "Arduino/2.0";
|
||||||
@ -16,10 +16,19 @@ const char* HttpClient::kPut = "PUT";
|
|||||||
const char* HttpClient::kDelete = "DELETE";
|
const char* HttpClient::kDelete = "DELETE";
|
||||||
const char* HttpClient::kContentLengthPrefix = "Content-Length: ";
|
const char* HttpClient::kContentLengthPrefix = "Content-Length: ";
|
||||||
|
|
||||||
HttpClient::HttpClient(Client& aClient)
|
HttpClient::HttpClient(Client& aClient, const char* aProxy, uint16_t aProxyPort)
|
||||||
: iClient(&aClient)
|
: iClient(&aClient), iProxyPort(aProxyPort)
|
||||||
{
|
{
|
||||||
resetState();
|
resetState();
|
||||||
|
if (aProxy)
|
||||||
|
{
|
||||||
|
// Resolve the IP address for the proxy
|
||||||
|
DNSClient dns;
|
||||||
|
dns.begin(Ethernet.dnsServerIP());
|
||||||
|
// Not ideal that we discard any errors here, but not a lot we can do in the ctor
|
||||||
|
// and we'll get a connect error later anyway
|
||||||
|
(void)dns.getHostByName(aProxy, iProxyAddress);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpClient::resetState()
|
void HttpClient::resetState()
|
||||||
@ -44,16 +53,29 @@ int HttpClient::startRequest(const char* aServerName, uint16_t aServerPort, cons
|
|||||||
return HttpErrAPI;
|
return HttpErrAPI;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!iClient->connect(aServerName, aServerPort))
|
if (iProxyPort)
|
||||||
{
|
{
|
||||||
|
if (!iClient->connect(iProxyAddress, iProxyPort))
|
||||||
|
{
|
||||||
#ifdef LOGGING
|
#ifdef LOGGING
|
||||||
Serial.println("Connection failed");
|
Serial.println("Proxy connection failed");
|
||||||
#endif
|
#endif
|
||||||
return HttpErrConnectionFailed;
|
return HttpErrConnectionFailed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!iClient->connect(aServerName, aServerPort))
|
||||||
|
{
|
||||||
|
#ifdef LOGGING
|
||||||
|
Serial.println("Connection failed");
|
||||||
|
#endif
|
||||||
|
return HttpErrConnectionFailed;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now we're connected, send the first part of the request
|
// Now we're connected, send the first part of the request
|
||||||
return sendInitialHeaders(aServerName, aURLPath, aHttpMethod, aUserAgent, aAcceptList);
|
return sendInitialHeaders(aServerName, IPAddress(0,0,0,0), aServerPort, aURLPath, aHttpMethod, aUserAgent, aAcceptList);
|
||||||
}
|
}
|
||||||
|
|
||||||
int HttpClient::startRequest(const IPAddress& aServerAddress, uint16_t aServerPort, const char* aServerName, const char* aURLPath, const char* aHttpMethod, const char* aUserAgent, const char* aAcceptList)
|
int HttpClient::startRequest(const IPAddress& aServerAddress, uint16_t aServerPort, const char* aServerName, const char* aURLPath, const char* aHttpMethod, const char* aUserAgent, const char* aAcceptList)
|
||||||
@ -63,19 +85,32 @@ int HttpClient::startRequest(const IPAddress& aServerAddress, uint16_t aServerPo
|
|||||||
return HttpErrAPI;
|
return HttpErrAPI;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!iClient->connect(aServerAddress, aServerPort))
|
if (iProxyPort)
|
||||||
{
|
{
|
||||||
|
if (!iClient->connect(iProxyAddress, iProxyPort))
|
||||||
|
{
|
||||||
#ifdef LOGGING
|
#ifdef LOGGING
|
||||||
Serial.println("Connection failed");
|
Serial.println("Proxy connection failed");
|
||||||
#endif
|
#endif
|
||||||
return HttpErrConnectionFailed;
|
return HttpErrConnectionFailed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!iClient->connect(aServerAddress, aServerPort))
|
||||||
|
{
|
||||||
|
#ifdef LOGGING
|
||||||
|
Serial.println("Connection failed");
|
||||||
|
#endif
|
||||||
|
return HttpErrConnectionFailed;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now we're connected, send the first part of the request
|
// Now we're connected, send the first part of the request
|
||||||
return sendInitialHeaders(aServerName, aURLPath, aHttpMethod, aUserAgent, aAcceptList);
|
return sendInitialHeaders(aServerName, aServerAddress, aServerPort, aURLPath, aHttpMethod, aUserAgent, aAcceptList);
|
||||||
}
|
}
|
||||||
|
|
||||||
int HttpClient::sendInitialHeaders(const char* aServerName, const char* aURLPath, const char* aHttpMethod, const char* aUserAgent, const char* aAcceptList)
|
int HttpClient::sendInitialHeaders(const char* aServerName, IPAddress aServerIP, uint16_t aPort, const char* aURLPath, const char* aHttpMethod, const char* aUserAgent, const char* aAcceptList)
|
||||||
{
|
{
|
||||||
#ifdef LOGGING
|
#ifdef LOGGING
|
||||||
Serial.println("Connected");
|
Serial.println("Connected");
|
||||||
@ -83,13 +118,33 @@ int HttpClient::sendInitialHeaders(const char* aServerName, const char* aURLPath
|
|||||||
// Send the HTTP command, i.e. "GET /somepath/ HTTP/1.0"
|
// Send the HTTP command, i.e. "GET /somepath/ HTTP/1.0"
|
||||||
print(aHttpMethod);
|
print(aHttpMethod);
|
||||||
print(" ");
|
print(" ");
|
||||||
|
if (iProxyPort)
|
||||||
|
{
|
||||||
|
// We're going through a proxy, send a full URL
|
||||||
|
print("http://");
|
||||||
|
if (aServerName)
|
||||||
|
{
|
||||||
|
// We've got a server name, so use it
|
||||||
|
print(aServerName);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// We'll have to use the IP address
|
||||||
|
print(aServerIP);
|
||||||
|
}
|
||||||
|
if (aPort != kHttpPort)
|
||||||
|
{
|
||||||
|
print(":");
|
||||||
|
print(aPort);
|
||||||
|
}
|
||||||
|
}
|
||||||
print(aURLPath);
|
print(aURLPath);
|
||||||
println(" HTTP/1.0");
|
println(" HTTP/1.0");
|
||||||
// The host header, if required
|
// The host header, if required
|
||||||
if (aServerName)
|
if (aServerName)
|
||||||
{
|
{
|
||||||
print("Host: ");
|
// print("Host: ");
|
||||||
println(aServerName);
|
// println(aServerName);
|
||||||
}
|
}
|
||||||
// And user-agent string
|
// And user-agent string
|
||||||
print("User-Agent: ");
|
print("User-Agent: ");
|
||||||
@ -339,14 +394,28 @@ bool HttpClient::endOfBodyReached()
|
|||||||
|
|
||||||
int HttpClient::read()
|
int HttpClient::read()
|
||||||
{
|
{
|
||||||
int ret =iClient->read();
|
uint8_t b[1];
|
||||||
|
int ret = read(b, 1);
|
||||||
|
if (ret == 1)
|
||||||
|
{
|
||||||
|
return b[0];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int HttpClient::read(uint8_t *buf, size_t size)
|
||||||
|
{
|
||||||
|
int ret =iClient->read(buf, size);
|
||||||
if (endOfHeadersReached() && iContentLength > 0)
|
if (endOfHeadersReached() && iContentLength > 0)
|
||||||
{
|
{
|
||||||
// We're outputting the body now and we've seen a Content-Length header
|
// We're outputting the body now and we've seen a Content-Length header
|
||||||
// So keep track of how many bytes are left
|
// So keep track of how many bytes are left
|
||||||
if (ret >= 0)
|
if (ret >= 0)
|
||||||
{
|
{
|
||||||
iBodyLengthConsumed++;
|
iBodyLengthConsumed += ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
|
22
HttpClient.h
22
HttpClient.h
@ -5,10 +5,11 @@
|
|||||||
#ifndef HttpClient_h
|
#ifndef HttpClient_h
|
||||||
#define HttpClient_h
|
#define HttpClient_h
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
#include <IPAddress.h>
|
#include <IPAddress.h>
|
||||||
#include "Ethernet.h"
|
#include "Ethernet.h"
|
||||||
#include "Client.h"
|
#include "Client.h"
|
||||||
#include <../b64.h>
|
#include <b64.h>
|
||||||
|
|
||||||
class HttpClient : public Client
|
class HttpClient : public Client
|
||||||
{
|
{
|
||||||
@ -31,13 +32,14 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
static const int kNoContentLengthHeader =-1;
|
static const int kNoContentLengthHeader =-1;
|
||||||
|
static const int kHttpPort =80;
|
||||||
static const char* kUserAgent;
|
static const char* kUserAgent;
|
||||||
static const char* kGet;
|
static const char* kGet;
|
||||||
static const char* kPost;
|
static const char* kPost;
|
||||||
static const char* kPut;
|
static const char* kPut;
|
||||||
static const char* kDelete;
|
static const char* kDelete;
|
||||||
|
|
||||||
HttpClient(Client& aClient);
|
HttpClient(Client& aClient, const char* aProxy =NULL, uint16_t aProxyPort =0);
|
||||||
|
|
||||||
/** 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
|
||||||
@ -274,15 +276,16 @@ public:
|
|||||||
int contentLength() { return iContentLength; };
|
int contentLength() { return iContentLength; };
|
||||||
|
|
||||||
// Inherited from Print
|
// Inherited from Print
|
||||||
virtual void write(uint8_t aByte) { iClient-> write(aByte); };
|
virtual size_t write(uint8_t aByte) { return iClient-> write(aByte); };
|
||||||
virtual void write(const char *aStr) { iClient->write(aStr); };
|
virtual size_t write(const char *aStr) { return iClient->write(aStr); };
|
||||||
virtual void write(const uint8_t *aBuffer, size_t aSize) { iClient->write(aBuffer, aSize); };
|
virtual size_t write(const uint8_t *aBuffer, size_t aSize) { return iClient->write(aBuffer, aSize); };
|
||||||
// Inherited from Stream
|
// Inherited from Stream
|
||||||
virtual int available() { return iClient->available(); };
|
virtual int available() { return iClient->available(); };
|
||||||
/** Read the next byte from the server.
|
/** Read the next byte from the server.
|
||||||
@return Byte read or -1 if there are no bytes available.
|
@return Byte read or -1 if there are no bytes available.
|
||||||
*/
|
*/
|
||||||
virtual int read();
|
virtual int read();
|
||||||
|
virtual int read(uint8_t *buf, size_t size);
|
||||||
virtual int peek() { return iClient->peek(); };
|
virtual int peek() { return iClient->peek(); };
|
||||||
virtual void flush() { return iClient->flush(); };
|
virtual void flush() { return iClient->flush(); };
|
||||||
|
|
||||||
@ -291,6 +294,7 @@ public:
|
|||||||
virtual int connect(const char *host, uint16_t port) { return iClient->connect(host, port); };
|
virtual int connect(const char *host, uint16_t port) { return iClient->connect(host, port); };
|
||||||
virtual void stop();
|
virtual void stop();
|
||||||
virtual uint8_t connected() { iClient->connected(); };
|
virtual uint8_t connected() { iClient->connected(); };
|
||||||
|
virtual operator bool() { return bool(iClient); };
|
||||||
protected:
|
protected:
|
||||||
/** Reset internal state data back to the "just initialised" state
|
/** Reset internal state data back to the "just initialised" state
|
||||||
*/
|
*/
|
||||||
@ -298,6 +302,9 @@ protected:
|
|||||||
/** Send the first part of the request and the initial headers.
|
/** Send the first part of the request and the initial headers.
|
||||||
@param aServerName Name of the server being connected to. If NULL, the
|
@param aServerName Name of the server being connected to. If NULL, the
|
||||||
"Host" header line won't be sent
|
"Host" header line won't be sent
|
||||||
|
@param aServerIP IP address of the server (only used if we're going through a
|
||||||
|
proxy and aServerName is NULL
|
||||||
|
@param aServerPort Port of the server being connected to.
|
||||||
@param aURLPath Url to request
|
@param aURLPath Url to request
|
||||||
@param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc.
|
@param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc.
|
||||||
@param aUserAgent User-Agent string to send. If NULL the default
|
@param aUserAgent User-Agent string to send. If NULL the default
|
||||||
@ -307,6 +314,8 @@ protected:
|
|||||||
@return 0 if successful, else error
|
@return 0 if successful, else error
|
||||||
*/
|
*/
|
||||||
int sendInitialHeaders(const char* aServerName,
|
int sendInitialHeaders(const char* aServerName,
|
||||||
|
IPAddress aServerIP,
|
||||||
|
uint16_t aPort,
|
||||||
const char* aURLPath,
|
const char* aURLPath,
|
||||||
const char* aHttpMethod,
|
const char* aHttpMethod,
|
||||||
const char* aUserAgent,
|
const char* aUserAgent,
|
||||||
@ -343,6 +352,9 @@ protected:
|
|||||||
int iBodyLengthConsumed;
|
int iBodyLengthConsumed;
|
||||||
// How far through a Content-Length header prefix we are
|
// How far through a Content-Length header prefix we are
|
||||||
const char* iContentLengthPtr;
|
const char* iContentLengthPtr;
|
||||||
|
// Address of the proxy to use, if we're using one
|
||||||
|
IPAddress iProxyAddress;
|
||||||
|
uint16_t iProxyPort;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
#include <HttpClient.h>
|
#include <HttpClient.h>
|
||||||
#include <b64.h>
|
#include <b64.h>
|
||||||
#include <Ethernet.h>
|
#include <Ethernet.h>
|
||||||
#include <Client.h>
|
#include <EthernetClient.h>
|
||||||
|
|
||||||
// This example downloads the URL "http://arduino.cc/"
|
// This example downloads the URL "http://arduino.cc/"
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ void loop()
|
|||||||
{
|
{
|
||||||
int err =0;
|
int err =0;
|
||||||
|
|
||||||
Client c;
|
EthernetClient c;
|
||||||
HttpClient http(c);
|
HttpClient http(c);
|
||||||
|
|
||||||
err = http.get(kHostname, 80, kPath);
|
err = http.get(kHostname, 80, kPath);
|
||||||
@ -77,8 +77,8 @@ void loop()
|
|||||||
unsigned long timeoutStart = millis();
|
unsigned long timeoutStart = millis();
|
||||||
char c;
|
char c;
|
||||||
// Whilst we haven't timed out & haven't reached the end of the body
|
// Whilst we haven't timed out & haven't reached the end of the body
|
||||||
while (http.connected() &&
|
while ( (http.connected() || http.available()) &&
|
||||||
( (millis() - timeoutStart) < kNetworkTimeout ))
|
((millis() - timeoutStart) < kNetworkTimeout) )
|
||||||
{
|
{
|
||||||
if (http.available())
|
if (http.available())
|
||||||
{
|
{
|
Loading…
x
Reference in New Issue
Block a user