예제 #1
0
    def __init__(self, environ):
        """Parses the given WSGI environ to construct the request."""
        self.method = environ["REQUEST_METHOD"]
        self.path = urllib.quote(environ.get("SCRIPT_NAME", ""))
        self.path += urllib.quote(environ.get("PATH_INFO", ""))
        self.uri = self.path
        self.arguments = {}
        self.query = environ.get("QUERY_STRING", "")
        if self.query:
            self.uri += "?" + self.query
            arguments = parse_qs_bytes(native_str(self.query))
            for name, values in arguments.iteritems():
                values = [v for v in values if v]
                if values:
                    self.arguments[name] = values
        self.version = "HTTP/1.1"
        self.headers = httputil.HTTPHeaders()
        if environ.get("CONTENT_TYPE"):
            self.headers["Content-Type"] = environ["CONTENT_TYPE"]
        if environ.get("CONTENT_LENGTH"):
            self.headers["Content-Length"] = environ["CONTENT_LENGTH"]
        for key in environ:
            if key.startswith("HTTP_"):
                self.headers[key[5:].replace("_", "-")] = environ[key]
        if self.headers.get("Content-Length"):
            self.body = environ["wsgi.input"].read(
                int(self.headers["Content-Length"]))
        else:
            self.body = ""
        self.protocol = environ["wsgi.url_scheme"]
        self.remote_ip = environ.get("REMOTE_ADDR", "")
        if environ.get("HTTP_HOST"):
            self.host = environ["HTTP_HOST"]
        else:
            self.host = environ["SERVER_NAME"]

        # Parse request body
        self.files = {}
        content_type = self.headers.get("Content-Type", "")
        if content_type.startswith("application/x-www-form-urlencoded"):
            for name, values in parse_qs_bytes(native_str(
                    self.body)).iteritems():
                self.arguments.setdefault(name, []).extend(values)
        elif content_type.startswith("multipart/form-data"):
            if 'boundary=' in content_type:
                boundary = content_type.split('boundary=', 1)[1]
                if boundary:
                    httputil.parse_multipart_form_data(utf8(boundary),
                                                       self.body,
                                                       self.arguments,
                                                       self.files)
            else:
                logging.warning("Invalid multipart/form-data")

        self._start_time = monotime()
        self._finish_time = None
예제 #2
0
def parse_request(data):
    try:
        req = ast.literal_eval(data)
        path, sep, query = req['uri'].partition('?')
        get_arguments = parse_qs_bytes(query, keep_blank_values=True)
        post_arguments = parse_qs_bytes(req['body'], keep_blank_values=True)
        host = req['headers']['Host']
        headers = req['headers']
        return headers, host, path, get_arguments, post_arguments
    except _BadRequestException as e:
        gen_log.info("Malformed HTTP request:%s", e)
        return
예제 #3
0
파일: wsgi.py 프로젝트: jamescasbon/tornado
    def __init__(self, environ):
        """Parses the given WSGI environ to construct the request."""
        self.method = environ["REQUEST_METHOD"]
        self.path = urllib.quote(from_wsgi_str(environ.get("SCRIPT_NAME", "")))
        self.path += urllib.quote(from_wsgi_str(environ.get("PATH_INFO", "")))
        self.uri = self.path
        self.arguments = {}
        self.query = environ.get("QUERY_STRING", "")
        if self.query:
            self.uri += "?" + self.query
            arguments = parse_qs_bytes(native_str(self.query))
            for name, values in arguments.iteritems():
                values = [v for v in values if v]
                if values:
                    self.arguments[name] = values
        self.version = "HTTP/1.1"
        self.headers = httputil.HTTPHeaders()
        if environ.get("CONTENT_TYPE"):
            self.headers["Content-Type"] = environ["CONTENT_TYPE"]
        if environ.get("CONTENT_LENGTH"):
            self.headers["Content-Length"] = environ["CONTENT_LENGTH"]
        for key in environ:
            if key.startswith("HTTP_"):
                self.headers[key[5:].replace("_", "-")] = environ[key]
        if self.headers.get("Content-Length"):
            self.body = environ["wsgi.input"].read(
                int(self.headers["Content-Length"]))
        else:
            self.body = ""
        self.protocol = environ["wsgi.url_scheme"]
        self.remote_ip = environ.get("REMOTE_ADDR", "")
        if environ.get("HTTP_HOST"):
            self.host = environ["HTTP_HOST"]
        else:
            self.host = environ["SERVER_NAME"]

        # Parse request body
        self.files = {}
        content_type = self.headers.get("Content-Type", "")
        if content_type.startswith("application/x-www-form-urlencoded"):
            for name, values in parse_qs_bytes(native_str(self.body)).iteritems():
                self.arguments.setdefault(name, []).extend(values)
        elif content_type.startswith("multipart/form-data"):
            if 'boundary=' in content_type:
                boundary = content_type.split('boundary=', 1)[1]
                if boundary:
                    httputil.parse_multipart_form_data(
                        utf8(boundary), self.body, self.arguments, self.files)
            else:
                logging.warning("Invalid multipart/form-data")

        self._start_time = time.time()
        self._finish_time = None
예제 #4
0
def parse_request(data):
    try:
        req = ast.literal_eval(data)
        path, sep, query = req['uri'].partition('?')
        get_arguments = parse_qs_bytes(query, keep_blank_values=True)
        post_arguments = parse_qs_bytes(req['body'], keep_blank_values=True)
        host = req['headers']['Host']
        headers = req['headers']
        return headers, host, path, get_arguments, post_arguments
    except _BadRequestException as e:
        gen_log.info("Malformed HTTP request:%s", e)
        return
예제 #5
0
    async def post(self):
        data = parse_qs_bytes(native_str(self.request.body),
                              keep_blank_values=True)
        appId, uid, channel_name, nick_name = list(
            map(lambda x: x[0].decode("utf-8"), [
                data['appid'], data['uid'], data['channel_name'],
                data['nick_name']
            ]))
        config_filename = f"configRec_{nick_name}.json"
        sdk_path = "./Agora_Recording_SDK_for_Linux_FULL/samples/cpp/"
        with open(os.path.join(sdk_path, config_filename), "w") as cfg:
            cfg.write('{"Recording_Dir" : "./' + nick_name + '"}')

        success = subprocess.run([
            sdk_path + 'recorder_local', "--appId", appId, "--uid", "0",
            "--channel", channel_name, "--appliteDir",
            "Agora_Recording_SDK_for_Linux_FULL/bin/", '--idle', '4',
            '--audioProfile', '2', '--cfgFilePath',
            os.path.join(sdk_path, config_filename), "--isMixingEnabled", "1"
        ])
        outfile = glob.glob(
            str(os.path.join(os.getcwd(), nick_name) + "/*.aac"))[0]
        bucket = self.db()
        blob = bucket.blob(nick_name + ".aac")
        blob.upload_from_filename(outfile)
        self.write(
            json.dumps({
                'status_code': 200,
                'message': 'everything seems to be fine',
                'url': blob.public_url
            }))
