Ejemplo n.º 1
0
 def start(self):
     '''Start the connection'''
     if not self.destination_path:
         self.log('No output file specified.')
         self.done = True
         return
     url = NSURL.URLWithString_(self.url)
     request = (
         NSMutableURLRequest.requestWithURL_cachePolicy_timeoutInterval_(
             url, NSURLRequestReloadIgnoringLocalCacheData,
             self.connection_timeout))
     if self.additional_headers:
         for header, value in self.additional_headers.items():
             request.setValue_forHTTPHeaderField_(value, header)
     # does the file already exist? See if we can resume a partial download
     if os.path.isfile(self.destination_path):
         stored_data = self.get_stored_headers()
         if (self.can_resume and 'expected-length' in stored_data and
             ('last-modified' in stored_data or 'etag' in stored_data)):
             # we have a partial file and we're allowed to resume
             self.resume = True
             local_filesize = os.path.getsize(self.destination_path)
             byte_range = 'bytes=%s-' % local_filesize
             request.setValue_forHTTPHeaderField_(byte_range, 'Range')
     if self.download_only_if_changed and not self.resume:
         stored_data = self.cache_data or self.get_stored_headers()
         if 'last-modified' in stored_data:
             request.setValue_forHTTPHeaderField_(
                 stored_data['last-modified'], 'if-modified-since')
         if 'etag' in stored_data:
             request.setValue_forHTTPHeaderField_(stored_data['etag'],
                                                  'if-none-match')
     self.connection = NSURLConnection.alloc().initWithRequest_delegate_(
         request, self)
Ejemplo n.º 2
0
 def start(self):
     '''Start the connection'''
     if not self.destination_path:
         self.log('No output file specified.')
         self.done = True
         return
     url = NSURL.URLWithString_(self.url)
     request = (
         NSMutableURLRequest.requestWithURL_cachePolicy_timeoutInterval_(
             url, NSURLRequestReloadIgnoringLocalCacheData,
             self.connection_timeout))
     if self.additional_headers:
         for header, value in self.additional_headers.items():
             request.setValue_forHTTPHeaderField_(value, header)
     # does the file already exist? See if we can resume a partial download
     if os.path.isfile(self.destination_path):
         stored_data = self.get_stored_headers()
         if (self.can_resume and 'expected-length' in stored_data and
                 ('last-modified' in stored_data or 'etag' in stored_data)):
             # we have a partial file and we're allowed to resume
             self.resume = True
             local_filesize = os.path.getsize(self.destination_path)
             byte_range = 'bytes=%s-' % local_filesize
             request.setValue_forHTTPHeaderField_(byte_range, 'Range')
     if self.download_only_if_changed and not self.resume:
         stored_data = self.cache_data or self.get_stored_headers()
         if 'last-modified' in stored_data:
             request.setValue_forHTTPHeaderField_(
                 stored_data['last-modified'], 'if-modified-since')
         if 'etag' in stored_data:
             request.setValue_forHTTPHeaderField_(
                 stored_data['etag'], 'if-none-match')
     self.connection = NSURLConnection.alloc().initWithRequest_delegate_(
         request, self)
Ejemplo n.º 3
0
Archivo: gurl.py Proyecto: zippyy/munki
    def start(self):
        '''Start the connection'''
        if not self.destination_path:
            self.log('No output file specified.')
            self.done = True
            return
        url = NSURL.URLWithString_(self.url)
        request = (
            NSMutableURLRequest.requestWithURL_cachePolicy_timeoutInterval_(
                url, NSURLRequestReloadIgnoringLocalCacheData,
                self.connection_timeout))
        if self.additional_headers:
            for header, value in self.additional_headers.items():
                request.setValue_forHTTPHeaderField_(value, header)
        # does the file already exist? See if we can resume a partial download
        if os.path.isfile(self.destination_path):
            stored_data = self.getStoredHeaders()
            if (self.can_resume and 'expected-length' in stored_data and
                ('last-modified' in stored_data or 'etag' in stored_data)):
                # we have a partial file and we're allowed to resume
                self.resume = True
                local_filesize = os.path.getsize(self.destination_path)
                byte_range = 'bytes=%s-' % local_filesize
                request.setValue_forHTTPHeaderField_(byte_range, 'Range')
        if self.download_only_if_changed and not self.resume:
            stored_data = self.cache_data or self.getStoredHeaders()
            if 'last-modified' in stored_data:
                request.setValue_forHTTPHeaderField_(
                    stored_data['last-modified'], 'if-modified-since')
            if 'etag' in stored_data:
                request.setValue_forHTTPHeaderField_(stored_data['etag'],
                                                     'if-none-match')
        if NSURLSESSION_AVAILABLE:
            configuration = (
                NSURLSessionConfiguration.defaultSessionConfiguration())

            # optional: ignore system http/https proxies (10.9+ only)
            if self.ignore_system_proxy is True:
                configuration.setConnectionProxyDictionary_({
                    kCFNetworkProxiesHTTPEnable:
                    False,
                    kCFNetworkProxiesHTTPSEnable:
                    False
                })

            # set minumum supported TLS protocol (defaults to TLS1)
            configuration.setTLSMinimumSupportedProtocol_(
                self.minimum_tls_protocol)

            self.session = (
                NSURLSession.sessionWithConfiguration_delegate_delegateQueue_(
                    configuration, self, None))
            self.task = self.session.dataTaskWithRequest_(request)
            self.task.resume()
        else:
            self.connection = NSURLConnection.alloc(
            ).initWithRequest_delegate_(request, self)
