2018-09-10 02:41:13 +02:00
|
|
|
// Copyright © 2016 The CefSharp Authors. All rights reserved.
|
2016-02-11 16:33:52 +10:00
|
|
|
//
|
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2018-09-10 02:41:13 +02:00
|
|
|
using System.IO;
|
2016-02-11 16:33:52 +10:00
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace CefSharp.Example.Filters
|
|
|
|
|
{
|
|
|
|
|
public class AppendResponseFilter : IResponseFilter
|
|
|
|
|
{
|
|
|
|
|
private static Encoding encoding = Encoding.UTF8;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Overflow from the output buffer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private List<byte> overflow = new List<byte>();
|
|
|
|
|
|
|
|
|
|
public AppendResponseFilter(string stringToAppend)
|
|
|
|
|
{
|
|
|
|
|
//Add the encoded string into the overflow.
|
|
|
|
|
overflow.AddRange(encoding.GetBytes(stringToAppend));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool IResponseFilter.InitFilter()
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FilterStatus IResponseFilter.Filter(Stream dataIn, out long dataInRead, Stream dataOut, out long dataOutWritten)
|
|
|
|
|
{
|
|
|
|
|
if (dataIn == null)
|
|
|
|
|
{
|
|
|
|
|
dataInRead = 0;
|
|
|
|
|
dataOutWritten = 0;
|
|
|
|
|
|
|
|
|
|
return FilterStatus.Done;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//We'll read all the data
|
|
|
|
|
dataInRead = dataIn.Length;
|
|
|
|
|
dataOutWritten = Math.Min(dataInRead, dataOut.Length);
|
|
|
|
|
|
2018-09-10 02:41:13 +02:00
|
|
|
if (dataIn.Length > 0)
|
|
|
|
|
{
|
2016-02-11 16:33:52 +10:00
|
|
|
//Copy all the existing data first
|
|
|
|
|
dataIn.CopyTo(dataOut);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-30 09:33:05 +10:00
|
|
|
// If we have overflow data and remaining space in the buffer then write the overflow.
|
2016-02-11 16:33:52 +10:00
|
|
|
if (overflow.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-10 02:41:13 +02:00
|
|
|
if (maxWrite == 0 && overflow.Count > 0)
|
2016-05-25 19:01:20 +10:00
|
|
|
{
|
|
|
|
|
//We haven't yet got space to append our data
|
|
|
|
|
return FilterStatus.NeedMoreData;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-11 16:33:52 +10:00
|
|
|
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();
|
|
|
|
|
}
|
2018-09-10 02:41:13 +02:00
|
|
|
}
|
2016-02-11 16:33:52 +10:00
|
|
|
|
2018-09-10 02:41:13 +02:00
|
|
|
if (overflow.Count > 0)
|
2016-02-11 16:33:52 +10:00
|
|
|
{
|
2018-09-10 02:41:13 +02:00
|
|
|
return FilterStatus.NeedMoreData;
|
2016-02-11 16:33:52 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FilterStatus.Done;
|
|
|
|
|
}
|
2016-05-20 08:13:14 +10:00
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2018-09-10 02:41:13 +02:00
|
|
|
|
2016-05-20 08:13:14 +10:00
|
|
|
}
|
2016-02-11 16:33:52 +10:00
|
|
|
}
|
|
|
|
|
}
|