def GetSchemeHostPort(environ):
  """Reconstructs scheme, host and port from wsgi environment.

  Args:
    environ: environment variables dictionary.
  Returns:
    reconstructed part of URI: scheme://host:port.
  """
  url = "{}://".format(environ["wsgi.url_scheme"])

  if environ.get("HTTP_HOST"):
    url += environ["HTTP_HOST"]
  else:
    url += environ["SERVER_NAME"]

    if environ["wsgi.url_scheme"] == "https":
      if environ["SERVER_PORT"] != "443":
        url += ":{}".format(environ["SERVER_PORT"])
    else:
      if environ["SERVER_PORT"] != "80":
        url += ":{}".format(environ["SERVER_PORT"])

  return url