Exemplo n.º 1
0
    def add_request(self,
                    method,
                    relative_url,
                    headers,
                    body,
                    source_ip,
                    server_name=None,
                    version=None,
                    instance_id=None):
        """Process an HTTP request.

    Args:
      method: A str containing the HTTP method of the request.
      relative_url: A str containing path and query string of the request.
      headers: A list of (key, value) tuples where key and value are both str.
      body: A str containing the request body.
      source_ip: The source ip address for the request.
      server_name: An optional str containing the server name to service this
          request. If unset, the request will be dispatched to the default
          server.
      version: An optional str containing the version to service this request.
          If unset, the request will be dispatched to the default version.
      instance_id: An optional str containing the instance_id of the instance to
          service this request. If unset, the request will be dispatched to
          according to the load-balancing for the server and version.

    Returns:
      A request_info.ResponseTuple containing the response information for the
      HTTP request.
    """
        try:
            header_dict = wsgiref.headers.Headers(headers)
            connection_host = header_dict.get('host')
            connection = httplib.HTTPConnection(connection_host)

            connection.putrequest(method,
                                  relative_url,
                                  skip_host='host' in header_dict,
                                  skip_accept_encoding='accept-encoding'
                                  in header_dict)

            for header_key, header_value in headers:
                connection.putheader(header_key, header_value)
            connection.endheaders()
            connection.send(body)

            response = connection.getresponse()
            response.read()
            response.close()

            return request_info.ResponseTuple(
                '%d %s' % (response.status, response.reason), [], '')
        except (httplib.HTTPException, socket.error):
            logging.exception(
                'An error occured while sending a %s request to "%s%s"',
                method, connection_host, relative_url)
            return request_info.ResponseTuple('0', [], '')
Exemplo n.º 2
0
    def add_request(self,
                    method,
                    relative_url,
                    headers,
                    body,
                    source_ip,
                    module_name=None,
                    version=None,
                    instance_id=None):
        requests_headers = structures.CaseInsensitiveDict()
        for k, v in headers:

            requests_headers[six.ensure_str(k)] = v
        requests_headers['X-AppEngine-User-IP'] = source_ip
        requests_headers['X-AppEngine-Fake-Is-Admin'] = '1'
        if 'host' in requests_headers:
            hostname = requests_headers['host']
        else:
            hostname = self.get_hostname(module_name, version)
        response = requests.request(method,
                                    'http://' + hostname +
                                    six.ensure_str(relative_url),
                                    headers=requests_headers,
                                    data=body)

        return request_info.ResponseTuple(response.status_code,
                                          response.headers, response.content)
Exemplo n.º 3
0
  def add_request(self, method, relative_url, headers, body, source_ip,
                  module_name=None, version=None, instance_id=None,
                  fake_login=False):
    """Process an HTTP request.

    Args:
      method: A str containing the HTTP method of the request.
      relative_url: A str containing path and query string of the request.
      headers: A list of (key, value) tuples where key and value are both str.
      body: A str containing the request body.
      source_ip: The source ip address for the request.
      module_name: An optional str containing the module name to service this
          request. If unset, the request will be dispatched according to the
          host header and relative_url.
      version: An optional str containing the version to service this request.
          If unset, the request will be dispatched according to the host header
          and relative_url.
      instance_id: An optional str containing the instance_id of the instance to
          service this request. If unset, the request will be dispatched
          according to the host header and relative_url and, if applicable, the
          load-balancing for the module and version.
      fake_login: A bool indicating whether login checks should be bypassed,
          i.e. "login: required" should be ignored for this request.

    Returns:
      A request_info.ResponseTuple containing the response information for the
      HTTP request.
    """
    if module_name:
      _module = self._get_module_with_soft_routing(module_name, version)
      inst = _module.get_instance(instance_id) if instance_id else None
    else:
      headers_dict = wsgiref.headers.Headers(headers)
      _module, inst = self._resolve_target(
          headers_dict['Host'], urlparse.urlsplit(relative_url).path)
    if inst:
      try:
        port = _module.get_instance_port(inst.instance_id)
      except request_info.NotSupportedWithAutoScalingError:
        port = _module.balanced_port
    else:
      port = _module.balanced_port
    environ = _module.build_request_environ(method, relative_url, headers, body,
                                            source_ip, port,
                                            fake_login=fake_login)
    start_response = start_response_utils.CapturingStartResponse()
    response = self._handle_request(environ,
                                    start_response,
                                    _module,
                                    inst)

    # merged_response can have side effects which modify start_response.*, so
    # we cannot safely inline it into the ResponseTuple initialization below.
    merged = start_response.merged_response(response)
    return request_info.ResponseTuple(start_response.status,
                                      start_response.response_headers,
                                      merged)
Exemplo n.º 4
0
 def add_request(self,
                 method,
                 relative_url,
                 headers,
                 body,
                 source_ip,
                 module_name=None,
                 version=None,
                 instance_id=None):
     request = urllib2.Request(url=self.java_app_base_url + relative_url,
                               data=body,
                               headers=dict(headers))
     response = urllib2.urlopen(request)
     return request_info_lib.ResponseTuple(str(response.getcode()), [], '')