Esempio n. 1
0
        def execute(self, item, context):
            if allowed_origins:
                origin = WebMethod._getAccessControlAllowOrigin(
                    allowed_origins,
                    context)
                if origin:
                    context.response.set_header("Access-Control-Allow-Origin",
                                                origin)
                elif context.request.HTTP_ORIGIN:
                    return

            if context.request.type == 'xmlrpc':
                rpc = xmlrpc
                rq_body = context.request.input
                context.response.content_type = 'application/xml'
            elif context.request.type == 'jsonrpc':
                rpc = jsonrpc
                rq_body = context.request.input.decode(context.request.charset)

            context.request.id, args = rpc.loads(rq_body)

            # execute method
            v = self.func(item, *args)

            if v is not None:
                context.response.write(
                    rpc.dumps(context.request.id, v, self.encoding))
                return v
            else:
                raise exceptions.InternalServerError(
                    'Remote method "%s" returns no value' %
                    context.request.method)
Esempio n. 2
0
 def thread_loop(self):
     # run as system
     time_to_run = int(time.time()) + self.interval
     context.user = _db.get_item('system')
     while self.running:
         time.sleep(1)
         if int(time.time()) >= time_to_run:
             time_to_run = int(time.time()) + self.interval
             try:
                 self.execute()
             except:
                 e = exceptions.InternalServerError()
                 e.emit()
Esempio n. 3
0
def get_transaction():
    """
    Returns a transaction handle required for database updates.
    Normally, this is used for having control over transaction commits.
    Currently, nested transactions are not supported.
    Subsequent calls to C{getTransaction} will return the same handle.

    @rtype: L{BaseTransaction<porcupine.db.basetransaction.BaseTransaction>}
    """
    if not context._is_txnal:
        raise exceptions.InternalServerError(
            "Not in a transactional context. Use @db.transactional().")
    return context._trans
Esempio n. 4
0
    def handle_request(self, rh, raw_request=None):
        if raw_request is None:
            raw_request = loads(rh.input_buffer)
        response = context.response = HttpResponse()
        request = context.request = HttpRequest(raw_request)

        item = None
        registration = None

        # get sessionid
        session_id = None
        cookies_enabled = True
        path_info = request.serverVariables['PATH_INFO']

        #print(path_info)

        # detect if sessionid is injected in the URL
        session_match = re.match(self._sid_pattern, path_info)
        if session_match:
            path_info = path_info.replace(session_match.group(), '', 1) or '/'
            request.serverVariables['PATH_INFO'] = path_info
            session_id = session_match.group(1)
            cookies_enabled = False
        # otherwise check cookie
        elif '_sid' in request.cookies:
            session_id = request.cookies['_sid'].value
            cookies_enabled = True

        try:
            try:
                path_tokens = path_info.split('/')

                if len(path_tokens) > 1:
                    dir_name = path_tokens[1]
                    web_app = pubdirs.dirs.get(dir_name, None)
                else:
                    web_app = None

                if web_app is None:
                    # create snapshot txn for reads
                    context._trans = _db.get_transaction(snapshot=True)
                    # try to get the requested object from the db
                    item = _db.get_item(path_info)
                    if item is not None and not item._isDeleted:
                        self._fetch_session(session_id, cookies_enabled)
                        self._dispatch_method(item)
                    else:
                        raise exceptions.NotFound(
                            'The resource "%s" does not exist' % path_info)
                else:
                    # request to a published directory
                    self._fetch_session(session_id, cookies_enabled)

                    # remove blank entry & app name to get the requested path
                    dir_path = '/'.join(path_tokens[2:])
                    registration = web_app.get_registration(
                        dir_path,
                        request.serverVariables['REQUEST_METHOD'],
                        request.serverVariables['HTTP_USER_AGENT'],
                        request.get_lang())
                    if not registration:
                        raise exceptions.NotFound(
                            'The resource "%s" does not exist' % path_info)

                    # apply pre-processing filters
                    [filter[0].apply(context, item, registration, **filter[1])
                     for filter in registration.filters
                     if filter[0].type == 'pre']

                    rtype = registration.type
                    if rtype == 1:  # psp page
                        # create snapshot txn for reads
                        context._trans = _db.get_transaction(snapshot=True)
                        ServerPage.execute(context, registration.context)
                    elif rtype == 0:  # static file
                        f_name = registration.context
                        if_none_match = request.HTTP_IF_NONE_MATCH
                        if if_none_match is not None and if_none_match == \
                                '"%s"' % misc.generate_file_etag(f_name):
                            response._code = 304
                        else:
                            response.load_from_file(f_name)
                            if not any([f[0].mutates_output
                                        for f in registration.filters
                                        if f[0].type == 'post']):
                                response.set_header(
                                    'ETag', '"%s"' %
                                    misc.generate_file_etag(f_name))
                            if registration.encoding:
                                response.charset = registration.encoding

            except exceptions.ResponseEnd as e:
                pass

            if registration is not None and response._code == 200:
                # do we have caching directive?
                if registration.max_age:
                    response.set_expiration(registration.max_age)
                # apply post-processing filters
                [filter[0].apply(context, item, registration, **filter[1])
                 for filter in registration.filters
                 if filter[0].type == 'post']

        except exceptions.InternalRedirect as e:
            lstPathInfo = e.args[0].split('?')
            raw_request['env']['PATH_INFO'] = lstPathInfo[0]
            if len(lstPathInfo) == 2:
                raw_request['env']['QUERY_STRING'] = lstPathInfo[1]
            else:
                raw_request['env']['QUERY_STRING'] = ''
            self.handle_request(rh, raw_request)

        except exceptions.DBReadOnly:
            context._teardown()
            # proxy request to master
            rep_mgr = _db.get_replication_manager()
            if rep_mgr.master is not None:
                master_addr = rep_mgr.master.req_address
                master_request = BaseRequest(rh.input_buffer)
                try:
                    master_response = master_request.get_response(master_addr)
                except:
                    pass
                else:
                    rh.write_buffer(master_response)
                    return

            e = exceptions.InternalServerError('Database is in read-only mode')
            e.output_traceback = False
            e.emit(context, item)

        except exceptions.PorcupineException as e:
            e.emit(context, item)

        except:
            e = exceptions.InternalServerError()
            e.emit(context, item)

        context._teardown()
        settings['requestinterfaces'][request.interface](rh, response)
Esempio n. 5
0
 def rtc_wrapper(*args, **kwargs):
     if not context._is_txnal:
         raise exceptions.InternalServerError(
             "Not in a transactional context. Use @db.transactional().")
     return function(*args, **kwargs)