[Zope-CVS] CVS: Packages/HTTPMounter - CHANGES.txt:1.6 HTTPMounter.py:1.16 VERSION.txt:1.6

Andreas Jung andreas@digicool.com
Thu, 29 Aug 2002 06:09:45 -0400


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

Modified Files:
	CHANGES.txt HTTPMounter.py VERSION.txt 
Log Message:
added support for basic authentication and SSL connections
(patches by Stefan Rinke)


=== Packages/HTTPMounter/CHANGES.txt 1.5 => 1.6 ===
--- Packages/HTTPMounter/CHANGES.txt:1.5	Tue Jun  4 08:40:11 2002
+++ Packages/HTTPMounter/CHANGES.txt	Thu Aug 29 06:09:43 2002
@@ -1,3 +1,8 @@
+Version 0.30 - 01/09/2002
+
+ - added support for basic authentication and SSL support
+   (patches by Stefan Rinke)
+
 Version 0.21 - 06/04/2002
 
  - ZMI management screen could not accessed (broken in 0.20)


=== Packages/HTTPMounter/HTTPMounter.py 1.15 => 1.16 ===
--- Packages/HTTPMounter/HTTPMounter.py:1.15	Thu Jun  6 10:11:55 2002
+++ Packages/HTTPMounter/HTTPMounter.py	Thu Aug 29 06:09:43 2002
@@ -23,6 +23,9 @@
 import string, time, urlparse, httplib, re
 from zLOG import INFO, LOG
 
+#for use with http basic auth
+from base64 import encodestring
+
 MAX_TRIES = 5        # max. number of retries
 TIME2SLEEP = 0.5     # time to sleep in seconds between retries
 
@@ -59,9 +62,11 @@
         self.id     = id
         self.title  = title
         self.url    = url
+        self.username = ''
+        self.password = ''
+        
         self._v_traversal = []  # keep path information relative to the mountpoint
 
-
     def __bobo_traverse__(self, request, entry_name):
         """ fool Zope traversal - hehehe"""
 
@@ -79,10 +84,25 @@
             # So we first retrieve the object we collected
             # traversal information from.
 
-            self.doRequest( string.join(self._v_traversal, '/') ) 
-            self._v_traversal = []
-
-            return getattr(self, entry_name)
+            tup  = urlparse.urlparse(self.url)
+            host = tup[1]
+            path = tup[2] 
+            if path[-1] != '/': path = path + '/'
+            path = path + string.join(self._v_traversal, '/')   
+
+            tries = 0
+            while tries < MAX_TRIES:
+
+                try:
+                    self._http_request(host, path)          
+                    self._v_traversal = []
+                    return getattr(self, entry_name)
+
+                except IOError:
+                    tries = tries + 1
+                    time.sleep(TIME2SLEEP)
+                 
+            raise IOError
 
         else:
 
@@ -105,31 +125,6 @@
         if REQUEST['URL1'][- len(self.id):] == self.id:
             sub_path = sub_path + self.default_document
 
-        self.doRequest(sub_path)
-
-        # get headers from original response and fake them into
-        # our response to the web client
-        for k in ('content-type','content-length','etag','last-modified'):
-            v = self._v_headers.getheader(k)
-            if v: RESPONSE.setHeader(k,v)
-
-        RESPONSE.write( self.getContent() )
-
-        return      
-
-
-    def __getitem__(self, item):
-        # check if we have the relative traversal stack
-        if not hasattr(self, '_v_traversal'):  
-            self._v_traversal = []
-        
-        self._v_traversal.append(item)
-        return self 
-        
-
-
-    def doRequest(self, sub_path):
-
         tries = 0
 
         while tries<MAX_TRIES:
@@ -140,56 +135,73 @@
                 if path[-1] != '/': path = path + '/'
                 path = path + sub_path
 
+                status, data, headers = self._http_request(host, path)
 
-                self._http_request(host, path)
+                # get headers from original response and fake them into
+                # our response to the web client
+                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)
 
-        raise IOError
+        return IOError
 
     index_html = __call__
 
-
     def _http_request(self, host, path):
         """ get URL """
                             
-        H = httplib.HTTP(host) 
+        if self.url[:5] == 'https':
+            from M2Crypto import httpslib
+            H = httpslib.HTTPS(host)
+        else:    
+            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')
+        if self.username:
+            b64 = encodestring( '%s:%s' % ( self.username, self.password ) )
+            H.putheader( 'Authorization', 'Basic %s' % b64 )
+            #print 'Authorization', 'Basic %s' % b64
         H.endheaders()
 
         status, msg, headers = H.getreply()
 
-        self._v_status  = status
-        self._v_msg     = msg
-        self._v_headers = headers
-        self._v_content = H.getfile().read()
-
-        return
+        content_len = 0
+        f = H.getfile()
+        buf = f.read()
+        l = len(buf)
+        self._v_content = ''
+        while l > 0:
+            self._v_content = self._v_content + buf
+            content_len = content_len + l
+            buf = f.read()
+            l = len(buf)
 
+        return (status, self._v_content, headers)
 
     def getContent(self):
         """ returns content"""
-
-        if not hasattr(self, '_v_content'):
-            self.doRequest( string.join(self._v_traversal, '/') )
-
-        self._v_traversal = []
         return self._v_content
 
 
-    def manage_preferences(self, title, url, default_document, RESPONSE=None, URL1=None):
+    def manage_preferences(self, title, url, username, password, default_document, RESPONSE=None, URL1=None):
         """ save preferences """
 
         self.title = title
         self.url   = url
         self.default_document = default_document
+        self.username = username
+        self.password = password
 
         RESPONSE.redirect(URL1 + '/manage_main?manage_tabs_message=Settings%20saved')
 


=== Packages/HTTPMounter/VERSION.txt 1.5 => 1.6 ===
--- Packages/HTTPMounter/VERSION.txt:1.5	Tue Jun  4 08:40:11 2002
+++ Packages/HTTPMounter/VERSION.txt	Thu Aug 29 06:09:43 2002
@@ -1 +1 @@
-0.21
+0.30