jellyfin-server/Emby.Server.Implementations/Services/ResponseHelper.cs

164 lines
5.4 KiB
C#
Raw Normal View History

2016-11-11 19:55:12 +00:00
using System;
2017-02-13 01:07:48 +00:00
using System.Collections.Generic;
2016-11-11 19:55:12 +00:00
using System.IO;
using System.Net;
2016-11-12 07:14:04 +00:00
using System.Text;
2016-11-11 19:55:12 +00:00
using System.Threading;
2017-02-13 01:07:48 +00:00
using System.Threading.Tasks;
2017-02-13 02:06:54 +00:00
using Emby.Server.Implementations.HttpServer;
2016-11-11 19:55:12 +00:00
using MediaBrowser.Model.Services;
2017-02-13 01:07:48 +00:00
namespace Emby.Server.Implementations.Services
2016-11-11 19:55:12 +00:00
{
2017-02-13 01:07:48 +00:00
public static class ResponseHelper
2016-11-11 19:55:12 +00:00
{
2018-09-12 17:26:21 +00:00
public static Task WriteToResponse(IResponse response, IRequest request, object result, CancellationToken cancellationToken)
2016-11-11 19:55:12 +00:00
{
if (result == null)
{
2017-05-25 18:18:19 +00:00
if (response.StatusCode == (int)HttpStatusCode.OK)
2017-02-13 01:07:48 +00:00
{
2017-05-25 18:18:19 +00:00
response.StatusCode = (int)HttpStatusCode.NoContent;
2017-02-13 01:07:48 +00:00
}
2017-05-25 18:18:19 +00:00
response.SetContentLength(0);
2018-09-12 17:26:21 +00:00
return Task.CompletedTask;
2016-11-11 19:55:12 +00:00
}
var httpResult = result as IHttpResult;
if (httpResult != null)
{
2017-05-25 18:18:19 +00:00
httpResult.RequestContext = request;
request.ResponseContentType = httpResult.ContentType ?? request.ResponseContentType;
2016-11-11 19:55:12 +00:00
}
var defaultContentType = request.ResponseContentType;
if (httpResult != null)
{
if (httpResult.RequestContext == null)
httpResult.RequestContext = request;
response.StatusCode = httpResult.Status;
2016-11-12 06:58:50 +00:00
response.StatusDescription = httpResult.StatusCode.ToString();
2017-09-03 07:28:58 +00:00
//if (string.IsNullOrEmpty(httpResult.ContentType))
//{
// httpResult.ContentType = defaultContentType;
//}
//response.ContentType = httpResult.ContentType;
2016-11-11 19:55:12 +00:00
}
var responseOptions = result as IHasHeaders;
if (responseOptions != null)
{
foreach (var responseHeaders in responseOptions.Headers)
{
2016-11-28 05:38:41 +00:00
if (string.Equals(responseHeaders.Key, "Content-Length", StringComparison.OrdinalIgnoreCase))
2016-11-11 19:55:12 +00:00
{
response.SetContentLength(long.Parse(responseHeaders.Value));
continue;
}
response.AddHeader(responseHeaders.Key, responseHeaders.Value);
}
}
//ContentType='text/html' is the default for a HttpResponse
//Do not override if another has been set
if (response.ContentType == null || response.ContentType == "text/html")
{
response.ContentType = defaultContentType;
}
if (new HashSet<string> { "application/json", }.Contains(response.ContentType))
{
response.ContentType += "; charset=utf-8";
}
2017-03-12 04:27:06 +00:00
var asyncStreamWriter = result as IAsyncStreamWriter;
if (asyncStreamWriter != null)
{
2018-09-12 17:26:21 +00:00
return asyncStreamWriter.WriteToAsync(response.OutputStream, cancellationToken);
2017-03-12 04:27:06 +00:00
}
var streamWriter = result as IStreamWriter;
if (streamWriter != null)
{
streamWriter.WriteTo(response.OutputStream);
2018-09-12 17:26:21 +00:00
return Task.CompletedTask;
2017-03-12 04:27:06 +00:00
}
2017-03-12 19:27:26 +00:00
var fileWriter = result as FileWriter;
if (fileWriter != null)
{
2018-09-12 17:26:21 +00:00
return fileWriter.WriteToAsync(response, cancellationToken);
2017-03-12 19:27:26 +00:00
}
2017-03-12 04:27:06 +00:00
var stream = result as Stream;
if (stream != null)
2016-11-11 19:55:12 +00:00
{
2018-09-12 17:26:21 +00:00
return CopyStream(stream, response.OutputStream);
2017-03-12 04:27:06 +00:00
}
var bytes = result as byte[];
if (bytes != null)
{
response.ContentType = "application/octet-stream";
response.SetContentLength(bytes.Length);
2017-09-03 07:28:58 +00:00
if (bytes.Length > 0)
{
2018-09-12 17:26:21 +00:00
return response.OutputStream.WriteAsync(bytes, 0, bytes.Length, cancellationToken);
2017-09-03 07:28:58 +00:00
}
2018-09-12 17:26:21 +00:00
return Task.CompletedTask;
2016-11-11 19:55:12 +00:00
}
var responseText = result as string;
if (responseText != null)
{
2017-03-12 04:27:06 +00:00
bytes = Encoding.UTF8.GetBytes(responseText);
2016-11-13 01:53:51 +00:00
response.SetContentLength(bytes.Length);
2017-09-03 07:28:58 +00:00
if (bytes.Length > 0)
{
2018-09-12 17:26:21 +00:00
return response.OutputStream.WriteAsync(bytes, 0, bytes.Length, cancellationToken);
2017-09-03 07:28:58 +00:00
}
2018-09-12 17:26:21 +00:00
return Task.CompletedTask;
2016-11-11 19:55:12 +00:00
}
2018-09-12 17:26:21 +00:00
return WriteObject(request, result, response);
}
private static async Task CopyStream(Stream src, Stream dest)
{
using (src)
{
await src.CopyToAsync(dest).ConfigureAwait(false);
}
2016-11-12 07:45:06 +00:00
}
public static async Task WriteObject(IRequest request, object result, IResponse response)
{
var contentType = request.ResponseContentType;
2017-02-13 02:06:54 +00:00
var serializer = RequestHelper.GetResponseWriter(HttpListenerHost.Instance, contentType);
2017-03-12 04:27:06 +00:00
2016-11-12 07:45:06 +00:00
using (var ms = new MemoryStream())
{
serializer(result, ms);
ms.Position = 0;
2017-09-03 07:28:58 +00:00
var contentLength = ms.Length;
response.SetContentLength(contentLength);
if (contentLength > 0)
{
await ms.CopyToAsync(response.OutputStream).ConfigureAwait(false);
}
2016-11-12 07:45:06 +00:00
}
//serializer(result, outputStream);
2016-11-11 19:55:12 +00:00
}
}
}