Beispiel #1
0
    def generate_webmethod_etag(context, item, registration, wrapper):
        if item is not None:
            # locate web method's descriptor
            filter_class = wrapper.__class__
            while wrapper.__class__.__module__ == filter_class.__module__:
                wrapper = wrapper.decorator

            method_id = [context.user._id,
                         struct.pack('>d', item.modified),
                         struct.pack('>q', hash(tuple(item.__props__.keys()))),
                         context.request.get_lang()]
            if item.isCollection:
                # add containment hash
                containment_hash = hash(tuple(item.containment))
                method_id.append(struct.pack('>q', containment_hash))
            try:
                # python 2.6
                func_code = wrapper.func.func_code
            except AttributeError:
                # python 3
                func_code = wrapper.func.__code__
            method_id.append(func_code.co_code)
            method_id.append(struct.pack('>q', hash(func_code.co_varnames)))
            # exclude None from consts
            method_id.append(struct.pack('>q', hash(func_code.co_consts[1:])))

            if wrapper.template is not None:
                func_dir = os.path.dirname(
                    sys.modules[wrapper.func.__module__].__file__)
                template_file = '%s%s%s' % (func_dir, os.path.sep,
                                            wrapper.template)
                method_id.append(misc.generate_file_etag(template_file))
            return misc.hash(*method_id).hexdigest()
Beispiel #2
0
    def apply(context, item, registration, **kwargs):
        if JSMerge.cache_folder is None:
            config = JSMerge.loadConfig()
            JSMerge.cache_folder = config['cache']
            if not (os.path.isdir(JSMerge.cache_folder)):
                os.makedirs(JSMerge.cache_folder)

        s_files = kwargs['files']
        if ',' in s_files:
            files = ([f.strip() for f in s_files.split(',')])
        else:
            files = misc.get_rto_by_name(s_files)()

        # check if output folder exists
        path = registration.path
        hash = misc.hash(*files).hexdigest()

        merged = path.replace(os.path.sep, '_')
        if os.name == 'nt':
            merged = merged.replace(os.path.altsep, '_').replace(':', '')

        glob_f = '%s/%s' % (JSMerge.cache_folder, merged)
        merged = '%s#%s.merge.js' % (glob_f, hash)

        revision = int(context.request.queryString['r'][0])

        JSMerge.lock.acquire()
        try:
            if (not os.path.isfile(merged)
                    or os.path.getmtime(merged) < revision):
                # remove old merged files
                oldfiles = glob.glob(glob_f + '*')
                [os.remove(old) for old in oldfiles]
                # generate new
                f = open(merged, 'w')
                for fname in files:
                    f.write(open(fname, 'r').read() + '\n')
                f.close()
        finally:
            JSMerge.lock.release()

        registration.context = merged
Beispiel #3
0
    def apply(context, item, registration, **kwargs):
        if JSMerge.cache_folder is None:
            config = JSMerge.loadConfig()
            JSMerge.cache_folder = config['cache']
            if not(os.path.isdir(JSMerge.cache_folder)):
                os.makedirs(JSMerge.cache_folder)

        s_files = kwargs['files']
        if ',' in s_files:
            files = ([f.strip() for f in s_files.split(',')])
        else:
            files = misc.get_rto_by_name(s_files)()

        # check if output folder exists
        path = registration.path
        hash = misc.hash(*files).hexdigest()

        merged = path.replace(os.path.sep, '_')
        if os.name == 'nt':
            merged = merged.replace(os.path.altsep, '_').replace(':', '')

        glob_f = '%s/%s' % (JSMerge.cache_folder, merged)
        merged = '%s#%s.merge.js' % (glob_f, hash)

        revision = int(context.request.queryString['r'][0])

        JSMerge.lock.acquire()
        try:
            if (not os.path.isfile(merged) or
                    os.path.getmtime(merged) < revision):
                # remove old merged files
                oldfiles = glob.glob(glob_f + '*')
                [os.remove(old) for old in oldfiles]
                # generate new
                f = open(merged, 'w')
                for fname in files:
                    f.write(open(fname, 'r').read() + '\n')
                f.close()
        finally:
            JSMerge.lock.release()

        registration.context = merged
 def generate_sig(session):
     return misc.hash(session.sessionid,
                      session.userid,
                      Session.secret,
                      algo='sha256').hexdigest()
Beispiel #5
0
    def _dispatch_method(self, item):
        method_name = context.request.method or '__blank__'
        method = None

        # get request parameters
        r_http_method = context.request.serverVariables['REQUEST_METHOD']
        r_browser = context.request.serverVariables['HTTP_USER_AGENT']
        r_qs = context.request.serverVariables['QUERY_STRING']
        r_lang = context.request.get_lang()

        method_key = misc.hash(str(hash(item.__class__)),
                               method_name,
                               r_http_method,
                               r_qs,
                               r_browser,
                               r_lang).digest()

        method = self._method_cache.get(method_key, None)
        if method is None:
            candidate_methods = [meth for meth in dir(item)
                                 if meth[:4 + len(method_name)] == \
                                 'WM_%s#' % method_name]

            if sys.version_info[0] == 2:
                # python 2.6
                kwargs = {
                    'cmp': lambda x, y: -cmp(
                        int(getattr(item, x).func_dict['cnd'][1] != '') +
                        int(getattr(item, x).func_dict['cnd'][3] != ''),
                        int(getattr(item, y).func_dict['cnd'][1] != '') +
                        int(getattr(item, y).func_dict['cnd'][3] != ''))}
            else:
                # python 3
                kwargs = {
                    'key': lambda x: - (
                        int(getattr(item, x).__dict__['cnd'][1] != '') +
                        int(getattr(item, x).__dict__['cnd'][3] != ''))}

            candidate_methods.sort(**kwargs)

            for method_name in candidate_methods:
                try:
                    # python 2.6
                    http_method, client, lang, qs = \
                        getattr(item, method_name).func_dict['cnd']
                except AttributeError:
                    # python 3
                    http_method, client, lang, qs = \
                        getattr(item, method_name).__dict__['cnd']

                if re.match(http_method, r_http_method) and \
                        re.search(qs, r_qs) and \
                        re.search(client, r_browser) and \
                        re.match(lang, r_lang):
                    method = method_name
                    break

            self._method_cache[method_key] = method

        if method is None:
            if context.request.type == 'http':
                raise exceptions.NotFound(
                    'Unknown method call "%s"' % method_name)
            else:
                # rpc call
                raise exceptions.RPCMethodNotFound(
                    'Remote method "%s" is not found' % method_name)
        else:
            # execute method
            getattr(item, method)(context)
Beispiel #6
0
 def generate_sig(session):
     return misc.hash(session.sessionid,
                      session.userid,
                      Session.secret,
                      algo='sha256').hexdigest()