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

140 lines
5.0 KiB
C#
Raw Normal View History

2016-11-11 12:55:12 -07:00
using System;
2017-02-12 18:07:48 -07:00
using System.Collections.Generic;
using System.Globalization;
2016-11-11 12:55:12 -07:00
using System.IO;
using System.Net;
2016-11-12 00:14:04 -07:00
using System.Text;
2016-11-11 12:55:12 -07:00
using System.Threading;
2017-02-12 18:07:48 -07:00
using System.Threading.Tasks;
2017-02-12 19:06:54 -07:00
using Emby.Server.Implementations.HttpServer;
2016-11-11 12:55:12 -07:00
using MediaBrowser.Model.Services;
2017-02-12 18:07:48 -07:00
namespace Emby.Server.Implementations.Services
2016-11-11 12:55:12 -07:00
{
2017-02-12 18:07:48 -07:00
public static class ResponseHelper
2016-11-11 12:55:12 -07:00
{
2018-09-12 10:26:21 -07:00
public static Task WriteToResponse(IResponse response, IRequest request, object result, CancellationToken cancellationToken)
2016-11-11 12:55:12 -07:00
{
if (result == null)
{
2017-05-25 11:18:19 -07:00
if (response.StatusCode == (int)HttpStatusCode.OK)
2017-02-12 18:07:48 -07:00
{
2017-05-25 11:18:19 -07:00
response.StatusCode = (int)HttpStatusCode.NoContent;
2017-02-12 18:07:48 -07:00
}
response.OriginalResponse.ContentLength = 0;
2018-09-12 10:26:21 -07:00
return Task.CompletedTask;
2016-11-11 12:55:12 -07:00
}
var httpResult = result as IHttpResult;
if (httpResult != null)
{
2017-05-25 11:18:19 -07:00
httpResult.RequestContext = request;
request.ResponseContentType = httpResult.ContentType ?? request.ResponseContentType;
2016-11-11 12:55:12 -07:00
}
var defaultContentType = request.ResponseContentType;
if (httpResult != null)
{
if (httpResult.RequestContext == null)
httpResult.RequestContext = request;
response.StatusCode = httpResult.Status;
2016-11-11 23:58:50 -07:00
response.StatusDescription = httpResult.StatusCode.ToString();
2016-11-11 12:55:12 -07:00
}
var responseOptions = result as IHasHeaders;
if (responseOptions != null)
{
foreach (var responseHeaders in responseOptions.Headers)
{
2016-11-27 22:38:41 -07:00
if (string.Equals(responseHeaders.Key, "Content-Length", StringComparison.OrdinalIgnoreCase))
2016-11-11 12:55:12 -07:00
{
response.OriginalResponse.ContentLength = long.Parse(responseHeaders.Value, CultureInfo.InvariantCulture);
2016-11-11 12:55:12 -07:00
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;
}
2019-02-06 13:31:41 -07:00
if (response.ContentType == "application/json")
2016-11-11 12:55:12 -07:00
{
response.ContentType += "; charset=utf-8";
}
switch (result)
2017-03-11 21:27:06 -07:00
{
case IAsyncStreamWriter asyncStreamWriter:
return asyncStreamWriter.WriteToAsync(response.OutputStream, cancellationToken);
case IStreamWriter streamWriter:
streamWriter.WriteTo(response.OutputStream);
return Task.CompletedTask;
case FileWriter fileWriter:
return fileWriter.WriteToAsync(response, cancellationToken);
case Stream stream:
return CopyStream(stream, response.OutputStream);
case byte[] bytes:
response.ContentType = "application/octet-stream";
response.OriginalResponse.ContentLength = bytes.Length;
if (bytes.Length > 0)
{
return response.OutputStream.WriteAsync(bytes, 0, bytes.Length, cancellationToken);
}
2017-03-11 21:27:06 -07:00
return Task.CompletedTask;
case string responseText:
var responseTextAsBytes = Encoding.UTF8.GetBytes(responseText);
response.OriginalResponse.ContentLength = responseTextAsBytes.Length;
2017-03-11 21:27:06 -07:00
if (responseTextAsBytes.Length > 0)
{
return response.OutputStream.WriteAsync(responseTextAsBytes, 0, responseTextAsBytes.Length, cancellationToken);
}
2016-11-11 12:55:12 -07:00
return Task.CompletedTask;
2016-11-11 12:55:12 -07:00
}
2018-09-12 10:26:21 -07: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 00:45:06 -07:00
}
public static async Task WriteObject(IRequest request, object result, IResponse response)
{
var contentType = request.ResponseContentType;
2017-02-12 19:06:54 -07:00
var serializer = RequestHelper.GetResponseWriter(HttpListenerHost.Instance, contentType);
2017-03-11 21:27:06 -07:00
2016-11-12 00:45:06 -07:00
using (var ms = new MemoryStream())
{
serializer(result, ms);
ms.Position = 0;
2017-09-03 00:28:58 -07:00
var contentLength = ms.Length;
response.OriginalResponse.ContentLength = contentLength;
2017-09-03 00:28:58 -07:00
if (contentLength > 0)
{
await ms.CopyToAsync(response.OutputStream).ConfigureAwait(false);
}
2016-11-12 00:45:06 -07:00
}
2016-11-11 12:55:12 -07:00
}
}
}