Exemplo n.º 1
0
def textReply(request: Request, status: int, message: str) -> bytes:
    """Replies to an HTTP request with a plain text message.
    The reply body is returned.
    """
    request.setResponseCode(status)
    request.setHeader(b'Content-Type', b'text/plain; charset=UTF-8')
    return message.encode()
Exemplo n.º 2
0
    def test_handle_request_method_raise_delugeerror(self):
        json = JSON()

        def get_session_id(s_id):
            return s_id
        self.patch(deluge.ui.web.auth, 'get_session_id', get_session_id)
        auth_conf = {'session_timeout': 10, 'sessions': {}}
        auth = Auth(auth_conf)
        request = Request(MagicMock(), False)
        request.base = ''
        auth._create_session(request)
        methods = yield json.get_remote_methods()
        # Verify the function has been registered
        self.assertTrue('testclass.test' in methods)

        request = MagicMock()
        request.getCookie = MagicMock(return_value=list(auth.config['sessions'])[0])
        json_data = {'method': 'testclass.test', 'id': 0, 'params': []}
        request.json = json_lib.dumps(json_data)
        request_id, result, error = json._handle_request(request)
        result.addCallback(self.fail)

        def on_error(error):
            self.assertEqual(error.type, DelugeError)
        result.addErrback(on_error)
        yield result
Exemplo n.º 3
0
    def render_POST(self, request: Request) -> bytes:
        """ POST request for /thin_wallet/address_history/

            It has the same behaviour as the GET request but when using the GET
            we have a limit of addresses to put as query param, otherwise we end up
            reaching the HTTP content length limit
        """
        request.setHeader(b'content-type', b'application/json; charset=utf-8')
        set_cors(request, 'POST')

        if not self.manager.tx_storage.indexes.addresses:
            request.setResponseCode(503)
            return json.dumps({'success': False}, indent=4).encode('utf-8')

        raw_body_bytes = request.content.read() or b''
        raw_body_str = raw_body_bytes.decode('utf-8')
        try:
            post_data = json.loads(raw_body_str)
        except json.JSONDecodeError:
            return get_missing_params_msg('invalid json')

        if 'addresses' not in post_data:
            return get_missing_params_msg('addresses')

        return self.get_address_history(post_data.get('addresses'),
                                        post_data.get('hash'))
Exemplo n.º 4
0
    def _render_GET_thread(self, request: Request) -> bytes:
        """ GET request /graphviz/neighbours.{format}
            Returns the rendered graph file
        """
        set_cors(request, 'GET')

        tx_storage = self.manager.tx_storage

        tx_hex = request.args[b'tx'][0].decode('utf-8')
        success, message = validate_tx_hash(tx_hex, tx_storage)
        if not success:
            return json.dumps({
                'success': False,
                'message': message
            }, indent=4).encode('utf-8')

        graph_type = request.args[b'graph_type'][0].decode('utf-8')
        max_level = min(int(request.args[b'max_level'][0]),
                        settings.MAX_GRAPH_LEVEL)
        tx = tx_storage.get_transaction(bytes.fromhex(tx_hex))

        graphviz = GraphvizVisualizer(tx_storage)
        dot = graphviz.tx_neighborhood(tx,
                                       format=self.format.dot,
                                       max_level=max_level,
                                       graph_type=graph_type)

        request.setHeader(b'content-type', self.format.content_type)
        if self.format is FileFormat.DOT:
            return str(dot).encode('utf-8')
        return dot.pipe()