Ejemplo n.º 4
0
    def start(self):
        '''Start the connection'''
        if not self.destination_path:
            self.log('No output file specified.')
            self.done = True
            return
        url = NSURL.URLWithString_(self.url)
        request = (
            NSMutableURLRequest.requestWithURL_cachePolicy_timeoutInterval_(
                url, NSURLRequestReloadIgnoringLocalCacheData,
                self.connection_timeout))
        if self.additional_headers:
            for header, value in self.additional_headers.items():
                request.setValue_forHTTPHeaderField_(value, header)
        # does the file already exist? See if we can resume a partial download
        if os.path.isfile(self.destination_path):
            stored_data = self.getStoredHeaders()
            if (self.can_resume and 'expected-length' in stored_data and
                    ('last-modified' in stored_data or 'etag' in stored_data)):
                # we have a partial file and we're allowed to resume
                self.resume = True
                local_filesize = os.path.getsize(self.destination_path)
                byte_range = 'bytes=%s-' % local_filesize
                request.setValue_forHTTPHeaderField_(byte_range, 'Range')
        if self.download_only_if_changed and not self.resume:
            stored_data = self.cache_data or self.getStoredHeaders()
            if 'last-modified' in stored_data:
                request.setValue_forHTTPHeaderField_(
                    stored_data['last-modified'], 'if-modified-since')
            if 'etag' in stored_data:
                request.setValue_forHTTPHeaderField_(
                    stored_data['etag'], 'if-none-match')
        if NSURLSESSION_AVAILABLE:
            configuration = (
                NSURLSessionConfiguration.defaultSessionConfiguration())

            # optional: ignore system http/https proxies (10.9+ only)
            if self.ignore_system_proxy is True:
                configuration.setConnectionProxyDictionary_(
                    {kCFNetworkProxiesHTTPEnable: False,
                     kCFNetworkProxiesHTTPSEnable: False})

            # set minumum supported TLS protocol (defaults to TLS1)
            configuration.setTLSMinimumSupportedProtocol_(
                self.minimum_tls_protocol)

            self.session = (
                NSURLSession.sessionWithConfiguration_delegate_delegateQueue_(
                    configuration, self, None))
            self.task = self.session.dataTaskWithRequest_(request)
            self.task.resume()
        else:
            self.connection = NSURLConnection.alloc().initWithRequest_delegate_(
                request, self)
Ejemplo n.º 5
0
    def start(self):
        """Start the connection."""
        url = NSURL.URLWithString_(self.url)
        request = NSMutableURLRequest.requestWithURL_cachePolicy_timeoutInterval_(
            url, NSURLRequestReloadIgnoringLocalCacheData,
            self.connection_timeout)
        if self.additional_headers:
            for header, value in self.additional_headers.items():
                request.setValue_forHTTPHeaderField_(value, header)
        request.setHTTPMethod_(self.method)

        if self.method == "POST":
            body_unicode = unicode(self.body)
            body_data = NSData.dataWithBytes_length_(
                NSString.stringWithString_(body_unicode).UTF8String(),
                len(body_unicode.encode("utf-8")),
            )
            request.setHTTPBody_(body_data)

        self.connection = NSURLConnection.alloc().initWithRequest_delegate_(
            request, self)