Skip to content

Commit

Permalink
JSONP fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fileformat committed Oct 10, 2024
1 parent d48c77b commit 5b8778a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 67 deletions.
7 changes: 6 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ RUN dotnet publish -c Release

RUN find .

#CMD ["/source/bin/Release/net8.0/publish/regexplanet-dotnet.dll"]
#
# final image
#
FROM mcr.microsoft.com/dotnet/aspnet:8.0-noble-chiseled-extra

ARG COMMIT="(not set)"
ARG LASTMOD="(not set)"
ENV COMMIT=$COMMIT
ENV LASTMOD=$LASTMOD

WORKDIR /app
COPY --from=builder /source/bin/Release/net8.0/publish /app
COPY ./wwwroot /app/wwwroot
Expand Down
143 changes: 77 additions & 66 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,77 @@
using System.Text.Json;
using Microsoft.AspNetCore.HttpLogging;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpLogging(logging =>
{
logging.LoggingFields = HttpLoggingFields.All;
logging.CombineLogs = true;
});

var app = builder.Build();

app.UseHttpLogging();

app.UseStaticFiles();

app.MapGet("/", () => "Running!");
app.MapGet("/status.json", IResult (HttpContext theContext) =>
{
var statusResult = new StatusResult(true,
$".NET {Environment.Version}",
Environment.Version.ToString(),
DateTime.UtcNow.ToString("o"),
Environment.GetEnvironmentVariable("LASTMOD") ?? "(local)",
Environment.GetEnvironmentVariable("COMMIT") ?? "(local)"
);

var callback = theContext.Request.Query["callback"];
if (string.IsNullOrEmpty(callback)) {
return Results.Json(statusResult);
}
string json = JsonSerializer.Serialize(statusResult);
return Results.Content($"{callback}({json});", "application/javascript");
});

app.MapPost("/test.json", static async (HttpContext theContext) =>
{
// read form variables
var form = await theContext.Request.ReadFormAsync();
var regex = form["regex"].FirstOrDefault();
var replacement = form["replacement"].FirstOrDefault();
var input = form["input"].ToArray() ?? new string[] { };

var html = $"{regex} {replacement} {input.Length} {input.FirstOrDefault()}";

var testOutput = new TestOutput(true, html);

var callback = theContext.Request.Query["callback"];
if (string.IsNullOrEmpty(callback)) {
return Results.Json(testOutput);
}
string json = JsonSerializer.Serialize(testOutput);
return Results.Content($"{callback}({json});", "application/javascript");
});

var hostname = Environment.GetEnvironmentVariable("HOSTNAME") ?? "0.0.0.0";
var port = Environment.GetEnvironmentVariable("PORT") ?? "4000";
var url = $"http://{hostname}:{port}";

app.Logger.LogInformation($"App started on {url}", url);

app.Run(url);

record StatusResult(Boolean success, string tech, string version, string timestamp, string lastmod, string commit);
record TestOutput(Boolean success, string html);

using System.Text.Json;
using Microsoft.AspNetCore.HttpLogging;

static async Task<bool> handleJsonp(HttpContext theContext, object data)
{
string jsonStr = JsonSerializer.Serialize(data);

var response = theContext.Response;
var callback = theContext.Request.Query["callback"];
theContext.Response.Clear();
if (string.IsNullOrEmpty(callback))
{
response.ContentType = "application/json";
response.Headers.Append("Access-Control-Allow-Origin", "*");
response.Headers.Append("Access-Control-Allow-Methods", "GET");
response.Headers.Append("Access-Control-Max-Age", "604800");
await response.WriteAsync(jsonStr);
} else {
response.ContentType = "text/javascript; charset=utf-8";
await response.WriteAsync($"{callback}({jsonStr});");
}
return true;
}

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpLogging(logging =>
{
logging.LoggingFields = HttpLoggingFields.All;
logging.CombineLogs = true;
});

var app = builder.Build();

app.UseHttpLogging();

app.UseStaticFiles();

app.MapGet("/", () => "Running!");
app.MapGet("/status.json", static async (HttpContext theContext) =>
{
var statusResult = new StatusResult(true,
$".NET {Environment.Version}",
Environment.Version.ToString(),
DateTime.UtcNow.ToString("o"),
Environment.GetEnvironmentVariable("LASTMOD") ?? "(local)",
Environment.GetEnvironmentVariable("COMMIT") ?? "(local)"
);

await handleJsonp(theContext, statusResult);
});

app.MapPost("/test.json", static async (HttpContext theContext) =>
{
// read form variables
var form = await theContext.Request.ReadFormAsync();
var regex = form["regex"].FirstOrDefault();
var replacement = form["replacement"].FirstOrDefault();
var input = form["input"].ToArray() ?? new string[] { };

var html = $"{regex} {replacement} {input.Length} {input.FirstOrDefault()}";

var testOutput = new TestOutput(true, html);

await handleJsonp(theContext, testOutput);
});

var hostname = Environment.GetEnvironmentVariable("HOSTNAME") ?? "0.0.0.0";
var port = Environment.GetEnvironmentVariable("PORT") ?? "4000";
var url = $"http://{hostname}:{port}";

app.Logger.LogInformation($"App started on {url}", url);

app.Run(url);

record StatusResult(Boolean success, string tech, string version, string timestamp, string lastmod, string commit);
record TestOutput(Boolean success, string html);

0 comments on commit 5b8778a

Please sign in to comment.