@@ -10,7 +10,8 @@ const char* HttpClient::kUserAgent = "Arduino/2.2.0";
1010const char * HttpClient::kContentLengthPrefix = HTTP_HEADER_CONTENT_LENGTH " : " ;
1111
1212HttpClient::HttpClient (Client& aClient, const char * aServerName, uint16_t aServerPort)
13- : iClient(&aClient), iServerName(aServerName), iServerAddress(), iServerPort(aServerPort)
13+ : iClient(&aClient), iServerName(aServerName), iServerAddress(), iServerPort(aServerPort),
14+ iConnectionClose(true )
1415{
1516 resetState ();
1617}
@@ -21,7 +22,8 @@ HttpClient::HttpClient(Client& aClient, const String& aServerName, uint16_t aSer
2122}
2223
2324HttpClient::HttpClient (Client& aClient, const IPAddress& aServerAddress, uint16_t aServerPort)
24- : iClient(&aClient), iServerName(NULL ), iServerAddress(aServerAddress), iServerPort(aServerPort)
25+ : iClient(&aClient), iServerName(NULL ), iServerAddress(aServerAddress), iServerPort(aServerPort),
26+ iConnectionClose(true )
2527{
2628 resetState ();
2729}
@@ -42,35 +44,56 @@ void HttpClient::stop()
4244 resetState ();
4345}
4446
47+ void HttpClient::connectionKeepAlive ()
48+ {
49+ iConnectionClose = false ;
50+ }
51+
4552void HttpClient::beginRequest ()
4653{
4754 iState = eRequestStarted;
4855}
4956
5057int HttpClient::startRequest (const char * aURLPath, const char * aHttpMethod)
5158{
59+ if (!iConnectionClose)
60+ {
61+ flushClientRx ();
62+
63+ resetState ();
64+ }
65+
5266 tHttpState initialState = iState;
5367 if ((eIdle != iState) && (eRequestStarted != iState))
5468 {
5569 return HTTP_ERROR_API;
5670 }
5771
58- if (iServerName) {
59- if (!iClient->connect (iServerName, iServerPort) > 0 )
60- {
61- #ifdef LOGGING
62- Serial.println (" Connection failed" );
63- #endif
64- return HTTP_ERROR_CONNECTION_FAILED;
72+ if (iConnectionClose || !iClient->connected ())
73+ {
74+ if (iServerName) {
75+ if (!iClient->connect (iServerName, iServerPort) > 0 )
76+ {
77+ #ifdef LOGGING
78+ Serial.println (" Connection failed" );
79+ #endif
80+ return HTTP_ERROR_CONNECTION_FAILED;
81+ }
82+ } else {
83+ if (!iClient->connect (iServerAddress, iServerPort) > 0 )
84+ {
85+ #ifdef LOGGING
86+ Serial.println (" Connection failed" );
87+ #endif
88+ return HTTP_ERROR_CONNECTION_FAILED;
89+ }
6590 }
66- } else {
67- if (!iClient->connect (iServerAddress, iServerPort) > 0 )
68- {
69- #ifdef LOGGING
70- Serial.println (" Connection failed" );
71- #endif
72- return HTTP_ERROR_CONNECTION_FAILED;
73- }
91+ }
92+ else
93+ {
94+ #ifdef LOGGING
95+ Serial.println (" Connection already open" );
96+ #endif
7497 }
7598
7699 // Now we're connected, send the first part of the request
@@ -111,9 +134,12 @@ int HttpClient::sendInitialHeaders(const char* aURLPath, const char* aHttpMethod
111134 // And user-agent string
112135 sendHeader (HTTP_HEADER_USER_AGENT, kUserAgent );
113136
114- // We don't support persistent connections, so tell the server to
115- // close this connection after we're done
116- sendHeader (HTTP_HEADER_CONNECTION, " close" );
137+ if (iConnectionClose)
138+ {
139+ // Tell the server to
140+ // close this connection after we're done
141+ sendHeader (HTTP_HEADER_CONNECTION, " close" );
142+ }
117143
118144 // Everything has gone well
119145 iState = eRequestStarted;
@@ -194,6 +220,17 @@ void HttpClient::finishHeaders()
194220 iState = eRequestSent;
195221}
196222
223+ void HttpClient::flushClientRx ()
224+ {
225+ if (iClient->connected ())
226+ {
227+ while (iClient->available ())
228+ {
229+ iClient->read ();
230+ }
231+ }
232+ }
233+
197234void HttpClient::endRequest ()
198235{
199236 if (iState < eRequestSent)
0 commit comments