[Zope-CVS] CVS: Packages/HTTPMounter - HTTPMounter.py:1.10

Andreas Jung andreas@digicool.com
Tue, 28 May 2002 10:44:24 -0400


Update of /cvs-repository/Packages/HTTPMounter
In directory cvs.zope.org:/tmp/cvs-serv17790

Modified Files:
	HTTPMounter.py 
Log Message:
objects are now retrieved using httplib instead of urllib. this
change was necessary to access the HTTP headers from the origin server.


=== Packages/HTTPMounter/HTTPMounter.py 1.9 => 1.10 ===
 from OFS.SimpleItem import SimpleItem
 from Acquisition import Implicit
-import string, time, urllib
+import string, time, urlparse, httplib
 
 MAX_TRIES = 5        # max. number of retries
 TIME2SLEEP = 0.5     # time to sleep in seconds between retries
@@ -42,46 +42,71 @@
          },
     )
 
-
     def __init__(self, id, title, url): 
         self.id     = id
         self.title  = title
         self.url    = url
 
-
     def __bobo_traverse__(self, request, entry_name):
         """ fool Zope traversal - hehehe"""
 
-        if entry_name in ('manage_main','manage_workspace', 'manage_preferences') \
-            or hasattr(self, entry_name): 
+        if entry_name in ('manage_main','manage_workspace', 'manage_preferences') or \
+            hasattr(self, entry_name): 
             return getattr(self, entry_name)
 
         return self 
 
-
     def __call__(self, REQUEST, RESPONSE):
         """ """
-        own_path = string.join(self.getPhysicalPath(), '/')
-        path_info = string.replace(REQUEST['URL1'] , REQUEST['SERVER_URL'],'')
-        sub_path = string.replace(path_info, own_path, '')
+        from zLOG import INFO, LOG
 
-        if not sub_path or sub_path=='/': sub_path = self.default_document
-        url = self.url 
-        if url[-1] != '/': url = url + '/'
-        url = url + sub_path
+        path_info = string.replace(REQUEST['URL1'] , REQUEST['SERVER_URL'],'')
+        path_elements = string.split(path_info,'/')
+        path_elements = filter(lambda x: x, path_elements)
+        pos = path_elements.index(self.id)
+        sub_path = string.join(path_elements[pos+1:], '/')
+        
+        if pos+1==len(path_elements): sub_path = sub_path +  self.default_document
 
         tries = 0
 
         while tries<MAX_TRIES:
             try:        
-                data = urllib.urlopen(url).read()
-                return data
+                tup  = urlparse.urlparse(self.url)
+                host = tup[1]
+                path = tup[2] 
+                if path[-1] != '/': path = path + '/'
+                path = path + sub_path
+                
+                status, data, headers = self._http_request(host, path)
+                for k in ('content-type','content-length','etag','last-modified'):
+                    v = headers.getheader(k)
+                    if v: RESPONSE.setHeader(k,v)
+
+                RESPONSE.write(data)           
+                return
+
             except IOError:
                 tries = tries + 1
                 time.sleep(TIME2SLEEP)
 
         return IOError
 
+    def _http_request(self, host, path):
+        """ get URL """
+                            
+        H = httplib.HTTP(host) 
+        H.putrequest('GET', path)
+        H.putheader('Accept','text/plain')
+        H.putheader('Accept','text/html')
+        H.putheader('Accept','image/jpeg')
+        H.putheader('Accept','image/gif')
+        H.endheaders()
+
+        status, msg, headers = H.getreply()
+        data = H.getfile().read()
+
+        return (status, data, headers)
 
     def manage_preferences(self, title, url, default_document, RESPONSE=None, URL1=None):
         """ save preferences """