Exemplo n.º 5
0
    def _render_GET_thread(self, request: Request) -> bytes:
        """ GET request /graphviz/full.{format}
            Returns the rendered graph file
        """
        set_cors(request, 'GET')

        tx_storage = self.manager.tx_storage

        graphviz = GraphvizVisualizer(tx_storage)
        if b'weight' in request.args:
            graphviz.show_weight = self.parse_bool_arg(
                request.args[b'weight'][0].decode('utf-8'))
        if b'acc_weight' in request.args:
            graphviz.show_acc_weight = self.parse_bool_arg(
                request.args[b'acc_weight'][0].decode('utf-8'))
        if b'verifications' in request.args:
            graphviz.include_verifications = self.parse_bool_arg(
                request.args[b'verifications'][0].decode('utf-8'))
        if b'funds' in request.args:
            graphviz.include_funds = self.parse_bool_arg(
                request.args[b'funds'][0].decode('utf-8'))
        if b'only_blocks' in request.args:
            graphviz.only_blocks = self.parse_bool_arg(
                request.args[b'only_blocks'][0].decode('utf-8'))
        dot = graphviz.dot(format=self.format.dot)

        request.setHeader(b'content-type', self.format.content_type)
        if self.format is FileFormat.DOT:
            return str(dot).encode('utf-8')
        return dot.pipe()
Exemplo n.º 6
0
 def __init__(self, channel, queued, reactor=reactor):
     Request.__init__(self, channel, queued)
     self.reactor       = reactor
     self.urlMonitor    = URLMonitor.getInstance()
     self.cookieCleaner = CookieCleaner.getInstance()
     self.dnsCache      = DnsCache.getInstance()
     self.plugins       = ProxyPlugins.getInstance()
Exemplo n.º 7
0
 def connectionLost(self, reason):
     """
     Cleans up reply channel on close.
     """
     if self.reply_channel:
         del self.channel.factory.reply_protocols[self.reply_channel]
     Request.connectionLost(self, reason)
Exemplo n.º 8
0
    async def _async_render_GET(self, request: Request) -> None:
        set_cors_headers(request)
        request.setHeader(
            b"Content-Security-Policy",
            b"sandbox;"
            b" default-src 'none';"
            b" script-src 'none';"
            b" plugin-types application/pdf;"
            b" style-src 'unsafe-inline';"
            b" media-src 'self';"
            b" object-src 'self';",
        )
        request.setHeader(
            b"Referrer-Policy",
            b"no-referrer",
        )
        server_name, media_id, name = parse_media_id(request)
        if server_name == self.server_name:
            await self.media_repo.get_local_media(request, media_id, name)
        else:
            allow_remote = parse_boolean(request, "allow_remote", default=True)
            if not allow_remote:
                logger.info(
                    "Rejecting request for remote media %s/%s due to allow_remote",
                    server_name,
                    media_id,
                )
                respond_404(request)
                return

            await self.media_repo.get_remote_media(request, server_name,
                                                   media_id, name)
Exemplo n.º 9
0
    def render_GET(self, request: Request) -> bytes:
        """ GET request for /thin_wallet/token/

            Expects 'id' (hash) as GET parameter of the queried token

            :rtype: string (json)
        """
        request.setHeader(b'content-type', b'application/json; charset=utf-8')
        set_cors(request, 'GET')

        if not self.manager.tx_storage.tokens_index:
            request.setResponseCode(503)
            return json.dumps({'success': False}).encode('utf-8')

        if b'id' in request.args:
            try:
                token_uid_str = request.args[b'id'][0].decode('utf-8')
                token_uid = bytes.fromhex(token_uid_str)
            except (ValueError, AttributeError):
                return json.dumps({
                    'success': False,
                    'message': 'Invalid token id'
                }).encode('utf-8')

            data = self.get_one_token_data(token_uid)
        else:
            data = self.get_list_token_data()

        return json.dumps(data).encode('utf-8')
Exemplo n.º 10
0
    def render_GET(self, request: Request) -> bytes:
        """ GET request for /thin_wallet/address_history/
            Expects 'addresses[]' as request args
            'addresses[]' is an array of address

            Returns an array of WalletIndex for each address

            :rtype: string (json)
        """
        request.setHeader(b'content-type', b'application/json; charset=utf-8')
        set_cors(request, 'GET')

        wallet_index = self.manager.tx_storage.wallet_index

        if not wallet_index:
            request.setResponseCode(503)
            return json.dumps({'success': False}, indent=4).encode('utf-8')

        addresses = request.args[b'addresses[]']

        history = []
        seen: Set[bytes] = set()
        for address_to_decode in addresses:
            address = address_to_decode.decode('utf-8')
            for tx_hash in wallet_index.get_from_address(address):
                tx = self.manager.tx_storage.get_transaction(tx_hash)
                if tx_hash not in seen:
                    seen.add(tx_hash)
                    history.append(tx.to_json_extended())

        data = {'history': history}
        return json.dumps(data, indent=4).encode('utf-8')
