Example #1
0
def fetch_tuple(path, querystring, server):
    """ Fetches the requested object as (status, headers, body, status num)"""
    response = ProxyRequestHandler.fetchurl('GET', path, querystring, server)
    statusline = str(response.status) + " " + response.reason
    headers = response.getheaders()
    body = response.read()
    response.close()
    return (statusline, headers, body, response.status)
Example #2
0
def fetch_tuple(path, querystring, server):
    """ Fetches the requested object as (status, headers, body, status num)"""
    response = ProxyRequestHandler.fetchurl('GET', path, querystring, server)
    statusline = str(response.status) + " " + response.reason
    headers = response.getheaders()
    body = response.read()
    response.close()
    return (statusline, headers, body, response.status)
Example #3
0
    def handle_request(self, query, path, querystring, app, server):
        """ handle a cacheable request. returns (status, headers, data) tuple.

        If it is found in the cache, just return the result directly from the
        cache. Otherwise make a request to the graph api server and return the
        result. If it is a 200 OK response, it gets saved in the cache, also.
        """
        accesstoken_parts = None
        accesstoken = None
        if 'access_token' in query:
            accesstoken = query['access_token'][0]
            accesstoken_parts = ProxyRequestHandler.parse_access_token(
                    query['access_token'][0])
            del query['access_token']
        appid = accesstoken_parts[0] if accesstoken_parts else '0'
        uid = accesstoken_parts[2] if accesstoken_parts else '0'

        usetable = '/' not in path  # use table for user directly
        # usetable = False
        fields = None
        if 'fields' in query and usetable:
            fields = query['fields'][0]
            del query['fields']

        key = path + "__" + appid
        subkey = uid + "__" + urllib.urlencode(query)
        value = None
        hashdict = None
        logging.debug('cache handling request with key ' + key +
                      ', and subkey ' + subkey + ' for user ' + uid)

        self.lock.acquire()
        if key in self.cache:
            # step 1. acquire the dictionary
            hashdict = self.cache[key]
            if subkey in hashdict:  # step 2: grab the relevant data if there
                value = hashdict[subkey]
        else:
            hashdict = HashedDictionary()
            self.cache[key] = hashdict
        self.lock.release()

        if value:  # step 3: return the data if available
            if usetable:
                (statusline, headers, table) = value
                return (statusline, headers, get_response(table, fields))
            else:
                return value

        # at this point, we have a cache miss
        # step 4: fetch data
        if usetable:
            (statusline, headers, table, status) = _fetchtable(query,
                    path, accesstoken, app, hashdict, subkey, server)
            # step 4.5: form a response body from the table
            if status != 200:
                # fetchtable returns body instead of table on error
                body = table
            else:
                for header in headers:
                    if header[0].upper() == 'CONTENT-LENGTH':
                        headers.remove(header)
                        break
                body = get_response(table, fields)
        else:
            (statusline, headers, body, status) = fetch_tuple(path,
                    querystring, server)
            if status == 200:
                hashdict[subkey] = ((statusline, headers, body), body)
        return (statusline, headers, body)
Example #4
0
 def __call__(self, environ, start_response):
     return ProxyRequestHandler(environ, start_response, self.validator,
                                self.cache, self.apps, self.server)
Example #5
0
    def handle_request(self, query, path, querystring, app, server):
        """ handle a cacheable request. returns (status, headers, data) tuple.

        If it is found in the cache, just return the result directly from the
        cache. Otherwise make a request to the graph api server and return the
        result. If it is a 200 OK response, it gets saved in the cache, also.
        """
        accesstoken_parts = None
        accesstoken = None
        if 'access_token' in query:
            accesstoken = query['access_token'][0]
            accesstoken_parts = ProxyRequestHandler.parse_access_token(
                query['access_token'][0])
            del query['access_token']
        appid = accesstoken_parts[0] if accesstoken_parts else '0'
        uid = accesstoken_parts[2] if accesstoken_parts else '0'

        usetable = '/' not in path  # use table for user directly
        # usetable = False
        fields = None
        if 'fields' in query and usetable:
            fields = query['fields'][0]
            del query['fields']

        key = path + "__" + appid
        subkey = uid + "__" + urllib.urlencode(query)
        value = None
        hashdict = None
        logging.debug('cache handling request with key ' + key +
                      ', and subkey ' + subkey + ' for user ' + uid)

        self.lock.acquire()
        if key in self.cache:
            # step 1. acquire the dictionary
            hashdict = self.cache[key]
            if subkey in hashdict:  # step 2: grab the relevant data if there
                value = hashdict[subkey]
        else:
            hashdict = HashedDictionary()
            self.cache[key] = hashdict
        self.lock.release()

        if value:  # step 3: return the data if available
            if usetable:
                (statusline, headers, table) = value
                return (statusline, headers, get_response(table, fields))
            else:
                return value

        # at this point, we have a cache miss
        # step 4: fetch data
        if usetable:
            (statusline, headers, table,
             status) = _fetchtable(query, path, accesstoken, app, hashdict,
                                   subkey, server)
            # step 4.5: form a response body from the table
            if status != 200:
                # fetchtable returns body instead of table on error
                body = table
            else:
                for header in headers:
                    if header[0].upper() == 'CONTENT-LENGTH':
                        headers.remove(header)
                        break
                body = get_response(table, fields)
        else:
            (statusline, headers, body,
             status) = fetch_tuple(path, querystring, server)
            if status == 200:
                hashdict[subkey] = ((statusline, headers, body), body)
        return (statusline, headers, body)