This commit is contained in:
2026-06-15 18:18:16 +08:00
parent 97c9fba14e
commit 2b9f134e5f
4164 changed files with 386922 additions and 79 deletions

View File

@@ -0,0 +1,271 @@
using System;
using System.Collections.Generic;
using System.IO;
using Best.HTTP.Hosts.Connections;
using Best.HTTP.Shared.Extensions;
using Best.HTTP.Shared.PlatformSupport.Memory;
using Best.HTTP.Shared.Streams;
using UnityEngine;
using static Best.HTTP.Hosts.Connections.HTTP1.Constants;
namespace Best.HTTP.Request.Upload.Forms
{
/// <summary>
/// An <see cref="UploadStreamBase"/> based implementation of the <c>multipart/form-data</c> Content-Type. It's very memory-effective, streams are read into memory in chunks.
/// </summary>
/// <remarks>
/// <para>The return value of <see cref="System.IO.Stream.Read(byte[], int, int)"/> is treated specially in the plugin:
/// <list type="bullet">
/// <item>
/// <term>Less than zero(<c>-1</c>) value </term>
/// <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>
/// </item>
/// <item>
/// <term>Zero (<c>0</c>)</term>
/// <description> means that the stream is closed, no more data can be expected.</description>
/// </item>
/// </list>
/// A zero value to signal stream closure can follow a less than zero value.</para>
/// </remarks>
public sealed class MultipartFormDataStream : UploadStreamBase
{
/// <summary>
/// Gets the length of this multipart/form-data stream.
/// </summary>
public override long Length { get => this._length; }
private long _length;
/// <summary>
/// A random boundary generated in the constructor.
/// </summary>
private string boundary;
private Queue<StreamList> fields = new Queue<StreamList>(1);
private StreamList currentField;
/// <summary>
/// Initializes a new instance of the MultipartFormDataStream class.
/// </summary>
public MultipartFormDataStream()
{
var hash = new Hash128();
hash.Append(this.GetHashCode());
this.boundary = $"com.Tivadar.Best.HTTP.boundary.{hash}";
}
/// <summary>
/// Initializes a new instance of the MultipartFormDataStream class with a custom boundary.
/// </summary>
public MultipartFormDataStream(string boundary)
{
this.boundary = boundary;
}
public override void BeforeSendHeaders(HTTPRequest request)
{
request.SetHeader("Content-Type", $"multipart/form-data; boundary=\"{this.boundary}\"");
var boundaryStream = new BufferPoolMemoryStream();
boundaryStream.WriteLine("--" + this.boundary + "--");
boundaryStream.Position = 0;
this.fields.Enqueue(new StreamList(boundaryStream));
if (this._length >= 0)
this._length += boundaryStream.Length;
}
/// <summary>
/// Adds a textual field to the multipart/form-data stream.
/// </summary>
/// <param name="fieldName">The name of the field.</param>
/// <param name="value">The textual value of the field.</param>
/// <returns>The MultipartFormDataStream instance for method chaining.</returns>
public MultipartFormDataStream AddField(string fieldName, string value)
=> AddField(fieldName, value, System.Text.Encoding.UTF8);
/// <summary>
/// Adds a textual field to the multipart/form-data stream.
/// </summary>
/// <param name="fieldName">The name of the field.</param>
/// <param name="value">The textual value of the field.</param>
/// <param name="encoding">The encoding to use for the value.</param>
/// <returns>The MultipartFormDataStream instance for method chaining.</returns>
public MultipartFormDataStream AddField(string fieldName, string value, System.Text.Encoding encoding)
{
var enc = encoding ?? System.Text.Encoding.UTF8;
var byteCount = enc.GetByteCount(value);
var buffer = BufferPool.Get(byteCount, true);
var stream = new BufferPoolMemoryStream();
enc.GetBytes(value, 0, value.Length, buffer, 0);
stream.Write(buffer, 0, byteCount);
stream.Position = 0;
string mime = encoding != null ? "text/plain; charset=" + encoding.WebName : null;
return AddStreamField(fieldName, stream, null, mime);
}
/// <summary>
/// Adds a stream field to the multipart/form-data stream.
/// </summary>
/// <param name="fieldName">The name of the field.</param>
/// <param name="data">The data containing the field data.</param>
/// <returns>The MultipartFormDataStream instance for method chaining.</returns>
public MultipartFormDataStream AddField(string fieldName, byte[] data)
=> AddStreamField(fieldName, new MemoryStream(data));
/// <summary>
/// Adds a stream field to the multipart/form-data stream.
/// </summary>
/// <param name="stream">The stream containing the field data.</param>
/// <param name="fieldName">The name of the field.</param>
/// <returns>The MultipartFormDataStream instance for method chaining.</returns>
public MultipartFormDataStream AddStreamField(string fieldName, System.IO.Stream stream)
=> AddStreamField(fieldName, stream, null, null);
/// <summary>
/// Adds a stream field to the multipart/form-data stream.
/// </summary>
/// <param name="stream">The stream containing the field data.</param>
/// <param name="fieldName">The name of the field.</param>
/// <param name="fileName">The name of the file, if applicable.</param>
/// <returns>The MultipartFormDataStream instance for method chaining.</returns>
public MultipartFormDataStream AddStreamField(string fieldName, System.IO.Stream stream, string fileName)
=> AddStreamField(fieldName, stream, fileName, null);
/// <summary>
/// Adds a stream field to the multipart/form-data stream.
/// </summary>
/// <param name="stream">The stream containing the field data.</param>
/// <param name="fieldName">The name of the field.</param>
/// <param name="fileName">The name of the file, if applicable.</param>
/// <param name="mimeType">The MIME type of the content.</param>
/// <returns>The MultipartFormDataStream instance for method chaining.</returns>
public MultipartFormDataStream AddStreamField(string fieldName, System.IO.Stream stream, string fileName, string mimeType)
{
var header = new BufferPoolMemoryStream();
header.WriteLine("--" + this.boundary);
header.WriteLine("Content-Disposition: form-data; name=\"" + fieldName + "\"" + (!string.IsNullOrEmpty(fileName) ? "; filename=\"" + fileName + "\"" : string.Empty));
// Set up Content-Type head for the form.
if (!string.IsNullOrEmpty(mimeType))
header.WriteLine("Content-Type: " + mimeType);
header.WriteLine();
header.Position = 0;
var footer = new BufferPoolMemoryStream();
footer.Write(EOL, 0, EOL.Length);
footer.Position = 0;
// all wrapped streams going to be disposed by the StreamList wrapper.
var wrapper = new StreamList(header, stream, footer);
try
{
if (this._length >= 0)
this._length += wrapper.Length;
}
catch
{
this._length = -1;
}
this.fields.Enqueue(wrapper);
return this;
}
/// <summary>
/// Adds the final boundary to the multipart/form-data stream before sending the request body.
/// </summary>
/// <param name="request">The HTTP request.</param>
/// <param name="threadSignaler">The thread signaler for handling asynchronous operations.</param>
public override void BeforeSendBody(HTTPRequest request, IThreadSignaler threadSignaler)
{
base.BeforeSendBody(request, threadSignaler);
}
/// <summary>
/// Reads data from the multipart/form-data stream into the provided buffer.
/// </summary>
/// <param name="buffer">The buffer to read data into.</param>
/// <param name="offset">The starting offset in the buffer.</param>
/// <param name="length">The maximum number of bytes to read.</param>
/// <returns>The number of bytes read into the buffer.</returns>
public override int Read(byte[] buffer, int offset, int length)
{
if (this.currentField == null && this.fields.Count == 0)
return -1;
if (this.currentField == null && this.fields.Count > 0)
this.currentField = this.fields.Dequeue();
int readCount = 0;
do
{
// read from the current stream
int count = this.currentField.Read(buffer, offset + readCount, length - readCount);
if (count > 0)
readCount += count;
else
{
// if the current field's stream is empty, go for the next one.
// dispose the current one first
try
{
this.currentField.Dispose();
}
catch
{ }
// no more fields/streams? exit
if (this.fields.Count == 0)
break;
// grab the next one
this.currentField = this.fields.Dequeue();
}
// exit when we reach the length goal, or there's no more streams to read from
} while (readCount < length && this.fields.Count > 0);
return readCount;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (fields != null)
{
foreach (var field in fields)
field.Dispose();
fields.Clear();
fields = null;
}
currentField?.Dispose();
currentField = null;
}
public override bool CanRead { get { return true; } }
public override bool CanSeek { get { return false; } }
public override bool CanWrite { get { return false; } }
public override long Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public override long Seek(long offset, SeekOrigin origin) => throw new NotImplementedException();
public override void SetLength(long value) => throw new NotImplementedException();
public override void Write(byte[] buffer, int offset, int count) => throw new NotImplementedException();
public override void Flush() { }
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d54866f088b8c154db3325a2771b58af
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,158 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Best.HTTP.Shared.PlatformSupport.Text;
namespace Best.HTTP.Request.Upload.Forms
{
/// <summary>
/// Readonly struct to hold key -> value pairs, where the value is either textual or binary.
/// </summary>
readonly struct FormField
{
public readonly string Key;
public readonly string TextValue;
public readonly byte[] BinaryValue;
public FormField(string key, string textValue)
{
this.Key = key;
this.TextValue = textValue;
this.BinaryValue = null;
}
public FormField(string key, byte[] binaryValue)
{
this.Key = key;
this.TextValue = null;
this.BinaryValue = binaryValue;
}
}
/// <summary>
/// An <see cref="UploadStreamBase"/> implementation representing a stream that prepares and sends data as URL-encoded form data in an HTTP request.
/// </summary>
/// <remarks>
/// <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>.
/// 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>
///
/// <para>The return value of <see cref="System.IO.Stream.Read(byte[], int, int)"/> is treated specially in the plugin:
/// <list type="bullet">
/// <item>
/// <term>Less than zero(<c>-1</c>) value </term>
/// <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>
/// </item>
/// <item>
/// <term>Zero (<c>0</c>)</term>
/// <description> means that the stream is closed, no more data can be expected.</description>
/// </item>
/// </list>
/// A zero value to signal stream closure can follow a less than zero value.</para>
/// <para>While it's possible, it's not advised to send binary data url-encoded!</para>
/// </remarks>
public sealed class UrlEncodedStream : UploadStreamBase
{
private const int EscapeTreshold = 256;
/// <summary>
/// Gets the length of the stream.
/// </summary>
public override long Length { get => this._memoryStream.Length; }
private MemoryStream _memoryStream;
/// <summary>
/// A list that holds the form's fields.
/// </summary>
private List<FormField> _fields = new List<FormField>();
/// <summary>
/// Sets up the HTTP request by adding the <c>"Content-Type"</c> header as <c>"application/x-www-form-urlencoded"</c>.
/// </summary>
/// <param name="request">The HTTP request.</param>
public override void BeforeSendHeaders(HTTPRequest request)
{
request.SetHeader("Content-Type", "application/x-www-form-urlencoded");
StringBuilder sb = StringBuilderPool.Get(_fields.Count * 4);
// Create a "field1=value1&field2=value2" formatted string
for (int i = 0; i < _fields.Count; ++i)
{
var field = _fields[i];
if (i > 0)
sb.Append("&");
sb.Append(EscapeString(field.Key));
sb.Append("=");
if (!string.IsNullOrEmpty(field.TextValue) || field.BinaryValue == null)
sb.Append(EscapeString(field.TextValue));
else
// If forced to this form type with binary data, we will create a base64 encoded string from it.
sb.Append(Convert.ToBase64String(field.BinaryValue, 0, field.BinaryValue.Length));
}
this._memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(StringBuilderPool.ReleaseAndGrab(sb)));
}
/// <summary>
/// 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.
/// </summary>
/// <param name="fieldName">The name of the field.</param>
/// <param name="content">The binary data content.</param>
/// <returns>The UrlEncodedStream instance for method chaining.</returns>
public UrlEncodedStream AddBinaryData(string fieldName, byte[] content)
{
_fields.Add(new FormField(fieldName, content));
return this;
}
public UrlEncodedStream AddField(string fieldName, string value)
{
_fields.Add(new FormField(fieldName, value));
return this;
}
public override int Read(byte[] buffer, int offset, int count) => this._memoryStream.Read(buffer, offset, count);
private static string EscapeString(string originalString)
{
if (originalString.Length < EscapeTreshold)
return Uri.EscapeDataString(originalString);
else
{
int loops = originalString.Length / EscapeTreshold;
StringBuilder sb = StringBuilderPool.Get(loops); //new StringBuilder(loops);
for (int i = 0; i <= loops; i++)
sb.Append(i < loops ?
Uri.EscapeDataString(originalString.Substring(EscapeTreshold * i, EscapeTreshold)) :
Uri.EscapeDataString(originalString.Substring(EscapeTreshold * i)));
return StringBuilderPool.ReleaseAndGrab(sb);
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
this._memoryStream?.Dispose();
this._memoryStream = null;
}
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public override long Seek(long offset, SeekOrigin origin) => throw new NotImplementedException();
public override void SetLength(long value) => throw new NotImplementedException();
public override void Write(byte[] buffer, int offset, int count) => throw new NotImplementedException();
public override void Flush() => throw new NotImplementedException();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0a92cdbf082f89b42b5db8ba2a373498
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: