Help save net neutrality! Learn more.
Close

Changed code to fix some errors

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
    - not all bytes are downloaded, not only the first chunk

    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;
                }
            }

     

Log in to post a comment.