def SetupEnvironment(cgi_path,
                     relative_url,
                     headers,
                     infile,
                     split_url=SplitURL,
                     get_user_info=dev_appserver_login.GetUserInfo):
  env = DEFAULT_ENV.copy()

  script_name, query_string = split_url(relative_url)




  env['_AH_ENCODED_SCRIPT_NAME'] = script_name
  env['SCRIPT_NAME'] = ''
  env['QUERY_STRING'] = query_string
  env['PATH_INFO'] = urllib.unquote(script_name)
  env['PATH_TRANSLATED'] = cgi_path
  env['CONTENT_TYPE'] = headers.getheader('content-type',
                                          'application/x-www-form-urlencoded')
  env['CONTENT_LENGTH'] = headers.getheader('content-length', '')

  cookies = ', '.join(headers.getheaders('cookie'))
 
  # AppScale
  # Here we check to see if the cookie is valid, if it is then set the 
  # email, user ID, and if they are an administrator. If not, then 
  # do not set the user credentials.
  email_addr, user_id, admin, valid_cookie = get_user_info(cookies)

  if valid_cookie:
    env['USER_EMAIL'] = email_addr
    env['USER_ID'] = user_id
    env['USER_NICKNAME'] = user_id
  else:
    env['USER_EMAIL'] = ""
    env['USER_ID'] = ""
    env['USER_NICKNAME'] = ""

  env['USER_IS_ADMIN'] = '0'
  if admin and valid_cookie:
    env['USER_IS_ADMIN'] = '1'

  if env['AUTH_DOMAIN'] == '*':

    auth_domain = 'gmail.com'
    parts = email_addr.split('@')
    if len(parts) == 2 and parts[1]:
      auth_domain = parts[1]
    env['AUTH_DOMAIN'] = auth_domain

  env['REQUEST_LOG_ID'] = _GenerateRequestLogId()
  env['REQUEST_ID_HASH'] = _generate_request_id_hash()


  for key in headers:
    if key in _IGNORE_REQUEST_HEADERS:
      continue
    adjusted_name = key.replace('-', '_').upper()
    env['HTTP_' + adjusted_name] = ', '.join(headers.getheaders(key))




  if DEVEL_PAYLOAD_HEADER in env:
    del env[DEVEL_PAYLOAD_HEADER]
    new_data = base64.standard_b64decode(infile.getvalue())
    infile.seek(0)
    infile.truncate()
    infile.write(new_data)
    infile.seek(0)
    env['CONTENT_LENGTH'] = str(len(new_data))



  if DEVEL_FAKE_IS_ADMIN_HEADER in env:
    del env[DEVEL_FAKE_IS_ADMIN_HEADER]

  token = GetGoogleSqlOAuth2RefreshToken(os.path.expanduser(
      rdbms.OAUTH_CREDENTIALS_PATH))
  if token:
    env['GOOGLE_SQL_OAUTH2_REFRESH_TOKEN'] = token

  return env
Sets up environment variables for a CGI.
dataset/ETHPy150Open AppScale/appscale/AppServer/google/appengine/tools/dev_appserver.py/SetupEnvironment