cefsharp 如何拦截筛选和修改response响应返回的数据 也可以针对特别的请求构造新的response

有些时候需要对cefsharp 请求返回的数据进行监控或者修改 .如下方法可以修改response响应.

sourceRequestHandler.GetResourceResponseFilter()支持筛选响应的数据。您可以检索原始响应数据,也可以将自定义的响应数据附加到响应中,就像在文件末尾注入一些自定义CSS一样。如果需要,可以重写响应。可用于接收任何请求的响应,AJAX(xhrhttpprequest)/POST/GET。 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using CefSharp;

namespace CefFilters {
    public class FindReplaceResponseFilter : IResponseFilter {
        private static readonly Encoding encoding = Encoding.UTF8;

        /// <summary>
        ///     The portion of the find string that is currently matching.
        /// </summary>
        private int findMatchOffset;

        /// <summary>
        ///     Overflow from the output buffer.
        /// </summary>
        private readonly List<byte> overflow = new List<byte>();

        /// <summary>
        ///     Number of times the the string was found/replaced.
        /// </summary>
        private int replaceCount;

        private Dictionary<string, string> dictionary;

        public FindReplaceResponseFilter(Dictionary<string, string> dictionary) {
            this.dictionary = dictionary;
        }

        bool IResponseFilter.InitFilter() {
            return true;
        }

        FilterStatus IResponseFilter.Filter(Stream dataIn, out long dataInRead, Stream dataOut, out long dataOutWritten) {
            // All data will be read.
            dataInRead = dataIn == null ? 0 : dataIn.Length;
            dataOutWritten = 0;

            // Write overflow then reset
            if (overflow.Count > 0) {
                // Write the overflow from last time.
                WriteOverflow(dataOut, ref dataOutWritten);
            }

            // Evaluate each character in the input buffer. Track how many characters in
            // a row match findString. If findString is completely matched then write
            // replacement. Otherwise, write the input characters as-is.
            for (var i = 0; i < dataInRead; ++i) {
                var readByte = (byte) dataIn.ReadByte();
                var charForComparison = Convert.ToChar(readByte);

                if (replaceCount < dictionary.Count) { 
                    var replace = dictionary.ElementAt(replaceCount);
                    if (charForComparison == replace.Key[findMatchOffset]) {
                        //We have a match, increment the counter
                        findMatchOffset++;

                        // If all characters match the string specified
                        if (findMatchOffset == replace.Key.Length) {
                            // Complete match of the find string. Write the replace string.
                            WriteString(replace.Value, replace.Value.Length, dataOut, ref dataOutWritten);


                            // Start over looking for a match.
                            findMatchOffset = 0;
                            replaceCount++;
                        }
                        continue;
                    }
                    // Character did not match the find string.
                    if (findMatchOffset > 0) {
                        // Write the portion of the find string that has matched so far.
                        WriteString(replace.Key, findMatchOffset, dataOut, ref dataOutWritten);

                        // Start over looking for a match.
                        findMatchOffset = 0;
                    }
                }

                // Write the current character.
                WriteSingleByte(readByte, dataOut, ref dataOutWritten);
            }

            if (overflow.Count > 0) {
                //If we end up with overflow data then we'll need to return NeedMoreData
                // On the next pass the data will be written, then the next batch will be processed.
                return FilterStatus.NeedMoreData;
            }

            // If a match is currently in-progress we need more data. Otherwise, we're
            // done.
            return findMatchOffset > 0 ? FilterStatus.NeedMoreData : FilterStatus.Done;
        }

        private void WriteOverflow(Stream dataOut, ref long dataOutWritten) {
            // Number of bytes remaining in the output buffer.
            var remainingSpace = dataOut.Length - dataOutWritten;
            // Maximum number of bytes we can write into the output buffer.
            var maxWrite = Math.Min(overflow.Count, remainingSpace);

            // Write the maximum portion that fits in the output buffer.
            if (maxWrite > 0) {
                dataOut.Write(overflow.ToArray(), 0, (int) maxWrite);
                dataOutWritten += maxWrite;
            }

            if (maxWrite < overflow.Count) {
                // Need to write more bytes than will fit in the output buffer. 
                // Remove the bytes that were written already
                overflow.RemoveRange(0, (int) (maxWrite - 1));
            }
            else {
                overflow.Clear();
            }
        }

        private void WriteString(string str, int stringSize, Stream dataOut, ref long dataOutWritten) {
            // Number of bytes remaining in the output buffer.
            var remainingSpace = dataOut.Length - dataOutWritten;
            // Maximum number of bytes we can write into the output buffer.
            var maxWrite = Math.Min(stringSize, remainingSpace);

            // Write the maximum portion that fits in the output buffer.
            if (maxWrite > 0) {
                var bytes = encoding.GetBytes(str);
                dataOut.Write(bytes, 0, (int) maxWrite);
                dataOutWritten += maxWrite;
            }

            if (maxWrite < stringSize) {
                // Need to write more bytes than will fit in the output buffer. Store the
                // remainder in the overflow buffer.
                overflow.AddRange(encoding.GetBytes(str.Substring((int) maxWrite, (int) (stringSize - maxWrite))));
            }
        }

        private void WriteSingleByte(byte data, Stream dataOut, ref long dataOutWritten) {
            // Number of bytes remaining in the output buffer.
            var remainingSpace = dataOut.Length - dataOutWritten;

            // Write the byte to the buffer or add it to the overflow
            if (remainingSpace > 0) {
                dataOut.WriteByte(data);
                dataOutWritten += 1;
            }
            else {
                // Need to write more bytes than will fit in the output buffer. Store the
                // remainder in the overflow buffer.
                overflow.Add(data);
            }
        }

        public void Dispose() {}
    }
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值