8000 Add connection keep alive support · sandeepmistry/ArduinoHttpClient@bfedff8 · GitHub
[go: up one dir, main page]

Skip to content

Commit bfedff8

Browse files
committed
Add connection keep alive support
1 parent 33804d4 commit bfedff8

File tree

2 files changed

+64
-20
lines changed

2 files changed

+64
-20
lines changed

HttpClient.cpp

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const char* HttpClient::kUserAgent = "Arduino/2.2.0";
1010
const char* HttpClient::kContentLengthPrefix = HTTP_HEADER_CONTENT_LENGTH ": ";
1111

1212
HttpClient::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

2324
HttpClient::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+
4552
void HttpClient::beginRequest()
4653
{
4754
iState = eRequestStarted;
4855
}
4956

5057
int 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+
197234
void HttpClient::endRequest()
198235
{
199236
if (iState < eRequestSent)

HttpClient.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class HttpClient : public Client
4848
HttpClient(Client& aClient, const String& aServerName, uint16_t aServerPort = kHttpPort);
4949
HttpClient(Client& aClient, const IPAddress& aServerAddress, uint16_t aServerPort = kHttpPort);
5050

51+
void connectionKeepAlive();
52+
5153
/** Start a more complex request.
5254
Use this when you need to send additional headers in the request,
5355
but you will also need to call endRequest() when you are finished.
@@ -247,6 +249,10 @@ class HttpClient : public Client
247249
*/
248250
void finishHeaders();
249251

252+
/** Reading any pending data from the client (used in connection keep alive mode)
253+
*/
254+
void flushClientRx();
255+
250256
// Number of milliseconds that we wait each time there isn't any data
251257
// available to be read (during status code and header processing)
252258
static const int kHttpWaitForDataDelay = 1000;
@@ -284,6 +290,7 @@ class HttpClient : public Client
284290
// How far through a Content-Length header prefix we are
285291
const char* iContentLengthPtr;
286292
uint32_t iHttpResponseTimeout;
293+
bool iConnectionClose;
287294
String iHeaderLine;
288295
};
289296

0 commit comments

Comments
 (0)
0