Created
October 31, 2020 13:48
-
-
Save martinrayenglish/26a276dcf37b7a2794efad43bb9408c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class RemoveHtmlElementResponseFilter : Stream | |
| { | |
| [NotNull] | |
| private readonly MemoryStream _internalStream = new MemoryStream(); | |
| private bool _disposed; | |
| public RemoveHtmlElementResponseFilter([NotNull] Stream stream) | |
| { | |
| ResponseStream = stream; | |
| } | |
| /// <inheritdoc /> | |
| public override bool CanRead { get; } = true; | |
| /// <inheritdoc /> | |
| public override bool CanSeek { get; } = true; | |
| /// <inheritdoc /> | |
| public override bool CanWrite { get; } = true; | |
| /// <inheritdoc /> | |
| public override long Length { get; } = 0L; | |
| /// <inheritdoc /> | |
| public override long Position { get; set; } | |
| /// <summary>Gets the response stream.</summary> | |
| [NotNull] | |
| private Stream ResponseStream { get; } | |
| /// <inheritdoc /> | |
| public override void Close() | |
| { | |
| ResponseStream.Close(); | |
| _internalStream.Close(); | |
| } | |
| public virtual Encoding Encoding | |
| { | |
| get | |
| { | |
| var currentOrNull = PageContext.CurrentOrNull; | |
| return currentOrNull != null ? currentOrNull.RequestContext.HttpContext.Response.ContentEncoding : Encoding.UTF8; | |
| } | |
| } | |
| /// <exception cref="InvalidOperationException"></exception> | |
| /// <inheritdoc /> | |
| public override void Flush() | |
| { | |
| var numArray = _internalStream.ToArray(); | |
| if (numArray.Length == 0) | |
| { | |
| return; | |
| } | |
| if (HttpContext.Current?.Server.GetLastError() != null) | |
| { | |
| const string msg = Constants.NonEditingHtml.ExceptionMessage; | |
| Log.SingleWarn(msg, this); | |
| TransmitData(numArray); | |
| } | |
| else | |
| { | |
| var html = Encoding.GetString(numArray); | |
| var loweredHtml = html.ToLowerInvariant(); | |
| foreach (var element in Constants.NonEditingHtml.TargetElements) | |
| { | |
| var targetElementWithAttribute = $"{element} {Constants.NonEditingHtml.DataExpOffAttribute}"; | |
| if (loweredHtml.Contains(targetElementWithAttribute.ToLowerInvariant())) | |
| { | |
| html = Regex.Replace(html, $"<{targetElementWithAttribute}.*?</{element}>", string.Empty, RegexOptions.Singleline | RegexOptions.IgnoreCase); | |
| } | |
| } | |
| TransmitData(Encoding.GetBytes(html)); | |
| } | |
| } | |
| /// <inheritdoc /> | |
| public override int Read(byte[] buffer, int offset, int count) | |
| { | |
| return ResponseStream.Read(buffer, offset, count); | |
| } | |
| /// <inheritdoc /> | |
| public override long Seek(long offset, SeekOrigin origin) | |
| { | |
| return ResponseStream.Seek(offset, origin); | |
| } | |
| /// <inheritdoc /> | |
| public override void SetLength(long value) | |
| { | |
| ResponseStream.SetLength(value); | |
| } | |
| /// <inheritdoc /> | |
| public override void Write(byte[] buffer, int offset, int count) | |
| { | |
| _internalStream.Write(buffer, offset, count); | |
| } | |
| /// <inheritdoc /> | |
| protected override void Dispose(bool disposing) | |
| { | |
| if (!_disposed) | |
| { | |
| if (disposing) | |
| { | |
| _internalStream.Dispose(); | |
| } | |
| _disposed = true; | |
| } | |
| base.Dispose(disposing); | |
| } | |
| private void TransmitData([NotNull] byte[] data) | |
| { | |
| if (data == null) | |
| { | |
| throw new ArgumentNullException(nameof(data)); | |
| } | |
| ResponseStream.Write(data, 0, data.Length); | |
| ResponseStream.Flush(); | |
| _internalStream.SetLength(0L); | |
| Position = 0L; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment