@@ -10,7 +10,8 @@ const char* HttpClient::kUserAgent = "Arduino/2.2.0";
10
10
const char * HttpClient::kContentLengthPrefix = HTTP_HEADER_CONTENT_LENGTH " : " ;
11
11
12
12
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 )
14
15
{
15
16
resetState ();
16
17
}
@@ -21,7 +22,8 @@ HttpClient::HttpClient(Client& aClient, const String& aServerName, uint16_t aSer
21
22
}
22
23
23
24
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 )
25
27
{
26
28
resetState ();
27
29
}
@@ -42,35 +44,56 @@ void HttpClient::stop()
42
44
resetState ();
43
45
}
44
46
47
+ void HttpClient::connectionKeepAlive ()
48
+ {
49
+ iConnectionClose = false ;
50
+ }
51
+
45
52
void HttpClient::beginRequest ()
46
53
{
47
54
iState = eRequestStarted;
48
55
}
49
56
50
57
int HttpClient::startRequest (const char * aURLPath, const char * aHttpMethod)
51
58
{
59
+ if (!iConnectionClose)
60
+ {
61
+ flushClientRx ();
62
+
63
+ resetState ();
64
+ }
65
+
52
66
tHttpState initialState = iState;
53
67
if ((eIdle != iState) && (eRequestStarted != iState))
54
68
{
55
69
return HTTP_ERROR_API;
56
70
}
57
71
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
+ }
65
90
}
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
74
97
}
75
98
76
99
// Now we're connected, send the first part of the request
@@ -111,9 +134,12 @@ int HttpClient::sendInitialHeaders(const char* aURLPath, const char* aHttpMethod
111
134
// And user-agent string
112
135
sendHeader (HTTP_HEADER_USER_AGENT, kUserAgent );
113
136
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
+ }
117
143
118
144
// Everything has gone well
119
145
iState = eRequestStarted;
@@ -194,6 +220,17 @@ void HttpClient::finishHeaders()
194
220
iState = eRequestSent;
195
221
}
196
222
223
+ void HttpClient::flushClientRx ()
224
+ {
225
+ if (iClient->connected ())
226
+ {
227
+ while (iClient->available ())
228
+ {
229
+ iClient->read ();
230
+ }
231
+ }
232
+ }
233
+
197
234
void HttpClient::endRequest ()
198
235
{
199
236
if (iState < eRequestSent)
0 commit comments