예제 #6
0
    def __init__(self, method, uri, version="HTTP/1.0", headers=None,
                 body=None, remote_ip=None, protocol=None, host=None,
                 files=None, connection=None):
        self.method = method
        self.uri = uri
        self.version = version
        self.headers = headers or httputil.HTTPHeaders()
        self.body = body or ""
        self.trace = None

        # set remote IP and protocol
        self.remote_ip = remote_ip
        if protocol:
            self.protocol = protocol
        elif connection and isinstance(connection.stream,
                                       iostream.SSLIOStream):
            self.protocol = "https"
        else:
            self.protocol = "http"

        # xheaders can override the defaults
        if connection and connection.xheaders:
            # Squid uses X-Forwarded-For, others use X-Real-Ip
            ip = self.headers.get("X-Forwarded-For", self.remote_ip)
            ip = ip.split(',')[-1].strip()
            ip = self.headers.get(
                "X-Real-Ip", ip)
            if netutil.is_valid_ip(ip):
                self.remote_ip = ip
            # AWS uses X-Forwarded-Proto
            proto = self.headers.get(
                "X-Scheme", self.headers.get("X-Forwarded-Proto", self.protocol))
            if proto in ("http", "https"):
                self.protocol = proto
            # Zipkin users 
            if options.server_trace:
                parent_span_id = self.headers.get("X-B3-ParentSpanId", None)
                trace_id = self.headers.get("X-B3-TraceId", None)
                span_id = self.headers.get("X-B3-SpanId", None)
                name =  method
                endpoint = Endpoint(ipv4=socket.gethostbyname(socket.gethostname()), port=port, service_name=service_name)
                
                self.trace = Trace(name=name, trace_id=trace_id, span_id=span_id, parent_span_id=parent_span_id)
                self.trace.set_endpoint(endpoint)

        self.host = host or self.headers.get("Host") or "127.0.0.1"
        self.files = files or {}
        self.connection = connection
        self._start_time = time.time()
        self._finish_time = None

        self.path, sep, self.query = uri.partition('?')
        self.arguments = parse_qs_bytes(self.query, keep_blank_values=True)
        self.query_arguments = copy.deepcopy(self.arguments)
        self.body_arguments = {}
        
        if options.server_trace:
            self.trace.record(Annotation.string('Url', uri))
            self.trace.record(Annotation.string('Header', self.headers))
            self.trace.record(Annotation.server_recv())
예제 #7
0
def parse_body_arguments(content_type, body, arguments, files, headers=None):
    if headers and 'Content-Encoding' in headers:
        print("Unsupported Content-Encoding: %s" % headers['Content-Encoding'])
        return
    if content_type.startswith("application/x-www-form-urlencoded"):
        try:
            uri_arguments = parse_qs_bytes(native_str(body), keep_blank_values=True)
        except Exception as e:
            print('Invalid x-www-form-urlencoded body: %s' % e)
            uri_arguments = {}
        for name, values in uri_arguments.items():
            if values:
                arguments.setdefault(name, []).extend(values)
    elif content_type.startswith("multipart/form-data"):
        try:
            fields = content_type.split(";")
            for field in fields:
                k, sep, v = field.strip().partition("=")
                if k == "boundary" and v:
                    parse_multipart_form_data(utf8(v), body, arguments, files)
                    break
            else:
                raise ValueError("multipart boundary not found")
        except Exception as e:
            print("Invalid multipart/form-data: %s" % e)
예제 #8
0
 def _on_request_body(self, data):
     try:
         self._request.body = data
         content_type = self._request.headers.get("Content-Type", "")
         if self._request.method in ("POST", "PUT"):
             if content_type.startswith("application/x-www-form-urlencoded"):
                 arguments = parse_qs_bytes(native_str(self._request.body), keep_blank_values = True)
                 for name, values in arguments.iteritems():
                     values = [v for v in values if v is not None]
                     if values:
                         self._request.arguments.setdefault(name, []).extend(
                             values)
             elif content_type.startswith("multipart/form-data"):
                 fields = content_type.split(";")
                 for field in fields:
                     k, sep, v = field.strip().partition("=")
                     if k == "boundary" and v:
                         httputil.parse_multipart_form_data(
                             utf8(v), data,
                             self._request.arguments,
                             self._request.files)
                         break
                 else:
                     logging.warning("Invalid multipart/form-data")
         self.request_callback(self._request)
     except BadRequestException, e:
         logging.info("Malformed HTTP request from %s: %s",
                      self.address[0], e)
         logging.info('Request:\n%s', data)
         self.no_keep_alive = True
         if not self.stream.closed():
             self.stream.write("HTTP/1.1 400 Bad request\r\n\r\n", self._finish_request)
         return
예제 #9
0
    def _on_access_token(self, redirect_uri, client_id, client_secret, future,
                         fields, response):
        if response.error:
            future.set_exception(
                AuthError("Github auth error: %s" % str(response)))
            return

        args = parse_qs_bytes(native_str(response.body))
        if "error" in args:
            future.set_exception(
                AuthError("Github auth error: %s" % args["error"][-1]))
            return

        access_token = args["access_token"][-1]
        if isinstance(access_token, bytes):
            access_token = access_token.decode()
        session = {
            "access_token": access_token,
        }

        self.github_request(path="/user",
                            callback=functools.partial(self._on_get_user_info,
                                                       future, session,
                                                       fields),
                            access_token=access_token)
예제 #10
0
    def __init__(self, method=None, uri=None, version="HTTP/1.0", headers=None,
                 body=None, host=None, files=None, connection=None,
                 start_line=None):
        if start_line is not None:
            method, uri, version = start_line
        self.method = method
        self.uri = uri
        self.version = version
        self.headers = headers or HTTPHeaders()
        self.body = body or ""

        # set remote IP and protocol
        context = getattr(connection, 'context', None)
        self.remote_ip = getattr(context, 'remote_ip')
        self.protocol = getattr(context, 'protocol', "http")

        self.host = host or self.headers.get("Host") or "127.0.0.1"
        self.files = files or {}
        self.connection = connection
        self._start_time = time.time()
        self._finish_time = None

        self.path, sep, self.query = uri.partition('?')
        self.arguments = parse_qs_bytes(self.query, keep_blank_values=True)
        self.query_arguments = copy.deepcopy(self.arguments)
        self.body_arguments = {}
예제 #11
0
def parse_body_arguments(content_type, body, arguments, files, headers=None):
    """Parses a form request body.

    Supports ``application/x-www-form-urlencoded`` and
    ``multipart/form-data``.  The ``content_type`` parameter should be
    a string and ``body`` should be a byte string.  The ``arguments``
    and ``files`` parameters are dictionaries that will be updated
    with the parsed contents.
    """
    if headers and 'Content-Encoding' in headers:
        gen_log.warning("Unsupported Content-Encoding: %s",
                        headers['Content-Encoding'])
        return
    if content_type.startswith("application/x-www-form-urlencoded"):
        try:
            uri_arguments = parse_qs_bytes(native_str(body), keep_blank_values=True)
        except Exception as e:
            gen_log.warning('Invalid x-www-form-urlencoded body: %s', e)
            uri_arguments = {}
        for name, values in uri_arguments.items():
            if values:
                arguments.setdefault(name, []).extend(values)
    elif content_type.startswith("multipart/form-data"):
        fields = content_type.split(";")
        for field in fields:
            k, sep, v = field.strip().partition("=")
            if k == "boundary" and v:
                parse_multipart_form_data(utf8(v), body, arguments, files)
                break
        else:
            gen_log.warning("Invalid multipart/form-data")
예제 #12
0
def parse_body_arguments(content_type, body, arguments, files, headers=None):
    """Parses a form request body.

    Supports ``application/x-www-form-urlencoded`` and
    ``multipart/form-data``.  The ``content_type`` parameter should be
    a string and ``body`` should be a byte string.  The ``arguments``
    and ``files`` parameters are dictionaries that will be updated
    with the parsed contents.
    """
    if headers and 'Content-Encoding' in headers:
        gen_log.warning("Unsupported Content-Encoding: %s",
                        headers['Content-Encoding'])
        return
    if content_type.startswith("application/x-www-form-urlencoded"):
        try:
            uri_arguments = parse_qs_bytes(native_str(body),
                                           keep_blank_values=True)
        except Exception as e:
            gen_log.warning('Invalid x-www-form-urlencoded body: %s', e)
            uri_arguments = {}
        for name, values in uri_arguments.items():
            if values:
                arguments.setdefault(name, []).extend(values)
    elif content_type.startswith("multipart/form-data"):
        try:
            fields = content_type.split(";")
            for field in fields:
                k, sep, v = field.strip().partition("=")
                if k == "boundary" and v:
                    parse_multipart_form_data(utf8(v), body, arguments, files)
                    break
            else:
                raise ValueError("multipart boundary not found")
        except Exception as e:
            gen_log.warning("Invalid multipart/form-data: %s", e)
