/src/DotNetty.Codecs.Http/Multipart/AbstractHttpData.cs

https://github.com/Azure/DotNetty · C# · 137 lines · 97 code · 35 blank · 5 comment · 7 complexity · af82bf4c81b444a0a77b33274d3864e0 MD5 · raw file

  1. // Copyright (c) Microsoft. All rights reserved.
  2. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
  3. // ReSharper disable ConvertToAutoPropertyWhenPossible
  4. // ReSharper disable ConvertToAutoProperty
  5. // ReSharper disable ConvertToAutoPropertyWithPrivateSetter
  6. namespace DotNetty.Codecs.Http.Multipart
  7. {
  8. using System;
  9. using System.Diagnostics.Contracts;
  10. using System.IO;
  11. using System.Text;
  12. using System.Text.RegularExpressions;
  13. using DotNetty.Buffers;
  14. using DotNetty.Common.Utilities;
  15. using DotNetty.Transport.Channels;
  16. public abstract class AbstractHttpData : AbstractReferenceCounted, IHttpData
  17. {
  18. static readonly Regex StripPattern = new Regex("(?:^\\s+|\\s+$|\\n)", RegexOptions.Compiled);
  19. static readonly Regex ReplacePattern = new Regex("[\\r\\t]", RegexOptions.Compiled);
  20. readonly string name;
  21. protected long DefinedSize;
  22. protected long Size;
  23. Encoding charset = HttpConstants.DefaultEncoding;
  24. bool completed;
  25. long maxSize = DefaultHttpDataFactory.MaxSize;
  26. protected AbstractHttpData(string name, Encoding charset, long size)
  27. {
  28. Contract.Requires(name != null);
  29. name = StripPattern.Replace(name, " ");
  30. name = ReplacePattern.Replace(name, "");
  31. if (string.IsNullOrEmpty(name))
  32. {
  33. throw new ArgumentException("empty name");
  34. }
  35. this.name = name;
  36. if (charset != null)
  37. {
  38. this.charset = charset;
  39. }
  40. this.DefinedSize = size;
  41. }
  42. public long MaxSize
  43. {
  44. get => this.maxSize;
  45. set => this.maxSize = value;
  46. }
  47. public void CheckSize(long newSize)
  48. {
  49. if (this.MaxSize >= 0 && newSize > this.MaxSize)
  50. {
  51. throw new IOException("Size exceed allowed maximum capacity");
  52. }
  53. }
  54. public string Name => this.name;
  55. public bool IsCompleted => this.completed;
  56. protected void SetCompleted() => this.completed = true;
  57. public Encoding Charset
  58. {
  59. get => this.charset;
  60. set
  61. {
  62. Contract.Requires(value != null);
  63. this.charset = value;
  64. }
  65. }
  66. public long Length => this.Size;
  67. public long DefinedLength => this.DefinedSize;
  68. public IByteBuffer Content
  69. {
  70. get
  71. {
  72. try
  73. {
  74. return this.GetByteBuffer();
  75. }
  76. catch (IOException e)
  77. {
  78. throw new ChannelException(e);
  79. }
  80. }
  81. }
  82. protected override void Deallocate() => this.Delete();
  83. public abstract int CompareTo(IInterfaceHttpData other);
  84. public abstract HttpDataType DataType { get; }
  85. public abstract IByteBufferHolder Copy();
  86. public abstract IByteBufferHolder Duplicate();
  87. public abstract IByteBufferHolder RetainedDuplicate();
  88. public abstract void SetContent(IByteBuffer buffer);
  89. public abstract void SetContent(Stream source);
  90. public abstract void AddContent(IByteBuffer buffer, bool last);
  91. public abstract void Delete();
  92. public abstract byte[] GetBytes();
  93. public abstract IByteBuffer GetByteBuffer();
  94. public abstract IByteBuffer GetChunk(int length);
  95. public virtual string GetString() => this.GetString(this.charset);
  96. public abstract string GetString(Encoding encoding);
  97. public abstract bool RenameTo(FileStream destination);
  98. public abstract bool IsInMemory { get; }
  99. public abstract FileStream GetFile();
  100. public abstract IByteBufferHolder Replace(IByteBuffer content);
  101. }
  102. }