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 authored Mar 18, 2024
1 parent 558830f commit 55b20fd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
39 changes: 35 additions & 4 deletions Utilities.Rest/Packages/com.utilities.rest/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 Utilities.Rest/Packages/com.utilities.rest/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
4 changes: 2 additions & 2 deletions Utilities.Rest/Packages/manifest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"dependencies": {
"com.unity.addressables": "1.21.20",
"com.unity.ide.rider": "3.0.27",
"com.unity.ide.rider": "3.0.28",
"com.unity.ide.visualstudio": "2.0.22",
"com.utilities.buildpipeline": "1.2.2"
"com.utilities.buildpipeline": "1.3.0"
},
"scopedRegistries": [
{
Expand Down

0 comments on commit 55b20fd

Please sign in to comment.