예제 #13
0
    def __init__(self,
                 method=None,
                 uri=None,
                 version="HTTP/1.0",
                 headers=None,
                 body=None,
                 host=None,
                 files=None,
                 connection=None,
                 start_line=None):
        if start_line is not None:
            method, uri, version = start_line
        self.method = method
        self.uri = uri
        self.version = version
        self.headers = headers or HTTPHeaders()
        self.body = body or b""

        # set remote IP and protocol
        context = getattr(connection, 'context', None)
        self.remote_ip = getattr(context, 'remote_ip', None)
        self.protocol = getattr(context, 'protocol', "http")

        self.host = host or self.headers.get("Host") or "127.0.0.1"
        self.files = files or {}
        self.connection = connection
        self._start_time = time.time()
        self._finish_time = None

        self.path, sep, self.query = uri.partition('?')
        self.arguments = parse_qs_bytes(self.query, keep_blank_values=True)
        self.query_arguments = copy.deepcopy(self.arguments)
        self.body_arguments = {}
예제 #14
0
    def decorated(self, *args, **kwargs):

        original_method(self, *args, **kwargs)

        arguments = parse_qs_bytes(self.query, keep_blank_values=True)
        for name, values in arguments.iteritems():
            self.arguments[name] = values
예제 #15
0
 def _on_request_body(self, data):
     self.reset_connection_timeout()
     self._request.body = data
     content_type = self._request.headers.get("Content-Type", "")
     if self._request.method in ("POST", "PUT"):
         if content_type.startswith("application/x-www-form-urlencoded"):
             arguments = parse_qs_bytes(native_str(self._request.body))
             for name, values in arguments.iteritems():
                 values = [v for v in values if v]
                 if values:
                     self._request.arguments.setdefault(name, []).extend(
                         values)
         elif content_type.startswith("multipart/form-data"):
             fields = content_type.split(";")
             for field in fields:
                 k, sep, v = field.strip().partition("=")
                 if k == "boundary" and v:
                     httputil.parse_multipart_form_data(
                         utf8(v), data,
                         self._request.arguments,
                         self._request.files)
                     break
             else:
                 logging.warning("Invalid multipart/form-data")
     self.request_callback(self._request)
예제 #16
0
파일: client.py 프로젝트: kplaube/torneira
    def create_request(self, uri, method="GET", headers={}, body=None, remote_ip=None):
        request = HTTPRequest(uri=uri, method=method, headers=headers, body=body, remote_ip=remote_ip)

        if body:
            arguments = parse_qs_bytes(native_str(body))
            for name, values in arguments.iteritems():
                values = [v for v in values if v]
                if values:
                    request.arguments.setdefault(name, []).extend(values)

        return request
예제 #17
0
    def decorated(self, data):

        if self._request.method in ("POST", "PUT"):
            content_type = self._request.headers.get("Content-Type", "")
            if content_type.startswith("application/x-www-form-urlencoded"):
                arguments = parse_qs_bytes(native_str(data), keep_blank_values=True)
                for name, values in arguments.iteritems():
                    has_values = bool([v for v in values if v])
                    if not has_values:
                        self._request.arguments.setdefault(name, []).extend(values)

        original_method(self, data)
예제 #18
0
    def _on_access_token(self, redirect_uri, client_id, client_secret,
                         callback, fields, response):
        args = escape.parse_qs_bytes(escape.native_str(response.body))

        #self.finish({"body":response.body, "args":args})
        session = {
            "access_token": args["access_token"][-1],
            "refresh_token": args["refresh_token"][-1],
            "expires_in": args["expires_in"][-1]
        }

        callback(session)
예제 #19
0
    def __init__(self,
                 method,
                 uri,
                 version="HTTP/1.0",
                 headers=None,
                 body=None,
                 remote_ip=None,
                 protocol=None,
                 host=None,
                 files=None,
                 connection=None):
        self.method = method
        self.uri = uri
        self.version = version
        self.headers = headers or httputil.HTTPHeaders()
        self.body = body or ""
        if connection and connection.xheaders:
            # Squid uses X-Forwarded-For, others use X-Real-Ip
            self.remote_ip = self.headers.get(
                "X-Real-Ip", self.headers.get("X-Forwarded-For", remote_ip))
            if not self._valid_ip(self.remote_ip):
                self.remote_ip = remote_ip
            # AWS uses X-Forwarded-Proto
            self.protocol = self.headers.get(
                "X-Scheme", self.headers.get("X-Forwarded-Proto", protocol))
            if self.protocol not in ("http", "https"):
                self.protocol = "http"
        else:
            self.remote_ip = remote_ip
            if protocol:
                self.protocol = protocol
            elif connection and isinstance(connection.stream,
                                           iostream.SSLIOStream):
                self.protocol = "https"
            else:
                self.protocol = "http"
        self.host = host or self.headers.get("Host") or "127.0.0.1"
        self.files = files or {}
        self.connection = connection
        self._start_time = time.time()
        self._finish_time = None

        scheme, netloc, path, query, fragment = urlparse.urlsplit(
            native_str(uri))
        self.path = path
        self.query = query
        arguments = parse_qs_bytes(query)
        self.arguments = {}
        for name, values in arguments.iteritems():
            values = [v for v in values if v]
            if values:
                self.arguments[name] = values
예제 #20
0
    def __init__(self,
                 method,
                 uri,
                 version="HTTP/1.0",
                 headers=None,
                 body=None,
                 remote_ip=None,
                 protocol=None,
                 host=None,
                 files=None,
                 connection=None):
        self.method = method
        self.uri = uri
        self.version = version
        self.headers = headers or httputil.HTTPHeaders()
        self.body = body or ""

        # set remote IP and protocol
        self.remote_ip = remote_ip
        if protocol:
            self.protocol = protocol
        elif connection and isinstance(connection.stream,
                                       iostream.SSLIOStream):
            self.protocol = "https"
        else:
            self.protocol = "http"

        # xheaders can override the defaults
        if connection and connection.xheaders:
            # Squid uses X-Forwarded-For, others use X-Real-Ip
            ip = self.headers.get("X-Forwarded-For", self.remote_ip)
            ip = ip.split(',')[-1].strip()
            ip = self.headers.get("X-Real-Ip", ip)
            if netutil.is_valid_ip(ip):
                self.remote_ip = ip
            # AWS uses X-Forwarded-Proto
            proto = self.headers.get(
                "X-Scheme", self.headers.get("X-Forwarded-Proto",
                                             self.protocol))
            if proto in ("http", "https"):
                self.protocol = proto

        self.host = host or self.headers.get("Host") or "127.0.0.1"
        self.files = files or {}
        self.connection = connection
        self._start_time = time.time()
        self._finish_time = None

        self.path, sep, self.query = uri.partition('?')
        self.arguments = parse_qs_bytes(self.query, keep_blank_values=True)
        self.query_arguments = copy.deepcopy(self.arguments)
        self.body_arguments = {}
예제 #21
0
 def get(self):
     code = self.get_argument('code', False)
     if code:
         token_url = self._oauth_request_token_url(
             redirect_uri=self.settings['redirect_url'],
             client_id=self.settings['facebook_api_key'],
             client_secret=self.settings['facebook_secret'],
             code=code,
         )
         req = urllib2.Request(token_url)
         response = urllib2.urlopen(req)
         args = escape.parse_qs_bytes(escape.native_str(response.read()))
         access_token = args['access_token'][-1]
