// Copyright © 2014 The CefSharp Authors. All rights reserved. // // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. using System; using System.IO; namespace CefSharp { /// /// Class used to implement a custom resource handler. The methods of this class will always be called on the CEF IO thread. /// Blocking the CEF IO thread will adversely affect browser performance. We suggest you execute your code in a Task (or similar). /// To implement async handling, spawn a new Task (or similar), keep a reference to the callback. When you have a /// fully populated stream, execute the callback. Once the callback Executes, GetResponseHeaders will be called where you /// can modify the response including headers, or even redirect to a new Url. Set your responseLength and headers /// Populate the dataOut stream in ReadResponse. For those looking for a sample implementation or upgrading from /// a previous version . For those upgrading, inherit from ResourceHandler instead of IResourceHandler /// add the override keywoard to existing methods e.g. ProcessRequestAsync. /// public interface IResourceHandler : IDisposable { /// /// Begin processing the request. /// /// 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. bool ProcessRequest(IRequest request, ICallback callback); /// /// Retrieve response header information. If the response length is not known /// set responseLength to -1 and ReadResponse() will be called until it /// returns false. If the response length is known set responseLength /// to a positive value and ReadResponse() will be called until it returns /// false or the specified number of bytes have been read. /// If an error occured while setting up the request you can set /// to indicate the error condition. /// /// Use the response object to set the mime type, http status code and other optional header values. /// If the response length is not known set responseLength to -1 /// To redirect the request to a new URL set redirectUrl to the new Url. void GetResponseHeaders(IResponse response, out long responseLength, out string redirectUrl); /// /// Read response data. If data is available immediately copy to /// dataOut, set bytesRead to the number of bytes copied, and return true. /// To read the data at a later time set bytesRead to 0, return true and call ICallback.Continue() when the /// data is available. To indicate response completion return false. /// /// Stream to write to /// Number of bytes copied to the stream /// The callback used to Continue or Cancel the request (async). /// If data is available immediately copy to dataOut, set bytesRead to the number of bytes copied, /// and return true.To indicate response completion return false. /// Depending on this size of your response this method may be called multiple times bool ReadResponse(Stream dataOut, out int bytesRead, ICallback callback); /// /// Return true if the specified cookie can be sent with the request or false /// otherwise. If false is returned for any cookie then no cookies will be sent /// with the request. /// /// cookie /// Return true if the specified cookie can be sent with the request or false /// otherwise. If false is returned for any cookie then no cookies will be sent /// with the request. bool CanGetCookie(Cookie cookie); /// /// Return true if the specified cookie returned with the response can be set or false otherwise. /// /// cookie /// Return true if the specified cookie returned with the response can be set or false otherwise. bool CanSetCookie(Cookie cookie); /// /// Request processing has been canceled. /// void Cancel(); } }