2
0

HttpResult.cs 1.8 KB

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