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)
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)
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)
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)
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)
def build_request(request, timeout): # type: (PreparedRequest, Union[float, Tuple[float, float]]) -> NSMutableURLRequest """Convert a requests `PreparedRequest` into a suitable NSMutableURLRequest.""" timeout = timeout if timeout else 0.0 nsrequest = NSMutableURLRequest.requestWithURL_cachePolicy_timeoutInterval_( NSURL.URLWithString_(request.url), NSURLRequestReloadIgnoringLocalCacheData, timeout, ) nsrequest.setHTTPMethod_(request.method) for k, v in request.headers.items(): k, v = k.lower(), v.lower() if k in _RESERVED_HEADERS: logging.debug('Ignoring reserved header: %s', k) continue nsrequest.setValue_forHTTPHeaderField_(v, k) return nsrequest