1
0
mirror of https://github.com/arduino-libraries/ArduinoHttpClient.git synced 2025-10-22 10:52:21 +03:00

Updated to new API as discussed on the Arduino Developers mailing list. Part of the process of moving the library to live as one of the core Arduino libraries.

The get/put/post calls have been streamlined to require fewer parameters in the basic case - i.e. you can just call http.get("www.mysite.com", "/somepath") to make a simple request.

The accept header has been removed from the list of possible parameters to get/put/post - if you need to use it then send it manually with sendHeader(...) instead.

You don't need to call finishRequest() after the initial call to get/put/post if you aren't going to send any headers.  However, if you /do/ want to send extra headers then you need to call beginRequest() before the get/put/post and endRequest() at the end of all the sent data (so after the data as well as the headers).  E.g.

	http.beginRequest();
	http.post("www.somesite.com", "/somepath");
	http.sendHeader("Content-Length", strlen(postdata));
	http.print(postdata);
	http.endRequest();
This commit is contained in:
amcewen
2012-03-30 15:11:13 +01:00
parent 222f718705
commit 44d790b8a6
7 changed files with 323 additions and 144 deletions

View File

@@ -1,5 +1,5 @@
// Class to simplify HTTP fetching on Arduino
// (c) Copyright MCQN Ltd. 2010-2011
// (c) Copyright MCQN Ltd. 2010-2012
// Released under Apache License, version 2.0
#ifndef HttpClient_h
@@ -9,28 +9,24 @@
#include <IPAddress.h>
#include "Ethernet.h"
#include "Client.h"
#include <b64.h>
static const int HTTP_SUCCESS =0;
// The end of the headers has been reached. This consumes the '\n'
// Could not connect to the server
static const int HTTP_ERROR_CONNECTION_FAILED =-1;
// This call was made when the HttpClient class wasn't expecting it
// to be called. Usually indicates your code is using the class
// incorrectly
static const int HTTP_ERROR_API =-2;
// Spent too long waiting for a reply
static const int HTTP_ERROR_TIMED_OUT =-3;
// The response from the server is invalid, is it definitely an HTTP
// server?
static const int HTTP_ERROR_INVALID_RESPONSE =-4;
class HttpClient : public Client
{
public:
enum
{
HttpSuccess = 0,
// The end of the headers has been reached. This consumes the '\n'
// Could not connect to the server
HttpErrConnectionFailed =-1,
// This call was made when the HttpClient class wasn't expecting it
// to be called. Usually indicates your code is using the class
// incorrectly
HttpErrAPI =-2,
// Spent too long waiting for a reply
HttpErrTimedOut =-3,
// The response from the server is invalid, is it definitely an HTTP
// server?
HttpErrInvalidResponse =-4,
};
static const int kNoContentLengthHeader =-1;
static const int kHttpPort =80;
static const char* kUserAgent;
@@ -39,8 +35,23 @@ public:
static const char* kPut;
static const char* kDelete;
// FIXME Write longer API request, using port and user-agent, example
// FIXME Update tempToPachube example to calculate Content-Length correctly
HttpClient(Client& aClient, const char* aProxy =NULL, uint16_t aProxyPort =0);
/** Start a more complex request.
Use this when you need to send additional headers in the request,
but you will also need to call endRequest() when you are finished.
*/
void beginRequest();
/** End a more complex request.
Use this when you need to have sent additional headers in the request,
but you will also need to call beginRequest() at the start.
*/
void endRequest();
/** Connect to the server and start to send a GET request.
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@@ -48,35 +59,56 @@ public:
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@param aAcceptList List of MIME types that the client will accept. If
NULL the "Accept" header line won't be sent
@return 0 if successful, else error
*/
int get(const char* aServerName, uint16_t aServerPort,
const char* aURLPath, const char* aUserAgent =NULL,
const char* aAcceptList =NULL)
{ return startRequest(aServerName, aServerPort, aURLPath, kGet, aUserAgent, aAcceptList); }
int get(const char* aServerName, uint16_t aServerPort, const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerName, aServerPort, aURLPath, kGet, aUserAgent); }
/** Connect to the server and start to send a GET request.
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int get(const char* aServerName, const char* aURLPath, const char* aUserAgent =NULL)
{ return startRequest(aServerName, kHttpPort, aURLPath, kGet, aUserAgent); }
/** 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.
@param aServerAddress IP address of the server to connect to
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aServerPort Port to connect to on the server
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int get(const IPAddress& aServerAddress,
const char* aServerName,
uint16_t aServerPort,
const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, kGet, aUserAgent); }
/** 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.
@param aServerAddress IP address of the server to connect to
@param aServerPort Port to connect to on the server
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@param aAcceptList List of MIME types that the client will accept. If
NULL the "Accept" header line won't be sent
@return 0 if successful, else error
*/
int get(const IPAddress& aServerAddress,
uint16_t aServerPort,
const char* aServerName,
const char* aURLPath,
const char* aUserAgent =NULL,
const char* aAcceptList =NULL)
{ return startRequest(aServerAddress, aServerPort, aServerName, aURLPath, kGet, aUserAgent, aAcceptList); }
const char* aUserAgent =NULL)
{ return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, kGet, aUserAgent); }
/** Connect to the server and start to send a POST request.
@param aServerName Name of the server being connected to. If NULL, the
@@ -85,37 +117,60 @@ public:
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@param aAcceptList List of MIME types that the client will accept. If
NULL the "Accept" header line won't be sent
@return 0 if successful, else error
*/
int post(const char* aServerName,
uint16_t aServerPort,
const char* aURLPath,
const char* aUserAgent =NULL,
const char* aAcceptList =NULL)
{ return startRequest(aServerName, aServerPort, aURLPath, kPost, aUserAgent, aAcceptList); }
const char* aUserAgent =NULL)
{ return startRequest(aServerName, aServerPort, aURLPath, kPost, aUserAgent); }
/** 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.
@param aServerAddress IP address of the server to connect to
@param aServerPort Port to connect to on the server
/** Connect to the server and start to send a POST request.
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@param aAcceptList List of MIME types that the client will accept. If
NULL the "Accept" header line won't be sent
@return 0 if successful, else error
*/
int post(const char* aServerName,
const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerName, kHttpPort, aURLPath, kPost, aUserAgent); }
/** 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.
@param aServerAddress IP address of the server to connect to
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aServerPort Port to connect to on the server
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int post(const IPAddress& aServerAddress,
const char* aServerName,
uint16_t aServerPort,
const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, kPost, aUserAgent); }
/** 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.
@param aServerAddress IP address of the server to connect to
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int post(const IPAddress& aServerAddress,
const char* aServerName,
const char* aURLPath,
const char* aUserAgent =NULL,
const char* aAcceptList =NULL)
{ return startRequest(aServerAddress, aServerPort, aServerName, aURLPath, kPost, aUserAgent, aAcceptList); }
const char* aUserAgent =NULL)
{ return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, kPost, aUserAgent); }
/** Connect to the server and start to send a PUT request.
@param aServerName Name of the server being connected to. If NULL, the
@@ -124,37 +179,60 @@ public:
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@param aAcceptList List of MIME types that the client will accept. If
NULL the "Accept" header line won't be sent
@return 0 if successful, else error
*/
int put(const char* aServerName,
uint16_t aServerPort,
const char* aURLPath,
const char* aUserAgent =NULL,
const char* aAcceptList =NULL)
{ return startRequest(aServerName, aServerPort, aURLPath, kPut, aUserAgent, aAcceptList); }
const char* aUserAgent =NULL)
{ return startRequest(aServerName, aServerPort, aURLPath, kPut, aUserAgent); }
/** 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.
@param aServerAddress IP address of the server to connect to
@param aServerPort Port to connect to on the server
/** Connect to the server and start to send a PUT request.
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@param aAcceptList List of MIME types that the client will accept. If
NULL the "Accept" header line won't be sent
@return 0 if successful, else error
*/
int put(const char* aServerName,
const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerName, kHttpPort, aURLPath, kPut, aUserAgent); }
/** 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.
@param aServerAddress IP address of the server to connect to
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aServerPort Port to connect to on the server
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int put(const IPAddress& aServerAddress,
const char* aServerName,
uint16_t aServerPort,
const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerAddress, aServerName, aServerPort, aURLPath, kPut, aUserAgent); }
/** 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.
@param aServerAddress IP address of the server to connect to
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aURLPath Url to request
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@return 0 if successful, else error
*/
int put(const IPAddress& aServerAddress,
const char* aServerName,
const char* aURLPath,
const char* aUserAgent =NULL,
const char* aAcceptList =NULL)
{ return startRequest(aServerAddress, aServerPort, aServerName, aURLPath, kPut, aUserAgent, aAcceptList); }
const char* aUserAgent =NULL)
{ return startRequest(aServerAddress, aServerName, kHttpPort, aURLPath, kPut, aUserAgent); }
/** Connect to the server and start to send the request.
@param aServerName Name of the server being connected to.
@@ -163,37 +241,31 @@ public:
@param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc.
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@param aAcceptList List of MIME types that the client will accept. If
NULL the "Accept" header line won't be sent
@return 0 if successful, else error
*/
int startRequest(const char* aServerName,
uint16_t aServerPort,
uint16_t aServerPort,
const char* aURLPath,
const char* aHttpMethod,
const char* aUserAgent,
const char* aAcceptList);
const char* aUserAgent);
/** Connect to the server and start to send the request.
@param aServerAddress IP address of the server to connect to.
@param aServerPort Port to connect to on the server
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@param aServerPort Port to connect to on the server
@param aURLPath Url to request
@param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc.
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@param aAcceptList List of MIME types that the client will accept. If
NULL the "Accept" header line won't be sent
@return 0 if successful, else error
*/
int startRequest(const IPAddress& aServerAddress,
uint16_t aServerPort,
const char* aServerName,
uint16_t aServerPort,
const char* aURLPath,
const char* aHttpMethod,
const char* aUserAgent,
const char* aAcceptList);
const char* aUserAgent);
/** Send an additional header line. This can only be called in between the
calls to startRequest and finishRequest.
@@ -253,7 +325,7 @@ public:
returned in the response. You can also use it after you've found all of
the headers you're interested in, and just want to get on with processing
the body.
@return HttpSuccess if successful, else an error code
@return HTTP_SUCCESS if successful, else an error code
*/
int skipResponseHeaders();
@@ -267,7 +339,8 @@ public:
@return true if we are now at the end of the body, else false
*/
bool endOfBodyReached();
virtual bool eof() { return endOfBodyReached(); };
virtual bool endOfStream() { return endOfBodyReached(); };
virtual bool completed() { return endOfBodyReached(); };
/** Return the length of the body.
@return Length of the body, in bytes, or kNoContentLengthHeader if no
@@ -276,8 +349,10 @@ public:
int contentLength() { return iContentLength; };
// Inherited from Print
virtual size_t write(uint8_t aByte) { return iClient-> write(aByte); };
virtual size_t write(const uint8_t *aBuffer, size_t aSize) { return iClient->write(aBuffer, aSize); };
// Note: 1st call to these indicates the user is sending the body, so if need
// Note: be we should finish the header first
virtual size_t write(uint8_t aByte) { if (iState < eRequestSent) { finishHeaders(); }; return iClient-> write(aByte); };
virtual size_t write(const uint8_t *aBuffer, size_t aSize) { if (iState < eRequestSent) { finishHeaders(); }; return iClient->write(aBuffer, aSize); };
// Inherited from Stream
virtual int available() { return iClient->available(); };
/** Read the next byte from the server.
@@ -298,6 +373,7 @@ protected:
/** Reset internal state data back to the "just initialised" state
*/
void resetState();
/** Send the first part of the request and the initial headers.
@param aServerName Name of the server being connected to. If NULL, the
"Host" header line won't be sent
@@ -308,8 +384,6 @@ protected:
@param aHttpMethod Type of HTTP request to make, e.g. "GET", "POST", etc.
@param aUserAgent User-Agent string to send. If NULL the default
user-agent kUserAgent will be sent
@param aAcceptList List of MIME types that the client will accept. If
NULL the "Accept" header line won't be sent
@return 0 if successful, else error
*/
int sendInitialHeaders(const char* aServerName,
@@ -317,14 +391,17 @@ protected:
uint16_t aPort,
const char* aURLPath,
const char* aHttpMethod,
const char* aUserAgent,
const char* aAcceptList);
const char* aUserAgent);
/* Let the server know that we've reached the end of the headers
*/
void finishHeaders();
// Number of milliseconds that we wait each time there isn't any data
// available to be read (during status code and header processing)
static const int kHttpWaitForDataDelay = 1000;
// Number of milliseconds that we'll wait in total without receiveing any
// data before returning HttpErrTimedOut (during status code and header
// data before returning HTTP_ERROR_TIMED_OUT (during status code and header
// processing)
static const int kHttpResponseTimeout = 30*1000;
static const char* kContentLengthPrefix;