예제 #22
0
    def __init__(
        self,
        method,
        uri,
        version="HTTP/1.0",
        headers=None,
        body=None,
        remote_ip=None,
        protocol=None,
        host=None,
        files=None,
        connection=None,
    ):
        self.method = method
        self.uri = uri
        self.version = version
        self.headers = headers or httputil.HTTPHeaders()
        self.body = body or ""

        # set remote IP and protocol
        self.remote_ip = remote_ip
        if protocol:
            self.protocol = protocol
        elif connection and isinstance(connection.stream, iostream.SSLIOStream):
            self.protocol = "https"
        else:
            self.protocol = "http"

        # xheaders can override the defaults
        if connection and connection.xheaders:
            # Squid uses X-Forwarded-For, others use X-Real-Ip
            ip = self.headers.get("X-Forwarded-For", self.remote_ip)
            ip = ip.split(",")[-1].strip()
            ip = self.headers.get("X-Real-Ip", ip)
            if netutil.is_valid_ip(ip):
                self.remote_ip = ip
            # AWS uses X-Forwarded-Proto
            proto = self.headers.get("X-Scheme", self.headers.get("X-Forwarded-Proto", self.protocol))
            if proto in ("http", "https"):
                self.protocol = proto

        self.host = host or self.headers.get("Host") or "127.0.0.1"
        self.files = files or {}
        self.connection = connection
        self._start_time = time.time()
        self._finish_time = None

        self.path, sep, self.query = uri.partition("?")
        self.arguments = parse_qs_bytes(self.query, keep_blank_values=True)
        self.query_arguments = copy.deepcopy(self.arguments)
        self.body_arguments = {}
예제 #23
0
    def _on_access_token(self, redirect_uri, client_id, client_secret, callback, fields, response):
        if response.error:
            gen_log.warning("Facebook auth error: %s" % str(response))
            callback(None)
            return

        args = escape.parse_qs_bytes(escape.native_str(response.body))
        session = {"access_token": args["access_token"][-1], "expires": args.get("expires")}
        self.facebook_request(
            path="/me",
            callback=self.async_callback(self._on_get_user_info, callback, session, fields),
            access_token=session["access_token"],
            fields=",".join(fields),
        )
예제 #24
0
    def url_for_page(self, page):

        query_arguments = parse_qs_bytes(self.request.query,
                                         keep_blank_values=False)
        args = []
        for key, values in query_arguments.items():
            if key == "page":
                continue

            for value in values:
                args.append((key, value))

        args.append(("page", page))
        return url_concat(self.request.path, args)
예제 #25
0
    def __init__(
        self,
        method,
        uri,
        version="HTTP/1.0",
        headers=None,
        body=None,
        remote_ip=None,
        protocol=None,
        host=None,
        files=None,
        connection=None,
    ):
        self.method = method
        self.uri = uri
        self.version = version
        self.headers = headers or httputil.HTTPHeaders()
        self.body = body or ""
        if connection and connection.xheaders:
            # Squid uses X-Forwarded-For, others use X-Real-Ip
            self.remote_ip = self.headers.get("X-Real-Ip", self.headers.get("X-Forwarded-For", remote_ip))
            if not self._valid_ip(self.remote_ip):
                self.remote_ip = remote_ip
            # AWS uses X-Forwarded-Proto
            self.protocol = self.headers.get("X-Scheme", self.headers.get("X-Forwarded-Proto", protocol))
            if self.protocol not in ("http", "https"):
                self.protocol = "http"
        else:
            self.remote_ip = remote_ip
            if protocol:
                self.protocol = protocol
            elif connection and isinstance(connection.stream, iostream.SSLIOStream):
                self.protocol = "https"
            else:
                self.protocol = "http"
        self.host = host or self.headers.get("Host") or "127.0.0.1"
        self.files = files or {}
        self.connection = connection
        self._start_time = time.time()
        self._finish_time = None

        scheme, netloc, path, query, fragment = urlparse.urlsplit(native_str(uri))
        self.path = path
        self.query = query
        arguments = parse_qs_bytes(query)
        self.arguments = {}
        for name, values in arguments.iteritems():
            values = [v for v in values if v]
            if values:
                self.arguments[name] = values
예제 #26
0
파일: wsgi.py 프로젝트: zinic/tornado
    def __init__(self, environ):
        """Parses the given WSGI environment to construct the request."""
        self.method = environ["REQUEST_METHOD"]
        self.path = urllib_parse.quote(
            from_wsgi_str(environ.get("SCRIPT_NAME", "")))
        self.path += urllib_parse.quote(
            from_wsgi_str(environ.get("PATH_INFO", "")))
        self.uri = self.path
        self.arguments = {}
        self.query_arguments = {}
        self.body_arguments = {}
        self.query = environ.get("QUERY_STRING", "")
        if self.query:
            self.uri += "?" + self.query
            self.arguments = parse_qs_bytes(native_str(self.query),
                                            keep_blank_values=True)
            self.query_arguments = copy.deepcopy(self.arguments)
        self.version = "HTTP/1.1"
        self.headers = httputil.HTTPHeaders()
        if environ.get("CONTENT_TYPE"):
            self.headers["Content-Type"] = environ["CONTENT_TYPE"]
        if environ.get("CONTENT_LENGTH"):
            self.headers["Content-Length"] = environ["CONTENT_LENGTH"]
        for key in environ:
            if key.startswith("HTTP_"):
                self.headers[key[5:].replace("_", "-")] = environ[key]
        if self.headers.get("Content-Length"):
            self.body = environ["wsgi.input"].read(
                int(self.headers["Content-Length"]))
        else:
            self.body = ""
        self.protocol = environ["wsgi.url_scheme"]
        self.remote_ip = environ.get("REMOTE_ADDR", "")
        if environ.get("HTTP_HOST"):
            self.host = environ["HTTP_HOST"]
        else:
            self.host = environ["SERVER_NAME"]

        # Parse request body
        self.files = {}
        httputil.parse_body_arguments(self.headers.get("Content-Type",
                                                       ""), self.body,
                                      self.body_arguments, self.files)

        for k, v in self.body_arguments.items():
            self.arguments.setdefault(k, []).extend(v)

        self._start_time = time.time()
        self._finish_time = None
예제 #27
0
파일: auth.py 프로젝트: afeide/LuoYunCloud
 def _on_access_token(self, redirect_uri, client_id, client_secret,
                     callback, response):
     if response.error:
         logging.warn('QQ auth error: %s' % str(response))
         callback(None)
         return
     args = escape.parse_qs_bytes(escape.native_str(response.body))
     session = {
         "access_token": args["access_token"][-1],
         "expires": args.get("expires_in")[0]
     }
     http = self.get_auth_http_client()
     response = yield gen.Task(http.fetch, url_concat(self._OAUTH_OPEND_ID_URL, {"access_token":session["access_token"]}))
     self._on_open_id(redirect_uri, client_id,
                      client_secret, callback, session, response)
예제 #28
0
파일: oauth_bz.py 프로젝트: j00454543/hello
 def _on_access_token(self, redirect_uri, client_id, client_secret,
                      callback, response):
     if response.error:
         logging.warning('QQ auth error: %s' % str(response))
         callback(None)
         return
     args = escape.parse_qs_bytes(escape.native_str(response.body))
     session = {
         "access_token": args["access_token"][-1],
         "expires": args.get("expires_in")[0]
     }
     http = self.get_auth_http_client()
     response = yield gen.Task(http.fetch, url_concat(self._OAUTH_OPEND_ID_URL, {"access_token": session["access_token"]}), validate_cert=False, request_timeout=100)
     self._on_open_id(redirect_uri, client_id,
                      client_secret, callback, session, response)
