ComputerGhost / FaviconFetcher

Scan a webpage for favicons, or just easily download the one you want.
MIT License
5 stars 3 forks source link

System.IO.EndOfstreamException when fetching from url #1

Closed florian-berger closed 5 years ago

florian-berger commented 5 years ago

Hello,

I have a little problem with using FaviconFetcher in my test software. In some cases I get an System.IO.EndOfStreamException with message

Unable to read beyond the end of the stream.

I am using the following code:

var fileName = Environment.ExpandEnvironmentVariables(Path.Combine("%temp%", Guid.NewGuid().ToString()));

var fetcher = new Fetcher();
var image = fetcher.FetchClosest(new Uri("https://paragongamers.net"), new Size(128, 128));
image?.Save(fileName);

The exception is thrown in the line starting with var image =. When changing the size to 32x32 or lower it works without any problem. Other URIs like 'https://google.com' are working without any problems when using 128x128.

Stacktrace:

at System.IO.BinaryReader.FillBuffer(Int32 numBytes) at System.IO.BinaryReader.ReadInt16() at FaviconFetcher.HttpSource._ExtractIcoSizes(Stream stream) at FaviconFetcher.HttpSource.DownloadImages(Uri uri) at FaviconFetcher.Utility.FetchJob.DownloadImages_ReturnPerfect(Uri uri) at FaviconFetcher.Utility.FetchJob.ScanAndFetch() at FaviconFetcher.Fetcher.Fetch(Uri uri, FetchOptions options) at FaviconFetcher.Fetcher.FetchClosest(Uri uri, Size size) at FaviconFetcherTest.MainWindow.LoadImage() in C:\Dev\CSharp\FaviconFetcherTest\FaviconFetcherTest\MainWindow.xaml.cs:line 29 at DevExpress.Mvvm.DelegateCommand.<>cDisplayClass4.<.ctor>b0(Object o) at DevExpress.Mvvm.DelegateCommand1.Execute(T parameter) at DevExpress.Mvvm.CommandBase1.System.Windows.Input.ICommand.Execute(Object parameter) at MS.Internal.Commands.CommandHelpers.CriticalExecuteCommandSource(ICommandSource commandSource, Boolean userInitiated) at System.Windows.Controls.Primitives.ButtonBase.OnClick() at System.Windows.Controls.Button.OnClick() at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e) at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent) at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e) at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args) at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted) at System.Windows.Input.InputManager.ProcessStagingArea() at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input) at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport) at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel) at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) at System.Windows.Application.RunDispatcher(Object ignore) at System.Windows.Application.RunInternal(Window window) at System.Windows.Application.Run(Window window) at System.Windows.Application.Run() at FaviconFetcherTest.App.Main()

Is it possible to prevent this exception without changing the dimensions of the expected image?

ComputerGhost commented 5 years ago

The cause is one of the icons being returned as "200 OK" despite being invalid. FaviconFetcher should skip it, but it is throwing the exception instead.

Are you getting this exception from the version on NuGet? I am able to replicate this behavior from that, but it is working for the GitHub version. I'll be able to get the right version up by Saturday morning.

florian-berger commented 5 years ago

Yes, I get this issue when using the NuGet version. Thanks for your quick response.

ComputerGhost commented 5 years ago

The NuGet package has been updated with a fix, and your test code is working now. Thanks for reporting this!