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

172 lines
6.1 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;
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
{
2017-05-21 21:54:02 -07:00
public static Task WriteToResponse(IResponse httpRes, IRequest httpReq, object result, CancellationToken cancellationToken)
2016-11-11 12:55:12 -07:00
{
if (result == null)
{
2017-02-12 18:07:48 -07:00
if (httpRes.StatusCode == (int)HttpStatusCode.OK)
{
httpRes.StatusCode = (int)HttpStatusCode.NoContent;
}
httpRes.SetContentLength(0);
2016-11-11 12:55:12 -07:00
return Task.FromResult(true);
}
var httpResult = result as IHttpResult;
if (httpResult != null)
{
httpResult.RequestContext = httpReq;
httpReq.ResponseContentType = httpResult.ContentType ?? httpReq.ResponseContentType;
2017-05-21 21:54:02 -07:00
return WriteToResponseInternal(httpRes, httpResult, httpReq, cancellationToken);
2016-11-11 12:55:12 -07:00
}
2017-05-21 21:54:02 -07:00
return WriteToResponseInternal(httpRes, result, httpReq, cancellationToken);
2016-11-11 12:55:12 -07:00
}
/// <summary>
/// Writes to response.
/// Response headers are customizable by implementing IHasHeaders an returning Dictionary of Http headers.
/// </summary>
2017-05-21 21:54:02 -07:00
private static async Task WriteToResponseInternal(IResponse response, object result, IRequest request, CancellationToken cancellationToken)
2016-11-11 12:55:12 -07:00
{
var defaultContentType = request.ResponseContentType;
var httpResult = result as IHttpResult;
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
if (string.IsNullOrEmpty(httpResult.ContentType))
{
httpResult.ContentType = defaultContentType;
}
response.ContentType = httpResult.ContentType;
if (httpResult.Cookies != null)
{
var httpRes = response as IHttpResponse;
if (httpRes != null)
{
foreach (var cookie in httpResult.Cookies)
{
httpRes.SetCookie(cookie);
}
}
}
}
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.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-11 21:27:06 -07:00
var asyncStreamWriter = result as IAsyncStreamWriter;
if (asyncStreamWriter != null)
{
2017-05-21 21:54:02 -07:00
await asyncStreamWriter.WriteToAsync(response.OutputStream, cancellationToken).ConfigureAwait(false);
2017-03-11 21:27:06 -07:00
return;
}
var streamWriter = result as IStreamWriter;
if (streamWriter != null)
{
streamWriter.WriteTo(response.OutputStream);
return;
}
2017-03-12 12:27:26 -07:00
var fileWriter = result as FileWriter;
if (fileWriter != null)
{
2017-05-21 21:54:02 -07:00
await fileWriter.WriteToAsync(response, cancellationToken).ConfigureAwait(false);
2017-03-12 12:27:26 -07:00
return;
}
2017-03-11 21:27:06 -07:00
var stream = result as Stream;
if (stream != null)
2016-11-11 12:55:12 -07:00
{
2017-03-11 21:27:06 -07:00
using (stream)
{
await stream.CopyToAsync(response.OutputStream).ConfigureAwait(false);
return;
}
}
var bytes = result as byte[];
if (bytes != null)
{
response.ContentType = "application/octet-stream";
response.SetContentLength(bytes.Length);
await response.OutputStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
2016-11-11 12:55:12 -07:00
return;
}
var responseText = result as string;
if (responseText != null)
{
2017-03-11 21:27:06 -07:00
bytes = Encoding.UTF8.GetBytes(responseText);
2016-11-12 18:53:51 -07:00
response.SetContentLength(bytes.Length);
2016-11-12 00:45:06 -07:00
await response.OutputStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
2016-11-11 12:55:12 -07:00
return;
}
2016-11-12 00:45:06 -07:00
await WriteObject(request, result, response).ConfigureAwait(false);
}
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;
response.SetContentLength(ms.Length);
await ms.CopyToAsync(response.OutputStream).ConfigureAwait(false);
}
//serializer(result, outputStream);
2016-11-11 12:55:12 -07:00
}
}
}