Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: v3 .NET #144

Merged
merged 16 commits into from
Jun 8, 2023
Prev Previous commit
Next Next commit
PR review changes
  • Loading branch information
Meldiron committed Feb 26, 2023
commit 01448884a2ec32de1e6862f56593b242662208d0
6 changes: 3 additions & 3 deletions runtimes/dotnet-6.0/example/Index.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace DotNetRuntime;

using Newtonsoft.Json;
using System.Text.Json;

public class Handler {
static readonly HttpClient http = new();
Expand All @@ -12,11 +12,11 @@ public async Task<RuntimeOutput> Main(RuntimeContext Context)
if (!(Context.Req.Body is String))
{
Dictionary<string, object> body = (Dictionary<string, object>) Context.Req.Body;
id = body.TryGetValue("id", out var value) ? (string) value : "1";
id = body.TryGetValue("id", out string value) ? value : "1";
}

var response = await http.GetStringAsync($"https://jsonplaceholder.typicode.com/todos/{id}");
var todo = JsonConvert.DeserializeObject<Dictionary<string, object>>(response, settings: null);
var todo = JsonSerializer.Deserialize<Dictionary<string, object>>(response) ?? new Dictionary<string, object>();

return Context.Res.Json(new()
{
Expand Down
2 changes: 1 addition & 1 deletion runtimes/dotnet-6.0/src/CustomResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public CustomResponse(string Body, int StatusCode, Dictionary<string, string>? H

public Task ExecuteAsync(HttpContext HttpContext)
{
string contentType = _Headers.TryGetValue("content-type", out var contentTypeValue) ? (string) contentTypeValue : "plain/text";
var contentType = _Headers.TryGetValue("content-type", out string contentTypeValue) ? contentTypeValue : "plain/text";

foreach (var Entry in _Headers)
{
Expand Down
47 changes: 24 additions & 23 deletions runtimes/dotnet-6.0/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using System.IO;
using System.Text;
using System.Text.Json;
using Newtonsoft.Json;
using System.Web;
using Microsoft.Extensions.Primitives;

var app = WebApplication.Create(args);
app.Urls.Add("http:https://0.0.0.0:3000");
Expand All @@ -13,17 +13,17 @@

static async Task<IResult> Execute(HttpRequest Request)
{
string Secret = Request.Headers.TryGetValue("x-open-runtimes-secret", out var SecretValue) ? (string) SecretValue : "";
var Secret = Request.Headers.TryGetValue("x-open-runtimes-secret", out StringValues SecretValue) ? SecretValue.ToString() : "";
if(Secret == "" || Secret != (Environment.GetEnvironmentVariable("OPEN_RUNTIMES_SECRET") ?? ""))
{
return new CustomResponse("Unauthorized. Provide correct \"x-open-runtimes-secret\" header.", 500);
}

StreamReader Reader = new StreamReader(Request.Body);
string BodyString = await Reader.ReadToEndAsync();
var Reader = new StreamReader(Request.Body);
var BodyString = await Reader.ReadToEndAsync();
object Body = BodyString;
Dictionary<string, string> Headers = new Dictionary<string, string>();
string Method = Request.Method;
var Headers = new Dictionary<string, string>();
var Method = Request.Method;

foreach (var Entry in Request.Headers)
{
Expand All @@ -37,15 +37,15 @@ static async Task<IResult> Execute(HttpRequest Request)
}
}

string ContentType = Request.Headers.TryGetValue("content-type", out var ContentTypeValue) ? (string) ContentTypeValue : "";
var ContentType = Request.Headers.TryGetValue("content-type", out StringValues ContentTypeValue) ? ContentTypeValue.ToString() : "";
if(ContentType.Contains("application/json"))
{
if(String.IsNullOrEmpty(BodyString))
{
Body = new Dictionary<string, object>();
} else
{
Body = JsonConvert.DeserializeObject<Dictionary<string, object>>(BodyString);
Body = JsonSerializer.Deserialize<Dictionary<string, object>>(BodyString) ?? new Dictionary<string, object>();
}
}

Expand All @@ -54,13 +54,13 @@ static async Task<IResult> Execute(HttpRequest Request)
Body = new Dictionary<string, object>();
}

String HostHeader = Request.Headers.TryGetValue("host", out var HostHeaderValue) ? (string) HostHeaderValue : "";
var HostHeader = Request.Headers.TryGetValue("host", out StringValues HostHeaderValue) ? HostHeaderValue.ToString() : "";

String Scheme = Request.Headers.TryGetValue("x-forwarded-proto", out var ProtoHeaderValue) ? (string) ProtoHeaderValue : "http";
String DefaultPort = Scheme == "https" ? "443" : "80";
var Scheme = Request.Headers.TryGetValue("x-forwarded-proto", out StringValues ProtoHeaderValue) ? ProtoHeaderValue.ToString() : "http";
var DefaultPort = Scheme == "https" ? "443" : "80";

String Host = "";
int Port = Int32.Parse(DefaultPort);
var Host = "";
var Port = Int32.Parse(DefaultPort);

if(HostHeader.Contains(":"))
{
Expand All @@ -72,26 +72,26 @@ static async Task<IResult> Execute(HttpRequest Request)
Port = Int32.Parse(DefaultPort);
}

String Path = Request.Path;
var Path = Request.Path;

String QueryString = Request.QueryString.Value ?? "";
var QueryString = Request.QueryString.Value ?? "";
if(QueryString.StartsWith("?")) {
QueryString = QueryString.Remove(0,1);
}

Dictionary<string, string> Query = new Dictionary<string, string>();
var Query = new Dictionary<string, string>();

foreach (String param in QueryString.Split("&"))
foreach (var param in QueryString.Split("&"))
{
String[] pair = param.Split("=", 2);
var pair = param.Split("=", 2);

if(pair.Length >= 1 && !String.IsNullOrEmpty(pair[0])) {
String Value = pair.Length == 2 ? pair[1] : "";
var Value = pair.Length == 2 ? pair[1] : "";
Query.Add(pair[0], Value);
}
}

String Url = Scheme + ":https://" + Host;
var Url = Scheme + ":https://" + Host;

if(Port != Int32.Parse(DefaultPort))
{
Expand All @@ -104,9 +104,10 @@ static async Task<IResult> Execute(HttpRequest Request)
Url += "?" + QueryString;
}

RuntimeRequest ContextRequest = new RuntimeRequest(BodyString, Body, Headers, Method, Url, Host, Scheme, Path, QueryString, Query, Port);
RuntimeResponse ContextResponse = new RuntimeResponse();
RuntimeContext Context = new RuntimeContext(ContextRequest, ContextResponse);

var ContextRequest = new RuntimeRequest(Url, Method, Scheme, Host, Port, Path, Query, QueryString, Headers, Body, BodyString);
var ContextResponse = new RuntimeResponse();
var Context = new RuntimeContext(ContextRequest, ContextResponse);

var originalOut = Console.Out;
var originalErr = Console.Error;
Expand Down
8 changes: 4 additions & 4 deletions runtimes/dotnet-6.0/src/RuntimeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class RuntimeContext
public RuntimeRequest Req { get; set; }
public RuntimeResponse Res { get; set; }

public ArrayList Logs = new ArrayList();
public ArrayList Errors = new ArrayList();
public List<String> Logs = new List<String>();
public List<String> Errors = new List<String>();

public RuntimeContext(RuntimeRequest Req, RuntimeResponse Res)
{
Expand All @@ -22,7 +22,7 @@ public void Log(object Message)
if (Message is IList || Message is IDictionary) {
this.Logs.Add(JsonSerializer.Serialize(Message));
} else {
this.Logs.Add(Message.ToString());
this.Logs.Add(Message.ToString() ?? "");
}
}

Expand All @@ -31,7 +31,7 @@ public void Error(object Message)
if (Message is IList || Message is IDictionary) {
this.Errors.Add(JsonSerializer.Serialize(Message));
} else {
this.Errors.Add(Message.ToString());
this.Errors.Add(Message.ToString() ?? "");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion runtimes/dotnet-6.0/src/RuntimeRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class RuntimeRequest
public Dictionary<string, string> Query { get; set; }
public int Port { get; set; }

public RuntimeRequest(string BodyString, object Body, Dictionary<string, string> Headers, string Method, string Url, string Host, string Scheme, string Path, string QueryString, Dictionary<string, string> Query, int Port)
public RuntimeRequest(string Url, string Method, string Scheme, string Host, int Port, string Path, Dictionary<string, string> Query, string QueryString, Dictionary<string, string> Headers, object Body, string BodyString)
{
this.BodyString = BodyString;
this.Body = Body;
Expand Down
24 changes: 12 additions & 12 deletions tests/resources/functions/dotnet-6.0/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
namespace DotNetRuntime;

using System;
using Newtonsoft.Json;
using System.Collections;
using System.Text.Json;

public class Handler {
static readonly HttpClient http = new();

public async Task<RuntimeOutput> Main(RuntimeContext Context)
{
string Action = Context.Req.Headers.TryGetValue("x-action", out var ActionValue) ? (string) ActionValue : "";
var Action = Context.Req.Headers.TryGetValue("x-action", out string ActionValue) ? ActionValue : "";

switch (Action)
{
Expand All @@ -35,9 +35,9 @@ public async Task<RuntimeOutput> Main(RuntimeContext Context)
Context.Res.Send("This should be ignored.");
return Context.Res.Send("This should be returned.");
case "headersResponse":
Dictionary<string, string> Headers = new Dictionary<string, string>();
var Headers = new Dictionary<string, string>();
Headers.Add("first-header", "first-value");
string SecondHeader = Context.Req.Headers.TryGetValue("x-open-runtimes-custom-in-header", out var SecondHeaderValue) ? (string) SecondHeaderValue : "missing";
var SecondHeader = Context.Req.Headers.TryGetValue("x-open-runtimes-custom-in-header", out string SecondHeaderValue) ? SecondHeaderValue : "missing";
Headers.Add("second-header", SecondHeader);
Headers.Add("x-open-runtimes-custom-out-header", "third-value");
return Context.Res.Send("OK", 200, Headers);
Expand All @@ -57,7 +57,7 @@ public async Task<RuntimeOutput> Main(RuntimeContext Context)
{ "host", Context.Req.Host }
});
case "requestHeaders":
Dictionary<string, object> Json = new Dictionary<string, object>();
var Json = new Dictionary<string, object>();

foreach (var Entry in Context.Req.Headers)
{
Expand All @@ -68,14 +68,14 @@ public async Task<RuntimeOutput> Main(RuntimeContext Context)
case "requestBodyPlaintext":
return Context.Res.Send((string) Context.Req.Body);
case "requestBodyJson":
string Key1 = "";
string Key2 = "";
var Key1 = "";
var Key2 = "";

if(Context.Req.Body is string) {
Key1 = "Missing key";
Key2 = "Missing key";
} else {
Dictionary<String, Object> Body = (Dictionary<String, Object>) Context.Req.Body;
var Body = (Dictionary<String, Object>) Context.Req.Body;

Key1 = Body.TryGetValue("key1", out var Key1Value) ? Key1Value.ToString() : "Missing key";
Key2 = Body.TryGetValue("key2", out var Key2Value) ? Key2Value.ToString() : "Missing key";
Expand All @@ -102,20 +102,20 @@ public async Task<RuntimeOutput> Main(RuntimeContext Context)
Context.Log(4.2);
Context.Log(true);

Dictionary<string, string> Obj = new Dictionary<string, string>();
var Obj = new Dictionary<string, string>();
Obj.Add("objectKey", "objectValue");

Context.Log(Obj);

ArrayList Arr = new ArrayList();
var Arr = new List<string>();
Arr.Add("arrayValue");

Context.Log(Arr);

return Context.Res.Send("");
case "library":
var response = await http.GetStringAsync($"https://jsonplaceholder.typicode.com/todos/" + Context.Req.BodyString);
var todo = JsonConvert.DeserializeObject<Dictionary<string, object>>(response, settings: null);
var response = await http.GetStringAsync($"https://jsonplaceholder.typicode.com/todos/{Context.Req.BodyString}");
var todo = JsonSerializer.Deserialize<Dictionary<string, object>>(response) ?? new Dictionary<string, object>();

return Context.Res.Json(new()
{
Expand Down