diff --git a/CefSharp.Example/CefSharp.Example.csproj b/CefSharp.Example/CefSharp.Example.csproj index 91c5005390..80677a2e6c 100644 --- a/CefSharp.Example/CefSharp.Example.csproj +++ b/CefSharp.Example/CefSharp.Example.csproj @@ -82,7 +82,7 @@ - + diff --git a/CefSharp.Example/Filters/MemoryStreamResponseFilter.cs b/CefSharp.Example/Filters/StreamResponseFilter.cs similarity index 75% rename from CefSharp.Example/Filters/MemoryStreamResponseFilter.cs rename to CefSharp.Example/Filters/StreamResponseFilter.cs index 7023d259ba..e45632aef8 100644 --- a/CefSharp.Example/Filters/MemoryStreamResponseFilter.cs +++ b/CefSharp.Example/Filters/StreamResponseFilter.cs @@ -8,22 +8,26 @@ namespace CefSharp.Example.Filters { /// - /// 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&) /// - 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; } @@ -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 @@ -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; } } } diff --git a/CefSharp.Example/Handlers/ExampleRequestHandler.cs b/CefSharp.Example/Handlers/ExampleRequestHandler.cs index 0468bdf16b..b570ada337 100644 --- a/CefSharp.Example/Handlers/ExampleRequestHandler.cs +++ b/CefSharp.Example/Handlers/ExampleRequestHandler.cs @@ -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/")) { diff --git a/CefSharp.Example/Handlers/ExampleResourceRequestHandler.cs b/CefSharp.Example/Handlers/ExampleResourceRequestHandler.cs index de4c5f38d6..caab82315a 100644 --- a/CefSharp.Example/Handlers/ExampleResourceRequestHandler.cs +++ b/CefSharp.Example/Handlers/ExampleResourceRequestHandler.cs @@ -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; @@ -12,7 +12,7 @@ namespace CefSharp.Example.Handlers { public class ExampleResourceRequestHandler : ResourceRequestHandler { - private readonly Dictionary responseDictionary = new Dictionary(); + private MemoryStream memoryStream; protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback) { @@ -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(); @@ -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); } } }