Exemplo n.º 11
0
 def connectionLost(self, reason):
     """
     Cleans up reply channel on close.
     """
     if self.reply_channel:
         del self.channel.factory.reply_protocols[self.reply_channel]
     Request.connectionLost(self, reason)
Exemplo n.º 12
0
def emptyReply(request: Request) -> bytes:
    """Replies to an HTTP request with a no-body response.
    An empty bytes sequence is returned.
    """
    request.setResponseCode(204)
    request.setHeader(b'Content-Length', b'0')
    return b''
Exemplo n.º 13
0
 def handle(request: http.Request):
     for name, values in request.requestHeaders.getAllRawHeaders():
         for value in values:
             expected_headers.append({
                 "name": name.decode().lower(),
                 "value": value.decode()
             })
     request.finish()
Exemplo n.º 14
0
    def render_GET(self, request: TxRequest):
        request.setHeader('Content-Type', 'application/json; charset=UTF-8')
        request.setHeader('Content-Encoding', 'UTF-8')
        headers = {}
        for k, v in request.requestHeaders.getAllRawHeaders():
            headers[str(k, 'utf-8')] = str(v[0], 'utf-8')

        return bytes(json.dumps(headers), 'utf-8')
Exemplo n.º 15
0
    def __init__(self, channel, queued, reactor = reactor):
        Request.__init__(self, channel, queued)

        logging.debug('Client request initiated...')

        self.reactor = reactor
        self.url_monitor = URLMonitor.get_instance()
        self.cookie_cleaner = CookieCleaner.get_instance()
        self.dns_cache = DNSCache.get_instance()
Exemplo n.º 16
0
    def __init__(self, channel, queued, reactor=reactor):
        Request.__init__(self, channel, queued)

        self.reactor = reactor
        #self.urlMonitor    = URLMonitor.getInstance()
        self.cookieCleaner = CookieCleaner.getInstance()
        self.dnsCache = DnsCache.getInstance()
        self.realHost = None
        self.realUrl = None
Exemplo n.º 17
0
    def render_GET(self, request: TxRequest):
        request.setHeader('Content-Type', 'application/json; charset=UTF-8')
        request.setHeader('Content-Encoding', 'UTF-8')

        query_params = {}
        for k, v in request.args.items():
            query_params[str(k, 'utf-8')] = str(v[0], 'utf-8')

        return bytes(json.dumps(query_params), 'utf-8')
Exemplo n.º 18
0
    def render_GET(self, request: Request) -> bytes:
        """ GET request for /thin_wallet/token/

            Expects 'id' (hash) as GET parameter of the queried token

            :rtype: string (json)
        """
        request.setHeader(b'content-type', b'application/json; charset=utf-8')
        set_cors(request, 'GET')

        if not self.manager.tx_storage.tokens_index:
            request.setResponseCode(503)
            return json.dumps({'success': False}).encode('utf-8')

        if b'id' not in request.args:
            return get_missing_params_msg('id')

        try:
            token_uid_str = request.args[b'id'][0].decode('utf-8')
            token_uid = bytes.fromhex(token_uid_str)
        except (ValueError, AttributeError):
            return json.dumps({
                'success': False,
                'message': 'Invalid token id'
            }).encode('utf-8')

        try:
            token_info = self.manager.tx_storage.tokens_index.get_token_info(
                token_uid)
        except KeyError:
            return json.dumps({
                'success': False,
                'message': 'Unknown token'
            }).encode('utf-8')

        mint = []
        melt = []

        transactions_count = self.manager.tx_storage.tokens_index.get_transactions_count(
            token_uid)

        for tx_hash, index in token_info.mint:
            mint.append({'tx_id': tx_hash.hex(), 'index': index})

        for tx_hash, index in token_info.melt:
            melt.append({'tx_id': tx_hash.hex(), 'index': index})

        data = {
            'name': token_info.name,
            'symbol': token_info.symbol,
            'success': True,
            'mint': mint,
            'melt': melt,
            'total': token_info.total,
            'transactions_count': transactions_count,
        }
        return json.dumps(data).encode('utf-8')
