HttpResult.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Net;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Model.Services;
  7. using ServiceStack;
  8. namespace Emby.Server.Implementations.Services
  9. {
  10. public class HttpResult
  11. : IHttpResult, IAsyncStreamWriter
  12. {
  13. public object Response { get; set; }
  14. public HttpResult(object response, string contentType, HttpStatusCode statusCode)
  15. {
  16. this.Headers = new Dictionary<string, string>();
  17. this.Cookies = new List<Cookie>();
  18. this.Response = response;
  19. this.ContentType = contentType;
  20. this.StatusCode = statusCode;
  21. }
  22. public string ContentType { get; set; }
  23. public IDictionary<string, string> Headers { get; private set; }
  24. public List<Cookie> Cookies { get; private set; }
  25. public int Status { get; set; }
  26. public HttpStatusCode StatusCode
  27. {
  28. get { return (HttpStatusCode)Status; }
  29. set { Status = (int)value; }
  30. }
  31. public IRequest RequestContext { get; set; }
  32. public async Task WriteToAsync(Stream responseStream, CancellationToken cancellationToken)
  33. {
  34. var response = RequestContext != null ? RequestContext.Response : null;
  35. var bytesResponse = this.Response as byte[];
  36. if (bytesResponse != null)
  37. {
  38. if (response != null)
  39. response.SetContentLength(bytesResponse.Length);
  40. await responseStream.WriteAsync(bytesResponse, 0, bytesResponse.Length).ConfigureAwait(false);
  41. return;
  42. }
  43. await ResponseHelper.WriteObject(this.RequestContext, this.Response, response).ConfigureAwait(false);
  44. }
  45. }
  46. }