diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-06-05 03:25:53 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-06-05 03:25:53 +0200 |
commit | 51d820e22a4517dbb06d38a4f07f7c48522ef811 (patch) | |
tree | 4a7749cf77223dff2414fd4b73cb17df43d7449e /GitRepoViewer.CorsProxy/Controllers/SimpleProxyController.cs | |
download | GitTools-51d820e22a4517dbb06d38a4f07f7c48522ef811.tar.xz |
Diffstat (limited to 'GitRepoViewer.CorsProxy/Controllers/SimpleProxyController.cs')
-rw-r--r-- | GitRepoViewer.CorsProxy/Controllers/SimpleProxyController.cs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/GitRepoViewer.CorsProxy/Controllers/SimpleProxyController.cs b/GitRepoViewer.CorsProxy/Controllers/SimpleProxyController.cs new file mode 100644 index 0000000..725c375 --- /dev/null +++ b/GitRepoViewer.CorsProxy/Controllers/SimpleProxyController.cs @@ -0,0 +1,50 @@ +using Microsoft.AspNetCore.Mvc; + +namespace GitRepoViewer.CorsProxy.Controllers; + +[ApiController] +// [Route("[controller]")] +[Route("/")] +public class SimpleProxyController : ControllerBase +{ + private readonly ILogger<SimpleProxyController> _logger; + + public SimpleProxyController(ILogger<SimpleProxyController> logger) + { + _logger = logger; + } + + [HttpGet("get/{*url}")] + public async Task Get(string url) + { + _logger.LogInformation("Proxying url: " + url); + + var request = new HttpRequestMessage(HttpMethod.Get, url); + + var client = new HttpClient(); + + var response = await client.SendAsync(request); + + if(!response.IsSuccessStatusCode) + _logger.LogWarning("Got status code: " + response.StatusCode + " for url: " + url); + //throw new Exception("Failed to fetch file: " + url); + + Response.StatusCode = (int) response.StatusCode; + Response.ContentType = response.Content.Headers.ContentType?.MediaType ?? "application/octet-stream"; + //copy headers + foreach (var (key, value) in response.Headers) + { + Response.Headers.Add(key, value.ToArray()); + } + //override cors + Response.Headers.Add("Access-Control-Allow-Origin", "*"); + Response.Headers.Add("Access-Control-Allow-Headers", "*"); + Response.Headers.Add("Access-Control-Allow-Methods", "*"); + Response.Headers.Add("Access-Control-Allow-Credentials", "true"); + Response.Headers.Add("Access-Control-Max-Age", "86400"); + + await response.Content.CopyToAsync(Response.Body); + + //return new FileStreamResult(await response.Content.ReadAsStreamAsync(), response.Content.Headers.ContentType?.MediaType ?? "application/octet-stream"); + } +} \ No newline at end of file |