mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-13 08:34:08 +02:00
Add base class for response filters to safely modify intercepted files
This commit is contained in:
parent
c91b635132
commit
77bc922d93
72
Core/Handling/Filters/ResponseFilterBase.cs
Normal file
72
Core/Handling/Filters/ResponseFilterBase.cs
Normal file
@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using CefSharp;
|
||||
|
||||
namespace TweetDuck.Core.Handling.Filters{
|
||||
abstract class ResponseFilterBase : IResponseFilter{
|
||||
private enum State{
|
||||
Reading, Writing, Done
|
||||
}
|
||||
|
||||
private readonly Encoding encoding;
|
||||
private byte[] responseData;
|
||||
|
||||
private State state;
|
||||
private int offset;
|
||||
|
||||
protected ResponseFilterBase(int totalBytes, Encoding encoding){
|
||||
this.responseData = new byte[totalBytes];
|
||||
this.encoding = encoding;
|
||||
this.state = State.Reading;
|
||||
}
|
||||
|
||||
FilterStatus IResponseFilter.Filter(Stream dataIn, out long dataInRead, Stream dataOut, out long dataOutWritten){
|
||||
int responseLength = responseData.Length;
|
||||
|
||||
if (state == State.Reading){
|
||||
int bytesToRead = Math.Min(responseLength-offset, (int)Math.Min(dataIn.Length, int.MaxValue));
|
||||
|
||||
dataIn.Read(responseData, offset, bytesToRead);
|
||||
offset += bytesToRead;
|
||||
|
||||
dataInRead = bytesToRead;
|
||||
dataOutWritten = 0;
|
||||
|
||||
if (offset >= responseLength){
|
||||
responseData = encoding.GetBytes(ProcessResponse(encoding.GetString(responseData)));
|
||||
state = State.Writing;
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
return FilterStatus.NeedMoreData;
|
||||
}
|
||||
else if (state == State.Writing){
|
||||
int bytesToWrite = Math.Min(responseLength-offset, (int)Math.Min(dataOut.Length, int.MaxValue));
|
||||
|
||||
if (bytesToWrite > 0){
|
||||
dataOut.Write(responseData, offset, bytesToWrite);
|
||||
offset += bytesToWrite;
|
||||
}
|
||||
|
||||
dataOutWritten = bytesToWrite;
|
||||
dataInRead = 0;
|
||||
|
||||
if (offset < responseLength){
|
||||
return FilterStatus.NeedMoreData;
|
||||
}
|
||||
else{
|
||||
state = State.Done;
|
||||
return FilterStatus.Done;
|
||||
}
|
||||
}
|
||||
else{
|
||||
throw new InvalidOperationException("This resource filter cannot be reused.");
|
||||
}
|
||||
}
|
||||
|
||||
public abstract bool InitFilter();
|
||||
protected abstract string ProcessResponse(string text);
|
||||
public abstract void Dispose();
|
||||
}
|
||||
}
|
@ -72,6 +72,7 @@
|
||||
<Compile Include="Core\FormManager.cs" />
|
||||
<Compile Include="Core\Handling\ContextMenuGuide.cs" />
|
||||
<Compile Include="Core\Handling\DragHandlerBrowser.cs" />
|
||||
<Compile Include="Core\Handling\Filters\ResponseFilterBase.cs" />
|
||||
<Compile Include="Core\Handling\General\BrowserProcessHandler.cs" />
|
||||
<Compile Include="Core\Handling\ContextMenuBase.cs" />
|
||||
<Compile Include="Core\Handling\ContextMenuBrowser.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user