HttpRequestOptions.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using Microsoft.Net.Http.Headers;
  6. namespace MediaBrowser.Common.Net
  7. {
  8. /// <summary>
  9. /// Class HttpRequestOptions
  10. /// </summary>
  11. public class HttpRequestOptions
  12. {
  13. /// <summary>
  14. /// Gets or sets the URL.
  15. /// </summary>
  16. /// <value>The URL.</value>
  17. public string Url { get; set; }
  18. public CompressionMethod? DecompressionMethod { get; set; }
  19. /// <summary>
  20. /// Gets or sets the accept header.
  21. /// </summary>
  22. /// <value>The accept header.</value>
  23. public string AcceptHeader
  24. {
  25. get => GetHeaderValue(HeaderNames.Accept);
  26. set => RequestHeaders[HeaderNames.Accept] = value;
  27. }
  28. /// <summary>
  29. /// Gets or sets the cancellation token.
  30. /// </summary>
  31. /// <value>The cancellation token.</value>
  32. public CancellationToken CancellationToken { get; set; }
  33. /// <summary>
  34. /// Gets or sets the user agent.
  35. /// </summary>
  36. /// <value>The user agent.</value>
  37. public string UserAgent
  38. {
  39. get => GetHeaderValue(HeaderNames.UserAgent);
  40. set => RequestHeaders[HeaderNames.UserAgent] = value;
  41. }
  42. /// <summary>
  43. /// Gets or sets the referrer.
  44. /// </summary>
  45. /// <value>The referrer.</value>
  46. public string Referer { get; set; }
  47. /// <summary>
  48. /// Gets or sets the host.
  49. /// </summary>
  50. /// <value>The host.</value>
  51. public string Host { get; set; }
  52. /// <summary>
  53. /// Gets or sets the progress.
  54. /// </summary>
  55. /// <value>The progress.</value>
  56. public IProgress<double> Progress { get; set; }
  57. /// <summary>
  58. /// Gets or sets a value indicating whether [enable HTTP compression].
  59. /// </summary>
  60. /// <value><c>true</c> if [enable HTTP compression]; otherwise, <c>false</c>.</value>
  61. public bool EnableHttpCompression { get; set; }
  62. public Dictionary<string, string> RequestHeaders { get; private set; }
  63. public string RequestContentType { get; set; }
  64. public string RequestContent { get; set; }
  65. public byte[] RequestContentBytes { get; set; }
  66. public bool BufferContent { get; set; }
  67. public bool LogRequest { get; set; }
  68. public bool LogRequestAsDebug { get; set; }
  69. public bool LogErrors { get; set; }
  70. public bool LogErrorResponseBody { get; set; }
  71. public bool EnableKeepAlive { get; set; }
  72. public CacheMode CacheMode { get; set; }
  73. public TimeSpan CacheLength { get; set; }
  74. public bool EnableDefaultUserAgent { get; set; }
  75. public bool AppendCharsetToMimeType { get; set; }
  76. private string GetHeaderValue(string name)
  77. {
  78. RequestHeaders.TryGetValue(name, out var value);
  79. return value;
  80. }
  81. /// <summary>
  82. /// Initializes a new instance of the <see cref="HttpRequestOptions"/> class.
  83. /// </summary>
  84. public HttpRequestOptions()
  85. {
  86. EnableHttpCompression = true;
  87. RequestHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  88. LogRequest = true;
  89. LogErrors = true;
  90. CacheMode = CacheMode.None;
  91. }
  92. }
  93. public enum CacheMode
  94. {
  95. None = 0,
  96. Unconditional = 1
  97. }
  98. public enum CompressionMethod
  99. {
  100. Deflate,
  101. Gzip
  102. }
  103. }