Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 5bcedee

Browse files
authored
Make HttpWebResponse.LastModified throw when invalid date is provided (#29006)
* make HttpWebResponse.LastModified throw when invalid date is provided * apply feedback * Update HttpDateParse.cs
1 parent b703f31 commit 5bcedee

File tree

4 files changed

+41
-20
lines changed

4 files changed

+41
-20
lines changed

src/System.Net.Requests/src/System/Net/HttpDateParse.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,5 +398,8 @@ public static bool ParseHttpDate(string dateString, out DateTime result)
398398

399399
return true;
400400
}
401+
402+
public static DateTime StringToDate(string s) =>
403+
HttpDateParse.ParseHttpDate(s, out DateTime dtOut) ? dtOut : throw new ProtocolViolationException(SR.net_baddate);
401404
}
402405
}

src/System.Net.Requests/src/System/Net/HttpWebRequest.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ private DateTime GetDateHeaderHelper(string headerName)
14501450
{
14511451
return DateTime.MinValue; // MinValue means header is not present
14521452
}
1453-
return StringToDate(headerValue);
1453+
return HttpDateParse.StringToDate(headerValue);
14541454
#if DEBUG
14551455
}
14561456
#endif
@@ -1471,20 +1471,6 @@ private void SetDateHeaderHelper(string headerName, DateTime dateTime)
14711471
#endif
14721472
}
14731473

1474-
// parse String to DateTime format.
1475-
private static DateTime StringToDate(String S)
1476-
{
1477-
DateTime dtOut;
1478-
if (HttpDateParse.ParseHttpDate(S, out dtOut))
1479-
{
1480-
return dtOut;
1481-
}
1482-
else
1483-
{
1484-
throw new ProtocolViolationException(SR.net_baddate);
1485-
}
1486-
}
1487-
14881474
// convert Date to String using RFC 1123 pattern
14891475
private static string DateToString(DateTime D)
14901476
{

src/System.Net.Requests/src/System/Net/HttpWebResponse.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,11 @@ public DateTime LastModified
150150
{
151151
return DateTime.Now;
152152
}
153-
DateTime dtOut;
154-
HttpDateParse.ParseHttpDate(lastmodHeaderValue, out dtOut);
155-
return dtOut;
153+
154+
return HttpDateParse.StringToDate(lastmodHeaderValue);
156155
}
157156
}
158157

159-
160158
/// <devdoc>
161159
/// <para>
162160
/// Gets the name of the server that sent the response.

src/System.Net.Requests/tests/HttpWebResponseHeaderTest.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,40 @@ await LoopbackServer.CreateServerAsync(async (server, url) =>
9494
});
9595
}
9696

97+
[Fact]
98+
public async Task LastModified_ValidDate_Success()
99+
{
100+
DateTime expected = TimeZoneInfo.ConvertTimeFromUtc(new DateTime(2018, 4, 10, 3, 4, 5, DateTimeKind.Utc), TimeZoneInfo.Local);
101+
await LoopbackServer.CreateServerAsync(async (server, url) =>
102+
{
103+
HttpWebRequest request = WebRequest.CreateHttp(url);
104+
Task<WebResponse> getResponse = request.GetResponseAsync();
105+
await server.AcceptConnectionSendResponseAndCloseAsync(HttpStatusCode.OK, "Last-Modified: Tue, 10 Apr 2018 03:04:05 GMT\r\n", "12345");
106+
107+
using (HttpWebResponse response = (HttpWebResponse)(await getResponse))
108+
{
109+
Assert.Equal(expected, response.LastModified);
110+
}
111+
});
112+
}
113+
114+
[Fact]
115+
public async Task LastModified_InvalidDate_Throws()
116+
{
117+
DateTime expected = TimeZoneInfo.ConvertTimeFromUtc(new DateTime(2018, 4, 10, 3, 4, 5, DateTimeKind.Utc), TimeZoneInfo.Local);
118+
await LoopbackServer.CreateServerAsync(async (server, url) =>
119+
{
120+
HttpWebRequest request = WebRequest.CreateHttp(url);
121+
Task<WebResponse> getResponse = request.GetResponseAsync();
122+
await server.AcceptConnectionSendResponseAndCloseAsync(HttpStatusCode.OK, "Last-Modified: invalid date\r\n", "12345");
123+
124+
using (HttpWebResponse response = (HttpWebResponse)(await getResponse))
125+
{
126+
Assert.Throws<ProtocolViolationException>(() => response.LastModified);
127+
}
128+
});
129+
}
130+
97131
[Fact]
98132
public async Task HttpWebResponse_Serialize_ExpectedResult()
99133
{
@@ -125,6 +159,6 @@ await LoopbackServer.CreateServerAsync(async (server, url) =>
125159
}
126160
}
127161
});
128-
}
162+
}
129163
}
130164
}

0 commit comments

Comments
 (0)