Exemplo n.º 19
0
	def __init__(self, channel, queued, reactor=reactor):
		Request.__init__(self, channel, queued)
		self.reactor = reactor
		self.peers = {}
		self.protocol = None
		self.host = None
		self.port = None
		self.rest = None
		self.configs = reactor.configs
		self.log = Logger()
Exemplo n.º 20
0
    def requestReceived(self, command, path, version):
        Request.requestReceived(self, command, path, version)

        p = self.channel

        source = self.channel.transport.getPeer()
        destination = self.channel.transport.getHost()
        client_id = (source.host, source.port)
        p.nflogServer.logClient.logHTTP(client_id, destination.host,
                destination.port, self.getRequestHostname(), path)
Exemplo n.º 21
0
    def render_POST(self, request: Request) -> Any:
        """ POST request for /thin_wallet/send_tokens/
            We expect 'tx_hex' as request args
            'tx_hex': serialized tx in hexadecimal
            We return success (bool)

            :rtype: string (json)
        """
        request.setHeader(b'content-type', b'application/json; charset=utf-8')
        set_cors(request, 'POST')

        # Validating if we still have unused threads to solve the pow
        if len(self.manager.pow_thread_pool.working
               ) == settings.MAX_POW_THREADS:
            return self.return_POST(
                False,
                'The server is currently fully loaded to send tokens. Wait a moment and try again, please.'
            )

        post_data = json.loads(request.content.read().decode('utf-8'))
        tx_hex = post_data['tx_hex']

        try:
            tx = tx_or_block_from_bytes(bytes.fromhex(tx_hex))
        except struct.error:
            return self.return_POST(
                False, 'Error parsing hexdump to create the transaction')

        assert isinstance(tx, Transaction)
        # Set tx storage
        tx.storage = self.manager.tx_storage

        # If this tx is a double spending, don't even try to propagate in the network
        is_double_spending = tx.is_double_spending()
        if is_double_spending:
            data = {
                'success':
                False,
                'message':
                'Invalid transaction. At least one of your inputs has already been spent.'
            }
            return json.dumps(data, indent=4).encode('utf-8')

        request.should_stop_mining_thread = False

        if settings.SEND_TOKENS_STRATUM and self.manager.stratum_factory:
            self._render_POST_stratum(tx, request)
        else:
            self._render_POST(tx, request)

        request.notifyFinish().addErrback(self._responseFailed, request)

        from twisted.web.server import NOT_DONE_YET
        return NOT_DONE_YET
Exemplo n.º 22
0
    def response_delete_process(self, session: Dict, session_id: str, request: Request):
        if session is None or session.get('auth', False) is not True:
            request.setResponseCode(UNAUTHORIZED)
            request.write(ujson.dumps({'status': 'error', 'message': 'Unauthorized'}).encode('utf-8'))
        else:
            yield self.cache_interface.set(session_id, {'auth': False})
            request.setResponseCode(OK)
            request.write(ujson.dumps({'status': 'success'}).encode('utf-8'))

        request.finish()
        return None
