Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ResourceHandler - Reset Stream.Position when CanSeek #2903

Closed
jpespartero opened this issue Sep 20, 2019 · 11 comments
Closed

ResourceHandler - Reset Stream.Position when CanSeek #2903

jpespartero opened this issue Sep 20, 2019 · 11 comments
Assignees
Milestone

Comments

@jpespartero
Copy link

jpespartero commented Sep 20, 2019

  • What version of the product are you using?
    Nuget 75.1.142

  • What architecture x86 or x64?
    x64

  • On what operating system?
    Win10

  • Are you using WinForms, WPF or OffScreen?
    WinForms

  • What steps will reproduce the problem?
    A custom ResourceHandler implementation that provides local memory images to the browser has stopped working correctly from the version 75.1.141.

The image is not displayed in the browser, but the ResourceHandler is providing the image stream.

The same code works fine up to version 73.1.30. Note that the interface has been updated to return a CefReturnValue instead of a bool.

` public override CefReturnValue ProcessRequestAsync(IRequest request, ICallback callback){

        var uri = new Uri(request.Url);
        string url = System.Net.WebUtility.UrlDecode(request.Url);
        Bitmap image = ...// Some code to get the image from memory
       
        if (image != null)
        {
            Stream = new MemoryStream();
            image.Save(Stream, ImageFormat.Png);
            MimeType = ResourceHandler.GetMimeType(".png");
        }

        callback.Continue();
        //return true;   // Up to version v73.1.30
        return CefReturnValue.Continue;   // From v75.1.141
    }`
  • What is the expected output? What do you see instead?
    Image displayed in the browser

  • Please provide any additional information below.

    • A stack trace if available, any Exception information.
      No exception is shown

    • Does the cef log provide any relevant information?
      No

@amaitland
Copy link
Member

Testing with 58c9585 and loading https://cefsharp.example/logo.png in either the WinForms and WPF example and the PNG file loads correctly. If you look at https://github.com/cefsharp/CefSharp/releases/tag/v75.1.142 you will see there is exactly one commit to the cefsharp/75 branch (the one referenced above).

I suspect what you are seeing is a subtle change in behaviour as part of the new Network Service changes, see #2743

Please no binary (zip, etc) links, fork the MinimalExample and push your changes to GitHub. (Alternatively use a code sharing service list Gist or Pastebin).

As per https://github.com/cefsharp/CefSharp/blob/master/.github/ISSUE_TEMPLATE/bug_report.md#bug-report please provide an example that reproduces the problem, and incomplete code snippet is not sufficient.

@amaitland amaitland changed the title The browser does not show an image resource provided by a ResourceHandler Custom ResourceHandler Unable to Load Image Sep 20, 2019
@amaitland amaitland added the unable-to-reproduce The problem could not be reproduced by developers label Sep 20, 2019
@TKGNET

This comment has been minimized.

@amaitland

This comment has been minimized.

@TKGNET

This comment has been minimized.

@amaitland

This comment has been minimized.

@TKGNET

This comment has been minimized.

@amaitland
Copy link
Member

Where exactly in your code are you using a ResourceHandler to load an image?

@TKGNET I asked this because I'm after an example that reproduces this issue. Your code doesn't actually do that. You are using a IResponseFilter which is actually unrelated to this issue specifically. You need to make changes to adapt to the new NetworkService implementation, see #2743 (comment)

@no-response
Copy link

no-response bot commented Oct 4, 2019

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.

@no-response no-response bot closed this as completed Oct 4, 2019
@jpespartero
Copy link
Author

sorry for the delay in the answer @amaitland. I have prepared an example project based on the WinForms minimal example.

https://github.com/jpespartero/CefSharp.MinimalExample

The ImageMemorySchemeHandler implemented contains a Dictionary with in-memory images and handles an url image request to provide the Stream and MimeType in the custom implementation of the ProcessRequestAsync method.

Maybe, I'm doing something wrong, but if load the image from a file in the Stream the image is displayed properly.

{
byte[] bytes = File.ReadAllBytes(file);
Stream = new MemoryStream(bytes);
var fileExtension = Path.GetExtension(file);
MimeType = GetMimeType(fileExtension);
}
callback.Continue();
return CefReturnValue.Continue;

@amaitland
Copy link
Member

Thanks for the example 👍

You need to reset the Stream.Position = 0; after you've saved the image to the MemoryStream.

This is a regression in CefSharp and a PR would to reset the Position would be welcome.

Generally speaking it's best to reset Stream.Position after you've written to it, specially before you hand the stream over to a 3rd party library.

@amaitland amaitland changed the title Custom ResourceHandler Unable to Load Image ResourceHandler - Reset Stream.Position when CanSeek Jan 14, 2020
@amaitland amaitland added known-issue priority-low Low priority up-for-grabs and removed unable-to-reproduce The problem could not be reproduced by developers labels Jan 14, 2020
@jpespartero
Copy link
Author

Thanks a lot for the quick response and the solution provided!
Yes, reseting the stream position solves the issue and the image is loaded properly. It is not a problem to do it once the image is saved, so from our side it is solved.

amaitland added a commit that referenced this issue Jan 22, 2020
- Prior to version 75 the Stream.Position was set to 0 before the ResourceHandler would read the Stream.
This restores that behaviour.
- Minor refactor of if statement for Stream.Can seek, only override ResponseLength with Stream.Length of not already set

Resolves #2903
@amaitland amaitland self-assigned this Jan 22, 2020
@amaitland amaitland added this to the 79.0.0 milestone Jan 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants