HTTPRange.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. namespace Best.HTTP
  2. {
  3. /// <summary>
  4. /// Represents an HTTP range that specifies the byte range of a response content, received as an answer for a range-request.
  5. /// </summary>
  6. public sealed class HTTPRange
  7. {
  8. /// <summary>
  9. /// Gets the position of the first byte in the range that the server sent.
  10. /// </summary>
  11. public long FirstBytePos { get; private set; }
  12. /// <summary>
  13. /// Gets the position of the last byte in the range that the server sent.
  14. /// </summary>
  15. public long LastBytePos { get; private set; }
  16. /// <summary>
  17. /// Gets the total length of the full entity-body on the server. Returns -1 if this length is unknown or difficult to determine.
  18. /// </summary>
  19. public long ContentLength { get; private set; }
  20. /// <summary>
  21. /// Gets a value indicating whether the HTTP range is valid.
  22. /// </summary>
  23. public bool IsValid { get; private set; }
  24. internal HTTPRange()
  25. {
  26. this.ContentLength = -1;
  27. this.IsValid = false;
  28. }
  29. internal HTTPRange(int contentLength)
  30. {
  31. this.ContentLength = contentLength;
  32. this.IsValid = false;
  33. }
  34. internal HTTPRange(long firstBytePosition, long lastBytePosition, long contentLength)
  35. {
  36. this.FirstBytePos = firstBytePosition;
  37. this.LastBytePos = lastBytePosition;
  38. this.ContentLength = contentLength;
  39. // A byte-content-range-spec with a byte-range-resp-spec whose last-byte-pos value is less than its first-byte-pos value, or whose instance-length value is less than or equal to its last-byte-pos value, is invalid.
  40. this.IsValid = this.FirstBytePos <= this.LastBytePos && this.ContentLength > this.LastBytePos;
  41. }
  42. public override string ToString()
  43. {
  44. return string.Format("{0}-{1}/{2} (valid: {3})", FirstBytePos, LastBytePos, ContentLength, IsValid);
  45. }
  46. }
  47. }