예제 #29
0
    def __init__(self, method, uri, version="HTTP/1.0", headers=None,
                 body=None, remote_ip=None, protocol=None, host=None,
                 files=None, connection=None):

        # 简单提一下 HTTPRequest。它是【客户端请求的代表】,它携带了所有和客户端请求的信息,
        # 因为 application 的回调__call__方法【只】接收 request 参数,
        # 当然是把所有信息包在其中。另外,由于服务器只把 request 对象暴露给 application 的回调,
        # 因此request 对象还需要提供 write,finish 方法来提供服务,
        # 其实就是对 HTTPConnection 对象的【封装调用】。其它也没什么了。
        self.method = method
        self.uri = uri
        self.version = version
        self.headers = headers or httputil.HTTPHeaders()
        self.body = body or ""

        # set remote IP and protocol
        self.remote_ip = remote_ip
        if protocol:
            self.protocol = protocol
        elif connection and isinstance(connection.stream,
                                       iostream.SSLIOStream):
            self.protocol = "https"
        else:
            self.protocol = "http"

        # xheaders can override the defaults
        if connection and connection.xheaders:
            # Squid uses X-Forwarded-For, others use X-Real-Ip
            ip = self.headers.get("X-Forwarded-For", self.remote_ip)
            ip = ip.split(',')[-1].strip()
            ip = self.headers.get(
                "X-Real-Ip", ip)
            if netutil.is_valid_ip(ip):
                self.remote_ip = ip
            # AWS uses X-Forwarded-Proto
            proto = self.headers.get(
                "X-Scheme", self.headers.get("X-Forwarded-Proto", self.protocol))
            if proto in ("http", "https"):
                self.protocol = proto

        self.host = host or self.headers.get("Host") or "127.0.0.1"
        self.files = files or {}
        self.connection = connection
        self._start_time = time.time()
        self._finish_time = None

        self.path, sep, self.query = uri.partition('?')
        self.arguments = parse_qs_bytes(self.query, keep_blank_values=True)
예제 #30
0
    def _on_access_token(self, redirect_uri, client_id, client_secret,
                         callback, fields, response):
        if response.error:
            logging.warning('Github auth error {0}'.format(str(response)))
            callback(None)
            return
        args = escape.parse_qs_bytes(escape.native_str(response.body))

        session = {}
        session['access_token'] = args.get('access_token')[0]

        self.github_request(
            path='/user',
            callback=self.async_callback(
                self._get_user_info, callback, session, fields),
            access_token=session['access_token'])
예제 #31
0
파일: handlers.py 프로젝트: reorx/stargazer
    def _on_access_token(self, callback, response):
        if response.error:
            logging.warning('Github auth error: %s' % str(response))
            callback(None)
            return

        args = escape.parse_qs_bytes(escape.native_str(response.body))
        logging.info('Got access token: %s', args)
        access_token = args['access_token'][0]
        token_type = args['token_type'][0]

        self.github_request(
            '/user',
            access_token=access_token,
            callback=self.async_callback(
                self._on_get_user_info, callback, access_token, token_type))
예제 #32
0
def parse_body_arguments(content_type, body, arguments, files):
    if content_type.startswith("application/x-www-form-urlencoded"):
        uri_arguments = parse_qs_bytes(native_str(body))
        for name, values in uri_arguments.iteritems():
            values = [v for v in values if v]
            if values:
                arguments.setdefault(name, []).extend(values)
    elif content_type.startswith("multipart/form-data"):
        fields = content_type.split(";")
        for field in fields:
            k, sep, v = field.strip().partition("=")
            if k == "boundary" and v:
                parse_multipart_form_data(utf8(v), body, arguments, files)
                break
        else:
            logging.warning("Invalid multipart/form-data")
예제 #33
0
def parse_body_arguments(content_type, body, arguments, files):
    if content_type.startswith("application/x-www-form-urlencoded"):
        uri_arguments = parse_qs_bytes(native_str(body))
        for name, values in uri_arguments.iteritems():
            values = [v for v in values if v]
            if values:
                arguments.setdefault(name, []).extend(values)
    elif content_type.startswith("multipart/form-data"):
        fields = content_type.split(";")
        for field in fields:
            k, sep, v = field.strip().partition("=")
            if k == "boundary" and v:
                parse_multipart_form_data(utf8(v), body, arguments, files)
                break
        else:
            logging.warning("Invalid multipart/form-data")
예제 #34
0
파일: utils.py 프로젝트: lmdu/sci
	def _on_access_token(self, redirect_uri, client_id, client_secret,
						 future, fields, response):
		if response.error:
			future.set_exception(AuthError('QQ auth error: %s' % str(response)))
			return

		args = escape.parse_qs_bytes(escape.native_str(response.body))
		session = {
			"access_token": args["access_token"][0],
			"expires": args.get("expires_in")[0]
		}
		
		http = self.get_auth_http_client()
		http.fetch("https://graph.qq.com/oauth2.0/me?access_token="+session["access_token"], 
				   self.async_callback(self._on_access_openid, redirect_uri, client_id,
									   client_secret, session, future, fields))
예제 #35
0
    def post(self):
        _log.debug('configuration set: %s', self.request)
        _log.debug('body: %s', self.request.body)

        # parse request
        arguments = parse_qs_bytes(self.request.body, keep_blank_values=False)
        _log.debug('arguments: %s', arguments)

        if arguments.has_key('assigned_rig') and arguments['assigned_rig'][0]:
            rig = arguments['assigned_rig'][0]
            _log.debug('set assigned rig to: %s', rig)
        else:
            rig = None

        if (self.app.mode
                == self.app.MODE_KR) and arguments['controll_aladin'][0]:
            aladin = self.aladin_modes_post[arguments['controll_aladin'][0]]

        def get_arg(arg):
            if arguments.has_key(arg):
                return arguments[arg][0]
            else:
                return None

        # services in need of reload
        need_reload = []

        config = ConfigLoader()
        if rig != config.get_system():
            config.set_system(rig)
            # reload controller service
            need_reload.append(ServiceReloader.SERVICE_CONTROLLER)

        if self.app.mode == self.app.MODE_KR:
            if aladin != config.get_aladin():
                config.set_aladin(aladin)
                need_reload.append(ServiceReloader.SERVICE_SERVO)

        # update configuration file
        config.write()

        if need_reload:
            _log.info('services needing reload: %s', need_reload)
            ServiceReloader.reload(need_reload)
            _log.info('reload complete')

        self.redirect('/?config_applied=1')
예제 #36
0
def parse_body_arguments(
    content_type: str,
    body: bytes,
    arguments: Dict[str, List[bytes]],
    files: Dict[str, List[HTTPFile]],
    headers: Optional[HTTPHeaders] = None,
) -> None:
    """Parses a form request body.

    Supports ``application/x-www-form-urlencoded`` and
    ``multipart/form-data``.  The ``content_type`` parameter should be
    a string and ``body`` should be a byte string.  The ``arguments``
    and ``files`` parameters are dictionaries that will be updated
    with the parsed contents.
    """
    if content_type.startswith("application/x-www-form-urlencoded"):
        if headers and "Content-Encoding" in headers:
            gen_log.warning(
                "Unsupported Content-Encoding: %s", headers["Content-Encoding"]
            )
            return
        try:
            # real charset decoding will happen in RequestHandler.decode_argument()
            uri_arguments = parse_qs_bytes(body, keep_blank_values=True)
        except Exception as e:
            gen_log.warning("Invalid x-www-form-urlencoded body: %s", e)
            uri_arguments = {}
        for name, values in uri_arguments.items():
            if values:
                arguments.setdefault(name, []).extend(values)
    elif content_type.startswith("multipart/form-data"):
        if headers and "Content-Encoding" in headers:
            gen_log.warning(
                "Unsupported Content-Encoding: %s", headers["Content-Encoding"]
            )
            return
        try:
            fields = content_type.split(";")
            for field in fields:
                k, sep, v = field.strip().partition("=")
                if k == "boundary" and v:
                    parse_multipart_form_data(utf8(v), body, arguments, files)
                    break
            else:
                raise ValueError("multipart boundary not found")
        except Exception as e:
            gen_log.warning("Invalid multipart/form-data: %s", e)