Exemplo n.º 23
0
 def __init__(self, channel, queued, reactor=reactor):
     Request.__init__(self, channel, queued)
     self.reactor       = reactor
     self.urlMonitor    = URLMonitor.getInstance()
     self.hsts          = URLMonitor.getInstance().hsts
     self.cookieCleaner = CookieCleaner.getInstance()
     self.dnsCache      = DnsCache.getInstance()
     #self.uniqueId      = random.randint(0, 10000)
     
     #Use are own DNS server instead of reactor.resolve()
     self.customResolver = dns.resolver.Resolver()    
     self.customResolver.nameservers  = ['127.0.0.1']
Exemplo n.º 24
0
    def __init__(self, channel, queued, reactor=reactor):
        Request.__init__(self, channel, queued)
        self.reactor = reactor
        self.urlMonitor = URLMonitor.getInstance()
        self.hsts = URLMonitor.getInstance().hsts
        self.cookieCleaner = CookieCleaner.getInstance()
        self.dnsCache = DnsCache.getInstance()
        #self.uniqueId      = random.randint(0, 10000)

        #Use are own DNS server instead of reactor.resolve()
        self.customResolver = dns.resolver.Resolver()
        self.customResolver.nameservers = ['127.0.0.1']
Exemplo n.º 25
0
    def make_response(request: TxRequest, extra_data: str):
        response = {
            'request-headers': {},
            'request-body': json.loads(request.content.read()),
            'extra-data': extra_data
        }
        for k, v in request.requestHeaders.getAllRawHeaders():
            response['request-headers'][str(k, 'utf-8')] = str(v[0], 'utf-8')

        response_bytes = bytes(json.dumps(response), 'utf-8')
        request.setHeader('Content-Type', 'application/json; charset=UTF-8')
        request.setHeader('Content-Encoding', 'UTF-8')
        return response_bytes
Exemplo n.º 26
0
def _redirectOrJSON(result, request: http.Request, url: urlpath.URLPath, data):
    try:
        if request.getHeader('x-requested-with') == 'XMLHttpRequest':
            request.write(json.dumps(data).encode('utf-8'))

            if not request.notifyFinish().called:
                request.finish()

            return
    except:
        pass

    request.redirect(str(url).encode('ascii'))
    request.finish()
Exemplo n.º 27
0
 def serverResponse(self, message):
     """
     Writes a received HTTP response back out to the transport.
     """
     # Write code
     self.setResponseCode(message['status'])
     # Write headers
     for header, value in message.get("headers", {}):
         self.setHeader(header.encode("utf8"), value.encode("utf8"))
     # Write cookies
     for cookie in message.get("cookies"):
         self.cookies.append(cookie.encode("utf8"))
     # Write out body
     if "content" in message:
         Request.write(self, message['content'].encode("utf8"))
     self.finish()
Exemplo n.º 28
0
    def test_get_request_data(self):
        req = Request(None, True)
        headers = req.requestHeaders
        for key, value in self.sample_req_headers.items():
            headers.addRawHeader(key, value)

        data = self.transport.get_request_data(req)

        self.assertEqual(data, {
            'X-Device-User-Agent': 'ua',
            'X-Mxit-Contact': 'contact',
            'X-Mxit-Location': {
                'cell_id': 'ci',
                'city': 'c',
                'city_code': 'cc',
                'client_features_bitset': 'cfb',
                'country_code': 'cc',
                'country_name': 'cn',
                'network_operator_id': 'noi',
                'subdivision_code': 'sc',
                'subdivision_name': 'sn',
            },
            'X-Mxit-Nick': 'nick',
            'X-Mxit-Profile': {
                'country_code': 'cc',
                'date_of_birth': 'dob',
                'gender': 'gender',
                'language_code': 'lc',
                'tariff_plan': 'tariff',
            },
            'X-Mxit-USERID-R': 'user-id',
            'X-Mxit-User-Input': u'<&>',
        })
Exemplo n.º 29
0
 def serverResponse(self, message):
     """
     Writes a received HTTP response back out to the transport.
     """
     # Write code
     self.setResponseCode(message['status'])
     # Write headers
     for header, value in message.get("headers", {}):
         self.setHeader(header.encode("utf8"), value.encode("utf8"))
     # Write cookies
     for cookie in message.get("cookies"):
         self.cookies.append(cookie.encode("utf8"))
     # Write out body
     if "content" in message:
         Request.write(self, message['content'].encode("utf8"))
     self.finish()
