jellyfin/SocketHttpListener/Net/HttpListenerResponse.cs

295 lines
8.8 KiB
C#
Raw Normal View History

using System;
2016-11-11 20:55:12 +01:00
using System.IO;
using System.Net;
using System.Text;
namespace SocketHttpListener.Net
{
2019-01-25 23:38:25 +01:00
public sealed partial class HttpListenerResponse : IDisposable
2016-11-11 20:55:12 +01:00
{
2017-06-15 19:22:05 +02:00
private BoundaryType _boundaryType = BoundaryType.None;
private CookieCollection _cookies;
private HttpListenerContext _httpContext;
private bool _keepAlive = true;
private HttpResponseStream _responseStream;
private string _statusDescription;
private WebHeaderCollection _webHeaders = new WebHeaderCollection();
2017-05-25 15:00:14 +02:00
public WebHeaderCollection Headers => _webHeaders;
2016-11-11 20:55:12 +01:00
2017-06-15 19:22:05 +02:00
public Encoding ContentEncoding { get; set; }
2016-11-11 20:55:12 +01:00
public string ContentType
{
get => Headers["Content-Type"];
2016-11-11 20:55:12 +01:00
set
{
2017-06-15 19:22:05 +02:00
CheckDisposed();
if (string.IsNullOrEmpty(value))
{
Headers.Remove("Content-Type");
}
else
{
Headers.Set("Content-Type", value);
}
2016-11-11 20:55:12 +01:00
}
}
private HttpListenerContext HttpListenerContext => _httpContext;
2016-11-11 20:55:12 +01:00
private HttpListenerRequest HttpListenerRequest => HttpListenerContext.Request;
2016-11-11 20:55:12 +01:00
2017-06-15 19:22:05 +02:00
internal EntitySendFormat EntitySendFormat
2016-11-11 20:55:12 +01:00
{
get => (EntitySendFormat)_boundaryType;
2016-11-11 20:55:12 +01:00
set
{
2017-06-15 19:22:05 +02:00
CheckDisposed();
CheckSentHeaders();
if (value == EntitySendFormat.Chunked && HttpListenerRequest.ProtocolVersion.Minor == 0)
{
throw new ProtocolViolationException("net_nochunkuploadonhttp10");
}
_boundaryType = (BoundaryType)value;
if (value != EntitySendFormat.ContentLength)
{
_contentLength = -1;
}
2016-11-11 20:55:12 +01:00
}
}
2017-06-15 19:22:05 +02:00
public bool SendChunked
2016-11-11 20:55:12 +01:00
{
get => EntitySendFormat == EntitySendFormat.Chunked;
set => EntitySendFormat = value ? EntitySendFormat.Chunked : EntitySendFormat.ContentLength;
2016-11-11 20:55:12 +01:00
}
2017-06-15 19:22:05 +02:00
// We MUST NOT send message-body when we send responses with these Status codes
private static readonly int[] s_noResponseBody = { 100, 101, 204, 205, 304 };
private static bool CanSendResponseBody(int responseCode)
2016-11-11 20:55:12 +01:00
{
2017-06-15 19:22:05 +02:00
for (int i = 0; i < s_noResponseBody.Length; i++)
2016-11-11 20:55:12 +01:00
{
2017-06-15 19:22:05 +02:00
if (responseCode == s_noResponseBody[i])
{
return false;
}
2016-11-11 20:55:12 +01:00
}
2017-06-15 19:22:05 +02:00
return true;
2016-11-11 20:55:12 +01:00
}
2017-06-15 19:22:05 +02:00
public long ContentLength64
2016-11-11 20:55:12 +01:00
{
get => _contentLength;
2016-11-11 20:55:12 +01:00
set
{
2017-06-15 19:22:05 +02:00
CheckDisposed();
CheckSentHeaders();
if (value >= 0)
{
_contentLength = value;
_boundaryType = BoundaryType.ContentLength;
}
else
{
throw new ArgumentOutOfRangeException(nameof(value));
2017-06-15 19:22:05 +02:00
}
2016-11-11 20:55:12 +01:00
}
}
2017-06-15 19:22:05 +02:00
public CookieCollection Cookies
2016-11-11 20:55:12 +01:00
{
get => _cookies ?? (_cookies = new CookieCollection());
set => _cookies = value;
2016-11-11 20:55:12 +01:00
}
2017-06-15 19:22:05 +02:00
public bool KeepAlive
2016-11-11 20:55:12 +01:00
{
get => _keepAlive;
2016-11-11 20:55:12 +01:00
set
{
2017-06-15 19:22:05 +02:00
CheckDisposed();
_keepAlive = value;
2016-11-11 20:55:12 +01:00
}
}
2017-06-15 19:22:05 +02:00
public Stream OutputStream
2016-11-11 20:55:12 +01:00
{
2017-06-15 19:22:05 +02:00
get
2016-11-11 20:55:12 +01:00
{
2017-06-15 19:22:05 +02:00
CheckDisposed();
EnsureResponseStream();
return _responseStream;
2016-11-11 20:55:12 +01:00
}
}
2017-06-15 19:22:05 +02:00
public string RedirectLocation
2016-11-11 20:55:12 +01:00
{
get => Headers["Location"];
2016-11-11 20:55:12 +01:00
set
{
2017-06-15 19:22:05 +02:00
// note that this doesn't set the status code to a redirect one
CheckDisposed();
if (string.IsNullOrEmpty(value))
{
Headers.Remove("Location");
}
else
{
Headers.Set("Location", value);
}
2016-11-11 20:55:12 +01:00
}
}
2017-06-15 19:22:05 +02:00
public string StatusDescription
2017-05-25 15:00:14 +02:00
{
2017-06-15 19:22:05 +02:00
get
2017-05-25 15:00:14 +02:00
{
2017-06-15 19:22:05 +02:00
if (_statusDescription == null)
2017-05-25 15:00:14 +02:00
{
2017-06-15 19:22:05 +02:00
// if the user hasn't set this, generated on the fly, if possible.
// We know this one is safe, no need to verify it as in the setter.
_statusDescription = HttpStatusDescription.Get(StatusCode);
2017-05-25 15:00:14 +02:00
}
2017-06-15 19:22:05 +02:00
if (_statusDescription == null)
2017-05-25 15:00:14 +02:00
{
2017-06-15 19:22:05 +02:00
_statusDescription = string.Empty;
2017-05-25 15:00:14 +02:00
}
2017-06-15 19:22:05 +02:00
return _statusDescription;
2017-05-25 15:00:14 +02:00
}
2017-06-15 19:22:05 +02:00
set
2017-05-25 15:00:14 +02:00
{
2017-06-15 19:22:05 +02:00
CheckDisposed();
if (value == null)
2017-05-25 15:00:14 +02:00
{
2017-06-15 19:22:05 +02:00
throw new ArgumentNullException(nameof(value));
}
// Need to verify the status description doesn't contain any control characters except HT. We mask off the high
// byte since that's how it's encoded.
for (int i = 0; i < value.Length; i++)
{
char c = (char)(0x000000ff & (uint)value[i]);
if ((c <= 31 && c != (byte)'\t') || c == 127)
2017-05-25 15:00:14 +02:00
{
2017-06-15 19:22:05 +02:00
throw new ArgumentException("net_WebHeaderInvalidControlChars");
2017-05-25 15:00:14 +02:00
}
2017-06-15 19:22:05 +02:00
}
_statusDescription = value;
2017-05-25 15:00:14 +02:00
}
}
2017-06-15 19:22:05 +02:00
public void AddHeader(string name, string value)
2016-11-11 20:55:12 +01:00
{
2017-06-15 19:22:05 +02:00
Headers.Set(name, value);
2016-11-11 20:55:12 +01:00
}
2017-06-15 19:22:05 +02:00
public void AppendHeader(string name, string value)
2016-11-11 20:55:12 +01:00
{
2017-06-15 19:22:05 +02:00
Headers.Add(name, value);
2016-11-11 20:55:12 +01:00
}
2017-06-15 19:22:05 +02:00
public void AppendCookie(Cookie cookie)
2016-11-11 20:55:12 +01:00
{
2017-06-15 19:22:05 +02:00
if (cookie == null)
2016-11-11 20:55:12 +01:00
{
2017-06-15 19:22:05 +02:00
throw new ArgumentNullException(nameof(cookie));
2016-11-11 20:55:12 +01:00
}
2017-06-15 19:22:05 +02:00
Cookies.Add(cookie);
2016-11-11 20:55:12 +01:00
}
2017-06-15 19:22:05 +02:00
private void ComputeCookies()
2016-11-28 06:38:41 +01:00
{
2017-06-15 19:22:05 +02:00
if (_cookies != null)
2016-11-28 06:38:41 +01:00
{
2017-06-15 19:22:05 +02:00
// now go through the collection, and concatenate all the cookies in per-variant strings
//string setCookie2 = null, setCookie = null;
//for (int index = 0; index < _cookies.Count; index++)
//{
// Cookie cookie = _cookies[index];
// string cookieString = cookie.ToServerString();
// if (cookieString == null || cookieString.Length == 0)
// {
// continue;
// }
// if (cookie.IsRfc2965Variant())
// {
// setCookie2 = setCookie2 == null ? cookieString : setCookie2 + ", " + cookieString;
// }
// else
// {
// setCookie = setCookie == null ? cookieString : setCookie + ", " + cookieString;
// }
//}
//if (!string.IsNullOrEmpty(setCookie))
//{
// Headers.Set(HttpKnownHeaderNames.SetCookie, setCookie);
// if (string.IsNullOrEmpty(setCookie2))
// {
// Headers.Remove(HttpKnownHeaderNames.SetCookie2);
// }
//}
//if (!string.IsNullOrEmpty(setCookie2))
//{
// Headers.Set(HttpKnownHeaderNames.SetCookie2, setCookie2);
// if (string.IsNullOrEmpty(setCookie))
// {
// Headers.Remove(HttpKnownHeaderNames.SetCookie);
// }
//}
2016-11-28 06:38:41 +01:00
}
}
2017-06-15 19:22:05 +02:00
public void Redirect(string url)
2016-11-11 20:55:12 +01:00
{
2017-06-15 19:22:05 +02:00
Headers["Location"] = url;
StatusCode = (int)HttpStatusCode.Redirect;
StatusDescription = "Found";
}
2016-11-11 20:55:12 +01:00
2017-06-15 19:22:05 +02:00
public void SetCookie(Cookie cookie)
{
if (cookie == null)
2016-11-11 20:55:12 +01:00
{
2017-06-15 19:22:05 +02:00
throw new ArgumentNullException(nameof(cookie));
2016-11-11 20:55:12 +01:00
}
2017-06-15 19:22:05 +02:00
//Cookie newCookie = cookie.Clone();
//int added = Cookies.InternalAdd(newCookie, true);
2016-11-11 20:55:12 +01:00
2017-06-15 19:22:05 +02:00
//if (added != 1)
2016-11-11 20:55:12 +01:00
//{
2017-06-15 19:22:05 +02:00
// // The Cookie already existed and couldn't be replaced.
// throw new ArgumentException("Cookie exists");
2016-11-11 20:55:12 +01:00
//}
2017-06-15 19:22:05 +02:00
}
2016-11-11 20:55:12 +01:00
2017-06-15 19:40:38 +02:00
void IDisposable.Dispose()
{
Dispose();
}
2017-05-05 19:55:38 +02:00
2017-06-15 19:22:05 +02:00
private void CheckDisposed()
{
if (Disposed)
2016-11-11 20:55:12 +01:00
{
2017-06-15 19:22:05 +02:00
throw new ObjectDisposedException(GetType().FullName);
2016-11-11 20:55:12 +01:00
}
}
2017-06-15 19:22:05 +02:00
private void CheckSentHeaders()
2016-11-11 20:55:12 +01:00
{
2017-06-15 19:22:05 +02:00
if (SentHeaders)
2016-11-11 20:55:12 +01:00
{
2017-06-15 19:22:05 +02:00
throw new InvalidOperationException();
2016-11-11 20:55:12 +01:00
}
2017-03-12 20:27:26 +01:00
}
2016-11-11 20:55:12 +01:00
}
2017-06-15 19:22:05 +02:00
}