コード例 #1
0
ファイル: middleware.py プロジェクト: vdombrovski/swift3
 def __call__(self, env, start_response):
     ctx = WSGIContext(self.app)
     app_iter = ctx._app_call(env)
     try:
         split_path(env['PATH_INFO'], 4, 4, True)
     except ValueError:
         pass  # not an object request; don't care
     else:
         if env['REQUEST_METHOD'] == 'DELETE' and \
                 ctx._response_status[:3] == '404':
             # Should be a cache hit
             if is_success(
                     get_container_info(env, self.app,
                                        swift_source='S3').get('status')):
                 # Convert to a successful response
                 ctx._response_status = '204 No Content'
                 ctx._response_headers = [
                     (h, '0' if h.lower() == 'content-length' else v)
                     for h, v in ctx._response_headers
                 ]
                 with closing_if_possible(app_iter):
                     for chunk in app_iter:
                         pass  # should be short; just drop it on the floor
                 app_iter = ['']
     start_response(ctx._response_status, ctx._response_headers)
     return app_iter
コード例 #2
0
    def __call__(self, env, start_response):
        # a lot of this is cribbed from listing_formats / swob.Request
        if env['REQUEST_METHOD'] != 'GET':
            # Nothing to translate
            return self.app(env, start_response)

        try:
            v, a, c = split_path(
                env.get('SCRIPT_NAME', '') + env['PATH_INFO'], 3, 3)
            if not valid_api_version(v):
                raise ValueError
        except ValueError:
            # not a container request; pass through
            return self.app(env, start_response)

        ctx = WSGIContext(self.app)
        resp_iter = ctx._app_call(env)

        content_type = content_length = cl_index = None
        for index, (header, value) in enumerate(ctx._response_headers):
            header = header.lower()
            if header == 'content-type':
                content_type = value.split(';', 1)[0].strip()
                if content_length:
                    break
            elif header == 'content-length':
                cl_index = index
                try:
                    content_length = int(value)
                except ValueError:
                    pass  # ignore -- we'll bail later
                if content_type:
                    break

        if content_type != 'application/json' or content_length is None or \
                content_length > MAX_CONTAINER_LISTING_CONTENT_LENGTH:
            start_response(ctx._response_status, ctx._response_headers,
                           ctx._response_exc_info)
            return resp_iter

        # We've done our sanity checks, slurp the response into memory
        with closing_if_possible(resp_iter):
            body = b''.join(resp_iter)

        try:
            listing = json.loads(body)
            for item in listing:
                if 'subdir' in item:
                    continue
                value, params = parse_header(item['hash'])
                if 's3_etag' in params:
                    item['s3_etag'] = '"%s"' % params.pop('s3_etag')
                    item['hash'] = value + ''.join('; %s=%s' % kv
                                                   for kv in params.items())
        except (TypeError, KeyError, ValueError):
            # If anything goes wrong above, drop back to original response
            start_response(ctx._response_status, ctx._response_headers,
                           ctx._response_exc_info)
            return [body]

        body = json.dumps(listing)
        ctx._response_headers[cl_index] = (
            ctx._response_headers[cl_index][0],
            str(len(body)),
        )
        start_response(ctx._response_status, ctx._response_headers,
                       ctx._response_exc_info)
        return [body]