예제 #1
0
 def post(self):
     action = self.request.get("admin_action")
     
     if (action == 'flush_cache'):
         memcache.flush_all()
     
     self.response.out.write(url_tools.dict_to_s(memcache.get_stats()))
예제 #2
0
    def fetch_and_cache(self, mirror_url):
        if url_tools.is_absolute_url(mirror_url):
            self.record_last_host = url_tools.get_host_from_url(mirror_url)
        else:
            mirror_url = url_tools.join(HTTP_PREFIX, self.record_last_host, mirror_url)
        
        host_name = url_tools.get_host_from_url(mirror_url)
            
        # http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html 8.1.3 Proxy Servers
        adjusted_headers = dict(self.request.headers)
        adjusted_headers['Connection'] = 'close'
        logging.debug("request headers '%s' of url: '%s'", url_tools.dict_to_s(adjusted_headers), mirror_url)
                      
        try:
            # fetch the requested url
            for attempt in range(FETCH_ATTEMPTS):
                response = urlfetch.fetch(mirror_url, self.request.body, self.method, adjusted_headers)
                logging.info('url fetch attempt %d for "%s" successful', attempt + 1, mirror_url)
                break
        except urlfetch.Error:
            exception_type = sys.exc_info()[0]
            logging.error('url fetch exception "%s" for "%s"', str(exception_type), mirror_url)
            return None

        transform_response = transform.ResponseTransformer(mirror_url, response)
        
        # cache the transformed entity and return
        mirror_content = MirrorEntity(mirror_url,
                                      host_name,
                                      transform_response.status_code,
                                      transform_response.headers,
                                      transform_response.content)
        memcache.add(mirror_url, mirror_content, config.EXPIRATION_RATE_S)

        return mirror_content