Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Fix managed RedirectLocation implementation
Browse files Browse the repository at this point in the history
Move the Windows implementation to shared and use it from managed.
  • Loading branch information
stephentoub committed May 29, 2017
1 parent 9f31883 commit a3db731
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 50 deletions.
26 changes: 26 additions & 0 deletions src/System.Net.HttpListener/src/System/Net/HttpListenerResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ public bool KeepAlive
}
}

public string RedirectLocation
{
get => Headers[HttpResponseHeader.Location];
set
{
// note that this doesn't set the status code to a redirect one
CheckDisposed();
if (string.IsNullOrEmpty(value))
{
Headers.Remove(HttpKnownHeaderNames.Location);
}
else
{
Headers.Set(HttpKnownHeaderNames.Location, value);
}
}
}

public string StatusDescription
{
get
Expand Down Expand Up @@ -123,6 +141,14 @@ public void AppendCookie(Cookie cookie)
Cookies.Add(cookie);
}

public void Redirect(string url)
{
if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"url={url}");
Headers[HttpResponseHeader.Location] = url;
StatusCode = (int)HttpStatusCode.Redirect;
StatusDescription = HttpStatusDescription.Get(StatusCode);
}

public void SetCookie(Cookie cookie)
{
if (cookie == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public sealed partial class HttpListenerResponse : IDisposable
private bool _clSet;
private HttpResponseStream _outputStream;
private Version _version = HttpVersion.Version11;
private string _location;
private int _statusCode = 200;
private bool _chunked;
private HttpListenerContext _context;
Expand Down Expand Up @@ -99,18 +98,6 @@ public Version ProtocolVersion
}
}

public string RedirectLocation
{
get => _location;
set
{
CheckDisposed();
CheckSentHeaders();

_location = value;
}
}

public bool SendChunked
{
get => _chunked;
Expand Down Expand Up @@ -185,12 +172,6 @@ public void CopyFrom(HttpListenerResponse templateResponse)
_version = templateResponse._version;
}

public void Redirect(string url)
{
StatusCode = 302; // Found
_location = url;
}

private bool FindCookie(Cookie cookie)
{
string name = cookie.Name;
Expand Down Expand Up @@ -280,9 +261,6 @@ internal void SendHeaders(bool closing, MemoryStream ms, bool isWebSocketHandsha
_webHeaders.Set(HttpKnownHeaderNames.Connection, HttpHeaderStrings.KeepAlive);
}

if (_location != null)
_webHeaders.Set(HttpKnownHeaderNames.Location, _location);

if (_cookies != null)
{
foreach (Cookie cookie in _cookies)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,6 @@ public Stream OutputStream
}
}

public string RedirectLocation
{
get => Headers[HttpResponseHeader.Location];
set
{
// note that this doesn't set the status code to a redirect one
CheckDisposed();
if (string.IsNullOrEmpty(value))
{
Headers.Remove(HttpKnownHeaderNames.Location);
}
else
{
Headers.Set(HttpKnownHeaderNames.Location, value);
}
}
}

public int StatusCode
{
get => _nativeResponse.StatusCode;
Expand Down Expand Up @@ -153,14 +135,6 @@ internal EntitySendFormat EntitySendFormat
}
}

public void Redirect(string url)
{
if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"url={url}");
Headers[HttpResponseHeader.Location] = url;
StatusCode = (int)HttpStatusCode.Redirect;
StatusDescription = HttpStatusDescription.Get(StatusCode);
}

public long ContentLength64
{
get => _contentLength;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ public async Task Redirect_Invoke_SetsRedirectionProperties(string url, int expe
}
}

// The managed implementation should set Location directly in the Headers rather than tracking it with its own field.
[ConditionalFact(nameof(Helpers) + "." + nameof(Helpers.IsWindowsImplementation))] // [ActiveIssue(19971, TestPlatforms.AnyUnix)]
[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))]
public async Task Redirect_Disposed_ThrowsObjectDisposedException()
{
HttpListenerResponse response = await GetResponse();
Expand Down

0 comments on commit a3db731

Please sign in to comment.