mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-03-06 02:06:21 +00:00
update responses
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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*/ }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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" />
|
||||
|
||||
Reference in New Issue
Block a user