This repository has been archived on 2021-03-09. You can view files and clone it, but cannot push or open issues or pull requests.
graf-uloh/server/Ksp.WebServer/Controllers/GrafikPageController.cs

84 lines
3.1 KiB
C#
Raw Normal View History

2020-09-27 20:08:02 +02:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using AngleSharp;
using AngleSharp.Dom;
using System.Net.Http;
using System.Net.Http.Headers;
using AngleSharp.Html;
using Microsoft.Extensions.Options;
2020-09-27 20:08:02 +02:00
namespace Ksp.WebServer.Controllers
{
[ApiController]
[Route("grafik")]
public class GrafikPageController : ControllerBase
{
private readonly ILogger<TasksController> logger;
private readonly IWebHostEnvironment env;
2020-10-17 20:10:56 +02:00
private readonly KspPageRewriter pageRewriter;
private readonly KspProxyConfig kspProxyConfig;
2020-09-27 20:08:02 +02:00
public GrafikPageController(
ILogger<TasksController> logger,
IWebHostEnvironment env,
2020-10-17 20:10:56 +02:00
IOptions<KspProxyConfig> kspProxyConfig,
KspPageRewriter pageRewriter)
2020-09-27 20:08:02 +02:00
{
this.env = env;
2020-10-17 20:10:56 +02:00
this.pageRewriter = pageRewriter;
this.kspProxyConfig = kspProxyConfig.Value;
2020-09-27 20:08:02 +02:00
this.logger = logger;
}
async Task<string> FetchBlankPage()
{
var c = new HttpClient();
var rq = new HttpRequestMessage(HttpMethod.Get, $"{kspProxyConfig.Host}/blank");
2020-09-27 20:08:02 +02:00
rq.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html"));
rq.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xhtml+xml"));
if (!string.IsNullOrEmpty(kspProxyConfig.Authorization))
rq.Headers.Authorization =
AuthenticationHeaderValue.Parse(kspProxyConfig.Authorization);
2020-09-27 20:08:02 +02:00
if (HttpContext.Request.Headers.TryGetValue("Cookie", out var x))
rq.Headers.Add("Cookie", x.AsEnumerable());
var rs = await c.SendAsync(rq);
return await rs.Content.ReadAsStringAsync();
}
[HttpGet]
public async Task<IActionResult> Get()
{
var grafikPage = await System.IO.File.ReadAllTextAsync(Path.Combine(env.ContentRootPath, "../../frontend/public/grafik.html"));
var p = new AngleSharp.Html.Parser.HtmlParser();
var grafik = p.ParseDocument(grafikPage);
var kspTemplate = p.ParseDocument(await FetchBlankPage());
2020-10-17 20:10:56 +02:00
pageRewriter.ModifyTree(kspTemplate, "grafik");
2020-09-27 20:08:02 +02:00
var innerBody = grafik.Body;
innerBody.Replace(kspTemplate.Body);
var page = grafik.Body.QuerySelector("#page");
2020-09-27 20:34:56 +02:00
page.InnerHtml = "";
page.AppendNodes(innerBody.ChildNodes.ToArray());
2020-09-27 20:08:02 +02:00
foreach(var headElement in kspTemplate.Head.QuerySelectorAll("link, script"))
{
headElement.RemoveFromParent();
grafik.Head.AppendChild(headElement);
}
var outputHtml = new StringWriter();
grafik.ToHtml(outputHtml, new PrettyMarkupFormatter() { Indentation = "\t", NewLine = "\n" });
return this.Content(outputHtml.ToString(), "text/html");
}
}
}