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

Make server name/address and port constructor arguments

This commit is contained in:
Sandeep Mistry 2016-06-17 10:56:27 -04:00
parent e3a6c20cd9
commit 0030d41287
3 changed files with 53 additions and 262 deletions

View File

@ -9,8 +9,14 @@
const char* HttpClient::kUserAgent = "Arduino/2.2.0"; const char* HttpClient::kUserAgent = "Arduino/2.2.0";
const char* HttpClient::kContentLengthPrefix = HTTP_HEADER_CONTENT_LENGTH ": "; const char* HttpClient::kContentLengthPrefix = HTTP_HEADER_CONTENT_LENGTH ": ";
HttpClient::HttpClient(Client& aClient) HttpClient::HttpClient(Client& aClient, const char* aServerName, uint16_t aServerPort)
: iClient(&aClient) : iClient(&aClient), iServerName(aServerName), iServerAddress(), iServerPort(aServerPort)
{
resetState();
}
HttpClient::HttpClient(Client& aClient, const IPAddress& aServerAddress, uint16_t aServerPort)
: iClient(&aClient), iServerName(NULL), iServerAddress(aServerAddress), iServerPort(aServerPort)
{ {
resetState(); resetState();
} }
@ -36,7 +42,7 @@ void HttpClient::beginRequest()
iState = eRequestStarted; iState = eRequestStarted;
} }
int HttpClient::startRequest(const char* aServerName, uint16_t aServerPort, const char* aURLPath, const char* aHttpMethod, const char* aUserAgent) int HttpClient::startRequest(const char* aURLPath, const char* aHttpMethod)
{ {
tHttpState initialState = iState; tHttpState initialState = iState;
if ((eIdle != iState) && (eRequestStarted != iState)) if ((eIdle != iState) && (eRequestStarted != iState))
@ -44,16 +50,26 @@ int HttpClient::startRequest(const char* aServerName, uint16_t aServerPort, cons
return HTTP_ERROR_API; return HTTP_ERROR_API;
} }
if (!iClient->connect(aServerName, aServerPort) > 0) if (iServerName) {
{ if (!iClient->connect(iServerName, iServerPort) > 0)
#ifdef LOGGING {
Serial.println("Connection failed"); #ifdef LOGGING
#endif Serial.println("Connection failed");
return HTTP_ERROR_CONNECTION_FAILED; #endif
return HTTP_ERROR_CONNECTION_FAILED;
}
} else {
if (!iClient->connect(iServerAddress, iServerPort) > 0)
{
#ifdef LOGGING
Serial.println("Connection failed");
#endif
return HTTP_ERROR_CONNECTION_FAILED;
}
} }
// Now we're connected, send the first part of the request // Now we're connected, send the first part of the request
int ret = sendInitialHeaders(aServerName, IPAddress(0,0,0,0), aServerPort, aURLPath, aHttpMethod, aUserAgent); int ret = sendInitialHeaders(aURLPath, aHttpMethod);
if ((initialState == eIdle) && (HTTP_SUCCESS == ret)) if ((initialState == eIdle) && (HTTP_SUCCESS == ret))
{ {
// This was a simple version of the API, so terminate the headers now // This was a simple version of the API, so terminate the headers now
@ -64,35 +80,7 @@ int HttpClient::startRequest(const char* aServerName, uint16_t aServerPort, cons
return ret; return ret;
} }
int HttpClient::startRequest(const IPAddress& aServerAddress, const char* aServerName, uint16_t aServerPort, const char* aURLPath, const char* aHttpMethod, const char* aUserAgent) int HttpClient::sendInitialHeaders(const char* aURLPath, const char* aHttpMethod)
{
tHttpState initialState = iState;
if ((eIdle != iState) && (eRequestStarted != iState))
{
return HTTP_ERROR_API;
}
if (!iClient->connect(aServerAddress, aServerPort) > 0)
{
#ifdef LOGGING
Serial.println("Connection failed");
#endif
return HTTP_ERROR_CONNECTION_FAILED;
}
// Now we're connected, send the first part of the request
int ret = sendInitialHeaders(aServerName, aServerAddress, aServerPort, aURLPath, aHttpMethod, aUserAgent);
if ((initialState == eIdle) && (HTTP_SUCCESS == ret))
{
// This was a simple version of the API, so terminate the headers now
finishHeaders();
}
// else we'll call it in endRequest or in the first call to print, etc.
return ret;
}
int HttpClient::sendInitialHeaders(const char* aServerName, IPAddress aServerIP, uint16_t aPort, const char* aURLPath, const char* aHttpMethod, const char* aUserAgent)
{ {
#ifdef LOGGING #ifdef LOGGING
Serial.println("Connected"); Serial.println("Connected");
@ -104,26 +92,20 @@ int HttpClient::sendInitialHeaders(const char* aServerName, IPAddress aServerIP,
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
if (aServerName) if (iServerName)
{ {
iClient->print("Host: "); iClient->print("Host: ");
iClient->print(aServerName); iClient->print(iServerName);
if (aPort != kHttpPort) if (iServerPort != kHttpPort)
{ {
iClient->print(":"); iClient->print(":");
iClient->print(aPort); iClient->print(iServerPort);
} }
iClient->println(); iClient->println();
} }
// And user-agent string // And user-agent string
if (aUserAgent) sendHeader(HTTP_HEADER_USER_AGENT, kUserAgent);
{
sendHeader(HTTP_HEADER_USER_AGENT, aUserAgent);
}
else
{
sendHeader(HTTP_HEADER_USER_AGENT, kUserAgent);
}
// We don't support persistent connections, so tell the server to // We don't support persistent connections, so tell the server to
// close this connection after we're done // close this connection after we're done
sendHeader(HTTP_HEADER_CONNECTION, "close"); sendHeader(HTTP_HEADER_CONNECTION, "close");

View File

@ -44,7 +44,8 @@ public:
// 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
HttpClient(Client& aClient); HttpClient(Client& aClient, const char* aServerName, uint16_t aServerPort = kHttpPort);
HttpClient(Client& aClient, const IPAddress& aServerAddress, uint16_t aServerPort = kHttpPort);
/** Start a more complex request. /** Start a more complex request.
Use this when you need to send additional headers in the request, Use this when you need to send additional headers in the request,
@ -59,219 +60,33 @@ public:
void endRequest(); void endRequest();
/** 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
"Host" header line won't be sent
@param aServerPort Port to connect to on the server
@param aURLPath Url to request @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 @return 0 if successful, else error
*/ */
int get(const char* aServerName, uint16_t aServerPort, const char* aURLPath, int get(const char* aURLPath)
const char* aUserAgent =NULL) { return startRequest(aURLPath, HTTP_METHOD_GET); }
{ return startRequest(aServerName, aServerPort, aURLPath, HTTP_METHOD_GET, 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, HTTP_METHOD_GET, 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, HTTP_METHOD_GET, 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 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,
const char* aURLPath,
const char* aUserAgent =NULL)
{ 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
"Host" header line won't be sent
@param aServerPort Port to connect to on the server
@param aURLPath Url to request @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 @return 0 if successful, else error
*/ */
int post(const char* aServerName, int post(const char* aURLPath)
uint16_t aServerPort, { return startRequest(aURLPath, HTTP_METHOD_POST); }
const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerName, aServerPort, aURLPath, HTTP_METHOD_POST, aUserAgent); }
/** 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
@return 0 if successful, else error
*/
int post(const char* aServerName,
const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerName, kHttpPort, aURLPath, HTTP_METHOD_POST, 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, HTTP_METHOD_POST, 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)
{ 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
"Host" header line won't be sent
@param aServerPort Port to connect to on the server
@param aURLPath Url to request @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 @return 0 if successful, else error
*/ */
int put(const char* aServerName, int put(const char* aURLPath)
uint16_t aServerPort, { return startRequest(aURLPath, HTTP_METHOD_PUT); }
const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerName, aServerPort, aURLPath, HTTP_METHOD_PUT, aUserAgent); }
/** 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
@return 0 if successful, else error
*/
int put(const char* aServerName,
const char* aURLPath,
const char* aUserAgent =NULL)
{ return startRequest(aServerName, kHttpPort, aURLPath, HTTP_METHOD_PUT, 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, HTTP_METHOD_PUT, 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)
{ 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 aServerPort Port to connect to on the server
@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
user-agent kUserAgent will be sent
@return 0 if successful, else error @return 0 if successful, else error
*/ */
int startRequest(const char* aServerName, int startRequest(const char* aURLPath,
uint16_t aServerPort, const char* aHttpMethod);
const char* aURLPath,
const char* aHttpMethod,
const char* aUserAgent);
/** Connect to the server and start to send the request.
@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 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
@return 0 if successful, else error
*/
int startRequest(const IPAddress& aServerAddress,
const char* aServerName,
uint16_t aServerPort,
const char* aURLPath,
const char* aHttpMethod,
const char* aUserAgent);
/** Send an additional header line. This can only be called in between the /** Send an additional header line. This can only be called in between the
calls to startRequest and finishRequest. calls to startRequest and finishRequest.
@ -383,23 +198,12 @@ protected:
void resetState(); void resetState();
/** 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
"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
user-agent kUserAgent will be sent
@return 0 if successful, else error @return 0 if successful, else error
*/ */
int sendInitialHeaders(const char* aServerName, int sendInitialHeaders(const char* aURLPath,
IPAddress aServerIP, const char* aHttpMethod);
uint16_t aPort,
const char* aURLPath,
const char* aHttpMethod,
const char* aUserAgent);
/* Let the server know that we've reached the end of the headers /* Let the server know that we've reached the end of the headers
*/ */
@ -424,8 +228,13 @@ protected:
eLineStartingCRFound, eLineStartingCRFound,
eReadingBody eReadingBody
} tHttpState; } tHttpState;
// Ethernet client we're using // Client we're using
Client* iClient; Client* iClient;
// Server we are connecting to
const char* iServerName;
IPAddress iServerAddress;
// Port of server we are connecting to
uint16_t iServerPort;
// Current state of the finite-state-machine // Current state of the finite-state-machine
tHttpState iState; tHttpState iState;
// Stores the status code for the response, once known // Stores the status code for the response, once known

View File

@ -51,9 +51,9 @@ void loop()
int err =0; int err =0;
WiFiClient c; WiFiClient c;
HttpClient http(c); HttpClient http(c, kHostname);
err = http.get(kHostname, kPath); err = http.get(kPath);
if (err == 0) if (err == 0)
{ {
Serial.println("startedRequest ok"); Serial.println("startedRequest ok");