Changeset 247


Ignore:
Timestamp:
07/16/07 12:56:56 (5 years ago)
Author:
marc
Message:

Abstracted RootResource? to DataResource? so Static.Data can be served with it.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/HACKING

    r224 r247  
    11NOTE: This guide is a rough reference, please take the time to read the code 
    2 line by line, as it is not that long. 
     2line by line, as it is not that long. It is also outdated in some parts. 
    33 
    44Abstract: 
  • trunk/server.py

    r246 r247  
    172172            return self.server.getHost() 
    173173 
     174 
    174175class ScreenshotServer(BaseHTTPServer): 
    175176    """ 
     
    197198        # Also we create our unique authentication handler this keeps track if the user authenticated or not 
    198199        self.authresource = AuthenticatedResource(self.gui) 
    199         self.root = RootResource(self.gui, self.authresource, self.itaka_globals.head_html + self.configuration['html']['html'] + self.itaka_globals.footer_html) 
     200        self.root = DataResource(self.gui, self.authresource, self.itaka_globals.head_html + self.configuration['html']['html'] + self.itaka_globals.footer_html) 
    200201        self.add_child_to_resource('root', '', self.root) 
    201202        self.add_child_to_resource('root', 'screenshot', ScreenshotResource(self.gui, self.authresource)) 
     
    204205        self.create_site(self.root) 
    205206 
     207 
    206208class AuthenticatedResource: 
    207209    """ 
    208     Helper object to handle authentication for resources. 
     210    Helper object to handle authentication for Resources. 
     211    Please read RFC 2617 to understand the HTTP Authentication process 
    209212    """ 
    210213 
     
    282285        
    283286        if not self.username and not self.password: 
    284             self.gui.log.failure(('RootResource', 'render'), (_('Client provided empty username and password'), _('Client %s provided empty username and password') % (self.ip)), 'WARNING') 
     287            self.gui.log.failure(('AuthenticatedResource', 'render'), (_('Client provided empty username and password'), _('Client %s provided empty username and password') % (self.ip)), 'WARNING') 
    285288            self._prompt_auth() 
    286289        else: 
    287290            if self.username != self.configuration['server']['username'] or self.password != self.configuration['server']['password']: 
    288                 self.gui.log.failure(('RootResource', 'render'), (_('Client provided incorrect username and password'), _('Client %s provided incorrect username and password: %s:%s') % (self.ip, self.username, self.password)), 'WARNING') 
     291                self.gui.log.failure(('AuthenticatedResource', 'render'), (_('Client provided incorrect username and password'), _('Client %s provided incorrect username and password: %s:%s') % (self.ip, self.username, self.password)), 'WARNING') 
    289292                self._prompt_auth() 
    290293            elif self.username == self.configuration['server']['username'] and self.password == self.configuration['server']['password']: 
     
    314317            return self.noauth 
    315318     
    316 class RootResource(static.Data): 
    317     """ 
    318     Main resource with authentication support 
    319  
    320     Please read RFC 2617 to understand the HTTP Authentication process 
     319class DataResource(static.Data): 
     320    """ 
     321    Generic Resource for data 
    321322    """ 
    322323 
     
    371372            return self.data 
    372373 
     374class FileResource(resource.Resource): 
     375    """  
     376    Generic Resource for file objects 
     377    """ 
     378 
     379    def __init__(self, gui_instance, auth_instance, path, type): 
     380        """  
     381        Constructor 
     382 
     383        @type gui_instance: Gui 
     384        @param gui_instance: An instance of our L{Gui} class 
     385 
     386        @type auth_instance: AuthenticatedResource 
     387        @param auth_instance: An instance of our L{AuthenticatedResource} class 
     388 
     389        @type path: string 
     390        @param path: The path to the file to be served 
     391 
     392        @type type: str 
     393        @param type: The MIME-type to bepassed to Content-Type 
     394        """ 
     395 
     396        self.gui = gui_instance 
     397        self.auth = auth_instance 
     398        self.itaka_globals = self.gui.itaka_globals 
     399 
     400        self.type = type 
     401        self.data = open(path, 'rb').read() 
     402        self.size = str(os.stat(path).st_size) 
     403 
     404    def render_GET(self, request): 
     405        """ 
     406        Handle GET requests 
     407 
     408        @type request: instance 
     409        @param request: twisted.web.server.Request instance 
     410        """ 
     411 
     412        self.configuration = self.gui.configuration 
     413        self.request = request 
     414 
     415        if self.configuration['server']['authentication']: 
     416            if self.auth.authenticated or self.auth.authenticate(self.request): 
     417                self.auth.set_request_data(self.data, self.size, self.type, True) 
     418            return self.auth.return_object_data() 
     419        else: 
     420            self.request.setHeader('Content-Type', self.type) 
     421            self.request.setHeader('Content-Length', self.size) 
     422            self.request.setHeader('Connection', 'close') 
     423        if self.itaka_globals.console_verbosity['normal']:  
     424            BaseMessage(_('Itaka shutting down')) 
     425            return self.data 
    373426 
    374427class ScreenshotResource(resource.Resource): 
    375428    """  
    376     Handle server requests and call for a screenshot 
     429    Handle request and call for a screenshot 
    377430    """ 
    378431 
     
    399452    def get_screenshot(self): 
    400453        """ 
    401         Takes a screenshot and notify the GUI. 
     454        Takes a screenshot and notifies the GUI. 
    402455        """ 
    403456 
     
    454507            self.request.setHeader('Connection', 'close') 
    455508            return self.data 
    456  
    457 class FileResource(resource.Resource): 
    458     """  
    459     Generic handler for file resources 
    460     """ 
    461  
    462     def __init__(self, gui_instance, auth_instance, path, type): 
    463         """  
    464         Constructor 
    465  
    466         @type gui_instance: Gui 
    467         @param gui_instance: An instance of our L{Gui} class 
    468  
    469         @type auth_instance: AuthenticatedResource 
    470         @param auth_instance: An instance of our L{AuthenticatedResource} class 
    471  
    472         @type path: string 
    473         @param path: The path to the file to be served 
    474  
    475         @type type: str 
    476         @param type: The MIME-type to bepassed to Content-Type 
    477         """ 
    478  
    479         self.gui = gui_instance 
    480         self.auth = auth_instance 
    481         self.itaka_globals = self.gui.itaka_globals 
    482  
    483         self.type = type 
    484         self.data = open(path, 'rb').read() 
    485         self.size = str(os.stat(path).st_size) 
    486  
    487     def render_GET(self, request): 
    488         """ 
    489         Handle GET requests 
    490  
    491         @type request: instance 
    492         @param request: twisted.web.server.Request instance 
    493         """ 
    494  
    495         self.configuration = self.gui.configuration 
    496         self.request = request 
    497  
    498         if self.configuration['server']['authentication']: 
    499             if self.auth.authenticated or self.auth.authenticate(self.request): 
    500                 self.auth.set_request_data(self.data, self.size, self.type, True) 
    501             return self.auth.return_object_data() 
    502         else: 
    503             self.request.setHeader('Content-Type', self.type) 
    504             self.request.setHeader('Content-Length', self.size) 
    505             self.request.setHeader('Connection', 'close') 
    506             return self.data 
Note: See TracChangeset for help on using the changeset viewer.