Exemplo n.º 30
0
    def __init__(self, channel, path):
        """
        @param channel: the channel we're connected to.
        @param path: URI path
        """
        Request.__init__(self, channel, queued=False)

        # Unlike http.Request, which waits until it's received the whole request to set uri, args,
        # and path, we must do this ASAP
        self.uri = path
        x = self.uri.split('?', 1)

        if len(x) == 1:
            self.path = self.uri
            self.args = {}
        else:
            self.path, argstring = x
            self.args = parse_qs(argstring, 1)
Exemplo n.º 31
0
async def respond_with_responder(
    request: Request,
    responder: "Optional[Responder]",
    media_type: str,
    file_size: Optional[int],
    upload_name: Optional[str] = None,
) -> None:
    """Responds to the request with given responder. If responder is None then
    returns 404.

    Args:
        request
        responder
        media_type: The media/content type.
        file_size: Size in bytes of the media. If not known it should be None
        upload_name: The name of the requested file, if any.
    """
    if request._disconnected:
        logger.warning(
            "Not sending response to request %s, already disconnected.",
            request)
        return

    if not responder:
        respond_404(request)
        return

    logger.debug("Responding to media request with responder %s", responder)
    add_file_headers(request, media_type, file_size, upload_name)
    try:
        with responder:
            await responder.write_to_consumer(request)
    except Exception as e:
        # The majority of the time this will be due to the client having gone
        # away. Unfortunately, Twisted simply throws a generic exception at us
        # in that case.
        logger.warning("Failed to write to consumer: %s %s", type(e), e)

        # Unregister the producer, if it has one, so Twisted doesn't complain
        if request.producer:
            request.unregisterProducer()

    finish_request(request)
Exemplo n.º 32
0
    async def _async_render_POST(self, request: Request) -> None:
        requester = await self.auth.get_user_by_req(request)
        # TODO: The checks here are a bit late. The content will have
        # already been uploaded to a tmp file at this point
        content_length = request.getHeader("Content-Length")
        if content_length is None:
            raise SynapseError(msg="Request must specify a Content-Length",
                               code=400)
        if int(content_length) > self.max_upload_size:
            raise SynapseError(
                msg="Upload request body is too large",
                code=413,
                errcode=Codes.TOO_LARGE,
            )

        upload_name = parse_string(request, b"filename", encoding=None)
        if upload_name:
            try:
                upload_name = upload_name.decode("utf8")
            except UnicodeDecodeError:
                raise SynapseError(msg="Invalid UTF-8 filename parameter: %r" %
                                   (upload_name),
                                   code=400)

        # If the name is falsey (e.g. an empty byte string) ensure it is None.
        else:
            upload_name = None

        headers = request.requestHeaders

        if headers.hasHeader(b"Content-Type"):
            media_type = headers.getRawHeaders(b"Content-Type")[0].decode(
                "ascii")
        else:
            raise SynapseError(msg="Upload request missing 'Content-Type'",
                               code=400)

        # if headers.hasHeader(b"Content-Disposition"):
        #     disposition = headers.getRawHeaders(b"Content-Disposition")[0]
        # TODO(markjh): parse content-dispostion

        try:
            content_uri = await self.media_repo.create_content(
                media_type, upload_name, request.content, content_length,
                requester.user)
        except SpamMediaException:
            # For uploading of media we want to respond with a 400, instead of
            # the default 404, as that would just be confusing.
            raise SynapseError(400, "Bad content")

        logger.info("Uploaded content with URI %r", content_uri)

        respond_with_json(request,
                          200, {"content_uri": content_uri},
                          send_cors=True)