예제 #37
0
    def __init__(self, environ):
        """Parses the given WSGI environment to construct the request."""
        self.method = environ["REQUEST_METHOD"]
        self.path = urllib_parse.quote(from_wsgi_str(environ.get("SCRIPT_NAME", "")))
        self.path += urllib_parse.quote(from_wsgi_str(environ.get("PATH_INFO", "")))
        self.uri = self.path
        self.arguments = {}
        self.query_arguments = {}
        self.body_arguments = {}
        self.query = environ.get("QUERY_STRING", "")
        if self.query:
            self.uri += "?" + self.query
            self.arguments = parse_qs_bytes(native_str(self.query),
                                            keep_blank_values=True)
            self.query_arguments = copy.deepcopy(self.arguments)
        self.version = "HTTP/1.1"
        self.headers = httputil.HTTPHeaders()
        if environ.get("CONTENT_TYPE"):
            self.headers["Content-Type"] = environ["CONTENT_TYPE"]
        if environ.get("CONTENT_LENGTH"):
            self.headers["Content-Length"] = environ["CONTENT_LENGTH"]
        for key in environ:
            if key.startswith("HTTP_"):
                self.headers[key[5:].replace("_", "-")] = environ[key]
        if self.headers.get("Content-Length"):
            self.body = environ["wsgi.input"].read(
                int(self.headers["Content-Length"]))
        else:
            self.body = ""
        self.protocol = environ["wsgi.url_scheme"]
        self.remote_ip = environ.get("REMOTE_ADDR", "")
        if environ.get("HTTP_HOST"):
            self.host = environ["HTTP_HOST"]
        else:
            self.host = environ["SERVER_NAME"]

        # Parse request body
        self.files = {}
        httputil.parse_body_arguments(self.headers.get("Content-Type", ""),
                                      self.body, self.body_arguments, self.files)

        for k, v in self.body_arguments.items():
            self.arguments.setdefault(k, []).extend(v)

        self._start_time = time.time()
        self._finish_time = None
예제 #38
0
    def post(self):
        _log.debug('configuration set: %s' , self.request)
        _log.debug('body: %s', self.request.body)

        # parse request
        arguments = parse_qs_bytes(self.request.body, keep_blank_values=False)
        _log.debug('arguments: %s', arguments)

        if arguments.has_key('assigned_rig') and arguments['assigned_rig'][0]:
            rig = arguments['assigned_rig'][0]
            _log.debug('set assigned rig to: %s', rig)
        else:
            rig = None

        if (self.app.mode == self.app.MODE_KR) and arguments['controll_aladin'][0]:
            aladin = self.aladin_modes_post[arguments['controll_aladin'][0]]

        def get_arg(arg):
            if arguments.has_key(arg):
                return arguments[arg][0]
            else:
                return None

        # services in need of reload
        need_reload = []

        config = ConfigLoader()
        if rig != config.get_system():
            config.set_system(rig)
            # reload controller service
            need_reload.append(ServiceReloader.SERVICE_CONTROLLER)

        if self.app.mode == self.app.MODE_KR:
            if aladin != config.get_aladin():
                config.set_aladin(aladin)
                need_reload.append(ServiceReloader.SERVICE_SERVO)

        # update configuration file
        config.write()

        if need_reload:
            _log.info('services needing reload: %s', need_reload)
            ServiceReloader.reload(need_reload)
            _log.info('reload complete')

        self.redirect('/?config_applied=1')
예제 #39
0
파일: alpha.py 프로젝트: xealot/vocalbroker
    def prepare(self):
        #Security
        validator = RequestValidator(TWILIO_AUTH_TOKEN)
        # the POST variables attached to the request (eg "From", "To")
        post_vars = dict([(k, v[0]) for k,v in parse_qs_bytes(self.request.body, True).items()])
        signature = self.request.headers.get('X-Twilio-Signature', '') # X-Twilio-Signature header value

        if not validator.validate(self.request.full_url(), post_vars, signature):
            logger.critical('BAD SIGNATURE')
            response = twiml.Response()
            response.say('Sorry')
            response.hangup()
            self.finish(str(response))
            return

        #Session: Each call is somewhat like a session.
        self.session = None
예제 #40
0
    def _on_access_token(self, redirect_uri, client_id, client_secret,
                         callback, response):
        if response.error:
            logging.warning('Facebook auth error: %s' % str(response))
            callback(None)
            return

        args = escape.parse_qs_bytes(escape.native_str(response.body))
        session = {
            "access_token": args["access_token"][-1],
            "expires": args.get("expires")
        }

        self.facebook_request(path="/me",
                              callback=self.async_callback(
                                  self._on_get_user_info, callback, session),
                              access_token=session["access_token"])
예제 #41
0
    def _on_access_token(self, callback, fields, response):
        if response.error:
            logging.warning('Facebook auth error: %s' % response)
            callback(None)
            return

        args = escape.parse_qs_bytes(escape.native_str(response.body))
        session = {
            "access_token": args["access_token"][-1],
            "expires": args.get("expires")
        }

        self.facebook_request(path="/me",
                              callback=self.async_callback(
                                  self._on_get_user_info, callback, session,
                                  fields),
                              access_token=session["access_token"],
                              fields=",".join(fields))
예제 #42
0
    def __init__(self,
                 method,
                 uri,
                 version="HTTP/1.0",
                 headers=None,
                 body=None,
                 remote_ip=None,
                 protocol=None,
                 host=None,
                 files=None,
                 connection=None):
        self.method = method
        self.uri = uri
        self.version = version
        self.headers = headers or httputil.HTTPHeaders()
        self.body = body or ""
        if connection and connection.xheaders:
            # Squid uses X-Forwarded-For, others use X-Real-Ip
            self.remote_ip = self.headers.get(
                "X-Real-Ip", self.headers.get("X-Forwarded-For", remote_ip))
            if not self._valid_ip(self.remote_ip):
                self.remote_ip = remote_ip
            # AWS uses X-Forwarded-Proto
            self.protocol = self.headers.get(
                "X-Scheme", self.headers.get("X-Forwarded-Proto", protocol))
            if self.protocol not in ("http", "https"):
                self.protocol = "http"
        else:
            self.remote_ip = remote_ip
            if protocol:
                self.protocol = protocol
            elif connection and isinstance(connection.stream,
                                           iostream.SSLIOStream):
                self.protocol = "https"
            else:
                self.protocol = "http"
        self.host = host or self.headers.get("Host") or "127.0.0.1"
        self.files = files or {}
        self.connection = connection
        self._start_time = time.time()
        self._finish_time = None

        self.path, sep, self.query = uri.partition('?')
        self.arguments = parse_qs_bytes(self.query, keep_blank_values=True)
