UrlEncodedStream.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using Best.HTTP.Shared.PlatformSupport.Text;
  6. namespace Best.HTTP.Request.Upload.Forms
  7. {
  8. /// <summary>
  9. /// Readonly struct to hold key -> value pairs, where the value is either textual or binary.
  10. /// </summary>
  11. readonly struct FormField
  12. {
  13. public readonly string Key;
  14. public readonly string TextValue;
  15. public readonly byte[] BinaryValue;
  16. public FormField(string key, string textValue)
  17. {
  18. this.Key = key;
  19. this.TextValue = textValue;
  20. this.BinaryValue = null;
  21. }
  22. public FormField(string key, byte[] binaryValue)
  23. {
  24. this.Key = key;
  25. this.TextValue = null;
  26. this.BinaryValue = binaryValue;
  27. }
  28. }
  29. /// <summary>
  30. /// An <see cref="UploadStreamBase"/> implementation representing a stream that prepares and sends data as URL-encoded form data in an HTTP request.
  31. /// </summary>
  32. /// <remarks>
  33. /// <para>This stream is used to send data as URL-encoded form data in an HTTP request. It sets the <c>"Content-Type"</c> header to <c>"application/x-www-form-urlencoded"</c>.
  34. /// URL-encoded form data is typically used for submitting form data to a web server. It is commonly used in HTTP POST requests to send data to a server, such as submitting HTML form data.</para>
  35. ///
  36. /// <para>The return value of <see cref="System.IO.Stream.Read(byte[], int, int)"/> is treated specially in the plugin:
  37. /// <list type="bullet">
  38. /// <item>
  39. /// <term>Less than zero(<c>-1</c>) value </term>
  40. /// <description> indicates that no data is currently available but more is expected in the future. In this case, when new data becomes available the IThreadSignaler object must be signaled.</description>
  41. /// </item>
  42. /// <item>
  43. /// <term>Zero (<c>0</c>)</term>
  44. /// <description> means that the stream is closed, no more data can be expected.</description>
  45. /// </item>
  46. /// </list>
  47. /// A zero value to signal stream closure can follow a less than zero value.</para>
  48. /// <para>While it's possible, it's not advised to send binary data url-encoded!</para>
  49. /// </remarks>
  50. public sealed class UrlEncodedStream : UploadStreamBase
  51. {
  52. private const int EscapeTreshold = 256;
  53. /// <summary>
  54. /// Gets the length of the stream.
  55. /// </summary>
  56. public override long Length { get => this._memoryStream.Length; }
  57. private MemoryStream _memoryStream;
  58. /// <summary>
  59. /// A list that holds the form's fields.
  60. /// </summary>
  61. private List<FormField> _fields = new List<FormField>();
  62. /// <summary>
  63. /// Sets up the HTTP request by adding the <c>"Content-Type"</c> header as <c>"application/x-www-form-urlencoded"</c>.
  64. /// </summary>
  65. /// <param name="request">The HTTP request.</param>
  66. public override void BeforeSendHeaders(HTTPRequest request)
  67. {
  68. request.SetHeader("Content-Type", "application/x-www-form-urlencoded");
  69. StringBuilder sb = StringBuilderPool.Get(_fields.Count * 4);
  70. // Create a "field1=value1&field2=value2" formatted string
  71. for (int i = 0; i < _fields.Count; ++i)
  72. {
  73. var field = _fields[i];
  74. if (i > 0)
  75. sb.Append("&");
  76. sb.Append(EscapeString(field.Key));
  77. sb.Append("=");
  78. if (!string.IsNullOrEmpty(field.TextValue) || field.BinaryValue == null)
  79. sb.Append(EscapeString(field.TextValue));
  80. else
  81. // If forced to this form type with binary data, we will create a base64 encoded string from it.
  82. sb.Append(Convert.ToBase64String(field.BinaryValue, 0, field.BinaryValue.Length));
  83. }
  84. this._memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(StringBuilderPool.ReleaseAndGrab(sb)));
  85. }
  86. /// <summary>
  87. /// Adds binary data to the form. It is not advised to send binary data with an URL-encoded form due to the conversion cost of binary to text conversion.
  88. /// </summary>
  89. /// <param name="fieldName">The name of the field.</param>
  90. /// <param name="content">The binary data content.</param>
  91. /// <returns>The UrlEncodedStream instance for method chaining.</returns>
  92. public UrlEncodedStream AddBinaryData(string fieldName, byte[] content)
  93. {
  94. _fields.Add(new FormField(fieldName, content));
  95. return this;
  96. }
  97. public UrlEncodedStream AddField(string fieldName, string value)
  98. {
  99. _fields.Add(new FormField(fieldName, value));
  100. return this;
  101. }
  102. public override int Read(byte[] buffer, int offset, int count) => this._memoryStream.Read(buffer, offset, count);
  103. private static string EscapeString(string originalString)
  104. {
  105. if (originalString.Length < EscapeTreshold)
  106. return Uri.EscapeDataString(originalString);
  107. else
  108. {
  109. int loops = originalString.Length / EscapeTreshold;
  110. StringBuilder sb = StringBuilderPool.Get(loops); //new StringBuilder(loops);
  111. for (int i = 0; i <= loops; i++)
  112. sb.Append(i < loops ?
  113. Uri.EscapeDataString(originalString.Substring(EscapeTreshold * i, EscapeTreshold)) :
  114. Uri.EscapeDataString(originalString.Substring(EscapeTreshold * i)));
  115. return StringBuilderPool.ReleaseAndGrab(sb);
  116. }
  117. }
  118. protected override void Dispose(bool disposing)
  119. {
  120. base.Dispose(disposing);
  121. this._memoryStream?.Dispose();
  122. this._memoryStream = null;
  123. }
  124. public override bool CanRead => true;
  125. public override bool CanSeek => false;
  126. public override bool CanWrite => false;
  127. public override long Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
  128. public override long Seek(long offset, SeekOrigin origin) => throw new NotImplementedException();
  129. public override void SetLength(long value) => throw new NotImplementedException();
  130. public override void Write(byte[] buffer, int offset, int count) => throw new NotImplementedException();
  131. public override void Flush() => throw new NotImplementedException();
  132. }
  133. }