Skip to content

Commit 1727c19

Browse files
authored
Adjusting to connection close as default for HttpClient (#396)
1 parent 6848544 commit 1727c19

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

nanoFramework.System.Net.Http/Http/Headers/HttpRequestHeaders.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,20 @@ public bool ConnectionClose
4343
{
4444
get
4545
{
46-
return Connection.Contains("close");
46+
return Connection.ToLower().Contains("close");
4747
}
4848

4949
set
5050
{
51-
throw new NotImplementedException();
51+
var connectionHeader = _headerStore.GetValues(HttpKnownHeaderNames.Connection);
52+
if (connectionHeader is not null)
53+
{
54+
connectionHeader[0] = value ? "close" : "keep-alive";
55+
}
56+
else
57+
{
58+
_headerStore.AddInternal(HttpKnownHeaderNames.Connection, value ? "close" : "keep-alive");
59+
}
5260
}
5361
}
5462

nanoFramework.System.Net.Http/Http/HttpClient.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ public HttpClient() : base(
140140
true)
141141
{
142142
_timeout = Threading.Timeout.InfiniteTimeSpan;
143+
144+
// The default in REST API is to close the connection after each request.
145+
DefaultRequestHeaders.ConnectionClose = true;
143146
}
144147

145148
#endregion Constructors

nanoFramework.System.Net.Http/Http/HttpClientHandler.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ protected internal override HttpResponseMessage Send(HttpRequestMessage request)
262262
{
263263
throw new HttpRequestException("An error occurred while sending the request", ex);
264264
}
265-
265+
266266
return CreateResponseMessage(wresponse, request);
267267
}
268268

@@ -348,7 +348,8 @@ HttpResponseMessage CreateResponseMessage(HttpWebResponse wr, HttpRequestMessage
348348

349349
bool GetConnectionKeepAlive(HttpRequestHeaders headers)
350350
{
351-
return headers.Connection.Equals("Keep-Alive");
351+
// In theory, the value should be lower case but it can with upper case.
352+
return headers.Connection.ToLower().Equals("keep-alive");
352353
}
353354

354355
internal void EnsureModifiability()

nanoFramework.System.Net.Http/Http/System.Net.HttpWebRequest.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,14 +1240,16 @@ private void PrepareHeaders()
12401240
// Otherwise: we send "Connection:Close" or "Connection:Keep-Alive"
12411241
if (m_httpRequestHeaders[HttpKnownHeaderNames.Connection] == null)
12421242
{
1243-
string connectionValue;
1243+
string connectionValue;
12441244
if (m_keepAlive)
12451245
{
1246-
connectionValue = "Keep-Alive";
1246+
// According to RFC, should be lower case
1247+
connectionValue = "keep-alive";
12471248
}
12481249
else
12491250
{
1250-
connectionValue = "Close";
1251+
// According to RFC, should be lower case
1252+
connectionValue = "close";
12511253
}
12521254

12531255
m_httpRequestHeaders.ChangeInternal(HttpKnownHeaderNames.Connection, connectionValue);

0 commit comments

Comments
 (0)