From 560fa2b867fdfaceb2d6ed88e5a112a0ad5a1aef Mon Sep 17 00:00:00 2001 From: amaitland Date: Fri, 15 Apr 2016 16:25:06 +1000 Subject: [PATCH] Improve ResourceHandler documentation, add a minor simplification and update the FlashResourceHandler example to demo how simple it is to use a ResourceHandler when extending FlashResourceHandler --- CefSharp.Example/FlashResourceHandler.cs | 30 +++++------- CefSharp/ResourceHandler.cs | 58 ++++++++++++++++++++---- 2 files changed, 61 insertions(+), 27 deletions(-) diff --git a/CefSharp.Example/FlashResourceHandler.cs b/CefSharp.Example/FlashResourceHandler.cs index 8c3b6dbdad..cf478e2a69 100644 --- a/CefSharp.Example/FlashResourceHandler.cs +++ b/CefSharp.Example/FlashResourceHandler.cs @@ -10,9 +10,6 @@ namespace CefSharp.Example { public class FlashResourceHandler : ResourceHandler { - private MemoryStream stream; - private string mime; - public override bool ProcessRequestAsync(IRequest request, ICallback callback) { Task.Run(() => @@ -25,11 +22,20 @@ public override bool ProcessRequestAsync(IRequest request, ICallback callback) // Get the stream associated with the response. var receiveStream = httpWebResponse.GetResponseStream(); - mime = httpWebResponse.ContentType; + var mime = httpWebResponse.ContentType; - stream = new MemoryStream(); + var stream = new MemoryStream(); receiveStream.CopyTo(stream); httpWebResponse.Close(); + + //Reset the stream position to 0 so the stream can be copied into the underlying unmanaged buffer + stream.Position = 0; + + //Populate the response values - No longer need to implement GetResponseHeaders (unless you need to perform a redirect) + ResponseLength = stream.Length; + MimeType = mime; + StatusCode = (int)HttpStatusCode.OK; + Stream = stream; callback.Continue(); } @@ -37,19 +43,5 @@ public override bool ProcessRequestAsync(IRequest request, ICallback callback) return true; } - - public override Stream GetResponse(IResponse response, out long responseLength, out string redirectUrl) - { - responseLength = stream.Length; - redirectUrl = null; - - response.MimeType = mime; - response.StatusCode = (int)HttpStatusCode.OK; - - //Reset the stream position to 0 so the stream can be copied into the underlying unmanaged buffer - stream.Position = 0; - - return stream; - } } } diff --git a/CefSharp/ResourceHandler.cs b/CefSharp/ResourceHandler.cs index 51dc8b0657..afedec6589 100644 --- a/CefSharp/ResourceHandler.cs +++ b/CefSharp/ResourceHandler.cs @@ -11,7 +11,10 @@ namespace CefSharp { /// - /// Class ResourceHandler. + /// Default implementation of . This latest implementation provides some simplification, at + /// a minimum you only need to override ProcessRequestAsync. See the project source on GitHub for working examples. + /// used to implement a custom request handler interface. The methods of this class will always be called on the IO thread. + /// Static helper methods are included like FromStream and FromString that make dealing with fixed resources easy. /// public class ResourceHandler : IResourceHandler { @@ -28,27 +31,31 @@ public class ResourceHandler : IResourceHandler /// /// Gets or sets the Mime Type. /// - /// The Mime Type. public string MimeType { get; set; } /// /// Gets or sets the resource stream. /// - /// The stream. public Stream Stream { get; set; } /// /// Gets or sets the http status code. /// - /// The http status code. public int StatusCode { get; set; } /// /// Gets or sets the status text. /// - /// The status text. public string StatusText { get; set; } + /// + /// Gets or sets ResponseLength, when you know the size of your + /// Stream (Response) set this property. This is optional. + /// If you use a MemoryStream and don't provide a value + /// here then it will be cast and it's size used + /// + public long? ResponseLength { get; set; } + /// /// Gets or sets the headers. /// @@ -85,6 +92,19 @@ private ResourceHandler(string mimeType, ResourceHandlerType type) Type = type; } + /// + /// Begin processing the request. If you have the data in memory you can execute the callback + /// immediately and return true. For Async processing you would typically spawn a Task to perform processing, + /// then return true. When the processing is complete execute callback.Continue(); In your processing Task, simply set + /// the StatusCode, StatusText, MimeType, ResponseLength and Stream + /// + /// The request object. + /// The callback used to Continue or Cancel the request (async). + /// To handle the request return true and call + /// once the response header information is available + /// can also be called from inside this method if + /// header information is available immediately). + /// To cancel the request return false. public virtual bool ProcessRequestAsync(IRequest request, ICallback callback) { callback.Continue(); @@ -92,6 +112,17 @@ public virtual bool ProcessRequestAsync(IRequest request, ICallback callback) return true; } + /// + /// Populate the response stream, response length. When this method is called + /// the response should be fully populated with data. + /// It is possible to redirect to another url at this point in time. + /// NOTE: It's no longer manditory to implement this method, you can simply populate the + /// properties of this instance and they will be set by the default implementation. + /// + /// The response object used to set Headers, StatusCode, etc + /// length of the response + /// If set the request will be redirect to specified Url + /// The response stream public virtual Stream GetResponse(IResponse response, out long responseLength, out string redirectUrl) { redirectUrl = null; @@ -102,15 +133,26 @@ public virtual Stream GetResponse(IResponse response, out long responseLength, o response.StatusText = StatusText; response.ResponseHeaders = Headers; - var memoryStream = Stream as MemoryStream; - if (memoryStream != null) + if(ResponseLength.HasValue) { - responseLength = memoryStream.Length; + responseLength = ResponseLength.Value; + } + else + { + //If no ResponseLength provided then attempt to infer the length + var memoryStream = Stream as MemoryStream; + if (memoryStream != null) + { + responseLength = memoryStream.Length; + } } return Stream; } + /// + /// Called if the request is cancelled + /// public virtual void Cancel() {