def _threadDoGETJob( self, request ): self._checkBandwidth( request ) # no permission check as any functional account can get thumbnails hash = request.hydrus_args[ 'hash' ] ( valid, mime ) = HG.server_controller.Read( 'service_has_file', self._service_key, hash ) if not valid: raise HydrusExceptions.NotFoundException( 'Thumbnail not found on this service!' ) if mime not in HC.MIMES_WITH_THUMBNAILS: raise HydrusExceptions.NotFoundException( 'That mime should not have a thumbnail!' ) path = ServerFiles.GetThumbnailPath( hash ) response_context = HydrusServerResources.ResponseContext( 200, mime = HC.APPLICATION_OCTET_STREAM, path = path ) return response_context
def GetUpdatePath(service_key, begin): path = GetExpectedUpdatePath(service_key, begin) if not os.path.exists(path): raise HydrusExceptions.NotFoundException('Update not found!') return path
def GetPath(file_type, hash): path = GetExpectedPath(file_type, hash) if not os.path.exists(path): raise HydrusExceptions.NotFoundException(file_type + ' not found!') return path
def GetThumbnailPath(hash): path = GetExpectedThumbnailPath(hash) if not os.path.exists(path): raise HydrusExceptions.NotFoundException('Thumbnail not found!') return path
def GetFilePath(hash): path = GetExpectedFilePath(hash) if not os.path.exists(path): raise HydrusExceptions.NotFoundException('File not found!') return path
def GetContentUpdatePackagePath(service_key, begin, subindex): path = GetExpectedContentUpdatePackagePath(service_key, begin, subindex) if not os.path.exists(path): raise HydrusExceptions.NotFoundException('Update not found!') return path
def _threadDoGETJob( self, request ): self._checkBandwidth( request ) # no permissions check as any functional account can get updates update_hash = request.hydrus_args[ 'update_hash' ] if not self._service.HasUpdateHash( update_hash ): raise HydrusExceptions.NotFoundException( 'This update hash does not exist on this service!' ) path = ServerFiles.GetFilePath( update_hash ) response_context = HydrusServerResources.ResponseContext( 200, mime = HC.APPLICATION_OCTET_STREAM, path = path ) return response_context
def _threadDoGETJob( self, request ): self._checkBandwidth( request ) # no permission check as any functional account can get files hash = request.hydrus_args[ 'hash' ] ( valid, mime ) = HG.server_controller.Read( 'service_has_file', self._service_key, hash ) if not valid: raise HydrusExceptions.NotFoundException( 'File not found on this service!' ) path = ServerFiles.GetFilePath( hash ) response_context = HydrusServerResources.ResponseContext( 200, mime = mime, path = path ) return response_context
def _threadDoPOSTJob( self, request ): raise HydrusExceptions.NotFoundException( 'This service does not support that request!' ) def _CleanUpTempFile( self, request ):
def _threadDoGETJob( self, request ): raise HydrusExceptions.NotFoundException( 'This service does not support that request!' )
def Request( self, method, path_and_query, request_headers, body, report_hooks = None, temp_path = None ): if report_hooks is None: report_hooks = [] if method == HC.GET: method_string = 'GET' elif method == HC.POST: method_string = 'POST' if 'User-Agent' not in request_headers: request_headers[ 'User-Agent' ] = 'hydrus/' + str( HC.NETWORK_VERSION ) path_and_query = HydrusData.ToByteString( path_and_query ) request_headers = { str( k ) : str( v ) for ( k, v ) in request_headers.items() } response = self._GetResponse( method_string, path_and_query, request_headers, body ) ( parsed_response, size_of_response ) = self._ReadResponse( response, report_hooks, temp_path ) response_headers = { k : v for ( k, v ) in response.getheaders() if k != 'set-cookie' } cookies = self._ParseCookies( response.getheader( 'set-cookie' ) ) self._last_request_time = HydrusData.GetNow() if response.status == 200: return ( parsed_response, None, size_of_response, response_headers, cookies ) elif response.status in ( 301, 302, 303, 307 ): location = response.getheader( 'Location' ) if location is None: raise Exception( 'Received an invalid redirection response.' ) else: url = location if ', ' in url: url = url.split( ', ' )[0] elif ' ' in url: # some booru is giving daft redirect responses HydrusData.Print( url ) url = urllib.quote( HydrusData.ToByteString( url ), safe = '/?=&' ) HydrusData.Print( url ) if not url.startswith( self._scheme ): # assume it is like 'index.php' or '/index.php', rather than 'http://blah.com/index.php' if url.startswith( '/' ): slash_sep = '' else: slash_sep = '/' url = self._scheme + '://' + self._host + slash_sep + url if response.status in ( 301, 307 ): # 301: moved permanently, repeat request # 307: moved temporarily, repeat request redirect_info = ( method, url ) elif response.status in ( 302, 303 ): # 302: moved temporarily, repeat request (except everyone treats it like 303 for no good f*****g reason) # 303: thanks, now go here with GET redirect_info = ( HC.GET, url ) return ( parsed_response, redirect_info, size_of_response, response_headers, cookies ) elif response.status == 304: raise HydrusExceptions.NotModifiedException() else: if response.status == 401: raise HydrusExceptions.PermissionException( parsed_response ) elif response.status == 403: raise HydrusExceptions.ForbiddenException( parsed_response ) elif response.status == 404: raise HydrusExceptions.NotFoundException( parsed_response ) elif response.status == 419: raise HydrusExceptions.SessionException( parsed_response ) elif response.status == 426: raise HydrusExceptions.NetworkVersionException( parsed_response ) elif response.status in ( 500, 501, 502, 503 ): server_header = response.getheader( 'Server' ) if server_header is not None and 'hydrus' in server_header: hydrus_service = True else: hydrus_service = False if response.status == 503 and hydrus_service: raise HydrusExceptions.ServerBusyException( 'Server is busy, please try again later.' ) else: raise Exception( parsed_response ) else: raise Exception( parsed_response )
def Request(self, method, path_and_query, request_headers, body, report_hooks=[], response_to_path=False): if method == HC.GET: method_string = 'GET' elif method == HC.POST: method_string = 'POST' if 'User-Agent' not in request_headers: request_headers['User-Agent'] = 'hydrus/' + HC.u( HC.NETWORK_VERSION) # it is important to only send str, not unicode, to httplib # it uses += to extend the message body, which propagates the unicode (and thus fails) when # you try to push non-ascii bytes as the body (e.g. during a file upload!) method_string = str(method_string) path_and_query = str(path_and_query) request_headers = { str(k): str(v) for (k, v) in request_headers.items() } try: self._connection.request(method_string, path_and_query, headers=request_headers, body=body) response = self._connection.getresponse() except (httplib.CannotSendRequest, httplib.BadStatusLine): # for some reason, we can't send a request on the current connection, so let's make a new one and try again! self._RefreshConnection() self._connection.request(method_string, path_and_query, headers=request_headers, body=body) response = self._connection.getresponse() if response.status == 200 and response_to_path: (temp_path, size_of_response) = self._WriteResponseToPath( response, report_hooks) parsed_response = temp_path else: (parsed_response, size_of_response) = self._ParseResponse(response, report_hooks) response_headers = { k: v for (k, v) in response.getheaders() if k != 'set-cookie' } cookies = self._ParseCookies(response.getheader('set-cookie')) self._last_request_time = HC.GetNow() if response.status == 200: return (parsed_response, None, size_of_response, response_headers, cookies) elif response.status in (301, 302, 303, 307): location = response.getheader('Location') if location is None: raise Exception(parsed_response) else: url = location if ' ' in url: # some booru is giving daft redirect responses print(url) url = urllib.quote(url, safe='/?=&') print(url) if not url.startswith(self._scheme): # assume it is like 'index.php' or '/index.php', rather than 'http://blah.com/index.php' if url.startswith('/'): slash_sep = '' else: slash_sep = '/' url = self._scheme + '://' + self._host + slash_sep + url if response.status in (301, 307): # 301: moved permanently, repeat request # 307: moved temporarily, repeat request redirect_info = (method, url) elif response.status in (302, 303): # 302: moved temporarily, repeat request (except everyone treats it like 303 for no good f*****g reason) # 303: thanks, now go here with GET redirect_info = (HC.GET, url) return (parsed_response, redirect_info, size_of_response, response_headers, cookies) elif response.status == 304: raise HydrusExceptions.NotModifiedException() else: if response.status == 401: raise HydrusExceptions.PermissionException(parsed_response) elif response.status == 403: raise HydrusExceptions.ForbiddenException(parsed_response) elif response.status == 404: raise HydrusExceptions.NotFoundException(parsed_response) elif response.status == 426: raise HydrusExceptions.NetworkVersionException(parsed_response) elif response.status in (500, 501, 502, 503): raise Exception(parsed_response) else: raise Exception(parsed_response)