This commit is contained in:
2020-07-09 08:50:24 +08:00
parent 13d25f4707
commit c523462b82
1818 changed files with 174940 additions and 582 deletions

View File

@@ -0,0 +1,79 @@
using BestHTTP.Extensions;
namespace BestHTTP.Forms
{
/// <summary>
/// A HTTP Form implementation to send textual and binary values.
/// </summary>
public sealed class HTTPMultiPartForm : HTTPFormBase
{
#region Private Fields
/// <summary>
/// A random boundary generated in the constructor.
/// </summary>
private string Boundary;
/// <summary>
///
/// </summary>
private byte[] CachedData;
#endregion
public HTTPMultiPartForm()
{
this.Boundary = this.GetHashCode().ToString("X");
}
#region IHTTPForm Implementation
public override void PrepareRequest(HTTPRequest request)
{
// Set up Content-Type header for the request
request.SetHeader("Content-Type", "multipart/form-data; boundary=\"" + Boundary + "\"");
}
public override byte[] GetData()
{
if (CachedData != null)
return CachedData;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
for (int i = 0; i < Fields.Count; ++i)
{
HTTPFieldData field = Fields[i];
// Set the boundary
ms.WriteLine("--" + Boundary);
// Set up Content-Disposition header to our form with the name
ms.WriteLine("Content-Disposition: form-data; name=\"" + field.Name + "\"" + (!string.IsNullOrEmpty(field.FileName) ? "; filename=\"" + field.FileName + "\"" : string.Empty));
// Set up Content-Type head for the form.
if (!string.IsNullOrEmpty(field.MimeType))
ms.WriteLine("Content-Type: " + field.MimeType);
ms.WriteLine("Content-Length: " + field.Payload.Length.ToString());
ms.WriteLine();
// Write the actual data to the MemoryStream
ms.Write(field.Payload, 0, field.Payload.Length);
ms.Write(HTTPRequest.EOL, 0, HTTPRequest.EOL.Length);
}
// Write out the trailing boundary
ms.WriteLine("--" + Boundary + "--");
IsChanged = false;
// Set the RawData of our request
return CachedData = ms.ToArray();
}
}
#endregion
};
}

View File

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

View File

@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BestHTTP.Forms
{
/// <summary>
/// A HTTP Form implementation to send textual values.
/// </summary>
public sealed class HTTPUrlEncodedForm : HTTPFormBase
{
private const int EscapeTreshold = 256;
private byte[] CachedData;
public override void PrepareRequest(HTTPRequest request)
{
request.SetHeader("Content-Type", "application/x-www-form-urlencoded");
}
public override byte[] GetData()
{
if (CachedData != null && !IsChanged)
return CachedData;
StringBuilder sb = new StringBuilder();
// 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.Name));
sb.Append("=");
if (!string.IsNullOrEmpty(field.Text) || field.Binary == null)
sb.Append(EscapeString(field.Text));
else
// If forced to this form type with binary data, we will create a string from the binary data first and encode this string.
sb.Append(EscapeString(Encoding.UTF8.GetString(field.Binary, 0, field.Binary.Length)));
}
IsChanged = false;
return CachedData = Encoding.UTF8.GetBytes(sb.ToString());
}
public static string EscapeString(string originalString)
{
if (originalString.Length < EscapeTreshold)
return Uri.EscapeDataString(originalString);
else
{
int loops = originalString.Length / EscapeTreshold;
StringBuilder sb = 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 sb.ToString();
}
}
}
}

View File

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

View File

@@ -0,0 +1,58 @@
#if !BESTHTTP_DISABLE_UNITY_FORM
using UnityEngine;
namespace BestHTTP.Forms
{
/// <summary>
/// For backward compatibility.
/// </summary>
public sealed class UnityForm : HTTPFormBase
{
public WWWForm Form { get; set; }
public UnityForm()
{
}
public UnityForm(WWWForm form)
{
Form = form;
}
public override void CopyFrom(HTTPFormBase fields)
{
this.Fields = fields.Fields;
this.IsChanged = true;
if (Form == null)
{
Form = new WWWForm();
if (Fields != null)
for (int i = 0; i < Fields.Count; ++i)
{
var field = Fields[i];
if (string.IsNullOrEmpty(field.Text) && field.Binary != null)
Form.AddBinaryData(field.Name, field.Binary, field.FileName, field.MimeType);
else
Form.AddField(field.Name, field.Text, field.Encoding);
}
}
}
public override void PrepareRequest(HTTPRequest request)
{
if (Form.headers.ContainsKey("Content-Type"))
request.SetHeader("Content-Type", Form.headers["Content-Type"] as string);
else
request.SetHeader("Content-Type", "application/x-www-form-urlencoded");
}
public override byte[] GetData()
{
return Form.data;
}
}
}
#endif

View File

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