def Request(self, method, url, request_headers={}, body='', return_everything=False, return_cookies=False, report_hooks=[], response_to_path=False, long_timeout=False): (response, size_of_response, response_headers, cookies) = self._fake_responses[(method, url)] if response_to_path: temp_path = HC.GetTempPath() with open(temp_path, 'wb') as f: f.write(response) response = temp_path if return_everything: return (response, size_of_response, response_headers, cookies) elif return_cookies: return (response, cookies) else: return response
def _WriteResponseToPath(self, response, report_hooks): content_length = response.getheader('Content-Length') if content_length is not None: content_length = int(content_length) temp_path = HC.GetTempPath() size_of_response = 0 with open(temp_path, 'wb') as f: for block in HC.ReadFileLikeAsBlocks(response, self.read_block_size): if HC.shutdown: raise Exception('Application is shutting down!') size_of_response += len(block) if content_length is not None and size_of_response > content_length: raise Exception('Response was longer than suggested!') f.write(block) for hook in report_hooks: hook(content_length, size_of_response) return (temp_path, size_of_response)
def ConvertToPngIfBmp(path): with open(path, 'rb') as f: header = f.read(2) if header == 'BM': temp_path = HC.GetTempPath() shutil.move(path, temp_path) pil_image = GeneratePILImage(temp_path) pil_image = pil_image.convert('P') pil_image.save(path, 'PNG') os.remove(temp_path)
def _callbackParsePOSTArgs(self, request): request.content.seek(0) if not request.requestHeaders.hasHeader('Content-Type'): raise HydrusExceptions.ForbiddenException( 'No Content-Type header found!') content_types = request.requestHeaders.getRawHeaders('Content-Type') content_type = content_types[0] try: mime = HC.mime_enum_lookup[content_type] except: raise HydrusExceptions.ForbiddenException( 'Did not recognise Content-Type header!') if mime == HC.APPLICATION_YAML: yaml_string = request.content.read() request.hydrus_request_data_usage += len(yaml_string) hydrus_args = yaml.safe_load(yaml_string) else: temp_path = HC.GetTempPath() with open(temp_path, 'wb') as f: for block in HC.ReadFileLikeAsBlocks(request.content, 65536): f.write(block) request.hydrus_request_data_usage += len(block) hydrus_args = ParseFileArguments(temp_path) request.hydrus_args = hydrus_args return request