Exemplo n.º 1
0
 def _agent_located(self, result, request, location, remaining):
     if result is None:
         return None
     host, port, _is_remote = result
     url = http.compose(request.path, host=host, port=port,
                        scheme=request.scheme)
     raise http.MovedPermanently(location=url)
Exemplo n.º 2
0
    def post(self, url, data, expiration=None):
        scheme, host, port, path, query = http.parse(url)
        location = http.compose(path, query)

        key = (scheme, host, port)

        now = time.time()
        exp = expiration + now if expiration is not None else None

        if key in self._quarantined:
            return self._add_pending(key, path, data, exp)

        if key not in self._peers:
            d = self._add_pending(key, path, data, exp)
            self._connect(key)
            return d

        d = defer.Deferred()
        self._post(key, location, data, exp, now, d)
        return d
Exemplo n.º 3
0
def parse(connection_string):
    """Parse a tunnel connection string and build a recipient from it."""
    scheme, host, port, location, _query = http.parse(str(connection_string))
    route = http.compose(host=host, port=port, scheme=scheme)
    key = location.lstrip("/")
    return recipient.Recipient(key, route, CHANNEL_TYPE)
Exemplo n.º 4
0
 def make_model_address(self, location):
     host, port = location[0]
     path = "/" + http.tuple2path(location[1:])
     return http.compose(host=host, port=port, path=path)
Exemplo n.º 5
0
 def make_model_address(self, location):
     host, port = location[0]
     path = "/" + http.tuple2path(location[1:])
     return http.compose(host=host, port=port, path=path,
                         query=http.compose_qs(self.arguments),
                         scheme=self.scheme)
Exemplo n.º 6
0
 def redirect(self, path, host=None, port=None, scheme=None):
     url = http.compose(path, host=host, port=port, scheme=scheme)
     raise http.MovedPermanently(location=url)
Exemplo n.º 7
0
    def onAllContentReceived(self):
        vout = self.channel.owner._version

        ctype = self.get_received_header("content-type")
        if ctype != "application/json":
            self._error(http.Status.UNSUPPORTED_MEDIA_TYPE,
                        "Message content type not supported, "
                        "only application/json is.")
            return

        agent_header = self.get_received_header("user-agent")
        if agent_header is None:
            self._error(http.Status.BAD_REQUEST, "No user agent specified.")
            return

        agent_name, agent_ver = http.parse_user_agent(agent_header)
        if ((agent_name != FEAT_IDENT)
            or len(agent_ver) != 1
            or not isinstance(agent_ver[0], int)):
            self._error(http.Status.BAD_REQUEST, "User agent not supported.")
            return

        vcli = agent_ver[0]

        if self.method is http.Methods.HEAD:
            vin = vcli if vcli is not None and vcli < vout else vout
            server_header = http.compose_user_agent(FEAT_IDENT, vin)
            self.set_header("server", server_header)
            self.set_length(0)
            self.finish()
            return

        if self.method is http.Methods.POST:
            if vcli is not None and vcli > vout:
                # not safe, better fail
                self._error(http.Status.UNSUPPORTED_MEDIA_TYPE,
                            "Message version not supported.")
                return

            host_header = self.get_received_header("host")
            if host_header is None:
                self._error(http.Status.BAD_REQUEST,
                            "Message without host header.")
                return

            scheme = self.channel.owner._scheme
            host, port = http.parse_host(host_header, scheme)
            uri = http.compose(self.uri, host=host, port=port, scheme=scheme)

            vin = vcli if vcli is not None else vout
            unserializer = json.Unserializer(registry=self._registry,
                                             source_ver=vin, target_ver=vout)
            body = "".join(self._buffer)
            try:
                data = unserializer.convert(body)
            except Exception as e:
                msg = "Error while unserializing tunnel message"
                error.handle_exception(self, e, msg)
                self._error(http.Status.BAD_REQUEST,
                            "Invalid message, unserialization failed.")
                return

            self.channel.owner._dispatch(uri, data)

            self.set_response_code(http.Status.OK)
            server_header = http.compose_user_agent(FEAT_IDENT, vin)
            self.set_header("server", server_header)
            self.set_length(0)
            self.finish()
            return

        self.set_response_code(http.Status.NOT_ALLOWED)
        self.set_header("content-type", "plain/text")
        self.write("Method not allowed, only POST and HEAD.")
        self.finish()
Exemplo n.º 8
0
 def _key2url(self, key):
     scheme, host, port = key
     return http.compose(host=host, port=port, scheme=scheme)
Exemplo n.º 9
0
Arquivo: gateway.py Projeto: f3at/feat
 def base_url(self):
     if self._server:
         return http.compose(scheme=self._server.scheme,
                             path='/', port=self.port,
                             host = self._host)