예제 #1
0
 def _DevAppServerAuthenticate(self):
   """Authenticates the user on the dev_appserver."""
   credentials = self.auth_function()
   value = dev_appserver_login.CreateCookieData(credentials[0], True)
   self.extra_headers["Cookie"] = ('dev_appserver_login="******"; Path=/;' % value)
def RetrieveURL(method,
                host_port,
                relative_url,
                user_info=None,
                body=None,
                extra_headers=[]):
    """Access a URL over HTTP and returns the results.

  Args:
    method: HTTP method to use, e.g., GET, POST
    host_port: Tuple (hostname, port) of the host to contact.
    relative_url: Relative URL to access on the remote host.
    user_info: If not None, send this user_info tuple in an HTTP Cookie header
      along with the request; otherwise, no header is included. The user_info
      tuple should be in the form (email, admin) where:
        email: The user's email address.
        admin: True if the user should be an admin; False otherwise.
      If email is empty, it will be as if the user is not logged in.
    body: Request body to write to the remote server. Should only be used with
      the POST method any other methods that expect a message body.
    extra_headers: List of (key, value) tuples for headers to send on the
      request.

  Returns:
    Tuple (status, content, headers) where:
      status: HTTP status code returned by the remote host, e.g. 404, 200, 500
      content: Data returned by the remote host.
      headers: Dictionary mapping header names to header values (both strings).

    If an exception is raised while accessing the remote host, both status and
    content will be set to None.
  """
    url_host = '%s:%d' % host_port

    logging.info('Connecting to %s', url_host)
    try:
        connection = httplib.HTTPConnection(url_host)

        logging.info('Sending request "%s %s"', method, relative_url)
        try:
            connection.putrequest(method, relative_url)

            if user_info is not None:
                email, admin = user_info
                auth_string = '%s=%s' % (dev_appserver_login.COOKIE_NAME,
                                         dev_appserver_login.CreateCookieData(
                                             email, admin))
                logging.info('Putting auth header: %s', auth_string)
                connection.putheader('Cookie', auth_string)

            if body is not None:
                connection.putheader('Content-length', len(body))

            for key, value in extra_headers:
                logging.info('Putting header: %s = %s', str(key), str(value))
                connection.putheader(str(key), str(value))

            connection.endheaders()

            if body is not None:
                connection.send(body)

            response = connection.getresponse()
            status = response.status
            content = response.read()
            headers = dict(response.getheaders())
            logging.info('Received response %s with content:\n%s', status,
                         content)

            return status, content, headers
        finally:
            connection.close()
    except (IOError, httplib.HTTPException, socket.error), e:
        logging.error('Encountered exception accessing HTTP server: %s', e)
        raise e