hydrus/include/HydrusServer.py

94 lines
2.6 KiB
Python
Raw Normal View History

2019-01-09 22:59:03 +00:00
from . import HydrusConstants as HC
from . import HydrusServerResources
2013-02-19 00:11:43 +00:00
import traceback
2019-08-21 21:34:01 +00:00
from twisted.web.http import _GenericHTTPChannelProtocol, HTTPChannel
2015-03-25 22:04:19 +00:00
from twisted.web.server import Request, Site
2013-10-02 22:06:06 +00:00
from twisted.web.resource import Resource
2019-01-09 22:59:03 +00:00
from . import HydrusData
2015-11-18 22:44:07 +00:00
import time
2013-02-19 00:11:43 +00:00
2014-07-09 22:15:14 +00:00
LOCAL_DOMAIN = HydrusServerResources.HydrusDomain( True )
REMOTE_DOMAIN = HydrusServerResources.HydrusDomain( False )
2013-10-02 22:06:06 +00:00
class HydrusRequest( Request ):
2013-02-19 00:11:43 +00:00
2013-10-02 22:06:06 +00:00
def __init__( self, *args, **kwargs ):
2013-02-19 00:11:43 +00:00
2013-10-02 22:06:06 +00:00
Request.__init__( self, *args, **kwargs )
2013-02-19 00:11:43 +00:00
2015-11-18 22:44:07 +00:00
self.start_time = time.clock()
2019-02-27 23:03:30 +00:00
self.parsed_request_args = None
2013-10-02 22:06:06 +00:00
self.hydrus_response_context = None
2019-04-17 21:51:50 +00:00
self.hydrus_account = None
2019-07-03 22:49:27 +00:00
self.client_api_permissions = None
2013-02-19 00:11:43 +00:00
2013-09-25 20:20:10 +00:00
2019-04-17 21:51:50 +00:00
class HydrusRequestLogging( HydrusRequest ):
2015-11-18 22:44:07 +00:00
def finish( self ):
2019-04-17 21:51:50 +00:00
HydrusRequest.finish( self )
2015-11-18 22:44:07 +00:00
host = self.getHost()
if self.hydrus_response_context is not None:
2019-01-09 22:59:03 +00:00
status_text = str( self.hydrus_response_context.GetStatusCode() )
2015-11-18 22:44:07 +00:00
2017-03-02 02:14:56 +00:00
elif hasattr( self, 'code' ):
status_text = str( self.code )
else:
status_text = '200'
2015-11-18 22:44:07 +00:00
2019-01-09 22:59:03 +00:00
message = str( host.port ) + ' ' + str( self.method, 'utf-8' ) + ' ' + str( self.path, 'utf-8' ) + ' ' + status_text + ' in ' + HydrusData.TimeDeltaToPrettyTimeDelta( time.clock() - self.start_time )
2015-11-18 22:44:07 +00:00
HydrusData.Print( message )
2019-08-21 21:34:01 +00:00
class FatHTTPChannel( HTTPChannel ):
totalHeadersSize = 1048576 # :^)
2013-10-02 22:06:06 +00:00
class HydrusService( Site ):
2013-09-25 20:20:10 +00:00
2017-03-02 02:14:56 +00:00
def __init__( self, service ):
2013-09-25 20:20:10 +00:00
2017-03-02 02:14:56 +00:00
self._service = service
2013-09-25 20:20:10 +00:00
2013-10-02 22:06:06 +00:00
root = self._InitRoot()
2013-09-25 20:20:10 +00:00
2013-10-02 22:06:06 +00:00
Site.__init__( self, root )
2013-09-25 20:20:10 +00:00
2019-08-21 21:34:01 +00:00
self.protocol = self._ProtocolFactory
2019-04-17 21:51:50 +00:00
if service.LogsRequests():
self.requestFactory = HydrusRequestLogging
else:
self.requestFactory = HydrusRequest
2013-09-25 20:20:10 +00:00
2013-10-02 22:06:06 +00:00
def _InitRoot( self ):
2013-09-25 20:20:10 +00:00
2013-10-02 22:06:06 +00:00
root = Resource()
2013-09-25 20:20:10 +00:00
2019-01-09 22:59:03 +00:00
root.putChild( b'', HydrusServerResources.HydrusResourceWelcome( self._service, REMOTE_DOMAIN ) )
root.putChild( b'favicon.ico', HydrusServerResources.hydrus_favicon )
root.putChild( b'robots.txt', HydrusServerResources.HydrusResourceRobotsTXT( self._service, REMOTE_DOMAIN ) )
2013-09-25 20:20:10 +00:00
2013-10-02 22:06:06 +00:00
return root
2013-09-25 20:20:10 +00:00
2019-08-21 21:34:01 +00:00
def _ProtocolFactory( self ):
return _GenericHTTPChannelProtocol( FatHTTPChannel() )