Changed code to fix some errors

Help
2011-04-11
2013-05-30
  • Markus Loibl

    Markus Loibl - 2011-04-11

    Hi,
    I'm not  a developer of this code, so I paste the changed code here.
    I changed the code for        
    private bool getFavicon(string url, ref MemoryStream ms, ref string message)
    to deal with:
    - redirect of downloads, so e.g. http -> redirects to https
    - all bytes are downloaded, not only the first chunk

    the modified binary can be found at
    http://www.mausoma.de/KeePassFaviconDownloader.zip

    Maybe you can replace your code with this one:

            private bool getFavicon(string url, ref MemoryStream ms, ref string message)
            {
                Stream s = null;
                Image img = null;
                MemoryStream memStream = new MemoryStream();

                try
                {
                    WebRequest webreq = WebRequest.Create(url);
                    webreq.Timeout = 10000; // don't think it's expecting too much for a few KB to be delivered inside 10 seconds.

                    WebResponse response = webreq.GetResponse();
                   
                    // START change
                   
                    if( response==null )
                    {
                        message += "Could not download favicon(s). This may be a temporary problem so you may want to try again later or post the contents of this error message on the KeePass Favicon Download forums at http://sourceforge.net/projects/keepass-favicon/support. Technical information which may help diagnose the problem is listed below, you can copy it to your clipboard by just clicking on this message and pressing CTRL-C.\n - No response from server";
                        return false;
                    }
                    if( string.Compare(response.ResponseUri.ToString(), url, StringComparison.InvariantCultureIgnoreCase) != 0 )
                    {
                        //Redirect ?
                        return getFavicon(response.ResponseUri.ToString(), ref ms, ref message);
                    }

                    s = response.GetResponseStream();

                    int count = 0;
                    byte buffer = new byte;
                    do
                    {
                        count = s.Read(buffer, 0, buffer.Length);       
                        memStream.Write(buffer, 0, count);   
                        if (count == 0)
                            break;
                    }   
                    while (true);
                    memStream.Position = 0;
                   
                    // END change

                    try { img = (new Icon(memStream)).ToBitmap(); }
                    catch (Exception)
                    {
                        // This shouldn't be useful unless someone has messed up their favicon format
                        try { img = Image.FromStream(memStream); }
                        catch (Exception) { throw; }
                    }

                }
                catch (WebException webException)
                {
                    // don't show this everytime a website has a missing favicon - it could get old fast.
                    message += "Could not download favicon(s). This may be a temporary problem so you may want to try again later or post the contents of this error message on the KeePass Favicon Download forums at http://sourceforge.net/projects/keepass-favicon/support. Technical information which may help diagnose the problem is listed below, you can copy it to your clipboard by just clicking on this message and pressing CTRL-C.\n" + webException.Status + ": " + webException.Message + ": " + webException.Response;
                    if (s != null)
                        s.Close();
                    return false;
                }
                catch (Exception generalException)
                {
                    // don't show this everytime a website has an invalid favicon - it could get old fast.
                    message += "Could not download favicon(s). This may be a temporary problem so you may want to try again later or post the contents of this error message on the KeePass Favicon Download forums at http://sourceforge.net/projects/keepass-favicon/support. Technical information which may help diagnose the problem is listed below, you can copy it to your clipboard by just clicking on this message and pressing CTRL-C.\n" + generalException.Message + ".";
                    if (s != null)
                        s.Close();
                    return false;
                }

                try
                {
                    Image imgNew = new Bitmap(img, new Size(16, 16));

                    ms = new MemoryStream();
                    imgNew.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    return true;
                }
                catch (Exception ex)
                {
                    message += "Could not process downloaded favicon. This may be a temporary problem so you may want to try again later or post the contents of this error message on the KeePass Favicon Download forums at http://sourceforge.net/projects/keepass-favicon/support. Technical information which may help diagnose the problem is listed below, you can copy it to your clipboard by just clicking on this message and pressing CTRL-C.\n" + ex.Message + ".";
                    if (s != null)
                        s.Close();
                    return false;
                }
            }

     
  • Luckyrat

    Luckyrat - 2011-05-08

    Thanks. I'll incorporate that into the next version which I'll hopefully be able to release in the next week or two.

    I'm interested to know if you actually found any examples where the 40KB buffer was insufficient? Your new code is certainly the right way to do it but I did think my lazy approach would be sufficient for favicon images.

    I didn't know that redirections had to be handled at the application level. I thought it was a detail that the framework would/should handle but also confess that I didn't think this situation would arise very frequently anyway. Again, I'm interested to hear your experiences that led you to develop the new code - was it just an occasional problematic favicon or a more widespread problem?

    Chris

     
  • Markus Loibl

    Markus Loibl - 2011-06-29

    Hi,
    I wrote the code because I had problems with many! websites. The problem of many site-icons is that the professional favicon includes many resolutions x many sizes, so overcoming the 40kb size.

    Redirecting takes place automatically within the browser, but opening via WebRequest doesn't do this - thats why I also included this.

    mausoma

     

Log in to post a comment.