Skip to content

Commit

Permalink
com.utilities.rest 2.5.5 (#71)
Browse files Browse the repository at this point in the history
- reformatted multipart form data debug logs. Removed byte content in logs.
  • Loading branch information
StephenHodgson committed Mar 18, 2024
1 parent 0d9459e commit ee2ab99
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
39 changes: 35 additions & 4 deletions Runtime/Rest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -1137,7 +1138,7 @@ async void CallbackThread()
}
}

#pragma warning disable CS4014 // We purposefully don't await this task so it will run on a background thread.
#pragma warning disable CS4014 // We purposefully don't await this task, so it will run on a background thread.
Task.Run(CallbackThread, cancellationToken);
#pragma warning restore CS4014
}
Expand All @@ -1146,9 +1147,39 @@ async void CallbackThread()

if (hasUpload && webRequest.uploadHandler != null)
{
requestBody = webRequest.uploadHandler.data is { Length: > 0 }
? Encoding.UTF8.GetString(webRequest.uploadHandler.data)
: string.Empty;
var contentType = webRequest.GetRequestHeader(content_type);
if (webRequest.uploadHandler.data is { Length: > 0 } &&
contentType.Contains("multipart/form-data"))
{
var boundary = contentType.Split(';')[1].Split('=')[1];
var encodedData = Encoding.UTF8.GetString(webRequest.uploadHandler.data);
var formData = encodedData.Split(new[] { $"\r\n--{boundary}\r\n", $"\r\n--{boundary}--\r\n" }, StringSplitOptions.RemoveEmptyEntries);
var formParts = new Dictionary<string, string>();

foreach (var form in formData)
{
var formFields = form.Split(new[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries);
var fieldHeader = formFields[0];
var key = fieldHeader.Split(new[] { "name=\"" }, StringSplitOptions.RemoveEmptyEntries)[1].Split("\"")[0];

if (fieldHeader.Contains("application/octet-stream"))
{
var fileName = fieldHeader.Split(new[] { "filename=\"" }, StringSplitOptions.RemoveEmptyEntries)[1].Split("\"")[0];
formParts.Add(key, fileName);
}
else
{
var value = formFields[1];
formParts.Add(key, value);
}
}

requestBody = JsonConvert.SerializeObject(new { contentType, formParts });
}
else
{
requestBody = string.Empty;
}
}

try
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "Utilities.Rest",
"description": "This package contains useful RESTful utilities for the Unity Game Engine.",
"keywords": [],
"version": "2.5.4",
"version": "2.5.5",
"unity": "2021.3",
"documentationUrl": "https://github.com/RageAgainstThePixel/com.utilities.rest#documentation",
"changelogUrl": "https://github.com/RageAgainstThePixel/com.utilities.rest/releases",
Expand Down

0 comments on commit ee2ab99

Please sign in to comment.