2020-05-29 09:28:19 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2016-11-11 19:55:12 +00:00
|
|
|
using System;
|
2019-04-24 12:06:54 +00:00
|
|
|
using System.Globalization;
|
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;
|
2019-09-02 06:19:29 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2016-11-11 19:55:12 +00:00
|
|
|
|
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
|
|
|
{
|
2019-07-28 21:53:19 +00:00
|
|
|
public static Task WriteToResponse(HttpResponse 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
|
|
|
}
|
2019-04-24 12:06:54 +00:00
|
|
|
|
2019-07-28 21:53:19 +00:00
|
|
|
response.ContentLength = 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)
|
2020-06-20 09:12:36 +00:00
|
|
|
{
|
2016-11-11 19:55:12 +00:00
|
|
|
httpResult.RequestContext = request;
|
2020-06-20 09:12:36 +00:00
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
|
|
|
response.StatusCode = httpResult.Status;
|
|
|
|
}
|
|
|
|
|
2020-05-29 09:28:19 +00:00
|
|
|
if (result is IHasHeaders responseOptions)
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2019-07-28 21:53:19 +00:00
|
|
|
response.ContentLength = long.Parse(responseHeaders.Value, CultureInfo.InvariantCulture);
|
2016-11-11 19:55:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-28 21:53:19 +00:00
|
|
|
response.Headers.Add(responseHeaders.Key, responseHeaders.Value);
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-14 09:11:11 +00:00
|
|
|
// ContentType='text/html' is the default for a HttpResponse
|
|
|
|
// Do not override if another has been set
|
2016-11-11 19:55:12 +00:00
|
|
|
if (response.ContentType == null || response.ContentType == "text/html")
|
|
|
|
{
|
|
|
|
response.ContentType = defaultContentType;
|
|
|
|
}
|
|
|
|
|
2019-02-06 20:31:41 +00:00
|
|
|
if (response.ContentType == "application/json")
|
2016-11-11 19:55:12 +00:00
|
|
|
{
|
|
|
|
response.ContentType += "; charset=utf-8";
|
|
|
|
}
|
|
|
|
|
2019-04-24 12:06:54 +00:00
|
|
|
switch (result)
|
2017-03-12 04:27:06 +00:00
|
|
|
{
|
2019-04-24 12:06:54 +00:00
|
|
|
case IAsyncStreamWriter asyncStreamWriter:
|
2019-07-28 21:53:19 +00:00
|
|
|
return asyncStreamWriter.WriteToAsync(response.Body, cancellationToken);
|
2019-04-24 12:06:54 +00:00
|
|
|
case IStreamWriter streamWriter:
|
2019-07-28 21:53:19 +00:00
|
|
|
streamWriter.WriteTo(response.Body);
|
2019-04-24 12:06:54 +00:00
|
|
|
return Task.CompletedTask;
|
|
|
|
case FileWriter fileWriter:
|
|
|
|
return fileWriter.WriteToAsync(response, cancellationToken);
|
|
|
|
case Stream stream:
|
2019-07-28 21:53:19 +00:00
|
|
|
return CopyStream(stream, response.Body);
|
2019-04-24 12:06:54 +00:00
|
|
|
case byte[] bytes:
|
|
|
|
response.ContentType = "application/octet-stream";
|
2019-07-28 21:53:19 +00:00
|
|
|
response.ContentLength = bytes.Length;
|
2019-04-24 12:06:54 +00:00
|
|
|
|
|
|
|
if (bytes.Length > 0)
|
|
|
|
{
|
2019-07-28 21:53:19 +00:00
|
|
|
return response.Body.WriteAsync(bytes, 0, bytes.Length, cancellationToken);
|
2019-04-24 12:06:54 +00:00
|
|
|
}
|
2017-03-12 04:27:06 +00:00
|
|
|
|
2019-04-24 12:06:54 +00:00
|
|
|
return Task.CompletedTask;
|
|
|
|
case string responseText:
|
|
|
|
var responseTextAsBytes = Encoding.UTF8.GetBytes(responseText);
|
2019-07-28 21:53:19 +00:00
|
|
|
response.ContentLength = responseTextAsBytes.Length;
|
2017-03-12 04:27:06 +00:00
|
|
|
|
2019-04-24 12:06:54 +00:00
|
|
|
if (responseTextAsBytes.Length > 0)
|
|
|
|
{
|
2019-07-28 21:53:19 +00:00
|
|
|
return response.Body.WriteAsync(responseTextAsBytes, 0, responseTextAsBytes.Length, cancellationToken);
|
2019-04-24 12:06:54 +00:00
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
|
2019-04-24 12:06:54 +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
|
|
|
}
|
|
|
|
|
2019-07-28 21:53:19 +00:00
|
|
|
public static async Task WriteObject(IRequest request, object result, HttpResponse response)
|
2016-11-12 07:45:06 +00:00
|
|
|
{
|
|
|
|
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;
|
2019-07-28 21:53:19 +00:00
|
|
|
response.ContentLength = contentLength;
|
2017-09-03 07:28:58 +00:00
|
|
|
|
|
|
|
if (contentLength > 0)
|
|
|
|
{
|
2019-07-28 21:53:19 +00:00
|
|
|
await ms.CopyToAsync(response.Body).ConfigureAwait(false);
|
2017-09-03 07:28:58 +00:00
|
|
|
}
|
2016-11-12 07:45:06 +00:00
|
|
|
}
|
2016-11-11 19:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|