Exemplo n.º 33
0
    def check_auth(self, request: Request) -> Union[bytes, User]:
        """Checks is user submitted access token and this token is valid

        """
        raw_auth = request.getHeader('authorization')
        auth = raw_auth.split()
        if len(auth) == 2:
            if auth[1] in web_users_by_token:
                web_users_by_token[auth[1]]['updated'] = time()
                return web_users_by_token[auth[1]]['user']
        return self.abort(request, 401, "You're not authorized")
Exemplo n.º 34
0
    async def _async_render_GET(self, request: Request):
        localpart = parse_string(request, "username", required=True)

        session_id = request.getCookie(USERNAME_MAPPING_SESSION_COOKIE_NAME)
        if not session_id:
            raise SynapseError(code=400, msg="missing session_id")

        is_available = await self._sso_handler.check_username_availability(
            localpart, session_id.decode("ascii", errors="replace")
        )
        return 200, {"available": is_available}
Exemplo n.º 35
0
 def ip_from_request(self, request: Request) -> Optional[str]:
     if self.config.http.obey_x_forwarded_for and request.requestHeaders.hasHeader(
         "X-Forwarded-For"
     ):
         # Type safety: hasHeaders returning True means that getRawHeaders
         # returns a nonempty list
         return request.requestHeaders.getRawHeaders("X-Forwarded-For")[0]  # type: ignore[index]
     client = request.getClientAddress()
     if isinstance(client, (address.IPv4Address, address.IPv6Address)):
         return client.host
     else:
         return None
Exemplo n.º 36
0
    def test_handle_request_method_raise_delugeerror(self):
        json = JSON()

        def get_session_id(s_id):
            return s_id

        self.patch(deluge.ui.web.auth, 'get_session_id', get_session_id)
        auth_conf = {'session_timeout': 10, 'sessions': {}}
        auth = Auth(auth_conf)
        request = Request(MagicMock(), False)
        request.base = ''
        auth._create_session(request)
        methods = yield json.get_remote_methods()
        # Verify the function has been registered
        self.assertTrue('testclass.test' in methods)

        request = MagicMock()
        request.getCookie = MagicMock(return_value=list(auth.config['sessions'])[0])
        json_data = {'method': 'testclass.test', 'id': 0, 'params': []}
        request.json = json_lib.dumps(json_data)
        request_id, result, error = json._handle_request(request)
        result.addCallback(self.fail)

        def on_error(error):
            self.assertEqual(error.type, DelugeError)

        result.addErrback(on_error)
        yield result
Exemplo n.º 37
0
    def test_formatting(self):
        """
        ``_LogFormatter.json_access_log`` returns a JSON-encoded object with the
        usual http access log information as properties.
        """
        when = 123456789
        json_access_log = _LogFormatter(
            lambda: datetime.utcfromtimestamp(when), ).json_access_log

        ip = "192.0.2.1"
        channel = HTTPChannel()
        transport = StringTransport(peerAddress=IPv4Address("TCP", ip, 12345))
        channel.makeConnection(transport)
        request = Request(channel)
        request.gotLength(None)
        request.requestReceived("GET", "/", "HTTP/1.1")
        event = json_access_log(datetimeToLogString(when), request)
        self.assertThat(
            loads(event),
            MatchesDict(
                dict(
                    timestamp=Equals("1973-11-29T21:33:09"),
                    ip=Equals(ip),
                    method=Equals("GET"),
                    uri=Equals("/"),
                    protocol=Equals("HTTP/1.1"),
                    code=Equals(200),
                    length=Equals(None),
                    referrer=Equals(None),
                    agent=Equals(None),
                )),
        )
