etwist/server.py
branchstable
changeset 7879 9aae456abab5
parent 7815 2a164a9cf81c
child 8149 3ed48646f354
equal deleted inserted replaced
7874:be04706eacc9 7879:9aae456abab5
    26 import os.path as osp
    26 import os.path as osp
    27 import select
    27 import select
    28 import traceback
    28 import traceback
    29 import threading
    29 import threading
    30 import re
    30 import re
    31 import hashlib
    31 from hashlib import md5 # pylint: disable=E0611
    32 from os.path import join
    32 from os.path import join
    33 from time import mktime
    33 from time import mktime
    34 from datetime import date, timedelta
    34 from datetime import date, timedelta
    35 from urlparse import urlsplit, urlunsplit
    35 from urlparse import urlsplit, urlunsplit
    36 from cgi import FieldStorage, parse_header
    36 from cgi import FieldStorage, parse_header
    75                             code=http.FORBIDDEN,
    75                             code=http.FORBIDDEN,
    76                             stream='Access forbidden')
    76                             stream='Access forbidden')
    77 
    77 
    78 
    78 
    79 class NoListingFile(static.File):
    79 class NoListingFile(static.File):
       
    80     def __init__(self, config, path=None):
       
    81         if path is None:
       
    82             path = config.static_directory
       
    83         static.File.__init__(self, path)
       
    84         self.config = config
    80 
    85 
    81     def set_expires(self, request):
    86     def set_expires(self, request):
    82         if not self.config.debugmode:
    87         if not self.config.debugmode:
    83             # XXX: Don't provide additional resource information to error responses
    88             # XXX: Don't provide additional resource information to error responses
    84             #
    89             #
    91 
    96 
    92 
    97 
    93 class DataLookupDirectory(NoListingFile):
    98 class DataLookupDirectory(NoListingFile):
    94     def __init__(self, config, path):
    99     def __init__(self, config, path):
    95         self.md5_version = config.instance_md5_version()
   100         self.md5_version = config.instance_md5_version()
    96         NoListingFile.__init__(self, path)
   101         NoListingFile.__init__(self, config, path)
    97         self.config = config
       
    98         self.here = path
   102         self.here = path
    99         self._defineChildResources()
   103         self._defineChildResources()
   100         if self.config.debugmode:
   104         if self.config.debugmode:
   101             self.data_modconcat_basepath = '/data/??'
   105             self.data_modconcat_basepath = '/data/??'
   102         else:
   106         else:
   132             # directory lookup
   136             # directory lookup
   133             self.putChild(path, resource)
   137             self.putChild(path, resource)
   134             return resource
   138             return resource
   135         else:
   139         else:
   136             self.set_expires(request)
   140             self.set_expires(request)
   137             return NoListingFile(filepath)
   141             return NoListingFile(self.config, filepath)
   138 
   142 
   139 
   143 
   140 class FCKEditorResource(NoListingFile):
   144 class FCKEditorResource(NoListingFile):
   141     def __init__(self, config, path):
       
   142         NoListingFile.__init__(self, path)
       
   143         self.config = config
       
   144 
   145 
   145     def getChild(self, path, request):
   146     def getChild(self, path, request):
   146         pre_path = request.path.split('/')[1:]
   147         pre_path = request.path.split('/')[1:]
   147         if pre_path[0] == 'https':
   148         if pre_path[0] == 'https':
   148             pre_path.pop(0)
   149             pre_path.pop(0)
   177         _, ext = osp.splitext(paths[0])
   178         _, ext = osp.splitext(paths[0])
   178         self._resources = {}
   179         self._resources = {}
   179         # create a unique / predictable filename. We don't consider cubes
   180         # create a unique / predictable filename. We don't consider cubes
   180         # version since uicache is cleared at server startup, and file's dates
   181         # version since uicache is cleared at server startup, and file's dates
   181         # are checked in debug mode
   182         # are checked in debug mode
   182         fname = 'cache_concat_' + hashlib.md5(';'.join(paths)).hexdigest() + ext
   183         fname = 'cache_concat_' + md5(';'.join(paths)).hexdigest() + ext
   183         filepath = osp.join(config.appdatahome, 'uicache', fname)
   184         filepath = osp.join(config.appdatahome, 'uicache', fname)
   184         LongTimeExpiringFile.__init__(self, config, filepath)
   185         LongTimeExpiringFile.__init__(self, config, filepath)
   185         self._concat_cached_filepath(filepath, paths)
   186         self._concat_cached_filepath(filepath, paths)
   186 
   187 
   187     def _resource(self, path):
   188     def _resource(self, path):
   237         self.appli = CubicWebPublisher(config, vreg=vreg)
   238         self.appli = CubicWebPublisher(config, vreg=vreg)
   238         self.base_url = config['base-url']
   239         self.base_url = config['base-url']
   239         self.https_url = config['https-url']
   240         self.https_url = config['https-url']
   240         global MAX_POST_LENGTH
   241         global MAX_POST_LENGTH
   241         MAX_POST_LENGTH = config['max-post-length']
   242         MAX_POST_LENGTH = config['max-post-length']
   242         self.putChild('static', NoListingFile(config.static_directory))
   243         self.putChild('static', NoListingFile(config))
   243         self.putChild('fckeditor', FCKEditorResource(self.config, ''))
   244         self.putChild('fckeditor', FCKEditorResource(self.config, ''))
   244         self.putChild('data', DataLookupDirectory(self.config, ''))
   245         self.putChild('data', DataLookupDirectory(self.config, ''))
   245 
   246 
   246     def init_publisher(self):
   247     def init_publisher(self):
   247         config = self.config
   248         config = self.config
   399         else:
   400         else:
   400             content = self.appli.need_login_content(request)
   401             content = self.appli.need_login_content(request)
   401         return HTTPResponse(twisted_request=request._twreq,
   402         return HTTPResponse(twisted_request=request._twreq,
   402                             stream=content, code=code,
   403                             stream=content, code=code,
   403                             headers=request.headers_out)
   404                             headers=request.headers_out)
       
   405 
       
   406     # these are overridden by set_log_methods below
       
   407     # only defining here to prevent pylint from complaining
       
   408     @classmethod
       
   409     def debug(cls, msg, *a, **kw):
       
   410         pass
       
   411     info = warning = error = critical = exception = debug
   404 
   412 
   405 
   413 
   406 JSON_PATHS = set(('json',))
   414 JSON_PATHS = set(('json',))
   407 FRAME_POST_PATHS = set(('validateform',))
   415 FRAME_POST_PATHS = set(('validateform',))
   408 
   416