示例#1
0
文件: __init__.py 项目: dotpot/Nuages
 def process_response(self, request, response):
     '''Completes the response with global headers that might have not
     been defined at the node level'''
     if (not issubclass(response.__class__, HttpResponse) or
         not response.node or 
         isinstance(response, HttpError) or
         request.method == 'OPTIONS'):
         return response
     
     try:
         node = response.node
         request = node.request
         etag = node.get_etag()
         last_modified = etag.last_modified
         
         if(request.META.get('HTTP_IF_MATCH', ETAG_WILDCARD) != etag or 
            request.META.get('HTTP_IF_UNMODIFIED_SINCE',
                             last_modified + DAY_DELTA) <= last_modified):
             raise PreconditionFailedError(self)
         
         if(request.META.get('HTTP_IF_NONE_MATCH') == etag or
             request.META.get('HTTP_IF_MODIFIED_SINCE',
                              last_modified - DAY_DELTA) >= last_modified):
             raise NotModifiedError(self)
         
         if request.is_secure():
             add_header_if_undefined(response, 'Strict-Transport-Security',
                                     'max-age=99999999')
         
         patch_vary_headers(response, ['Accept'])
         
         #Content type adjustments
         content_type = response.get('Content-Type',
                                     settings.DEFAULT_CONTENT_TYPE)
         if '; charset' in content_type:
             response['Content-Type'] = ('%s; charset=%s' % 
                                         (content_type,
                                          settings.DEFAULT_CHARSET))
                                     
         response['Etag'] = etag
         response['Last-Modified'] = etag.last_modified
                     
         if request.method != 'HEAD' and response.payload:
             response.content = self.__serialize(response.payload,
                                                 content_type=content_type)
         return response
     except(HttpError), http_exception:
         return self.process_exception(request, http_exception)
示例#2
0
    def process_response(self, request, response):
        '''Completes the response with global headers that might have not
        been defined at the node level'''
        if (not issubclass(response.__class__, HttpResponse)
                or not response.node or isinstance(response, HttpError)
                or request.method == 'OPTIONS'):
            return response

        try:
            node = response.node
            request = node.request
            etag = node.get_etag()
            last_modified = etag.last_modified

            if (request.META.get('HTTP_IF_MATCH', ETAG_WILDCARD) != etag
                    or request.META.get('HTTP_IF_UNMODIFIED_SINCE',
                                        last_modified + DAY_DELTA) <=
                    last_modified):
                raise PreconditionFailedError(self)

            if (request.META.get('HTTP_IF_NONE_MATCH') == etag or
                    request.META.get('HTTP_IF_MODIFIED_SINCE', last_modified -
                                     DAY_DELTA) >= last_modified):
                raise NotModifiedError(self)

            if request.is_secure():
                add_header_if_undefined(response, 'Strict-Transport-Security',
                                        'max-age=99999999')

            patch_vary_headers(response, ['Accept'])

            #Content type adjustments
            content_type = response.get('Content-Type',
                                        settings.DEFAULT_CONTENT_TYPE)
            if '; charset' in content_type:
                response['Content-Type'] = (
                    '%s; charset=%s' %
                    (content_type, settings.DEFAULT_CHARSET))

            response['Etag'] = etag
            response['Last-Modified'] = etag.last_modified

            if request.method != 'HEAD' and response.payload:
                response.content = self.__serialize(response.payload,
                                                    content_type=content_type)
            return response
        except (HttpError), http_exception:
            return self.process_exception(request, http_exception)
示例#3
0
    def process_response(self, request, response):
        """Completes the response with global headers that might have not
        been defined at the node level"""
        if (
            not issubclass(response.__class__, HttpResponse)
            or not response.node
            or isinstance(response, HttpError)
            or request.method == "OPTIONS"
        ):
            return response

        try:
            node = response.node
            request = node.request
            etag = node.get_etag()
            last_modified = etag.last_modified

            if (
                request.META.get("HTTP_IF_MATCH", ETAG_WILDCARD) != etag
                or request.META.get("HTTP_IF_UNMODIFIED_SINCE", last_modified + DAY_DELTA) <= last_modified
            ):
                raise PreconditionFailedError(self)

            if (
                request.META.get("HTTP_IF_NONE_MATCH") == etag
                or request.META.get("HTTP_IF_MODIFIED_SINCE", last_modified - DAY_DELTA) >= last_modified
            ):
                raise NotModifiedError(self)

            if request.is_secure():
                add_header_if_undefined(response, "Strict-Transport-Security", "max-age=99999999")

            response["Etag"] = etag
            response["Last-Modified"] = etag.last_modified

            if not response.payload:
                del response["Content-Type"]
                response["Content-Length"] = "0"
            else:
                ApiResponseFormatter(request, response).format()
                patch_vary_headers(response, ["Accept"])

            return response
        except (HttpError), http_exception:
            return self.process_exception(request, http_exception)