Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions vrc-oscquery-lib/OSCQueryHttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
using System.Net;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

using Newtonsoft.Json;

namespace VRC.OSCQuery
{
public class OSCQueryHttpServer : IDisposable
Expand Down Expand Up @@ -33,7 +34,8 @@ public OSCQueryHttpServer(OSCQueryService oscQueryService, ILogger<OSCQueryServi
_listener.Prefixes.Add(prefix);
_preMiddleware = new List<Func<HttpListenerContext, Action, Task>>
{
HostInfoMiddleware
HostInfoMiddleware,
ValueMiddleware
};
_postMiddleware = new List<Func<HttpListenerContext, Action, Task>>
{
Expand Down Expand Up @@ -118,6 +120,45 @@ private async Task HostInfoMiddleware(HttpListenerContext context, Action next)
}
}

private async Task ValueMiddleware(HttpListenerContext context, Action next)
{
if (!context.Request.RawUrl.Contains(Attributes.VALUE))
{
next();
return;
}

try
{
var node = _oscQuery.RootNode.GetNodeWithPath(context.Request.Url.LocalPath);
var returnString = "";
if (node.Access == Attributes.AccessValues.WriteOnly)
{
context.Response.StatusCode = (int)HttpStatusCode.NoContent;
context.Response.ContentType = "text/plain";
returnString = "Inappropriate Request";
}
else
{
context.Response.ContentType = "application/json";
returnString = $"{{ \"VALUE\": {JsonConvert.SerializeObject(node.Value, OSCQueryNode.WriteSettings)} }}";
}

// Send Response
context.Response.Headers.Add("pragma:no-cache");
context.Response.ContentLength64 = returnString.Length;
using (var sw = new StreamWriter(context.Response.OutputStream))
{
await sw.WriteAsync(returnString);
await sw.FlushAsync();
}
}
catch (Exception e)
{
Logger.LogError($"Could not construct and send Value: {e.Message}");
}
}

private static string _pathToResources;

private static string PathToResources
Expand Down
2 changes: 1 addition & 1 deletion vrc-oscquery-lib/OSCQueryNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public static void AddConverter(JsonConverter c)
WriteSettings.Converters.Add(c);
}

private static JsonSerializerSettings WriteSettings = new JsonSerializerSettings()
internal static JsonSerializerSettings WriteSettings = new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore,
};
Expand Down