2
0

HttpResult.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. var contentLength = bytesResponse.Length;
  38. if (response != null)
  39. response.SetContentLength(contentLength);
  40. if (contentLength > 0)
  41. {
  42. await responseStream.WriteAsync(bytesResponse, 0, contentLength).ConfigureAwait(false);
  43. }
  44. return;
  45. }
  46. await ResponseHelper.WriteObject(this.RequestContext, this.Response, response).ConfigureAwait(false);
  47. }
  48. }
  49. }