Beispiel #1
0
    def setup_class(self, request, full_url, headers):
        querystring = OrderedDict()
        if hasattr(request, "body"):
            # Boto
            self.body = request.body
        else:
            # Flask server

            # FIXME: At least in Flask==0.10.1, request.data is an empty string
            # and the information we want is in request.form. Keeping self.body
            # definition for back-compatibility
            self.body = request.data

            querystring = OrderedDict()
            for key, value in request.form.items():
                querystring[key] = [value]

        raw_body = self.body
        if isinstance(self.body, six.binary_type):
            self.body = self.body.decode("utf-8")

        if not querystring:
            querystring.update(
                parse_qs(urlparse(full_url).query, keep_blank_values=True))
        if not querystring:
            if "json" in request.headers.get("content-type", []):
                # No-op case here for dynamodb to avoid tring to qs parse a json body
                if self.aws_service_spec:
                    decoded = json.loads(self.body)

                    target = request.headers.get(
                        "x-amz-target") or request.headers.get("X-Amz-Target")
                    service, method = target.split(".")
                    input_spec = self.aws_service_spec.input_spec(method)
                    flat = flatten_json_request_body("", decoded, input_spec)
                    for key, value in flat.items():
                        querystring[key] = [value]
            elif self.body:
                try:
                    querystring.update(
                        OrderedDict((key, [value]) for key, value in parse_qsl(
                            raw_body, keep_blank_values=True)))
                except UnicodeEncodeError:
                    pass  # ignore encoding errors, as the body may not contain a legitimate querystring
        if not querystring:
            querystring.update(headers)

        try:
            querystring = _decode_dict(querystring)
        except UnicodeDecodeError:
            pass  # ignore decoding errors, as the body may not contain a legitimate querystring

        self.uri = full_url
        self.path = urlparse(full_url).path
        self.querystring = querystring
        self.data = querystring
        self.method = request.method
        self.region = self.get_region_from_url(request, full_url)
        self.uri_match = None

        self.headers = request.headers
        if "host" not in self.headers:
            self.headers["host"] = urlparse(full_url).netloc
        self.response_headers = {"server": "amazon.com"}