Skip to content

Commit

Permalink
Example - Update ResponseFilter example to reflect NetworkService cha…
Browse files Browse the repository at this point in the history
…nges

- MemoryStreamResponseFilter renamed to StreamResponseFilter
- Stream is now passed into StreamResponseFilter constructor
- ExampleResourceRequestHandler updated to reflect that it now represents a single resource request
- ExampleResourceRequestHandler is now used for all requests from the CefSharpSchemeHandlerFactory.SchemeName custom scheme

Issue #2743
  • Loading branch information
amaitland committed Sep 30, 2019
1 parent 6ead2b3 commit 195569d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 31 deletions.
2 changes: 1 addition & 1 deletion CefSharp.Example/CefSharp.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<Compile Include="Filters\FindReplaceMultiResponseFilter.cs" />
<Compile Include="Filters\FindReplaceResponseFilter.cs" />
<Compile Include="Filters\ExperimentalStreamResponseFilter.cs" />
<Compile Include="Filters\MemoryStreamResponseFilter.cs" />
<Compile Include="Filters\StreamResponseFilter.cs" />
<Compile Include="FlashResourceHandler.cs" />
<Compile Include="Filters\PassThruResponseFilter.cs" />
<Compile Include="Handlers\BrowserProcessHandler.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@
namespace CefSharp.Example.Filters
{
/// <summary>
/// MemoryStreamResponseFilter - copies all data from IResponseFilter.Filter
/// to a MemoryStream. This is provided as an example to get you started and has not been
/// StreamResponseFilter - copies all data from IResponseFilter.Filter
/// to the provided Stream. This is provided as an example to get you started and has not been
/// production tested. If you experience problems you should refer to the CEF documentation
/// and ask any questions you have on http://magpcss.org/ceforum/index.php
/// Make sure to ask your question in the context of the CEF API (remember that CefSharp is just a wrapper).
/// https://magpcss.org/ceforum/apidocs3/projects/(default)/CefResponseFilter.html#Filter(void*,size_t,size_t&,void*,size_t,size_t&)
/// </summary>
public class MemoryStreamResponseFilter : IResponseFilter
public class StreamResponseFilter : IResponseFilter
{
private MemoryStream memoryStream;
private Stream responseStream;

bool IResponseFilter.InitFilter()
public StreamResponseFilter(Stream stream)
{
//NOTE: We could initialize this earlier, just one possible use of InitFilter
memoryStream = new MemoryStream();
responseStream = stream;
}

bool IResponseFilter.InitFilter()
{
//Will only be called a single time.
//The filter will not be installed if this method returns false.
return true;
}

Expand All @@ -47,7 +51,7 @@ FilterStatus IResponseFilter.Filter(Stream dataIn, out long dataInRead, Stream d
dataOut.Write(readBytes, 0, readBytes.Length);

//Write buffer to the memory stream
memoryStream.Write(readBytes, 0, readBytes.Length);
responseStream.Write(readBytes, 0, readBytes.Length);

//If we read less than the total amount avaliable then we need
//return FilterStatus.NeedMoreData so we can then write the rest
Expand All @@ -61,13 +65,7 @@ FilterStatus IResponseFilter.Filter(Stream dataIn, out long dataInRead, Stream d

void IDisposable.Dispose()
{
memoryStream.Dispose();
memoryStream = null;
}

public byte[] Data
{
get { return memoryStream.ToArray(); }
responseStream = null;
}
}
}
1 change: 1 addition & 0 deletions CefSharp.Example/Handlers/ExampleRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser
{
//NOTE: In most cases you examine the request.Url and only handle requests you are interested in
if (request.Url.ToLower().StartsWith("https://cefsharp.example")
|| request.Url.ToLower().StartsWith(CefSharpSchemeHandlerFactory.SchemeName)
|| request.Url.ToLower().StartsWith("mailto:")
|| request.Url.ToLower().StartsWith("https://googlechrome.github.io/samples/service-worker/"))
{
Expand Down
25 changes: 10 additions & 15 deletions CefSharp.Example/Handlers/ExampleResourceRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// 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;
using System.IO;
using System.Text;
using CefSharp.Example.Filters;
using CefSharp.Handler;
Expand All @@ -12,7 +12,7 @@ namespace CefSharp.Example.Handlers
{
public class ExampleResourceRequestHandler : ResourceRequestHandler
{
private readonly Dictionary<UInt64, MemoryStreamResponseFilter> responseDictionary = new Dictionary<UInt64, MemoryStreamResponseFilter>();
private MemoryStream memoryStream;

protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
Expand Down Expand Up @@ -127,9 +127,8 @@ protected override IResponseFilter GetResourceResponseFilter(IWebBrowser chromiu
}

//Only called for our customScheme
var dataFilter = new MemoryStreamResponseFilter();
responseDictionary.Add(request.Identifier, dataFilter);
return dataFilter;
memoryStream = new MemoryStream();
return new StreamResponseFilter(memoryStream);
}

//return new PassThruResponseFilter();
Expand All @@ -139,17 +138,13 @@ protected override IResponseFilter GetResourceResponseFilter(IWebBrowser chromiu
protected override void OnResourceLoadComplete(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
{
var url = new Uri(request.Url);
if (url.Scheme == CefSharpSchemeHandlerFactory.SchemeName)
if (url.Scheme == CefSharpSchemeHandlerFactory.SchemeName && memoryStream != null)
{
MemoryStreamResponseFilter filter;
if (responseDictionary.TryGetValue(request.Identifier, out filter))
{
//TODO: Do something with the data here
var data = filter.Data;
var dataLength = filter.Data.Length;
//NOTE: You may need to use a different encoding depending on the request
var dataAsUtf8String = Encoding.UTF8.GetString(data);
}
//TODO: Do something with the data here
var data = memoryStream.ToArray();
var dataLength = data.Length;
//NOTE: You may need to use a different encoding depending on the request
var dataAsUtf8String = Encoding.UTF8.GetString(data);
}
}
}
Expand Down

0 comments on commit 195569d

Please sign in to comment.