Disconnected operation.
[bootmanager.git] / source / BootServerRequest.py
index f37b2ce..75fad3c 100644 (file)
@@ -2,50 +2,15 @@
 
 # Copyright (c) 2003 Intel Corporation
 # All rights reserved.
-
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions are
-# met:
-
-#     * Redistributions of source code must retain the above copyright
-#       notice, this list of conditions and the following disclaimer.
-
-#     * Redistributions in binary form must reproduce the above
-#       copyright notice, this list of conditions and the following
-#       disclaimer in the documentation and/or other materials provided
-#       with the distribution.
-
-#     * Neither the name of the Intel Corporation nor the names of its
-#       contributors may be used to endorse or promote products derived
-#       from this software without specific prior written permission.
-
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
-# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
-# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
-# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
-# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
-# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-# EXPORT LAWS: THIS LICENSE ADDS NO RESTRICTIONS TO THE EXPORT LAWS OF
-# YOUR JURISDICTION. It is licensee's responsibility to comply with any
-# export regulations applicable in licensee's jurisdiction. Under
-# CURRENT (May 2000) U.S. export regulations this software is eligible
-# for export from the U.S. and can be downloaded by or otherwise
-# exported or reexported worldwide EXCEPT to U.S. embargoed destinations
-# which include Cuba, Iraq, Libya, North Korea, Iran, Syria, Sudan,
-# Afghanistan and any other country to which the U.S. has embargoed
-# goods and services.
-
+#
+# Copyright (c) 2004-2006 The Trustees of Princeton University
+# All rights reserved.
 
 import os, sys
 import re
 import string
 import urllib
+import tempfile
 
 # try to load pycurl
 try:
@@ -210,126 +175,34 @@ class BootServerRequest:
     def MakeRequest( self, PartialPath, GetVars,
                      PostVars, DoSSL, DoCertCheck,
                      ConnectTimeout= DEFAULT_CURL_CONNECT_TIMEOUT,
-                     MaxTransferTime= DEFAULT_CURL_MAX_TRANSFER_TIME):
-
-        if PYCURL_LOADED == 0:
-            self.Error( "MakeRequest method requires pycurl." )
-            return None
+                     MaxTransferTime= DEFAULT_CURL_MAX_TRANSFER_TIME,
+                     FormData= None):
 
-        self.CheckProxy()
-        
-        self.Message( "Attempting to retrieve %s" % PartialPath )
-        
-        # we can't do ssl and check the cert if we don't have a bootcd
-        if DoSSL and DoCertCheck and not self.HAS_BOOTCD:
-            self.Error( "No boot cd exists (needed to use -c and -s.\n" )
-            return None
-
-        # didn't pass a path in? just get the root doc
-        if PartialPath == "":
-            PartialPath= "/"
-
-        # ConnectTimeout has to be greater than 0
-        if ConnectTimeout <= 0:
-            self.Error( "Connect timeout must be greater than zero.\n" )
-            return None
-
-        # setup the post and get vars for the request
-        if PostVars:
-            dopostdata= 1
-            postdata = urllib.urlencode(PostVars)
-            self.Message( "Posting data:\n%s\n" % postdata )
+        if hasattr(tempfile, "NamedTemporaryFile"):
+            buffer = tempfile.NamedTemporaryFile()
+            buffer_name = buffer.name
         else:
-            dopostdata= 0
-
-        getstr= ""
-        if GetVars:
-            getstr= "?" + urllib.urlencode(GetVars)
-            self.Message( "Get data:\n%s\n" % getstr )
-
-        # now, attempt to make the request, starting at the first
-        # server in the list
-        for server in self.BOOTSERVER_CERTS:
-            self.Message( "Contacting server %s." % server )
-            
-            certpath = self.BOOTSERVER_CERTS[server]
-            
-            curl= pycurl.Curl()
-
-            # don't want curl sending any signals
-            curl.setopt(pycurl.NOSIGNAL, 1)
-
-            curl.setopt(pycurl.CONNECTTIMEOUT, ConnectTimeout)
-            self.Message( "Connect timeout is %s seconds" % \
-                          ConnectTimeout )
-
-            curl.setopt(pycurl.TIMEOUT, MaxTransferTime)
-            self.Message( "Max transfer time is %s seconds" % \
-                          MaxTransferTime )
-            
-            curl.setopt(pycurl.FOLLOWLOCATION, 1)
-            curl.setopt(pycurl.MAXREDIRS, 2)
-
-            if self.USE_PROXY:
-                curl.setopt(pycurl.PROXY, self.PROXY )
-            
-            if DoSSL:
-                curl.setopt(pycurl.SSLVERSION, self.CURL_SSL_VERSION)
-
-                url = "https://%s/%s%s" % (server,PartialPath,getstr)
-                if DoCertCheck:
-                    curl.setopt(pycurl.CAINFO, certpath)
-                    curl.setopt(pycurl.SSL_VERIFYPEER, 2)    
-                    self.Message( "Using SSL version %d and verifying peer." % \
-                                  self.CURL_SSL_VERSION )
-                else:
-                    curl.setopt(pycurl.SSL_VERIFYPEER, 0)
-                    self.Message( "Using SSL version %d" % \
-                                  self.CURL_SSL_VERSION )
-            else:
-                url = "http://%s/%s%s" % (server,PartialPath,getstr)
-
-            if dopostdata:
-                curl.setopt(pycurl.POSTFIELDS, postdata)
-                
-            curl.setopt(pycurl.URL, url)
-            self.Message( "URL: %s" % url )
-            
-            # setup the output buffer
-            buffer = StringIO()
-            curl.setopt(pycurl.WRITEFUNCTION, buffer.write)
-            
-            try:
-                self.Message( "Fetching..." )
-                curl.perform()
-                self.Message( "Done." )
-                
-                http_result= curl.getinfo(pycurl.HTTP_CODE)
-                curl.close()
-
-                # check the code, return the string only if it was successfull
-                if http_result == self.HTTP_SUCCESS:
-                    self.Message( "Successfull!" )
-                    return buffer.getvalue()
-                else:
-                    self.Message( "Failure, resultant http code: %d" % \
-                                  http_result )
-                    return None
-                
-            except pycurl.error, err:
-                errno, errstr= err
-                self.Error( "connect to %s failed; curl error %d: '%s'\n" %
-                       (server,errno,errstr) ) 
-
-        self.Error( "Unable to successfully contact any boot servers.\n" )
-        return None
-   
-
+            buffer_name = tempfile.mktemp("MakeRequest")
+            buffer = open(buffer_name, "w+")
+
+        ok = self.DownloadFile(PartialPath, GetVars, PostVars,
+                               DoSSL, DoCertCheck, buffer_name,
+                               ConnectTimeout,
+                               MaxTransferTime,
+                               FormData)
+
+        # check the code, return the string only if it was successfull
+        if ok:
+            buffer.seek(0)
+            return buffer.read()
+        else:
+            return None
 
     def DownloadFile(self, PartialPath, GetVars, PostVars,
                      DoSSL, DoCertCheck, DestFilePath,
                      ConnectTimeout= DEFAULT_CURL_CONNECT_TIMEOUT,
-                     MaxTransferTime= DEFAULT_CURL_MAX_TRANSFER_TIME):
+                     MaxTransferTime= DEFAULT_CURL_MAX_TRANSFER_TIME,
+                     FormData= None):
 
         self.Message( "Attempting to retrieve %s" % PartialPath )
 
@@ -424,6 +297,10 @@ class BootServerRequest:
                 if dopostdata:
                     curl.setopt(pycurl.POSTFIELDS, postdata)
 
+                # setup multipart/form-data upload
+                if FormData:
+                    curl.setopt(pycurl.HTTPPOST, FormData)
+
                 curl.setopt(pycurl.URL, url)
             else:
 
@@ -439,6 +316,9 @@ class BootServerRequest:
                 if dopostdata:
                     cmdline = cmdline + "--data '" + postdata + "' "
 
+                if FormData:
+                    cmdline = cmdline + "".join(["--form '" + field + "' " for field in FormData])
+
                 if not self.VERBOSE:
                     cmdline = cmdline + "--silent "
                     
@@ -491,7 +371,7 @@ class BootServerRequest:
                 if not outfile.closed:
                     try:
                         os.unlink(DestFilePath)
-                        outfile.close
+                        outfile.close()
                     except OSError:
                         pass