2018-06-15 02:57:03 +02:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System;
|
2016-05-12 09:11:05 +02:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
2017-01-18 09:12:22 +01:00
|
|
|
|
namespace Teknik.Utilities
|
2016-05-12 09:11:05 +02:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Result for relaying an HttpWebResponse
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class HttpWebResponseResult : ActionResult
|
|
|
|
|
{
|
|
|
|
|
private readonly HttpWebResponse _response;
|
|
|
|
|
private readonly ActionResult _innerResult;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Relays an HttpWebResponse as verbatim as possible.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="responseToRelay">The HTTP response to relay</param>
|
|
|
|
|
public HttpWebResponseResult(HttpWebResponse responseToRelay)
|
|
|
|
|
{
|
|
|
|
|
if (responseToRelay == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("response");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_response = responseToRelay;
|
|
|
|
|
|
|
|
|
|
Stream contentStream;
|
|
|
|
|
if (responseToRelay.ContentEncoding.Contains("gzip"))
|
|
|
|
|
{
|
|
|
|
|
contentStream = new GZipStream(responseToRelay.GetResponseStream(), CompressionMode.Decompress);
|
|
|
|
|
}
|
|
|
|
|
else if (responseToRelay.ContentEncoding.Contains("deflate"))
|
|
|
|
|
{
|
|
|
|
|
contentStream = new DeflateStream(responseToRelay.GetResponseStream(), CompressionMode.Decompress);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
contentStream = responseToRelay.GetResponseStream();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(responseToRelay.CharacterSet))
|
|
|
|
|
{
|
|
|
|
|
// File result
|
|
|
|
|
_innerResult = new FileStreamResult(contentStream, responseToRelay.ContentType);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Text result
|
|
|
|
|
var contentResult = new ContentResult();
|
|
|
|
|
contentResult = new ContentResult();
|
|
|
|
|
contentResult.Content = new StreamReader(contentStream).ReadToEnd();
|
|
|
|
|
_innerResult = contentResult;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-15 02:57:03 +02:00
|
|
|
|
public override void ExecuteResult(ActionContext context)
|
2016-05-12 09:11:05 +02:00
|
|
|
|
{
|
|
|
|
|
var clientResponse = context.HttpContext.Response;
|
|
|
|
|
clientResponse.StatusCode = (int)_response.StatusCode;
|
|
|
|
|
|
|
|
|
|
foreach (var headerKey in _response.Headers.AllKeys)
|
|
|
|
|
{
|
|
|
|
|
switch (headerKey)
|
|
|
|
|
{
|
|
|
|
|
case "Content-Length":
|
|
|
|
|
case "Transfer-Encoding":
|
|
|
|
|
case "Content-Encoding":
|
|
|
|
|
// Handled by IIS
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2018-06-15 02:57:03 +02:00
|
|
|
|
clientResponse.Headers.Add(headerKey, _response.Headers[headerKey]);
|
2016-05-12 09:11:05 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_innerResult.ExecuteResult(context);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-27 01:25:57 +02:00
|
|
|
|
|
|
|
|
|
public enum ResultType
|
|
|
|
|
{
|
|
|
|
|
Passthrough,
|
|
|
|
|
Json
|
|
|
|
|
}
|
2016-05-12 09:11:05 +02:00
|
|
|
|
}
|