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
Fix standards
  • Loading branch information
abnegate committed Jun 7, 2023
commit 29d8c7b1cbb58e002e3e3aab27e3061f893de6d6
39 changes: 19 additions & 20 deletions runtimes/dotnet-6.0/src/CustomResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,33 @@ namespace DotNetRuntime
{
class CustomResponse : IResult
{
private readonly string _Body;
private readonly int _StatusCode;
private readonly Dictionary<string, string> _Headers;
private readonly string _body;
private readonly int _statusCode;
private readonly Dictionary<string, string> _headers;

public CustomResponse(string Body, int StatusCode, Dictionary<string, string>? Headers = null)
public CustomResponse(string body, int StatusCode, Dictionary<string, string>? headers = null)
{
if(Headers == null)
{
Headers = new Dictionary<string,string>();
}

_Body = Body;
_StatusCode = StatusCode;
_Headers = Headers;
_body = body;
_statusCode = statusCode;
_headers = headers ?? new Dictionary<string, string>();
}

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

foreach (var Entry in _Headers)
foreach (var entry in _headers)
{
HttpContext.Response.Headers.Add(Entry.Key, Entry.Value);
httpContext.Response.Headers.Add(entry.Key, entry.Value);
}
HttpContext.Response.StatusCode = _StatusCode;
HttpContext.Response.ContentType = contentType;
HttpContext.Response.ContentLength = Encoding.UTF8.GetByteCount(_Body);
return HttpContext.Response.WriteAsync(_Body);

httpContext.Response.StatusCode = _statusCode;
httpContext.Response.ContentType = contentType;
httpContext.Response.ContentLength = Encoding.UTF8.GetByteCount(_body);

return httpContext.Response.WriteAsync(_body);
}
}
}
157 changes: 78 additions & 79 deletions runtimes/dotnet-6.0/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,156 +11,155 @@
app.MapMethods("/{*path}", new[] { "GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD", "TRACE" }, Execute);
app.Run();

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

var Reader = new StreamReader(Request.Body);
var BodyString = await Reader.ReadToEndAsync();
object Body = BodyString;
var Headers = new Dictionary<string, string>();
var Method = Request.Method;
var reader = new StreamReader(request.Body);
var bodystring = await reader.ReadToEndAsync();
object body = bodystring;
var headers = new Dictionary<string, string>();
var method = request.Method;

foreach (var Entry in Request.Headers)
foreach (var entry in Request.Headers)
{
var Header = Entry.Key;
var Value = Entry.Value;
Header = Header.ToLower();
var header = entry.Key.ToLower();
var value = entry.Value;

if(!(Header.StartsWith("x-open-runtimes-")))
{
Headers.Add(Header, Value);
headers.Add(header, value);
}
}

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

if(Body == null)
if(body == null)
{
Body = new Dictionary<string, object>();
body = new Dictionary<string, object>();
}

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

var Scheme = Request.Headers.TryGetValue("x-forwarded-proto", out StringValues ProtoHeaderValue) ? ProtoHeaderValue.ToString() : "http";
var 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";

var Host = "";
var Port = Int32.Parse(DefaultPort);
var host = "";
var port = Int32.Parse(defaultPort);

if(HostHeader.Contains(":"))
if(hostHeader.Contains(":"))
{
Host = HostHeader.Split(":")[0];
Port = Int32.Parse(HostHeader.Split(":")[1]);
} else
host = hostHeader.Split(":")[0];
port = Int32.Parse(HostHeader.Split(":")[1]);
}
else
{
Host = HostHeader;
Port = Int32.Parse(DefaultPort);
host = HostHeader;
port = Int32.Parse(defaultPort);
}

var Path = Request.Path;
var path = request.Path;

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

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

