Skip to content

Commit

Permalink
support application/x-www-form-urlencoded
Browse files Browse the repository at this point in the history
  • Loading branch information
SlashNephy committed Jul 14, 2024
1 parent 8a8b8b6 commit f060dd4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
22 changes: 19 additions & 3 deletions SimpleVoiceroid2Proxy/Server/Controller.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using System.Web;

namespace SimpleVoiceroid2Proxy.Server;

Expand Down Expand Up @@ -47,13 +50,26 @@ public async Task HandleAsync(HttpContext context)

private async Task HandleGetTalkAsync(HttpContext context)
{
await HandleTalkAsync(context, context.Query["text"]);
await HandleTalkAsync(context, context.Query.GetValues("text")?.FirstOrDefault());
}

private async Task HandlePostTalkAsync(HttpContext context)
{
var payload = await JsonSerializer.DeserializeAsync<Dictionary<string, object?>>(context.Request.InputStream);
await HandleTalkAsync(context, (string?)payload!["text"]);
string? text = null;
if (context.RequestMediaType == "application/x-www-form-urlencoded")
{
using var reader = new StreamReader(context.Request.InputStream);
var body = reader.ReadToEnd();
var form = HttpUtility.ParseQueryString(body);
text = form.GetValues("text")?.FirstOrDefault();
}
else
{
var payload = await JsonSerializer.DeserializeAsync<Dictionary<string, JsonElement>>(context.Request.InputStream);
text = payload!["text"].GetString();
}

await HandleTalkAsync(context, text);
}

private async Task HandleTalkAsync(HttpContext context, string? text)
Expand Down
9 changes: 9 additions & 0 deletions SimpleVoiceroid2Proxy/Server/HttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
Expand All @@ -14,6 +15,14 @@ public sealed class HttpContext(HttpListenerContext Context) : IDisposable
public HttpListenerRequest Request => Context.Request;
public HttpListenerResponse Response => Context.Response;
public NameValueCollection Query => HttpUtility.ParseQueryString(Request.Url.Query, Encoding.UTF8);
public string? RequestMediaType
{
get
{
MediaTypeHeaderValue.TryParse(Request.ContentType, out var value);
return value.MediaType;
}
}

public async Task RespondJson(HttpStatusCode code, Dictionary<string, object?> payload)
{
Expand Down
1 change: 1 addition & 0 deletions SimpleVoiceroid2Proxy/SimpleVoiceroid2Proxy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="System.Net.Http" />
<Reference Include="System.Web" />
<PackageReference Include="Codeer.Friendly.Windows.Grasp" Version="2.14.2" />
<PackageReference Include="RM.Friendly.WPFStandardControls" Version="1.59.1" />
Expand Down
17 changes: 2 additions & 15 deletions SimpleVoiceroid2Proxy/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@
"Codeer.Friendly.Windows": "2.17.0"
}
},
"Microsoft.NETFramework.ReferenceAssemblies": {
"type": "Direct",
"requested": "[1.0.3, )",
"resolved": "1.0.3",
"contentHash": "vUc9Npcs14QsyOD01tnv/m8sQUnGTGOw1BCmKcv77LBJY7OxhJ+zJF7UD/sCL3lYNFuqmQEVlkfS4Quif6FyYg==",
"dependencies": {
"Microsoft.NETFramework.ReferenceAssemblies.net481": "1.0.3"
}
},
"RM.Friendly.WPFStandardControls": {
"type": "Direct",
"requested": "[1.59.1, )",
Expand Down Expand Up @@ -80,11 +71,6 @@
"System.Threading.Tasks.Extensions": "4.5.4"
}
},
"Microsoft.NETFramework.ReferenceAssemblies.net481": {
"type": "Transitive",
"resolved": "1.0.3",
"contentHash": "Vv/20vgHS7VglVOVh8J3Iz/MA+VYKVRp9f7r2qiKBMuzviTOmocG70yq0Q8T5OTmCONkEAIJwETD1zhEfLkAXQ=="
},
"System.Buffers": {
"type": "Transitive",
"resolved": "4.5.1",
Expand Down Expand Up @@ -133,6 +119,7 @@
"resolved": "4.5.0",
"contentHash": "okurQJO6NRE/apDIP23ajJ0hpiNmJ+f0BwOlB/cSqTLQlw5upkf+5+96+iG2Jw40G1fCVCyPz/FhIABUjMR+RQ=="
}
}
},
".NETFramework,Version=v4.8.1/win7-x86": {}
}
}

0 comments on commit f060dd4

Please sign in to comment.