Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ResourceInterceptor : IResourceInterceptor
- {
- public ResourceResponse OnRequest(ResourceRequest request)
- {
- Stream stream = null;
- if (request.Url.ToString() == "url you want to intercept")
- {
- //you may want to route to another local file
- stream = = new FileStream("yourLocalFile.txt", FileMode.Open);
- }
- else
- {
- return null;
- }
- byte[] buffer = new byte[stream.Length];
- stream.Read(buffer, 0, buffer.Length);
- // Initialize unmanaged memory to hold the array.
- int size = Marshal.SizeOf(buffer[0]) * buffer.Length;
- IntPtr pnt = Marshal.AllocHGlobal(size);
- try
- {
- // Copy the array to unmanaged memory.
- Marshal.Copy(buffer, 0, pnt, buffer.Length);
- // Alternatively, you can pin the array in the managed heap.
- // Note however that pinning objects seriously disrupts GC operation.
- // Being able to move objects around in the heap is one of the reasons
- // why modern GCs can (somewhat) keep up with manual memory management.
- // By pinning objects in the managed heap, the GC looses it's one
- // performance advantage over manual memory management:
- // a relatively un-fragmented heap.
- //GCHandle handle = GCHandle.Alloc( buffer, GCHandleType.Pinned );
- //IntPtr pnt = handle.AddrOfPinnedObject();
- // Create a ResourceResponse. A copy is made of the supplied buffer.
- return ResourceResponse.Create((uint)buffer.Length, pnt, "text/html");
- }
- finally
- {
- // Data is not owned by the ResourceResponse. A copy is made
- // of the supplied buffer. We can safely free the unmanaged memory.
- Marshal.FreeHGlobal(pnt);
- stream.Close();
- }
- }
- public bool OnFilterNavigation(NavigationRequest request)
- {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement