Attachment 'fixhttp.py'

Download

   1 import wx
   2 import wx.html
   3 import os,sys
   4 import urlparse
   5 import time
   6 import mimetools
   7 import urllib2
   8 from httplib import HTTPMessage
   9 try:
  10     from cStringIO import StringIO
  11 except ImportError:
  12     from StringIO import StringIO
  13 
  14 def openAURL(url,proxy=None):
  15     """ Open a URL with an optional proxy. """
  16     import socket
  17     socket.setdefaulttimeout(60)
  18 
  19     if proxy is None:
  20         proxy    = urllib2.ProxyHandler({})
  21     else:
  22         proxy    = urllib2.ProxyHandler({'http': 'http://' + proxy})
  23     opener   = urllib2.build_opener(proxy, urllib2.HTTPHandler)
  24     return opener.open(url)
  25 
  26 class fshHTTP( wx.FileSystemHandler ):
  27     """
  28         Replacement for the C++ version.
  29         This fixes the problem with malformed URLs.
  30         Please note that it ONLY does the HTTP protocol.
  31         Similar fixes should be done for other FSHs!
  32     """
  33 
  34     def __init__( self ):
  35         wx.FileSystemHandler.__init__( self )
  36 
  37     def CanOpen( self, u ):
  38         return ((self.GetProtocol(u) == 'http') or (self.GetProtocol(u) == 'HTTP'))
  39 
  40     def OpenFile( self, fs, u ):
  41         # fix the bad url, it's indicated by a double slash in the path.
  42         bad=urlparse.urlsplit(u)
  43         nu=bad[2].rpartition('//')[2]
  44         fu = urlparse.urlunsplit((bad[0],bad[1],nu,bad[3],bad[4]))
  45         print "Original URL :`"+u+"'"
  46         print "Fixed URL ...:`"+fu+"'"
  47         # Open the fixed url
  48         h=openAURL(fu)
  49         # Get the mime header
  50         mmsgs = str(h.info())+"\n\n"
  51         # Parse the mime headers
  52         mio = StringIO(mmsgs)
  53         mmdcd = HTTPMessage(mio)
  54         amh=mmdcd.readheaders()
  55         # Get the mime type
  56         mtype = mmdcd.gettype()
  57         data = h.read()
  58         sio = StringIO(data)
  59         wxis = wx.InputStream(sio)
  60         wxfs = wx.FSFile(wxis, u, (mtype).lower(), self.GetAnchor(u), wx.DateTime())
  61         return wxfs
  62 
  63 _fshHTTP = fshHTTP()
  64 
  65 
  66 class exHtmlWindow(wx.html.HtmlWindow):
  67     def __init__(self, parent, id, frame):
  68         wx.html.HtmlWindow.__init__(self,parent,id)
  69         # Remove all the default handlers, we only do HTTP
  70         wx.FileSystem.CleanUpHandlers()
  71         # Add our own fixed handler
  72         wx.FileSystem.AddHandler( _fshHTTP )
  73         # Other handlers can be added here.
  74 
  75     def OnOpeningURL(self, type, url):
  76         kind = 'PAGE'
  77         if type == wx.html.HTML_URL_IMAGE:
  78             kind = 'IMAGE'
  79         if type == wx.html.HTML_URL_OTHER:
  80             kind = 'OTHER'
  81         print kind+" '"+url+"' "
  82         return wx.html.HTML_OPEN
  83 
  84     def OnLinkClicked(self, link):
  85         self.LoadPage(link.GetHref())
  86 
  87     def LoadPage(self, url):
  88         print "\nLoadPage: `"+url+"'"
  89 
  90         super(exHtmlWindow, self).LoadPage(url)
  91 
  92 class exFrame (wx.Frame):
  93     def __init__(self, parent, ID, title):
  94         wx.Frame.__init__(self,parent,ID,title,wx.DefaultPosition,wx.Size(600,750))
  95         self.panel = exHtmlPanel(self, -1, self)
  96         self.CreateStatusBar()
  97         self.SetStatusText("Default status bar")
  98         mnu_file = wx.Menu()
  99         mnu_file.Append(101, "E&xit", "Exit the browser")
 100         menuBar = wx.MenuBar()
 101         menuBar.Append(mnu_file, "F&ile")
 102         self.SetMenuBar(menuBar)
 103         wx.EVT_MENU(self, 101, self.Exit)
 104 
 105     def Exit(self, event):
 106         self.Close(True)
 107 
 108 class exHtmlPanel(wx.Panel):
 109     def __init__(self, parent, id, frame):
 110         wx.Panel.__init__(self, parent, -1)
 111         self.frame = frame
 112         self.cwd = os.path.split(sys.argv[0])[0]
 113         if not self.cwd:
 114             self.cwd = os.getcwd
 115         self.html = exHtmlWindow(self, -1, self.frame)
 116         self.html.SetStandardFonts()
 117         self.html.SetRelatedFrame(self.frame, "%s")
 118         self.html.SetRelatedStatusBar(0)
 119 
 120         self.box = wx.BoxSizer(wx.VERTICAL)
 121         self.box.Add(self.html, 1, wx.GROW)
 122         subbox = wx.BoxSizer(wx.HORIZONTAL)
 123         btn = wx.Button(self, 1202, "Clear")
 124         wx.EVT_BUTTON(self, 1202, self.OnClear)
 125         subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
 126         btn = wx.Button(self, 1203, "Load Page")
 127         wx.EVT_BUTTON(self, 1203, self.OnLoadPage)
 128         subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
 129         btn = wx.Button(self, 1204, "Back")
 130         wx.EVT_BUTTON(self, 1204, self.OnBack)
 131         subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
 132         btn = wx.Button(self, 1205, "Forward")
 133         wx.EVT_BUTTON(self, 1205, self.OnForward)
 134         subbox.Add(btn, 1, wx.GROW | wx.ALL, 2)
 135         self.box.Add(subbox, 0, wx.GROW)
 136         self.SetSizer(self.box)
 137         self.SetAutoLayout(True)
 138 
 139     def OnLoadPage(self, event):
 140         dlg = wx.TextEntryDialog(self, 'Location:')
 141         if dlg.ShowModal() == wx.ID_OK:
 142             loc = dlg.GetValue()
 143         dlg.Destroy()
 144         self.html.LoadPage(loc)
 145 
 146     def OnClear(self, event):
 147         self.html.SetPage("")
 148 
 149     def OnBack(self, event):
 150         if not self.html.HistoryBack():
 151             wx.MessageBox("No more items in history!")
 152 
 153     def OnForward(self, event):
 154         if not self.html.HistoryForward():
 155             wx.MessageBox("No more items in history!")
 156 
 157     # This URL will show the problem in stdout.
 158     # If click the "Start Page" link it shows the problem even better.
 159     def OnInit(self):
 160         self.html.LoadPage('http://trac.wxwidgets.org/')
 161 
 162 class exApp(wx.App):
 163     def __init__(self, flag):
 164         wx.App.__init__(self, flag)
 165         wx.Log_SetActiveTarget(wx.LogStderr())
 166 
 167     def OnInit(self):
 168         frame = exFrame(None, -1, "Example Browser")
 169         frame.Show(True)
 170         self.SetTopWindow(frame)
 171         frame.panel.OnInit()
 172         return True
 173 
 174 app = exApp(0)
 175 app.MainLoop()
 176 
 177 """
 178 Example run, with commentary.
 179 
 180 $ python fixhttp.py
 181 
 182 LoadPage: `http://trac.wxwidgets.org/'
 183 PAGE 'http://trac.wxwidgets.org/'
 184 Original URL :`http://trac.wxwidgets.org/'
 185 Fixed URL ...:`http://trac.wxwidgets.org/'
 186 IMAGE 'http://trac.wxwidgets.org/chrome/site/logo9.jpg' <-- THIS URL IS CORRECT!
 187 Original URL :`http://trac.wxwidgets.org//chrome/site/logo9.jpg' <-- It got changed?!
 188 Fixed URL ...:`http://trac.wxwidgets.org/chrome/site/logo9.jpg'
 189 IMAGE 'http://trac.wxwidgets.org/chrome/common/trac_logo_mini.png'
 190 Original URL :`http://trac.wxwidgets.org//chrome/common/trac_logo_mini.png' <-- Here too!
 191 Fixed URL ...:`http://trac.wxwidgets.org/chrome/common/trac_logo_mini.png'
 192 
 193 LoadPage: `/wiki/WikiStart'
 194 PAGE 'http://trac.wxwidgets.org/wiki/WikiStart'
 195 Original URL :`http://trac.wxwidgets.org//wiki/WikiStart' <-- Here too!
 196 Fixed URL ...:`http://trac.wxwidgets.org/wiki/WikiStart'
 197 IMAGE 'http://trac.wxwidgets.org/chrome/site/logo9.jpg'
 198 Original URL :`http://trac.wxwidgets.org//wiki//chrome/site/logo9.jpg' <-- Here too!
 199 Fixed URL ...:`http://trac.wxwidgets.org/chrome/site/logo9.jpg'
 200 IMAGE 'http://trac.wxwidgets.org/chrome/common/trac_logo_mini.png'
 201 Original URL :`http://trac.wxwidgets.org//wiki//chrome/common/trac_logo_mini.png' <-- Here too!
 202 Fixed URL ...:`http://trac.wxwidgets.org/chrome/common/trac_logo_mini.png'
 203 
 204 URLs become even MORE scrambled with cgi's! But the fix resolves the problem.
 205 This bug needs to be forwarded to WxWidgets dev team so that can fix the mistake.
 206 
 207 """

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2011-03-29 23:18:44, 6.9 KB) [[attachment:fixhttp.py]]
  • [get | view] (2010-11-06 16:34:14, 2.1 KB) [[attachment:mybox.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.