aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Emby.Server.Core/ApplicationHost.cs8
-rw-r--r--Emby.Server.Implementations/HttpServer/HttpListenerHost.cs17
-rw-r--r--Emby.Server.Implementations/HttpServer/HttpResultFactory.cs91
-rw-r--r--Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs7
-rw-r--r--Emby.Server.Implementations/HttpServer/StreamWriter.cs12
-rw-r--r--MediaBrowser.Api/MediaBrowser.Api.csproj1
-rw-r--r--MediaBrowser.Api/Playback/StaticRemoteStreamWriter.cs12
-rw-r--r--MediaBrowser.Api/TestService.cs77
-rw-r--r--MediaBrowser.Model/Services/IHttpResult.cs5
-rw-r--r--MediaBrowser.Model/Services/IRequest.cs16
-rw-r--r--ServiceStack/Host/ContentTypes.cs20
-rw-r--r--ServiceStack/Host/HttpResponseStreamWrapper.cs95
-rw-r--r--ServiceStack/Host/ServiceController.cs3
-rw-r--r--ServiceStack/HttpResponseExtensionsInternal.cs90
-rw-r--r--ServiceStack/HttpResult.cs193
-rw-r--r--ServiceStack/ServiceStack.csproj1
-rw-r--r--SocketHttpListener.Portable/Net/EndPointListener.cs3
-rw-r--r--SocketHttpListener.Portable/Net/HttpConnection.cs10
-rw-r--r--SocketHttpListener.Portable/Net/HttpListener.cs11
-rw-r--r--SocketHttpListener.Portable/Net/HttpListenerContext.cs1
-rw-r--r--SocketHttpListener.Portable/Net/ResponseStream.cs37
21 files changed, 174 insertions, 536 deletions
diff --git a/Emby.Server.Core/ApplicationHost.cs b/Emby.Server.Core/ApplicationHost.cs
index 9e0aee325..0c0ef894e 100644
--- a/Emby.Server.Core/ApplicationHost.cs
+++ b/Emby.Server.Core/ApplicationHost.cs
@@ -554,7 +554,7 @@ namespace Emby.Server.Core
ZipClient = new ZipClient(FileSystemManager);
RegisterSingleInstance(ZipClient);
- RegisterSingleInstance<IHttpResultFactory>(new HttpResultFactory(LogManager, FileSystemManager, JsonSerializer, XmlSerializer));
+ RegisterSingleInstance<IHttpResultFactory>(new HttpResultFactory(LogManager, FileSystemManager, JsonSerializer, MemoryStreamFactory));
RegisterSingleInstance<IServerApplicationHost>(this);
RegisterSingleInstance<IServerApplicationPaths>(ApplicationPaths);
@@ -614,6 +614,9 @@ namespace Emby.Server.Core
RegisterSingleInstance<ISearchEngine>(() => new SearchEngine(LogManager, LibraryManager, UserManager));
+ CertificatePath = GetCertificatePath(true);
+ Certificate = GetCertificate(CertificatePath);
+
HttpServer = HttpServerFactory.CreateServer(this, LogManager, ServerConfigurationManager, NetworkManager, MemoryStreamFactory, "Emby", "web/index.html", textEncoding, SocketFactory, CryptographyProvider, JsonSerializer, XmlSerializer, EnvironmentInfo, Certificate);
HttpServer.GlobalResponse = LocalizationManager.GetLocalizedString("StartupEmbyServerIsLoading");
RegisterSingleInstance(HttpServer, false);
@@ -995,9 +998,6 @@ namespace Emby.Server.Core
/// </summary>
private void StartServer()
{
- CertificatePath = GetCertificatePath(true);
- Certificate = GetCertificate(CertificatePath);
-
try
{
ServerManager.Start(GetUrlPrefixes());
diff --git a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs
index 64f498b12..49c664eec 100644
--- a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs
+++ b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs
@@ -86,9 +86,7 @@ namespace Emby.Server.Implementations.HttpServer
public string GlobalResponse { get; set; }
- public override void Configure()
- {
- var mapExceptionToStatusCode = new Dictionary<Type, int>
+ readonly Dictionary<Type, int> _mapExceptionToStatusCode = new Dictionary<Type, int>
{
{typeof (InvalidOperationException), 500},
{typeof (NotImplementedException), 500},
@@ -102,6 +100,8 @@ namespace Emby.Server.Implementations.HttpServer
{typeof (NotSupportedException), 500}
};
+ public override void Configure()
+ {
var requestFilters = _appHost.GetExports<IRequestFilter>().ToList();
foreach (var filter in requestFilters)
{
@@ -240,7 +240,12 @@ namespace Emby.Server.Implementations.HttpServer
return;
}
- httpRes.StatusCode = 500;
+ int statusCode;
+ if (!_mapExceptionToStatusCode.TryGetValue(ex.GetType(), out statusCode))
+ {
+ statusCode = 500;
+ }
+ httpRes.StatusCode = statusCode;
httpRes.ContentType = "text/html";
httpRes.Write(ex.Message);
@@ -518,6 +523,10 @@ namespace Emby.Server.Implementations.HttpServer
{
await handler.ProcessRequestAsync(httpReq, httpRes, operationName).ConfigureAwait(false);
}
+ else
+ {
+ ErrorHandler(new FileNotFoundException(), httpReq);
+ }
}
catch (Exception ex)
{
diff --git a/Emby.Server.Implementations/HttpServer/HttpResultFactory.cs b/Emby.Server.Implementations/HttpServer/HttpResultFactory.cs
index bbd556661..f65331ec7 100644
--- a/Emby.Server.Implementations/HttpServer/HttpResultFactory.cs
+++ b/Emby.Server.Implementations/HttpServer/HttpResultFactory.cs
@@ -34,19 +34,16 @@ namespace Emby.Server.Implementations.HttpServer
private readonly ILogger _logger;
private readonly IFileSystem _fileSystem;
private readonly IJsonSerializer _jsonSerializer;
- private readonly IXmlSerializer _xmlSerializer;
+ private readonly IMemoryStreamFactory _memoryStreamFactory;
/// <summary>
/// Initializes a new instance of the <see cref="HttpResultFactory" /> class.
/// </summary>
- /// <param name="logManager">The log manager.</param>
- /// <param name="fileSystem">The file system.</param>
- /// <param name="jsonSerializer">The json serializer.</param>
- public HttpResultFactory(ILogManager logManager, IFileSystem fileSystem, IJsonSerializer jsonSerializer, IXmlSerializer xmlSerializer)
+ public HttpResultFactory(ILogManager logManager, IFileSystem fileSystem, IJsonSerializer jsonSerializer, IMemoryStreamFactory memoryStreamFactory)
{
_fileSystem = fileSystem;
_jsonSerializer = jsonSerializer;
- _xmlSerializer = xmlSerializer;
+ _memoryStreamFactory = memoryStreamFactory;
_logger = logManager.GetLogger("HttpResultFactory");
}
@@ -59,17 +56,13 @@ namespace Emby.Server.Implementations.HttpServer
/// <returns>System.Object.</returns>
public object GetResult(object content, string contentType, IDictionary<string, string> responseHeaders = null)
{
- return GetHttpResult(content, contentType, responseHeaders);
+ return GetHttpResult(content, contentType, true, responseHeaders);
}
/// <summary>
/// Gets the HTTP result.
/// </summary>
- /// <param name="content">The content.</param>
- /// <param name="contentType">Type of the content.</param>
- /// <param name="responseHeaders">The response headers.</param>
- /// <returns>IHasHeaders.</returns>
- private IHasHeaders GetHttpResult(object content, string contentType, IDictionary<string, string> responseHeaders = null)
+ private IHasHeaders GetHttpResult(object content, string contentType, bool addCachePrevention, IDictionary<string, string> responseHeaders = null)
{
IHasHeaders result;
@@ -98,7 +91,7 @@ namespace Emby.Server.Implementations.HttpServer
}
else
{
- result = new HttpResult(content, contentType);
+ result = new HttpResult(content, contentType, HttpStatusCode.OK);
}
}
}
@@ -107,7 +100,11 @@ namespace Emby.Server.Implementations.HttpServer
responseHeaders = new Dictionary<string, string>();
}
- responseHeaders["Expires"] = "-1";
+ if (addCachePrevention)
+ {
+ responseHeaders["Expires"] = "-1";
+ }
+
AddResponseHeaders(result, responseHeaders);
return result;
@@ -184,8 +181,6 @@ namespace Emby.Server.Implementations.HttpServer
/// <returns></returns>
public object ToOptimizedResult<T>(IRequest request, T dto)
{
- request.Response.Dto = dto;
-
var compressionType = GetCompressionType(request);
if (compressionType == null)
{
@@ -204,6 +199,7 @@ namespace Emby.Server.Implementations.HttpServer
}
}
+ // Do not use the memoryStreamFactory here, they don't place nice with compression
using (var ms = new MemoryStream())
{
using (var compressionStream = GetCompressionStream(ms, compressionType))
@@ -213,12 +209,9 @@ namespace Emby.Server.Implementations.HttpServer
var compressedBytes = ms.ToArray();
- var httpResult = new HttpResult(compressedBytes, request.ResponseContentType)
- {
- Status = request.Response.StatusCode
- };
+ var httpResult = new StreamWriter(compressedBytes, request.ResponseContentType, _logger);
- httpResult.Headers["Content-Length"] = compressedBytes.Length.ToString(UsCulture);
+ //httpResult.Headers["Content-Length"] = compressedBytes.Length.ToString(UsCulture);
httpResult.Headers["Content-Encoding"] = compressionType;
return httpResult;
@@ -226,6 +219,16 @@ namespace Emby.Server.Implementations.HttpServer
}
}
+ private static Stream GetCompressionStream(Stream outputStream, string compressionType)
+ {
+ if (compressionType == "deflate")
+ return new DeflateStream(outputStream, CompressionMode.Compress, true);
+ if (compressionType == "gzip")
+ return new GZipStream(outputStream, CompressionMode.Compress, true);
+
+ throw new NotSupportedException(compressionType);
+ }
+
public static string GetRealContentType(string contentType)
{
return contentType == null
@@ -233,7 +236,7 @@ namespace Emby.Server.Implementations.HttpServer
: contentType.Split(';')[0].ToLower().Trim();
}
- public static string SerializeToXmlString(object from)
+ private string SerializeToXmlString(object from)
{
using (var ms = new MemoryStream())
{
@@ -253,16 +256,6 @@ namespace Emby.Server.Implementations.HttpServer
}
}
- private static Stream GetCompressionStream(Stream outputStream, string compressionType)
- {
- if (compressionType == "deflate")
- return new DeflateStream(outputStream, CompressionMode.Compress);
- if (compressionType == "gzip")
- return new GZipStream(outputStream, CompressionMode.Compress);
-
- throw new NotSupportedException(compressionType);
- }
-
/// <summary>
/// Gets the optimized result using cache.
/// </summary>
@@ -358,23 +351,7 @@ namespace Emby.Server.Implementations.HttpServer
return hasHeaders;
}
- IHasHeaders httpResult;
-
- var stream = result as Stream;
-
- if (stream != null)
- {
- httpResult = new StreamWriter(stream, contentType, _logger);
- }
- else
- {
- // Otherwise wrap into an HttpResult
- httpResult = new HttpResult(result, contentType ?? "text/html", HttpStatusCode.NotModified);
- }
-
- AddResponseHeaders(httpResult, responseHeaders);
-
- return httpResult;
+ return GetHttpResult(result, contentType, false, responseHeaders);
}
/// <summary>
@@ -603,7 +580,7 @@ namespace Emby.Server.Implementations.HttpServer
{
stream.Dispose();
- return GetHttpResult(new byte[] { }, contentType);
+ return GetHttpResult(new byte[] { }, contentType, true);
}
return new StreamWriter(stream, contentType, _logger)
@@ -630,13 +607,13 @@ namespace Emby.Server.Implementations.HttpServer
if (isHeadRequest)
{
- return GetHttpResult(new byte[] { }, contentType);
+ return GetHttpResult(new byte[] { }, contentType, true);
}
- return GetHttpResult(contents, contentType, responseHeaders);
+ return GetHttpResult(contents, contentType, true, responseHeaders);
}
- public static byte[] Compress(string text, string compressionType)
+ private byte[] Compress(string text, string compressionType)
{
if (compressionType == "deflate")
return Deflate(text);
@@ -647,12 +624,12 @@ namespace Emby.Server.Implementations.HttpServer
throw new NotSupportedException(compressionType);
}
- public static byte[] Deflate(string text)
+ private byte[] Deflate(string text)
{
return Deflate(Encoding.UTF8.GetBytes(text));
}
- public static byte[] Deflate(byte[] bytes)
+ private byte[] Deflate(byte[] bytes)
{
// In .NET FX incompat-ville, you can't access compressed bytes without closing DeflateStream
// Which means we must use MemoryStream since you have to use ToArray() on a closed Stream
@@ -666,12 +643,12 @@ namespace Emby.Server.Implementations.HttpServer
}
}
- public static byte[] GZip(string text)
+ private byte[] GZip(string text)
{
return GZip(Encoding.UTF8.GetBytes(text));
}
- public static byte[] GZip(byte[] buffer)
+ private byte[] GZip(byte[] buffer)
{
using (var ms = new MemoryStream())
using (var zipStream = new GZipStream(ms, CompressionMode.Compress))
diff --git a/Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs b/Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs
index de0b33fe3..9de86e9cc 100644
--- a/Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs
+++ b/Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs
@@ -77,8 +77,6 @@ namespace Emby.Server.Implementations.HttpServer.SocketSharp
get { return _response.OutputStream; }
}
- public object Dto { get; set; }
-
public void Write(string text)
{
var bOutput = System.Text.Encoding.UTF8.GetBytes(text);
@@ -120,11 +118,6 @@ namespace Emby.Server.Implementations.HttpServer.SocketSharp
}
}
- public void End()
- {
- Close();
- }
-
public void Flush()
{
_response.OutputStream.Flush();
diff --git a/Emby.Server.Implementations/HttpServer/StreamWriter.cs b/Emby.Server.Implementations/HttpServer/StreamWriter.cs
index 15488abaa..33378949c 100644
--- a/Emby.Server.Implementations/HttpServer/StreamWriter.cs
+++ b/Emby.Server.Implementations/HttpServer/StreamWriter.cs
@@ -25,6 +25,8 @@ namespace Emby.Server.Implementations.HttpServer
/// <value>The source stream.</value>
private Stream SourceStream { get; set; }
+ private byte[] SourceBytes { get; set; }
+
/// <summary>
/// The _options
/// </summary>
@@ -40,7 +42,6 @@ namespace Emby.Server.Implementations.HttpServer
public Action OnComplete { get; set; }
public Action OnError { get; set; }
- private readonly byte[] _bytes;
/// <summary>
/// Initializes a new instance of the <see cref="StreamWriter" /> class.
@@ -73,14 +74,13 @@ namespace Emby.Server.Implementations.HttpServer
/// <param name="contentType">Type of the content.</param>
/// <param name="logger">The logger.</param>
public StreamWriter(byte[] source, string contentType, ILogger logger)
- : this(new MemoryStream(source), contentType, logger)
{
if (string.IsNullOrEmpty(contentType))
{
throw new ArgumentNullException("contentType");
}
- _bytes = source;
+ SourceBytes = source;
Logger = logger;
Headers["Content-Type"] = contentType;
@@ -92,9 +92,11 @@ namespace Emby.Server.Implementations.HttpServer
{
try
{
- if (_bytes != null)
+ var bytes = SourceBytes;
+
+ if (bytes != null)
{
- await responseStream.WriteAsync(_bytes, 0, _bytes.Length);
+ await responseStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
}
else
{
diff --git a/MediaBrowser.Api/MediaBrowser.Api.csproj b/MediaBrowser.Api/MediaBrowser.Api.csproj
index df491ce85..c1a7347b5 100644
--- a/MediaBrowser.Api/MediaBrowser.Api.csproj
+++ b/MediaBrowser.Api/MediaBrowser.Api.csproj
@@ -142,6 +142,7 @@
<Compile Include="System\ActivityLogWebSocketListener.cs" />
<Compile Include="System\SystemService.cs" />
<Compile Include="Movies\TrailersService.cs" />
+ <Compile Include="TestService.cs" />
<Compile Include="TvShowsService.cs" />
<Compile Include="UserLibrary\ArtistsService.cs" />
<Compile Include="UserLibrary\BaseItemsByNameService.cs" />
diff --git a/MediaBrowser.Api/Playback/StaticRemoteStreamWriter.cs b/MediaBrowser.Api/Playback/StaticRemoteStreamWriter.cs
index c4a25a926..6bb3b6b80 100644
--- a/MediaBrowser.Api/Playback/StaticRemoteStreamWriter.cs
+++ b/MediaBrowser.Api/Playback/StaticRemoteStreamWriter.cs
@@ -1,6 +1,8 @@
using MediaBrowser.Common.Net;
using System.Collections.Generic;
using System.IO;
+using System.Threading;
+using System.Threading.Tasks;
using MediaBrowser.Model.Services;
namespace MediaBrowser.Api.Playback
@@ -8,7 +10,7 @@ namespace MediaBrowser.Api.Playback
/// <summary>
/// Class StaticRemoteStreamWriter
/// </summary>
- public class StaticRemoteStreamWriter : IStreamWriter, IHasHeaders
+ public class StaticRemoteStreamWriter : IAsyncStreamWriter, IHasHeaders
{
/// <summary>
/// The _input stream
@@ -34,15 +36,11 @@ namespace MediaBrowser.Api.Playback
get { return _options; }
}
- /// <summary>
- /// Writes to.
- /// </summary>
- /// <param name="responseStream">The response stream.</param>
- public void WriteTo(Stream responseStream)
+ public async Task WriteToAsync(Stream responseStream, CancellationToken cancellationToken)
{
using (_response)
{
- _response.Content.CopyTo(responseStream, 819200);
+ await _response.Content.CopyToAsync(responseStream, 81920, cancellationToken).ConfigureAwait(false);
}
}
}
diff --git a/MediaBrowser.Api/TestService.cs b/MediaBrowser.Api/TestService.cs
new file mode 100644
index 000000000..5340b816c
--- /dev/null
+++ b/MediaBrowser.Api/TestService.cs
@@ -0,0 +1,77 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using MediaBrowser.Model.Services;
+
+namespace MediaBrowser.Api
+{
+ [Route("/Test/String", "GET")]
+ public class GetString
+ {
+ }
+
+ [Route("/Test/OptimizedString", "GET")]
+ public class GetOptimizedString
+ {
+ }
+
+ [Route("/Test/Bytes", "GET")]
+ public class GetBytes
+ {
+ }
+
+ [Route("/Test/OptimizedBytes", "GET")]
+ public class GetOptimizedBytes
+ {
+ }
+
+ [Route("/Test/Stream", "GET")]
+ public class GetStream
+ {
+ }
+
+ [Route("/Test/OptimizedStream", "GET")]
+ public class GetOptimizedStream
+ {
+ }
+
+ [Route("/Test/BytesWithContentType", "GET")]
+ public class GetBytesWithContentType
+ {
+ }
+
+ public class TestService : BaseApiService
+ {
+ public object Get(GetString request)
+ {
+ return "Welcome to Emby!";
+ }
+ public object Get(GetOptimizedString request)
+ {
+ return ToOptimizedResult("Welcome to Emby!");
+ }
+ public object Get(GetBytes request)
+ {
+ return Encoding.UTF8.GetBytes("Welcome to Emby!");
+ }
+ public object Get(GetOptimizedBytes request)
+ {
+ return ToOptimizedResult(Encoding.UTF8.GetBytes("Welcome to Emby!"));
+ }
+ public object Get(GetBytesWithContentType request)
+ {
+ return ApiEntryPoint.Instance.ResultFactory.GetResult(Encoding.UTF8.GetBytes("Welcome to Emby!"), "text/html");
+ }
+ public object Get(GetStream request)
+ {
+ return new MemoryStream(Encoding.UTF8.GetBytes("Welcome to Emby!"));
+ }
+ public object Get(GetOptimizedStream request)
+ {
+ return ToOptimizedResult(new MemoryStream(Encoding.UTF8.GetBytes("Welcome to Emby!")));
+ }
+ }
+}
diff --git a/MediaBrowser.Model/Services/IHttpResult.cs b/MediaBrowser.Model/Services/IHttpResult.cs
index 36ffeb284..fcb137c6b 100644
--- a/MediaBrowser.Model/Services/IHttpResult.cs
+++ b/MediaBrowser.Model/Services/IHttpResult.cs
@@ -20,11 +20,6 @@ namespace MediaBrowser.Model.Services
HttpStatusCode StatusCode { get; set; }
/// <summary>
- /// The HTTP Status Description
- /// </summary>
- string StatusDescription { get; set; }
-
- /// <summary>
/// The HTTP Response ContentType
/// </summary>
string ContentType { get; set; }
diff --git a/MediaBrowser.Model/Services/IRequest.cs b/MediaBrowser.Model/Services/IRequest.cs
index 5a4d24007..455a69d37 100644
--- a/MediaBrowser.Model/Services/IRequest.cs
+++ b/MediaBrowser.Model/Services/IRequest.cs
@@ -137,34 +137,18 @@ namespace MediaBrowser.Model.Services
Stream OutputStream { get; }
/// <summary>
- /// The Response DTO
- /// </summary>
- object Dto { get; set; }
-
- /// <summary>
/// Write once to the Response Stream then close it.
/// </summary>
/// <param name="text"></param>
void Write(string text);
/// <summary>
- /// Buffer the Response OutputStream so it can be written in 1 batch
- /// </summary>
- bool UseBufferedStream { get; set; }
-
- /// <summary>
/// Signal that this response has been handled and no more processing should be done.
/// When used in a request or response filter, no more filters or processing is done on this request.
/// </summary>
void Close();
/// <summary>
- /// Calls Response.End() on ASP.NET HttpResponse otherwise is an alias for Close().
- /// Useful when you want to prevent ASP.NET to provide it's own custom error page.
- /// </summary>
- void End();
-
- /// <summary>
/// Response.Flush() and OutputStream.Flush() seem to have different behaviour in ASP.NET
/// </summary>
void Flush();
diff --git a/ServiceStack/Host/ContentTypes.cs b/ServiceStack/Host/ContentTypes.cs
index 22fdc3e50..58ba29801 100644
--- a/ServiceStack/Host/ContentTypes.cs
+++ b/ServiceStack/Host/ContentTypes.cs
@@ -13,37 +13,31 @@ namespace ServiceStack.Host
public void SerializeToStream(IRequest req, object response, Stream responseStream)
{
var contentType = req.ResponseContentType;
- var serializer = GetResponseSerializer(contentType);
- if (serializer == null)
- throw new NotSupportedException("ContentType not supported: " + contentType);
+ var serializer = GetStreamSerializer(contentType);
- var httpRes = new HttpResponseStreamWrapper(responseStream, req)
- {
- Dto = req.Response.Dto
- };
- serializer(req, response, httpRes);
+ serializer(response, responseStream);
}
- public Action<IRequest, object, IResponse> GetResponseSerializer(string contentType)
+ public Action<object, IResponse> GetResponseSerializer(string contentType)
{
var serializer = GetStreamSerializer(contentType);
if (serializer == null) return null;
- return (httpReq, dto, httpRes) => serializer(httpReq, dto, httpRes.OutputStream);
+ return (dto, httpRes) => serializer(dto, httpRes.OutputStream);
}
- public Action<IRequest, object, Stream> GetStreamSerializer(string contentType)
+ public Action<object, Stream> GetStreamSerializer(string contentType)
{
switch (GetRealContentType(contentType))
{
case "application/xml":
case "text/xml":
case "text/xml; charset=utf-8": //"text/xml; charset=utf-8" also matches xml
- return (r, o, s) => ServiceStackHost.Instance.SerializeToXml(o, s);
+ return (o, s) => ServiceStackHost.Instance.SerializeToXml(o, s);
case "application/json":
case "text/json":
- return (r, o, s) => ServiceStackHost.Instance.SerializeToJson(o, s);
+ return (o, s) => ServiceStackHost.Instance.SerializeToJson(o, s);
}
return null;
diff --git a/ServiceStack/Host/HttpResponseStreamWrapper.cs b/ServiceStack/Host/HttpResponseStreamWrapper.cs
deleted file mode 100644
index 33038da72..000000000
--- a/ServiceStack/Host/HttpResponseStreamWrapper.cs
+++ /dev/null
@@ -1,95 +0,0 @@
-using System.Collections.Generic;
-using System.IO;
-using System.Net;
-using System.Text;
-using MediaBrowser.Model.Services;
-
-namespace ServiceStack.Host
-{
- public class HttpResponseStreamWrapper : IHttpResponse
- {
- private static readonly UTF8Encoding UTF8EncodingWithoutBom = new UTF8Encoding(false);
-
- public HttpResponseStreamWrapper(Stream stream, IRequest request)
- {
- this.OutputStream = stream;
- this.Request = request;
- this.Headers = new Dictionary<string, string>();
- this.Items = new Dictionary<string, object>();
- }
-
- public Dictionary<string, string> Headers { get; set; }
-
- public object OriginalResponse
- {
- get { return null; }
- }
-
- public IRequest Request { get; private set; }
-
- public int StatusCode { set; get; }
- public string StatusDescription { set; get; }
- public string ContentType { get; set; }
-
- public void AddHeader(string name, string value)
- {
- this.Headers[name] = value;
- }
-
- public string GetHeader(string name)
- {
- return this.Headers[name];
- }
-
- public void Redirect(string url)
- {
- this.Headers["Location"] = url;
- }
-
- public Stream OutputStream { get; private set; }
-
- public object Dto { get; set; }
-
- public void Write(string text)
- {
- var bytes = UTF8EncodingWithoutBom.GetBytes(text);
- OutputStream.Write(bytes, 0, bytes.Length);
- }
-
- public bool UseBufferedStream { get; set; }
-
- public void Close()
- {
- if (IsClosed) return;
-
- OutputStream.Dispose();
- IsClosed = true;
- }
-
- public void End()
- {
- Close();
- }
-
- public void Flush()
- {
- OutputStream.Flush();
- }
-
- public bool IsClosed { get; private set; }
-
- public void SetContentLength(long contentLength) {}
-
- public bool KeepAlive { get; set; }
-
- public Dictionary<string, object> Items { get; private set; }
-
- public void SetCookie(Cookie cookie)
- {
- }
-
- public void ClearCookies()
- {
- }
- }
-} \ No newline at end of file
diff --git a/ServiceStack/Host/ServiceController.cs b/ServiceStack/Host/ServiceController.cs
index 703f06365..7eb1253b3 100644
--- a/ServiceStack/Host/ServiceController.cs
+++ b/ServiceStack/Host/ServiceController.cs
@@ -210,9 +210,6 @@ namespace ServiceStack.Host
//Executes the service and returns the result
var response = await ServiceExecGeneral.Execute(serviceType, req, service, requestDto, requestType.GetOperationName()).ConfigureAwait(false);
- if (req.Response.Dto == null)
- req.Response.Dto = response;
-
return response;
}
}
diff --git a/ServiceStack/HttpResponseExtensionsInternal.cs b/ServiceStack/HttpResponseExtensionsInternal.cs
index 1195f63ca..88b82bdf6 100644
--- a/ServiceStack/HttpResponseExtensionsInternal.cs
+++ b/ServiceStack/HttpResponseExtensionsInternal.cs
@@ -33,8 +33,11 @@ namespace ServiceStack
var stream = result as Stream;
if (stream != null)
{
- WriteTo(stream, response.OutputStream);
- return true;
+ using (stream)
+ {
+ await stream.CopyToAsync(response.OutputStream).ConfigureAwait(false);
+ return true;
+ }
}
var bytes = result as byte[];
@@ -43,35 +46,13 @@ namespace ServiceStack
response.ContentType = "application/octet-stream";
response.SetContentLength(bytes.Length);
- response.OutputStream.Write(bytes, 0, bytes.Length);
+ await response.OutputStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
return true;
}
return false;
}
- public static long WriteTo(Stream inStream, Stream outStream)
- {
- var memoryStream = inStream as MemoryStream;
- if (memoryStream != null)
- {
- memoryStream.WriteTo(outStream);
- return memoryStream.Position;
- }
-
- var data = new byte[4096];
- long total = 0;
- int bytesRead;
-
- while ((bytesRead = inStream.Read(data, 0, data.Length)) > 0)
- {
- outStream.Write(data, 0, bytesRead);
- total += bytesRead;
- }
-
- return total;
- }
-
/// <summary>
/// End a ServiceStack Request with no content
/// </summary>
@@ -85,7 +66,7 @@ namespace ServiceStack
httpRes.SetContentLength(0);
}
- public static Task WriteToResponse(this IResponse httpRes, MediaBrowser.Model.Services.IRequest httpReq, object result)
+ public static Task WriteToResponse(this IResponse httpRes, IRequest httpReq, object result)
{
if (result == null)
{
@@ -98,19 +79,10 @@ namespace ServiceStack
{
httpResult.RequestContext = httpReq;
httpReq.ResponseContentType = httpResult.ContentType ?? httpReq.ResponseContentType;
- var httpResSerializer = ContentTypes.Instance.GetResponseSerializer(httpReq.ResponseContentType);
- return httpRes.WriteToResponse(httpResult, httpResSerializer, httpReq);
+ return httpRes.WriteToResponseInternal(httpResult, httpReq);
}
- var serializer = ContentTypes.Instance.GetResponseSerializer(httpReq.ResponseContentType);
- return httpRes.WriteToResponse(result, serializer, httpReq);
- }
-
- private static object GetDto(object response)
- {
- if (response == null) return null;
- var httpResult = response as IHttpResult;
- return httpResult != null ? httpResult.Response : response;
+ return httpRes.WriteToResponseInternal(result, httpReq);
}
/// <summary>
@@ -119,17 +91,11 @@ namespace ServiceStack
/// </summary>
/// <param name="response">The response.</param>
/// <param name="result">Whether or not it was implicity handled by ServiceStack's built-in handlers.</param>
- /// <param name="defaultAction">The default action.</param>
/// <param name="request">The serialization context.</param>
/// <returns></returns>
- public static async Task WriteToResponse(this IResponse response, object result, Action<IRequest, object, IResponse> defaultAction, MediaBrowser.Model.Services.IRequest request)
+ private static async Task WriteToResponseInternal(this IResponse response, object result, IRequest request)
{
var defaultContentType = request.ResponseContentType;
- if (result == null)
- {
- response.EndRequestWithNoContent();
- return;
- }
var httpResult = result as IHttpResult;
if (httpResult != null)
@@ -137,10 +103,8 @@ namespace ServiceStack
if (httpResult.RequestContext == null)
httpResult.RequestContext = request;
- response.Dto = response.Dto ?? GetDto(httpResult);
-
response.StatusCode = httpResult.Status;
- response.StatusDescription = httpResult.StatusDescription ?? httpResult.StatusCode.ToString();
+ response.StatusDescription = httpResult.StatusCode.ToString();
if (string.IsNullOrEmpty(httpResult.ContentType))
{
httpResult.ContentType = defaultContentType;
@@ -159,21 +123,12 @@ namespace ServiceStack
}
}
}
- else
- {
- response.Dto = result;
- }
- /* Mono Error: Exception: Method not found: 'System.Web.HttpResponse.get_Headers' */
var responseOptions = result as IHasHeaders;
if (responseOptions != null)
{
- //Reserving options with keys in the format 'xx.xxx' (No Http headers contain a '.' so its a safe restriction)
- const string reservedOptions = ".";
-
foreach (var responseHeaders in responseOptions.Headers)
{
- if (responseHeaders.Key.Contains(reservedOptions)) continue;
if (responseHeaders.Key == "Content-Length")
{
response.SetContentLength(long.Parse(responseHeaders.Value));
@@ -196,42 +151,25 @@ namespace ServiceStack
response.ContentType += "; charset=utf-8";
}
- var disposableResult = result as IDisposable;
var writeToOutputStreamResult = await WriteToOutputStream(response, result).ConfigureAwait(false);
if (writeToOutputStreamResult)
{
response.Flush(); //required for Compression
- if (disposableResult != null) disposableResult.Dispose();
return;
}
- if (httpResult != null)
- result = httpResult.Response;
-
var responseText = result as string;
if (responseText != null)
{
if (response.ContentType == null || response.ContentType == "text/html")
response.ContentType = defaultContentType;
- response.Write(responseText);
+ response.Write(responseText);
return;
}
- if (defaultAction == null)
- {
- throw new ArgumentNullException("defaultAction", String.Format(
- "As result '{0}' is not a supported responseType, a defaultAction must be supplied",
- (result != null ? result.GetType().GetOperationName() : "")));
- }
-
-
- if (result != null)
- defaultAction(request, result, response);
-
- if (disposableResult != null)
- disposableResult.Dispose();
+ var serializer = ContentTypes.Instance.GetResponseSerializer(defaultContentType);
+ serializer(result, response);
}
-
}
}
diff --git a/ServiceStack/HttpResult.cs b/ServiceStack/HttpResult.cs
index 23a5cdffb..e25002b3e 100644
--- a/ServiceStack/HttpResult.cs
+++ b/ServiceStack/HttpResult.cs
@@ -13,31 +13,7 @@ namespace ServiceStack
public class HttpResult
: IHttpResult, IAsyncStreamWriter
{
- public HttpResult()
- : this((object)null, null)
- {
- }
-
- public HttpResult(object response)
- : this(response, null)
- {
- }
-
- public HttpResult(object response, string contentType)
- : this(response, contentType, HttpStatusCode.OK)
- {
- }
-
- public HttpResult(HttpStatusCode statusCode, string statusDescription)
- : this()
- {
- StatusCode = statusCode;
- StatusDescription = statusDescription;
- }
-
- public HttpResult(object response, HttpStatusCode statusCode)
- : this(response, null, statusCode)
- { }
+ public object Response { get; set; }
public HttpResult(object response, string contentType, HttpStatusCode statusCode)
{
@@ -49,102 +25,12 @@ namespace ServiceStack
this.StatusCode = statusCode;
}
- public HttpResult(Stream responseStream, string contentType)
- : this(null, contentType, HttpStatusCode.OK)
- {
- this.ResponseStream = responseStream;
- }
-
- public HttpResult(string responseText, string contentType)
- : this(null, contentType, HttpStatusCode.OK)
- {
- this.ResponseText = responseText;
- }
-
- public HttpResult(byte[] responseBytes, string contentType)
- : this(null, contentType, HttpStatusCode.OK)
- {
- this.ResponseStream = new MemoryStream(responseBytes);
- }
-
- public string ResponseText { get; private set; }
-
- public Stream ResponseStream { get; private set; }
-
public string ContentType { get; set; }
public IDictionary<string, string> Headers { get; private set; }
public List<Cookie> Cookies { get; private set; }
- public string ETag { get; set; }
-
- public TimeSpan? Age { get; set; }
-
- public TimeSpan? MaxAge { get; set; }
-
- public DateTime? Expires { get; set; }
-
- public DateTime? LastModified { get; set; }
-
- public Func<IDisposable> ResultScope { get; set; }
-
- public string Location
- {
- set
- {
- if (StatusCode == HttpStatusCode.OK)
- StatusCode = HttpStatusCode.Redirect;
-
- this.Headers["Location"] = value;
- }
- }
-
- public void SetPermanentCookie(string name, string value)
- {
- SetCookie(name, value, DateTime.UtcNow.AddYears(20), null);
- }
-
- public void SetPermanentCookie(string name, string value, string path)
- {
- SetCookie(name, value, DateTime.UtcNow.AddYears(20), path);
- }
-
- public void SetSessionCookie(string name, string value)
- {
- SetSessionCookie(name, value, null);
- }
-
- public void SetSessionCookie(string name, string value, string path)
- {
- path = path ?? "/";
- this.Headers["Set-Cookie"] = string.Format("{0}={1};path=" + path, name, value);
- }
-
- public void SetCookie(string name, string value, TimeSpan expiresIn, string path)
- {
- var expiresAt = DateTime.UtcNow.Add(expiresIn);
- SetCookie(name, value, expiresAt, path);
- }
-
- public void SetCookie(string name, string value, DateTime expiresAt, string path, bool secure = false, bool httpOnly = false)
- {
- path = path ?? "/";
- var cookie = string.Format("{0}={1};expires={2};path={3}", name, value, expiresAt.ToString("R"), path);
- if (secure)
- cookie += ";Secure";
- if (httpOnly)
- cookie += ";HttpOnly";
-
- this.Headers["Set-Cookie"] = cookie;
- }
-
- public void DeleteCookie(string name)
- {
- var cookie = string.Format("{0}=;expires={1};path=/", name, DateTime.UtcNow.AddDays(-1).ToString("R"));
- this.Headers["Set-Cookie"] = cookie;
- }
-
public int Status { get; set; }
public HttpStatusCode StatusCode
@@ -153,75 +39,12 @@ namespace ServiceStack
set { Status = (int)value; }
}
- public string StatusDescription { get; set; }
-
- public object Response { get; set; }
-
- public MediaBrowser.Model.Services.IRequest RequestContext { get; set; }
-
- public string View { get; set; }
-
- public string Template { get; set; }
-
- public int PaddingLength { get; set; }
+ public IRequest RequestContext { get; set; }
public async Task WriteToAsync(Stream responseStream, CancellationToken cancellationToken)
{
- try
- {
- await WriteToInternalAsync(responseStream, cancellationToken).ConfigureAwait(false);
- responseStream.Flush();
- }
- finally
- {
- DisposeStream();
- }
- }
-
- public static Task WriteTo(Stream inStream, Stream outStream, CancellationToken cancellationToken)
- {
- var memoryStream = inStream as MemoryStream;
- if (memoryStream != null)
- {
- memoryStream.WriteTo(outStream);
- return Task.FromResult(true);
- }
-
- return inStream.CopyToAsync(outStream, 81920, cancellationToken);
- }
-
- public async Task WriteToInternalAsync(Stream responseStream, CancellationToken cancellationToken)
- {
var response = RequestContext != null ? RequestContext.Response : null;
- if (this.ResponseStream != null)
- {
- if (response != null)
- {
- var ms = ResponseStream as MemoryStream;
- if (ms != null)
- {
- response.SetContentLength(ms.Length);
-
- await ms.CopyToAsync(responseStream, 81920, cancellationToken).ConfigureAwait(false);
- return;
- }
- }
-
- await WriteTo(this.ResponseStream, responseStream, cancellationToken).ConfigureAwait(false);
- return;
- }
-
- if (this.ResponseText != null)
- {
- var bytes = Encoding.UTF8.GetBytes(this.ResponseText);
- if (response != null)
- response.SetContentLength(bytes.Length);
-
- await responseStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
- return;
- }
-
var bytesResponse = this.Response as byte[];
if (bytesResponse != null)
{
@@ -234,17 +57,5 @@ namespace ServiceStack
ContentTypes.Instance.SerializeToStream(this.RequestContext, this.Response, responseStream);
}
-
- private void DisposeStream()
- {
- try
- {
- if (ResponseStream != null)
- {
- this.ResponseStream.Dispose();
- }
- }
- catch { /*ignore*/ }
- }
}
}
diff --git a/ServiceStack/ServiceStack.csproj b/ServiceStack/ServiceStack.csproj
index 3402339a6..5413d4e55 100644
--- a/ServiceStack/ServiceStack.csproj
+++ b/ServiceStack/ServiceStack.csproj
@@ -73,7 +73,6 @@
<Compile Include="Host\ContentTypes.cs" />
<Compile Include="ReflectionExtensions.cs" />
<Compile Include="StringMapTypeDeserializer.cs" />
- <Compile Include="Host\HttpResponseStreamWrapper.cs" />
<Compile Include="HttpResult.cs" />
<Compile Include="ServiceStackHost.cs" />
<Compile Include="ServiceStackHost.Runtime.cs" />
diff --git a/SocketHttpListener.Portable/Net/EndPointListener.cs b/SocketHttpListener.Portable/Net/EndPointListener.cs
index b50660ad0..52385e2ba 100644
--- a/SocketHttpListener.Portable/Net/EndPointListener.cs
+++ b/SocketHttpListener.Portable/Net/EndPointListener.cs
@@ -119,7 +119,6 @@ namespace SocketHttpListener.Net
if (listener == null)
return false;
- context.Listener = listener;
context.Connection.Prefix = prefix;
return true;
}
@@ -129,7 +128,7 @@ namespace SocketHttpListener.Net
if (context == null || context.Request == null)
return;
- context.Listener.UnregisterContext(context);
+ listener.UnregisterContext(context);
}
HttpListener SearchListener(Uri uri, out ListenerPrefix prefix)
diff --git a/SocketHttpListener.Portable/Net/HttpConnection.cs b/SocketHttpListener.Portable/Net/HttpConnection.cs
index d31da4132..db34c4218 100644
--- a/SocketHttpListener.Portable/Net/HttpConnection.cs
+++ b/SocketHttpListener.Portable/Net/HttpConnection.cs
@@ -1,7 +1,6 @@
using System;
using System.IO;
using System.Text;
-using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Model.Cryptography;
using MediaBrowser.Model.IO;
@@ -210,12 +209,7 @@ namespace SocketHttpListener.Net
// TODO: can we get this stream before reading the input?
if (o_stream == null)
{
- HttpListener listener = context.Listener;
-
- if (listener == null)
- return new ResponseStream(stream, context.Response, true, _memoryStreamFactory, _textEncoding);
-
- o_stream = new ResponseStream(stream, context.Response, listener.IgnoreWriteExceptions, _memoryStreamFactory, _textEncoding);
+ o_stream = new ResponseStream(stream, context.Response, _memoryStreamFactory, _textEncoding);
}
return o_stream;
}
@@ -257,7 +251,7 @@ namespace SocketHttpListener.Net
Close(true);
return;
}
- HttpListener listener = context.Listener;
+ HttpListener listener = epl.Listener;
if (last_listener != listener)
{
RemoveConnection();
diff --git a/SocketHttpListener.Portable/Net/HttpListener.cs b/SocketHttpListener.Portable/Net/HttpListener.cs
index 83660100a..2b0f75d01 100644
--- a/SocketHttpListener.Portable/Net/HttpListener.cs
+++ b/SocketHttpListener.Portable/Net/HttpListener.cs
@@ -28,7 +28,6 @@ namespace SocketHttpListener.Net
HttpListenerPrefixCollection prefixes;
AuthenticationSchemeSelector auth_selector;
string realm;
- bool ignore_write_exceptions;
bool unsafe_ntlm_auth;
bool listening;
bool disposed;
@@ -92,16 +91,6 @@ namespace SocketHttpListener.Net
}
}
- public bool IgnoreWriteExceptions
- {
- get { return ignore_write_exceptions; }
- set
- {
- CheckDisposed();
- ignore_write_exceptions = value;
- }
- }
-
public bool IsListening
{
get { return listening; }
diff --git a/SocketHttpListener.Portable/Net/HttpListenerContext.cs b/SocketHttpListener.Portable/Net/HttpListenerContext.cs
index 84c6a8c19..182fd2d2a 100644
--- a/SocketHttpListener.Portable/Net/HttpListenerContext.cs
+++ b/SocketHttpListener.Portable/Net/HttpListenerContext.cs
@@ -18,7 +18,6 @@ namespace SocketHttpListener.Net
HttpConnection cnc;
string error;
int err_status = 400;
- internal HttpListener Listener;
private readonly ILogger _logger;
private readonly ICryptoProvider _cryptoProvider;
private readonly IMemoryStreamFactory _memoryStreamFactory;
diff --git a/SocketHttpListener.Portable/Net/ResponseStream.cs b/SocketHttpListener.Portable/Net/ResponseStream.cs
index 6ecbf9742..07788ea41 100644
--- a/SocketHttpListener.Portable/Net/ResponseStream.cs
+++ b/SocketHttpListener.Portable/Net/ResponseStream.cs
@@ -17,17 +17,15 @@ namespace SocketHttpListener.Net
class ResponseStream : Stream
{
HttpListenerResponse response;
- bool ignore_errors;
bool disposed;
bool trailer_sent;
Stream stream;
private readonly IMemoryStreamFactory _memoryStreamFactory;
private readonly ITextEncoding _textEncoding;
- internal ResponseStream(Stream stream, HttpListenerResponse response, bool ignore_errors, IMemoryStreamFactory memoryStreamFactory, ITextEncoding textEncoding)
+ internal ResponseStream(Stream stream, HttpListenerResponse response, IMemoryStreamFactory memoryStreamFactory, ITextEncoding textEncoding)
{
this.response = response;
- this.ignore_errors = ignore_errors;
_memoryStreamFactory = memoryStreamFactory;
_textEncoding = textEncoding;
this.stream = stream;
@@ -130,18 +128,7 @@ namespace SocketHttpListener.Net
internal void InternalWrite(byte[] buffer, int offset, int count)
{
- if (ignore_errors)
- {
- try
- {
- stream.Write(buffer, offset, count);
- }
- catch { }
- }
- else
- {
- stream.Write(buffer, offset, count);
- }
+ stream.Write(buffer, offset, count);
}
public override void Write(byte[] buffer, int offset, int count)
@@ -214,23 +201,13 @@ namespace SocketHttpListener.Net
InternalWrite(bytes, 0, bytes.Length);
}
- try
- {
- if (count > 0)
- {
- await stream.WriteAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
- }
-
- if (response.SendChunked)
- stream.Write(crlf, 0, 2);
- }
- catch
+ if (count > 0)
{
- if (!ignore_errors)
- {
- throw;
- }
+ await stream.WriteAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false);
}
+
+ if (response.SendChunked)
+ stream.Write(crlf, 0, 2);
}
//public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count,