foreach (var param in QueryString.Split("&"))
var query = new Dictionary<string, string>();
foreach (var param in querystring.Split("&"))
{
var pair = param.Split("=", 2);

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

var Url = Scheme + ":https://" + Host;
var url = $"{scheme}:https://{host}";

if(Port != Int32.Parse(DefaultPort))
if(port != Int32.Parse(defaultPort))
{
Url += ":" + Port.ToString();
url += $":{port.ToString()};
}

Url += Path;
url += Path;

if(!String.IsNullOrEmpty(QueryString)) {
Url += "?" + QueryString;
if (!string.IsNullOrEmpty(Querystring))
{
url += $"?{querystring}";
}


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 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;

var Customstd = new StringBuilder();
var CustomstdWriter = new StringWriter(Customstd);
Console.SetOut(CustomstdWriter);
Console.SetError(CustomstdWriter);
var customStd = new stringBuilder();
var customStdWriter = new stringWriter(customStd);
Console.SetOut(customStdWriter);
Console.SetError(customStdWriter);

RuntimeOutput? Output = null;
RuntimeOutput? output = null;

try
{
var CodeHandler = new Handler();
Output = await CodeHandler.Main(Context);
output = new Handler().Main(context));
}
catch (Exception e)
{
Context.Error(e.ToString());
Output = Context.Res.Send("", 500, new Dictionary<string,string>());
context.Error(e.ToString());
output = context.Res.Send("", 500, new Dictionary<string,string>());
}
finally {
finally
{
Console.SetOut(originalOut);
Console.SetError(originalErr);
}

if(Output == null)
if(output == null)
{
Context.Error("Return statement missing. return Context.Res.Empty() if no response is expected.");
Output = Context.Res.Send("", 500, new Dictionary<string,string>());
context.Error("Return statement missing. return context.Res.Empty() if no response is expected.");
output = context.Res.Send("", 500, new Dictionary<string,string>());
}

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

foreach (var Entry in Output.Headers)
var outputHeaders = new Dictionary<string, string>();
foreach (var entry in output.Headers)
{
var Header = Entry.Key;
var Value = Entry.Value;
Header = Header.ToLower();
var header = entry.Key.ToLower();
var value = entry.Value;

if(!(Header.StartsWith("x-open-runtimes-")))
if (!(Header.StartsWith("x-open-runtimes-")))
{
OutputHeaders.Add(Header, Value);
outputHeaders.Add(header, value);
}
}

if(!String.IsNullOrEmpty(Customstd.ToString()))
if(!string.IsNullOrEmpty(customStd.ToString()))
{
Context.Log("Unsupported log noticed. Use Context.Log() or Context.Error() for logging.");
context.Log("Unsupported log noticed. Use context.Log() or context.Error() for logging.");
}

OutputHeaders.Add("x-open-runtimes-logs", System.Web.HttpUtility.UrlEncode(String.Join("\n", Context.Logs.Cast<string>().ToArray())));
OutputHeaders.Add("x-open-runtimes-errors", System.Web.HttpUtility.UrlEncode(String.Join("\n", Context.Errors.Cast<string>().ToArray())));
outputHeaders.Add("x-open-runtimes-logs", System.Web.HttpUtility.UrlEncode(string.Join("\n", context.Logs)));
outputHeaders.Add("x-open-runtimes-errors", System.Web.HttpUtility.UrlEncode(string.Join("\n", context.Errors)));

return new CustomResponse(Output.Body, Output.StatusCode, OutputHeaders);
return new CustomResponse(output.body, output.StatusCode, outputHeaders);
}
26 changes: 13 additions & 13 deletions runtimes/dotnet-6.0/src/RuntimeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@ public class RuntimeContext
public RuntimeRequest Req { get; set; }
public RuntimeResponse Res { get; set; }

public List<String> Logs = new List<String>();
public List<String> Errors = new List<String>();
public List<string> Logs { get; private set; } = new List<string>();
public List<string> Errors { get; private set; } = new List<string>();

public RuntimeContext(RuntimeRequest Req, RuntimeResponse Res)
public RuntimeContext(RuntimeRequest req, RuntimeResponse ses)
{
this.Req = Req;
this.Res = Res;
Req = req;
Res = res;
}

public void Log(object Message)
public void Log(object message)
{
if (Message is IList || Message is IDictionary) {
this.Logs.Add(JsonSerializer.Serialize(Message));
if (message is IList || message is IDictionary) {
Logs.Add(JsonSerializer.Serialize(message));
} else {
this.Logs.Add(Message.ToString() ?? "");
Logs.Add(message.ToString() ?? "");
}
}

public void Error(object Message)
public void Error(object message)
{
if (Message is IList || Message is IDictionary) {
this.Errors.Add(JsonSerializer.Serialize(Message));
if (message is IList || message is IDictionary) {
Errors.Add(JsonSerializer.Serialize(message));
} else {
this.Errors.Add(Message.ToString() ?? "");
Errors.Add(message.ToString() ?? "");
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions runtimes/dotnet-6.0/src/RuntimeOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ public class RuntimeOutput
public int StatusCode { get; set; }
public Dictionary<string, string> Headers { get; set; }

public RuntimeOutput(string Body, int StatusCode, Dictionary<string, string> Headers)
public RuntimeOutput(string body, int statusCode, Dictionary<string, string> headers)
{
this.Body = Body;
this.StatusCode = StatusCode;
this.Headers = Headers;
Body = body;
StatusCode = statusCode;
Headers = headers;
}
}
}
Expand Down
Loading