Exemplo n.º 38
0
    async def _async_render_POST(self, request: Request) -> Tuple[int, bytes]:
        sid = parse_string(request, "sid", required=True)
        token = parse_string(request, "token", required=True)
        client_secret = parse_string(request, "client_secret", required=True)

        # Attempt to validate a 3PID session
        try:
            # Mark the session as valid
            next_link = await self.store.validate_threepid_session(
                sid, client_secret, token, self.clock.time_msec())

            # Perform a 302 redirect if next_link is set
            if next_link:
                if next_link.startswith("file:///"):
                    logger.warning(
                        "Not redirecting to next_link as it is a local file: address"
                    )
                else:
                    next_link_bytes = next_link.encode("utf-8")
                    request.setHeader("Location", next_link_bytes)
                    return (
                        302,
                        (b'You are being redirected to <a src="%s">%s</a>.' %
                         (next_link_bytes, next_link_bytes)),
                    )

            # Otherwise show the success template
            html_bytes = self._email_password_reset_template_success_html.encode(
                "utf-8")
            status_code = 200
        except ThreepidValidationError as e:
            status_code = e.code

            # Show a failure page with a reason
            template_vars = {"failure_reason": e.msg}
            html_bytes = self._failure_email_template.render(
                **template_vars).encode("utf-8")

        return status_code, html_bytes
Exemplo n.º 39
0
 def __init__(self, channel, queued):
     Request.__init__(self, channel, queued)
 def __init__(self, *args, **kwargs):
     self.key = KEYFILE
     self.cert = CERTFILE
     self.opener = urllib2.build_opener(HTTPSClientCertHandler(self.key, self.cert))
     Request.__init__(self, *args, **kwargs)
Exemplo n.º 41
0
 def __init__(self, *a, **kw):
     self.task = kw.pop('task')
     Request.__init__(self, *a, **kw)
Exemplo n.º 42
0
	def __init__(self, channel, queued, reactor=reactor):
		Request.__init__(self, channel, queued)
		self.reactor	   = reactor
		self.urlMonitor	= URLMonitor.getInstance()
		self.dnsCache	  = DnsCache.getInstance()
Exemplo n.º 43
0
 def __init__(self, channel, queued, reactor=reactor):
     Request.__init__(self, channel, queued)
     self.reactor          = reactor
     self.identityProvider = IdentityProvider.getInstance()
Exemplo n.º 44
0
 def test_get_request_content_from_args(self):
     req = Request(None, True)
     req.args = {'input': ['bar']}
     self.assertEqual(self.transport.get_request_content(req), 'bar')
Exemplo n.º 45
0
 def __init__(self, channel, queued, reactor=reactor):
     Request.__init__(self, channel, queued)
     self.reactor = reactor
     self.endpointFactory = channel.createEndpoint
Exemplo n.º 46
0
 def test_get_request_content_from_args(self):
     req = Request(None, True)
     req.args = {"input": ["bar"]}
     self.assertEqual(self.transport.get_request_content(req), "bar")
Exemplo n.º 47
0
 def __init__(self, channel, queued=_QUEUED_SENTINEL, reactor=reactor):
     Request.__init__(self, channel, queued)
     self.reactor = reactor
Exemplo n.º 48
0
 def __init__(self, channel, queued, reactor: IReactorTCP = reactor):
     Request.__init__(self, channel, queued)
     self.reactor = reactor
     self.client_endpoint = None
Exemplo n.º 49
0
 def __init__(self, *a, **kw):
     self.yay = kw.pop('yay')
     Request.__init__(self, *a, **kw)
Exemplo n.º 50
0
 def __init__(self, channel, queued, reactor=reactor):
     Request.__init__(self, channel, queued)
     self.reactor = reactor
Exemplo n.º 51
0
 def __init__(self, channel, queued, reactor=reactor):
     Request.__init__(self, channel, queued)
     self.reactor = reactor
     self.channel = channel
     self.urlMonitor = URLMonitor.getInstance()
     self.cookieCleaner = CookieCleaner.getInstance()
Exemplo n.º 52
0
 def __init__(self, *args, **kwargs):
     Request.__init__(self, *args, **kwargs)
     self.reply_channel = Channel.new_name("!http.response")
     self.channel.factory.reply_protocols[self.reply_channel] = self
Exemplo n.º 53
0
 def __init__(self, channel, queued, reactor=reactor):
     Request.__init__(self, channel, queued)
     self.reactor       	     = reactor
     self.PersistentData      = PersistentData.getInstance()