lib/http.py
branchsites
changeset 42 5a72c00c4ae4
parent 25 8f143b1ce0d1
equal deleted inserted replaced
41:9585441a4bfb 42:5a72c00c4ae4
    27         # get the querystring
    27         # get the querystring
    28         self.arg_str = env.get('QUERY_STRING', '')
    28         self.arg_str = env.get('QUERY_STRING', '')
    29 
    29 
    30         # parse query args
    30         # parse query args
    31         self.arg_dict = cgi.parse_qs(self.arg_str, True)
    31         self.arg_dict = cgi.parse_qs(self.arg_str, True)
    32     
    32  
    33     def get_script_dir (self) :
    33     @property
       
    34     def site_host (self) :
       
    35         """
       
    36             Returns the site's hostname (DNS name)
       
    37         """
       
    38         
       
    39         return self.env['HTTP_HOST']
       
    40   
       
    41     @property
       
    42     def site_root (self) :
    34         """
    43         """
    35             Returns the URL path to the requested script's directory with no trailing slash, i.e.
    44             Returns the URL path to the requested script's directory with no trailing slash, i.e.
    36 
    45 
    37             /               -> 
    46             /               -> 
    38             /foo.cgi        -> 
    47             /foo.cgi        -> 
    39             /foo/bar.cgi    -> /foo
    48             /foo/bar.cgi    -> /foo
    40         """
    49         """
    41 
    50 
    42         return os.path.dirname(self.env['SCRIPT_NAME']).rstrip('/')
    51         return os.path.dirname(self.env['SCRIPT_NAME']).rstrip('/')
    43     
    52     
    44     def get_page_prefix (self) :
    53     @property
       
    54     def page_prefix (self) :
    45         """
    55         """
    46             Returns the URL path root for page URLs, based on REQUEST_URI with PATH_INFO removed
    56             Returns the URL path root for page URLs, based on REQUEST_URI with PATH_INFO removed
    47 
    57 
    48             /                   -> 
    58             /                   -> 
    49             /foo.cgi            -> /foo.cgi
    59             /foo.cgi            -> /foo.cgi
    66         # sanity-check
    76         # sanity-check
    67         assert request_path.endswith(page_name)
    77         assert request_path.endswith(page_name)
    68         
    78         
    69         # trim
    79         # trim
    70         return request_path[:-len(page_name)].rstrip('/')
    80         return request_path[:-len(page_name)].rstrip('/')
    71 
    81     
    72     def get_page_name (self) :
    82     def get_page_name (self) :
    73         """
    83         """
    74             Returns the requested page path with no leading slash, i.e.
    84             Returns the requested page path with no leading slash, i.e.
    75 
    85 
    76             /foo.cgi        -> 
    86             /foo.cgi        -> 
    86         if path_info :
    96         if path_info :
    87             return os.path.normpath(path_info).lstrip('/')
    97             return os.path.normpath(path_info).lstrip('/')
    88 
    98 
    89         else :
    99         else :
    90             return ''
   100             return ''
       
   101     
       
   102     def get_args (self) :
       
   103         """
       
   104             Iterate over all available (key, value) pairs from the query string
       
   105         """
       
   106 
       
   107         return cgi.parse_qsl(self.arg_str)
    91 
   108 
    92 class Response (object) :
   109 class Response (object) :
    93     """
   110     """
    94         HTTP Response with headers and data
   111         HTTP Response with headers and data
    95     """
   112     """