HTTPFormBase.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace BestHTTP.Forms
  6. {
  7. /// <summary>
  8. /// Base class of a concrete implementation. Don't use it to create a form, use instead one of the already wrote implementation(HTTPMultiPartForm, HTTPUrlEncodedForm), or create a new one by inheriting from this base class.
  9. /// </summary>
  10. public class HTTPFormBase
  11. {
  12. const int LongLength = 256;
  13. #region Properties
  14. /// <summary>
  15. /// A list that holds the form's fields.
  16. /// </summary>
  17. public List<HTTPFieldData> Fields { get; set; }
  18. /// <summary>
  19. /// Returns true if the Fields has no element.
  20. /// </summary>
  21. public bool IsEmpty { get { return Fields == null || Fields.Count == 0; } }
  22. /// <summary>
  23. /// True if new fields has been added to our list.
  24. /// </summary>
  25. public bool IsChanged { get; protected set; }
  26. /// <summary>
  27. /// True if there are at least one form-field with binary data.
  28. /// </summary>
  29. public bool HasBinary { get; protected set; }
  30. /// <summary>
  31. /// True if there are at least one form-field with a long textual data.
  32. /// </summary>
  33. public bool HasLongValue { get; protected set; }
  34. #endregion
  35. #region Field Management
  36. public void AddBinaryData(string fieldName, byte[] content)
  37. {
  38. AddBinaryData(fieldName, content, null, null);
  39. }
  40. public void AddBinaryData(string fieldName, byte[] content, string fileName)
  41. {
  42. AddBinaryData(fieldName, content, fileName, null);
  43. }
  44. public void AddBinaryData(string fieldName, byte[] content, string fileName, string mimeType)
  45. {
  46. if (Fields == null)
  47. Fields = new List<HTTPFieldData>();
  48. HTTPFieldData field = new HTTPFieldData();
  49. field.Name = fieldName;
  50. if (fileName == null)
  51. field.FileName = fieldName + ".dat";
  52. else
  53. field.FileName = fileName;
  54. if (mimeType == null)
  55. field.MimeType = "application/octet-stream";
  56. else
  57. field.MimeType = mimeType;
  58. field.Binary = content;
  59. Fields.Add(field);
  60. HasBinary = IsChanged = true;
  61. }
  62. public void AddField(string fieldName, string value)
  63. {
  64. AddField(fieldName, value, System.Text.Encoding.UTF8);
  65. }
  66. public void AddField(string fieldName, string value, System.Text.Encoding e)
  67. {
  68. if (Fields == null)
  69. Fields = new List<HTTPFieldData>();
  70. HTTPFieldData field = new HTTPFieldData();
  71. field.Name = fieldName;
  72. field.FileName = null;
  73. if (e != null)
  74. field.MimeType = "text/plain; charset=" + e.WebName;
  75. field.Text = value;
  76. field.Encoding = e;
  77. Fields.Add(field);
  78. IsChanged = true;
  79. HasLongValue |= value.Length > LongLength;
  80. }
  81. #endregion
  82. #region Virtual Functions
  83. /// <summary>
  84. /// It should 'clone' all the data from the given HTTPFormBase object.
  85. /// Called after the form-implementation created.
  86. /// </summary>
  87. public virtual void CopyFrom(HTTPFormBase fields)
  88. {
  89. this.Fields = new List<HTTPFieldData>(fields.Fields);
  90. this.IsChanged = true;
  91. this.HasBinary = fields.HasBinary;
  92. this.HasLongValue = fields.HasLongValue;
  93. }
  94. /// <summary>
  95. /// Prepares the request to sending a form. It should set only the headers.
  96. /// </summary>
  97. public virtual void PrepareRequest(HTTPRequest request)
  98. {
  99. throw new NotImplementedException();
  100. }
  101. /// <summary>
  102. /// Prepares and returns with the form's raw data.
  103. /// </summary>
  104. public virtual byte[] GetData()
  105. {
  106. throw new NotImplementedException();
  107. }
  108. #endregion
  109. }
  110. }