예제 #43
0
    def __init__(
        self,
        method: Optional[str] = None,
        uri: Optional[str] = None,
        version: str = "HTTP/1.0",
        headers: Optional[HTTPHeaders] = None,
        body: Optional[bytes] = None,
        host: Optional[str] = None,
        files: Optional[Dict[str, List["HTTPFile"]]] = None,
        connection: Optional["HTTPConnection"] = None,
        start_line: Optional["RequestStartLine"] = None,
        server_connection: Optional[object] = None,
    ) -> None:
        if start_line is not None:
            method, uri, version = start_line
        self.method = method
        self.uri = uri
        self.version = version
        self.headers = headers or HTTPHeaders()
        self.body = body or b""

        # set remote IP and protocol
        context = getattr(connection, "context", None)
        self.remote_ip = getattr(context, "remote_ip", None)
        self.protocol = getattr(context, "protocol", "http")

        self.host = (
            host
            or self.headers.get("X-Forwarded-Host")
            or self.headers.get("Host")
            or "127.0.0.1"
        )
        self.host_name = split_host_and_port(self.host.lower())[0]
        self.files = files or {}
        self.connection = connection
        self.server_connection = server_connection
        self._start_time = time.time()
        self._finish_time = None

        if uri is not None:
            self.path, sep, self.query = uri.partition("?")
        self.arguments = parse_qs_bytes(self.query, keep_blank_values=True)
        self.query_arguments = copy.deepcopy(self.arguments)
        self.body_arguments = {}  # type: Dict[str, List[bytes]]
예제 #44
0
    def data_received(self, data):
        if not self.boundary:
            self.boundary = self.get_boundary()

        if self.content_type.startswith('application/x-www-form-urlencoded'):
            try:
                uri_arguments = parse_qs_bytes(native_str(data),
                                               keep_blank_values=True)
                for name, values in uri_arguments.items():
                    if values:
                        self.add_argument(name, values)
            except:
                pass
            chunks = []
        elif self.content_type.startswith(
                'multipart/form-data') and self.boundary:
            chunks = data.split(b"--" + self.boundary)
        else:
            if not self.ctx.get('name'):
                name = self.request.headers.get('X-Name', 'file')
                filename = self.request.headers.get('X-Filename', 'filename')
                chunks = [
                    'Content-Disposition: form-data; name="%s"; filename="%s"\r\nContent-Type: %s\r\n\r\n%s\r\n'
                    % (name, filename, self.content_type, data)
                ]
            else:
                chunks = [data]

        for chunk in chunks:
            if len(chunk) == 0:
                pass
            elif len(chunk) == 4:
                self._data_received_part_end()
            else:
                header, p, part = chunk.partition(b"\r\n\r\n")
                if part:
                    if self.ctx.get('name', None):
                        self._data_received_part_end()
                    name, filename, content_type = self.get_name_from_header(
                        header)
                    self.data_received_header(name, filename, content_type)
                    self._data_received_part(part[0:-2])
                else:
                    self._data_received_part(chunk)
예제 #45
0
    def __init__(
        self,
        method,
        uri,
        version="HTTP/1.0",
        headers=None,
        body=None,
        remote_ip=None,
        protocol=None,
        host=None,
        files=None,
        connection=None,
    ):
        self.method = method
        self.uri = uri
        self.version = version
        self.headers = headers or httputil.HTTPHeaders()
        self.body = body or ""
        if connection and connection.xheaders:
            # Squid uses X-Forwarded-For, others use X-Real-Ip
            self.remote_ip = self.headers.get("X-Real-Ip", self.headers.get("X-Forwarded-For", remote_ip))
            if not self._valid_ip(self.remote_ip):
                self.remote_ip = remote_ip
            # AWS uses X-Forwarded-Proto
            self.protocol = self.headers.get("X-Scheme", self.headers.get("X-Forwarded-Proto", protocol))
            if self.protocol not in ("http", "https"):
                self.protocol = "http"
        else:
            self.remote_ip = remote_ip
            if protocol:
                self.protocol = protocol
            elif connection and isinstance(connection.stream, iostream.SSLIOStream):
                self.protocol = "https"
            else:
                self.protocol = "http"
        self.host = host or self.headers.get("Host") or "127.0.0.1"
        self.files = files or {}
        self.connection = connection
        self._start_time = time.time()
        self._finish_time = None

        self.path, sep, self.query = uri.partition("?")
        self.arguments = parse_qs_bytes(self.query, keep_blank_values=True)
예제 #46
0
    def _on_access_token(self, redirect_uri, client_id, client_secret, future,
                         fields, response):
        if response.error:
            future.set_exception(
                AuthError('Facebook auth error: %s' % str(response)))
            return

        args = escape.parse_qs_bytes(escape.native_str(response.body))
        session = {
            "access_token": args["access_token"][-1],
            "expires": args.get("expires")
        }

        self.facebook_request(path="/me",
                              callback=functools.partial(
                                  self._on_get_user_info, future, session,
                                  fields),
                              access_token=session["access_token"],
                              fields=",".join(fields))
예제 #47
0
	def _on_access_token(self, redirect_uri, client_id, client_secret,
						 future, fields, response):
		if response.error:
			future.set_exception(AuthError('Facebook auth error: %s' % str(response)))
			return

		args = escape.parse_qs_bytes(escape.native_str(response.body))
		session = {
			"access_token": args["access_token"][-1],
			"expires": args.get("expires")
		}

		self.facebook_request(
			path="/me",
			callback=self.async_callback(
				self._on_get_user_info, future, session, fields),
			access_token=session["access_token"],
			fields=",".join(fields)
		)
예제 #48
0
    def _on_access_token(self, redirect_uri, client_id, client_secret,
                         callback, fields, response):
        if response.error:
            logging.warning('Github auth error: %s' % str(response))
            callback(None)
            return

        args = escape.parse_qs_bytes(escape.native_str(response.body))
        session = {
            "access_token": args["access_token"][-1],
        }

        self.github_request(
            path="/user",
            callback=self.async_callback(
                self._on_get_user_info, callback, session, fields),
            access_token=session["access_token"],
            fields=",".join(fields)
        )
예제 #49
0
def parse_body_arguments(content_type, body, arguments, files, headers=None):
    """Parses a form request body.

    Supports ``application/x-www-form-urlencoded`` and
    ``multipart/form-data``.  The ``content_type`` parameter should be
    a string and ``body`` should be a byte string.  The ``arguments``
    and ``files`` parameters are dictionaries that will be updated
    with the parsed contents.
    """
    if headers and 'Content-Encoding' in headers:
        gen_log.warning("Unsupported Content-Encoding: %s",
                        headers['Content-Encoding'])
        return
    # HTTP 协议中定义了两种形式的表单提交方式(还有一种 raw 格式,算作未定义)。
    # 一种是用『&』符号分割的『application/x-www-form-urlencoded』形式,一种是
    # 自定义 boundary 的『multipart/form-data』形式,具体可参考:
    # https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4

    # application/x-www-form-urlencoded 形式
    if content_type.startswith("application/x-www-form-urlencoded"):
        try:
            uri_arguments = parse_qs_bytes(native_str(body),
                                           keep_blank_values=True)
        except Exception as e:
            gen_log.warning('Invalid x-www-form-urlencoded body: %s', e)
            uri_arguments = {}
        for name, values in uri_arguments.items():
            if values:
                arguments.setdefault(name, []).extend(values)
    # multipart/form-data 形式
    elif content_type.startswith("multipart/form-data"):
        try:
            fields = content_type.split(";")
            for field in fields:
                k, sep, v = field.strip().partition("=")
                if k == "boundary" and v:
                    parse_multipart_form_data(utf8(v), body, arguments, files)
                    break
            else:
                raise ValueError("multipart boundary not found")
        except Exception as e:
            gen_log.warning("Invalid multipart/form-data: %s", e)
예제 #50
0
    def _on_access_token(self, redirect_uri, client_id, client_secret, future,
                         fields, response):
        if response.error:
            future.set_exception(AuthError('QQ auth error: %s' %
                                           str(response)))
            return

        args = escape.parse_qs_bytes(escape.native_str(response.body))
        session = {
            "access_token": args["access_token"][0],
            "expires": args.get("expires_in")[0]
        }

        http = self.get_auth_http_client()
        http.fetch(
            "https://graph.qq.com/oauth2.0/me?access_token=" +
            session["access_token"],
            self.async_callback(self._on_access_openid, redirect_uri,
                                client_id, client_secret, session, future,
                                fields))
