9
9
const char * HttpClient::kUserAgent = " Arduino/2.2.0" ;
10
10
const char * HttpClient::kContentLengthPrefix = HTTP_HEADER_CONTENT_LENGTH " : " ;
11
11
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)
14
20
{
15
21
resetState ();
16
22
}
@@ -36,52 +42,34 @@ void HttpClient::beginRequest()
36
42
iState = eRequestStarted;
37
43
}
38
44
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)
40
46
{
41
47
tHttpState initialState = iState;
42
48
if ((eIdle != iState) && (eRequestStarted != iState))
43
49
{
44
50
return HTTP_ERROR_API;
45
51
}
46
52
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
+ }
81
69
}
82
70
83
71
// 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);
85
73
if ((initialState == eIdle) && (HTTP_SUCCESS == ret))
86
74
{
87
75
// 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
92
80
return ret;
93
81
}
94
82
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)
96
84
{
97
85
#ifdef LOGGING
98
86
Serial.println (" Connected" );
@@ -104,26 +92,20 @@ int HttpClient::sendInitialHeaders(const char* aServerName, IPAddress aServerIP,
104
92
iClient->print (aURLPath);
105
93
iClient->println (" HTTP/1.1" );
106
94
// The host header, if required
107
- if (aServerName )
95
+ if (iServerName )
108
96
{
109
97
iClient->print (" Host: " );
110
- iClient->print (aServerName );
111
- if (aPort != kHttpPort )
98
+ iClient->print (iServerName );
99
+ if (iServerPort != kHttpPort )
112
100
{
113
101
iClient->print (" :" );
114
- iClient->print (aPort );
102
+ iClient->print (iServerPort );
115
103
}
116
104
iClient->println ();
117
105
}
118
106
// 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
+
127
109
// We don't support persistent connections, so tell the server to
128
110
// close this connection after we're done
129
111
sendHeader (HTTP_HEADER_CONNECTION, " close" );
0 commit comments