def _hook_into_application(self, environ, start_response):
        log.info('Passing request to application')
        request_id = [None]
        def start_glimpse_response(status, response_headers, exc_info=None):
            request_id[0] = self._track_request(response_headers)
            inject_data_from_environ(request_id[0], environ)
            lowered_headers = [(key.lower(), value.lower()) 
                               for key, value in response_headers]
            write = start_response(status, response_headers, exc_info)

            if ('content-type', 'text/html') in lowered_headers:
                def middleware_write(data):
                    write(self._filter_data(data, request_id[0]))
            else:
                middleware_write = write

            return middleware_write
        
        data = self._application(environ, start_glimpse_response)

        return (self._filter_data(item, request_id[0]) for item in data)
    def _execute_resource(self, environ, start_response):
        resource_url = environ['PATH_INFO'][len('/glimpse'):]
        log.info('Got a request for {0}'.format(resource_url))

        query_data = self._parse_query_string(environ.get('QUERY_STRING', ''))
        request = _Request(query_data, environ)

        resource, arguments = self._match_resource(resource_url)
        if resource is None:
            resource = resource_configuration.default_resource
            arguments = []
            status = '404 No matching resource'
        else:
            status = request.response_status

        response_data = resource.handle(request, *arguments)

        log.debug('Url arguments: %s', str(arguments))
        log.debug('Response headers: %s',
                  str(request.get_response_header_list()))

        start_response(status, request.get_response_header_list())
        return [response_data]
 def __init__(self, application):
     log.info('Loading Glimpse middleware')
     self._application = application