예제 #51
0
파일: client.py 프로젝트: lonjoy/torneira
    def create_request(self,
                       uri,
                       method="GET",
                       headers={},
                       body=None,
                       remote_ip=None):
        request = HTTPRequest(uri=uri,
                              method=method,
                              headers=headers,
                              body=body,
                              remote_ip=remote_ip)

        if body:
            arguments = parse_qs_bytes(native_str(body))
            for name, values in arguments.iteritems():
                values = [v for v in values if v]
                if values:
                    request.arguments.setdefault(name, []).extend(values)

        return request
예제 #52
0
파일: httputil.py 프로젝트: lbolla/tornado
def parse_body_arguments(content_type, body, arguments, files):
    """Parses a form request body.

    Supports "application/x-www-form-urlencoded" and "multipart/form-data".
    The content_type parameter should be a string and body should be
    a byte string.  The arguments and files parameters are dictionaries
    that will be updated with the parsed contents.
    """
    if content_type.startswith("application/x-www-form-urlencoded"):
        uri_arguments = parse_qs_bytes(native_str(body), keep_blank_values=True)
        for name, values in uri_arguments.items():
            if values:
                arguments.setdefault(name, []).extend(values)
    elif content_type.startswith("multipart/form-data"):
        fields = content_type.split(";")
        for field in fields:
            k, sep, v = field.strip().partition("=")
            if k == "boundary" and v:
                parse_multipart_form_data(utf8(v), body, arguments, files)
                break
        else:
            gen_log.warning("Invalid multipart/form-data")
예제 #53
0
    def _on_access_token(self, redirect_uri, client_id, client_secret,
                         future, fields, response):
        if response.error:
            future.set_exception(
                AuthError("Github auth error: %s" % str(response)))
            return

        args = parse_qs_bytes(native_str(response.body))

        if "error" in args:
            future.set_exception(
                AuthError("Github auth error: %s" % args["error"][-1]))
            return

        session = {
            "access_token": args["access_token"][-1],
        }

        self.github_request(path="/user",
                            callback=self.async_callback(
                                self._on_get_user_info, future, session, fields),
                            access_token=session["access_token"])
예제 #54
0
    def __init__(
        self,
        method: str = None,
        uri: str = None,
        version: str = "HTTP/1.0",
        headers: HTTPHeaders = None,
        body: bytes = None,
        host: str = None,
        files: Dict[str, List["HTTPFile"]] = None,
        connection: "HTTPConnection" = None,
        start_line: "RequestStartLine" = None,
        server_connection: object = None,
    ) -> None:
        if start_line is not None:
            method, uri, version = start_line
        self.method = method
        self.uri = uri
        self.version = version
        self.headers = headers or HTTPHeaders()
        self.body = body or b""

        # set remote IP and protocol
        context = getattr(connection, "context", None)
        self.remote_ip = getattr(context, "remote_ip", None)
        self.protocol = getattr(context, "protocol", "http")

        self.host = host or self.headers.get("Host") or "127.0.0.1"
        self.host_name = split_host_and_port(self.host.lower())[0]
        self.files = files or {}
        self.connection = connection
        self.server_connection = server_connection
        self._start_time = time.time()
        self._finish_time = None

        if uri is not None:
            self.path, sep, self.query = uri.partition("?")
        self.arguments = parse_qs_bytes(self.query, keep_blank_values=True)
        self.query_arguments = copy.deepcopy(self.arguments)
        self.body_arguments = {}  # type: Dict[str, List[bytes]]
예제 #55
0
    def post(self):
        _url = self.get_argument('url', default=None)
        data = self.get_argument('data', default=None)

        self.client.is_params = False if self.get_argument('is_params', default=False) == '0' else True
        if not _url:
            raise UnexpectedReuqestDataException

        if not _url.startswith('ws://') and not _url.startswith('wss://'):
            raise InvalidWebSocketURLException

        url = urlparse.urlparse(_url)
        # `query_str` is the query string of websocket
        query_str = url.query
        url = '%s://%s%s' % (url.scheme, url.netloc, url.path)
        if self.request.body:
            query_str = query_str + '&' + self.request.body
        query_str = encoder._encode_params(parse_qs_bytes(query_str))

        if not data and not query_str:
            logging.warning('No query strings in url, is it HTTP headers injection?')
        #    raise UnexpectedReuqestDataException

        if query_str:
            logging.info('Request query string: %s' % query_str)
        if data:
            logging.info('Request message: %s' % data)

        if not self.client.ws:
            self.run_websocket('%s?%s' % (url, query_str))
        else:
            self.client.has_send = False
            try:
                self.client.ws.send(data if data else '')
            except WebSocketException, e:
                self.client.ws.close()
                self.client.ws = None
                logging.error('Error occur: %s' % str(e))
                self.finish()
예제 #56
0
def parse_body_arguments(content_type, body, arguments, files):
    """Parses a form request body.

    Supports ``application/x-www-form-urlencoded`` and
    ``multipart/form-data``.  The ``content_type`` parameter should be
    a string and ``body`` should be a byte string.  The ``arguments``
    and ``files`` parameters are dictionaries that will be updated
    with the parsed contents.
    """
    if content_type.startswith("application/x-www-form-urlencoded"):
        uri_arguments = parse_qs_bytes(native_str(body), keep_blank_values=True)
        for name, values in uri_arguments.items():
            if values:
                arguments.setdefault(name, []).extend(values)
    elif content_type.startswith("multipart/form-data"):
        fields = content_type.split(";")
        for field in fields:
            k, sep, v = field.strip().partition("=")
            if k == "boundary" and v:
                parse_multipart_form_data(utf8(v), body, arguments, files)
                break
        else:
            gen_log.warning("Invalid multipart/form-data")
예제 #57
0
 def _on_request_body(self, data):
     self._request.body = data
     content_type = self._request.headers.get("Content-Type", "")
     if self._request.method in ("POST", "PATCH", "PUT"):
         if content_type.startswith("application/x-www-form-urlencoded"):
             arguments = parse_qs_bytes(native_str(self._request.body))
             for name, values in arguments.iteritems():
                 values = [v for v in values if v]
                 if values:
                     self._request.arguments.setdefault(name,
                                                        []).extend(values)
         elif content_type.startswith("multipart/form-data"):
             fields = content_type.split(";")
             for field in fields:
                 k, sep, v = field.strip().partition("=")
                 if k == "boundary" and v:
                     httputil.parse_multipart_form_data(
                         utf8(v), data, self._request.arguments,
                         self._request.files)
                     break
             else:
                 logging.warning("Invalid multipart/form-data")
     self.request_callback(self._request)
예제 #58
0
def parse_body_arguments(content_type, body, arguments, files):
    """Parses a form request body.

    Supports "application/x-www-form-urlencoded" and "multipart/form-data".
    The content_type parameter should be a string and body should be
    a byte string.  The arguments and files parameters are dictionaries
    that will be updated with the parsed contents.
    """
    if content_type.startswith("application/x-www-form-urlencoded"):
        uri_arguments = parse_qs_bytes(native_str(body))
        for name, values in uri_arguments.iteritems():
            values = [v for v in values if v]
            if values:
                arguments.setdefault(name, []).extend(values)
    elif content_type.startswith("multipart/form-data"):
        fields = content_type.split(";")
        for field in fields:
            k, sep, v = field.strip().partition("=")
            if k == "boundary" and v:
                parse_multipart_form_data(utf8(v), body, arguments, files)
                break
        else:
            logging.warning("Invalid multipart/form-data")