Skip to content

Commit 0030d41

Browse files
committed
Make server name/address and port constructor arguments
1 parent e3a6c20 commit 0030d41

File tree

3 files changed

+53
-262
lines changed

3 files changed

+53
-262
lines changed

HttpClient.cpp

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@
99
const char* HttpClient::kUserAgent = "Arduino/2.2.0";
1010
const char* HttpClient::kContentLengthPrefix = HTTP_HEADER_CONTENT_LENGTH ": ";
1111

12-
HttpClient::HttpClient(Client& aClient)
13-
: iClient(&aClient)
12+
HttpClient::HttpClient(Client& aClient, const char* aServerName, uint16_t aServerPort)
13+
: iClient(&aClient), iServerName(aServerName), iServerAddress(), iServerPort(aServerPort)
14+
{
15+
resetState();
16+
}
17+
18+
HttpClient::HttpClient(Client& aClient, const IPAddress& aServerAddress, uint16_t aServerPort)
19+
: iClient(&aClient), iServerName(NULL), iServerAddress(aServerAddress), iServerPort(aServerPort)
1420
{
1521
resetState();
1622
}
@@ -36,52 +42,34 @@ void HttpClient::beginRequest()
3642
iState = eRequestStarted;
3743
}
3844

39-
int HttpClient::startRequest(const char* aServerName, uint16_t aServerPort, const char* aURLPath, const char* aHttpMethod, const char* aUserAgent)
45+
int HttpClient::startRequest(const char* aURLPath, const char* aHttpMethod)
4046
{
4147
tHttpState initialState = iState;
4248
if ((eIdle != iState) && (eRequestStarted != iState))
4349
{
4450
return HTTP_ERROR_API;
4551
}
4652

47-
if (!iClient->connect(aServerName, aServerPort) > 0)
48-
{
49-
#ifdef LOGGING
50-
Serial.println("Connection failed");
51-
#endif
52-
return HTTP_ERROR_CONNECTION_FAILED;
53-
}
54-
55-
// Now we're connected, send the first part of the request
56-
int ret = sendInitialHeaders(aServerName, IPAddress(0,0,0,0), aServerPort, aURLPath, aHttpMethod, aUserAgent);
57-
if ((initialState == eIdle) && (HTTP_SUCCESS == ret))
58-
{
59-
// This was a simple version of the API, so terminate the headers now
60-
finishHeaders();
61-
}
62-
// else we'll call it in endRequest or in the first call to print, etc.
63-
64-
return ret;
65-
}
66-
67-
int HttpClient::startRequest(const IPAddress& aServerAddress, const char* aServerName, uint16_t aServerPort, const char* aURLPath, const char* aHttpMethod, const char* aUserAgent)
68-
{
69-
tHttpState initialState = iState;
70-
if ((eIdle != iState) && (eRequestStarted != iState))
71-
{
72-
return HTTP_ERROR_API;
73-
}
74-
75-
if (!iClient->connect(aServerAddress, aServerPort) > 0)
76-
{
77-
#ifdef LOGGING
78-
Serial.println("Connection failed");
79-
#endif
80-
return HTTP_ERROR_CONNECTION_FAILED;
53+
if (iServerName) {
54+
if (!iClient->connect(iServerName, iServerPort) > 0)
55+
{
56+
#ifdef LOGGING
57+
Serial.println("Connection failed");
58+
#endif
59+
return HTTP_ERROR_CONNECTION_FAILED;
60+
}
61+
} else {
62+
if (!iClient->connect(iServerAddress, iServerPort) > 0)
63+
{
64+
#ifdef LOGGING
65+
Serial.println("Connection failed");
66+
#endif
67+
return HTTP_ERROR_CONNECTION_FAILED;
68+
}
8169
}
8270

8371
// Now we're connected, send the first part of the request
84-
int ret = sendInitialHeaders(aServerName, aServerAddress, aServerPort, aURLPath, aHttpMethod, aUserAgent);
72+
int ret = sendInitialHeaders(aURLPath, aHttpMethod);
8573
if ((initialState == eIdle) && (HTTP_SUCCESS == ret))
8674
{
8775
// This was a simple version of the API, so terminate the headers now
@@ -92,7 +80,7 @@ int HttpClient::startRequest(const IPAddress& aServerAddress, const char* aServe
9280
return ret;
9381
}
9482

95-
int HttpClient::sendInitialHeaders(const char* aServerName, IPAddress aServerIP, uint16_t aPort, const char* aURLPath, const char* aHttpMethod, const char* aUserAgent)
83+
int HttpClient::sendInitialHeaders(const char* aURLPath, const char* aHttpMethod)
9684
{
9785
#ifdef LOGGING
9886
Serial.println("Connected");
@@ -104,26 +92,20 @@ int HttpClient::sendInitialHeaders(const char* aServerName, IPAddress aServerIP,
10492
iClient->print(aURLPath);
10593
iClient->println(" HTTP/1.1");
10694
// The host header, if required
107-
if (aServerName)
95+
if (iServerName)
10896
{
10997
iClient->print("Host: ");
110-
iClient->print(aServerName);
111-
if (aPort != kHttpPort)
98+
iClient->print(iServerName);
99+
if (iServerPort != kHttpPort)
112100
{
113101
iClient->print(":");
114-
iClient->print(aPort);
102+
iClient->print(iServerPort);
115103
}
116104
iClient->println();
117105
}
118106
// And user-agent string
119-
if (aUserAgent)
120-
{
121-
sendHeader(HTTP_HEADER_USER_AGENT, aUserAgent);
122-
}
123-
else
124-
{
125-
sendHeader(HTTP_HEADER_USER_AGENT, kUserAgent);
126-
}
107+
sendHeader(HTTP_HEADER_USER_AGENT, kUserAgent);
108+
127109
// We don't support persistent connections, so tell the server to
128110
// close this connection after we're done
129111
sendHeader(HTTP_HEADER_CONNECTION, "close");

0 commit comments

Comments
 (0)