jellyfin-server/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs

153 lines
4.2 KiB
C#
Raw Normal View History

2014-07-19 01:28:40 +00:00
using System;
2016-06-19 16:53:43 +00:00
using System.Collections.Generic;
2014-07-19 01:28:40 +00:00
using System.IO;
using System.Net;
using MediaBrowser.Model.Logging;
using ServiceStack;
using ServiceStack.Host;
2015-01-03 19:38:22 +00:00
using HttpListenerResponse = SocketHttpListener.Net.HttpListenerResponse;
2016-10-25 19:02:04 +00:00
using IHttpResponse = MediaBrowser.Model.Services.IHttpResponse;
using IRequest = MediaBrowser.Model.Services.IRequest;
2014-07-19 01:28:40 +00:00
namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp
{
public class WebSocketSharpResponse : IHttpResponse
{
private readonly ILogger _logger;
2016-10-25 19:02:04 +00:00
private readonly HttpListenerResponse _response;
2014-07-19 01:28:40 +00:00
2016-06-19 16:53:43 +00:00
public WebSocketSharpResponse(ILogger logger, HttpListenerResponse response, IRequest request)
2014-07-19 01:28:40 +00:00
{
_logger = logger;
2016-10-25 19:02:04 +00:00
this._response = response;
2016-06-19 16:53:43 +00:00
Items = new Dictionary<string, object>();
Request = request;
2014-07-19 01:28:40 +00:00
}
2016-06-19 16:53:43 +00:00
public IRequest Request { get; private set; }
2014-07-19 01:28:40 +00:00
public bool UseBufferedStream { get; set; }
2016-06-19 16:53:43 +00:00
public Dictionary<string, object> Items { get; private set; }
2014-07-19 01:28:40 +00:00
public object OriginalResponse
{
2016-10-25 19:02:04 +00:00
get { return _response; }
2014-07-19 01:28:40 +00:00
}
public int StatusCode
{
2016-10-25 19:02:04 +00:00
get { return this._response.StatusCode; }
set { this._response.StatusCode = value; }
2014-07-19 01:28:40 +00:00
}
public string StatusDescription
{
2016-10-25 19:02:04 +00:00
get { return this._response.StatusDescription; }
set { this._response.StatusDescription = value; }
2014-07-19 01:28:40 +00:00
}
public string ContentType
{
2016-10-25 19:02:04 +00:00
get { return _response.ContentType; }
set { _response.ContentType = value; }
2014-07-19 01:28:40 +00:00
}
2016-10-25 19:02:04 +00:00
//public ICookies Cookies { get; set; }
2014-07-19 01:28:40 +00:00
public void AddHeader(string name, string value)
{
if (string.Equals(name, "Content-Type", StringComparison.OrdinalIgnoreCase))
{
ContentType = value;
return;
}
2016-10-25 19:02:04 +00:00
_response.AddHeader(name, value);
2014-07-19 01:28:40 +00:00
}
2016-06-19 16:53:43 +00:00
public string GetHeader(string name)
{
2016-10-25 19:02:04 +00:00
return _response.Headers[name];
2016-06-19 16:53:43 +00:00
}
2014-07-19 01:28:40 +00:00
public void Redirect(string url)
{
2016-10-25 19:02:04 +00:00
_response.Redirect(url);
2014-07-19 01:28:40 +00:00
}
public Stream OutputStream
{
2016-10-25 19:02:04 +00:00
get { return _response.OutputStream; }
2014-07-19 01:28:40 +00:00
}
public object Dto { get; set; }
public void Write(string text)
{
2016-10-07 15:08:13 +00:00
var bOutput = System.Text.Encoding.UTF8.GetBytes(text);
2016-10-25 19:02:04 +00:00
_response.ContentLength64 = bOutput.Length;
2014-07-19 01:28:40 +00:00
2016-10-25 19:02:04 +00:00
var outputStream = _response.OutputStream;
2016-10-07 15:08:13 +00:00
outputStream.Write(bOutput, 0, bOutput.Length);
Close();
2014-07-19 01:28:40 +00:00
}
public void Close()
{
if (!this.IsClosed)
{
this.IsClosed = true;
try
{
2016-10-25 19:02:04 +00:00
this._response.CloseOutputStream(_logger);
2014-07-19 01:28:40 +00:00
}
catch (Exception ex)
{
_logger.ErrorException("Error closing HttpListener output stream", ex);
}
}
}
public void End()
{
Close();
}
public void Flush()
{
2016-10-25 19:02:04 +00:00
_response.OutputStream.Flush();
2014-07-19 01:28:40 +00:00
}
public bool IsClosed
{
get;
private set;
}
public void SetContentLength(long contentLength)
{
//you can happily set the Content-Length header in Asp.Net
//but HttpListener will complain if you do - you have to set ContentLength64 on the response.
//workaround: HttpListener throws "The parameter is incorrect" exceptions when we try to set the Content-Length header
2016-10-25 19:02:04 +00:00
_response.ContentLength64 = contentLength;
2014-07-19 01:28:40 +00:00
}
public void SetCookie(Cookie cookie)
{
var cookieStr = cookie.AsHeaderValue();
2016-10-25 19:02:04 +00:00
_response.Headers.Add(HttpHeaders.SetCookie, cookieStr);
2014-07-19 01:28:40 +00:00
}
public bool SendChunked
{
2016-10-25 19:02:04 +00:00
get { return _response.SendChunked; }
set { _response.SendChunked = value; }
2014-07-19 01:28:40 +00:00
}
2014-11-15 15:51:49 +00:00
public bool KeepAlive { get; set; }
2016-06-19 16:53:43 +00:00
public void ClearCookies()
{
}
2014-07-19 01:28:40 +00:00
}
}