Esempio n. 1
0
int				put_env_in_dll(char **envp, t_execution *exe)
{
  int				i;

  i = -1;
  if (!(exe->env->env_dll = init_dll()))
    return (FAILURE);
  while (envp[++i])
    if (add_env_variable(exe->env->env_dll, my_strdup(envp[i])) == FAILURE)
      return (FAILURE);
  if (put_env_in_tab(exe->env) == FAILURE)
    return (FAILURE);
  return (SUCCESS);
}
Esempio n. 2
0
void init_superglobals( lua_State* L ) 
{
    // Create and fillup the _SERVER table
    lua_newtable( L );
    {
        add_env_variable( L, "SOFTWARE", "SERVER_SOFTWARE" );
        add_env_variable( L, "NAME", "SERVER_NAME" );
        add_env_variable( L, "GATEWAY_INTERFACE", "GATEWAY_INTERFACE" );
        add_env_variable( L, "PROTOCOL", "SERVER_PROTOCOL" );
        add_env_variable( L, "PORT", "SERVER_PORT" );
        add_env_variable( L, "REQUEST_METHOD", "REQUEST_METHOD" );
        add_env_variable( L, "PATH_INFO", "PATH_INFO" );
        add_env_variable( L, "PATH_TRANSLATED", "PATH_TRANSLATED" );
        add_env_variable( L, "SCRIPT_NAME", "SCRIPT_NAME" );
        add_env_variable( L, "QUERY_STRING", "QUERY_STRING" );
        add_env_variable( L, "REMOTE_HOST", "REMOTE_HOST" );
        add_env_variable( L, "REMOTE_ADDR", "REMOTE_ADDR" );
        add_env_variable( L, "AUTH_TYPE", "AUTH_TYPE" );
        add_env_variable( L, "REMOTE_USER", "REMOTE_USER" );
        add_env_variable( L, "REMOTE_IDENT", "REMOTE_IDENT" );
        add_env_variable( L, "CONTENT_TYPE", "CONTENT_TYPE" );
        add_env_variable( L, "CONTENT_LENGTH", "CONTENT_LENGTH" );
    }
    lua_setglobal( L, "_SERVER" );

    // Create and fillup _GET table
    lua_newtable( L );
    DEBUGLOG( "Decoding query_string" );
    {
        decode_url_parameter_string( L, getenv( "QUERY_STRING" ) );
    }
    lua_setglobal( L, "_GET" );

    // Create and fillup _POST table
    lua_newtable( L );
    DEBUGLOG( "Decoding post data" );
    {        
        char* postdata       = NULL;
        int readBytes        = 0;
        int overallReadBytes = 0;
        char* contentType    = getenv( "CONTENT_TYPE" );

        if ( contentType != NULL && !strcmp( contentType, "application/x-www-form-urlencoded" ) ) 
        {
            postdata = smalloc( sizeof( char ) * 1024 );
            memset( postdata, 0, 1024 );
            
            while( ( readBytes = fread( postdata + overallReadBytes, sizeof( char ), 1023, stdin ) ) != 0 ) 
            {
                overallReadBytes += readBytes;
                postdata = srealloc( postdata, sizeof( char ) * ( overallReadBytes + 1024 ) );
                memset( postdata + overallReadBytes, 0, 1024 );
            }

            decode_url_parameter_string( L, postdata );
            free( postdata );
        }
    }
    lua_setglobal( L, "_POST" );

    // Create and fillup _HEADER table
    lua_newtable( L );
    DEBUGLOG( "Setting _HEADER" );
    {
       char** env;
       // Loop through all defined environment variables
       for( env = environ; *env; env++ ) 
       {      
           int i;
           int len;
           char* envdata;

           if( strstr( *env, "HTTP_" ) != *env ) 
           {
               // The variable name does not start with HTTP_
               continue;
           }
           
           // Split at the equal sign
           envdata = strdup( *env );
           len = strlen( envdata );
           for( i = 5; i < len; i++ ) 
           {
               if ( envdata[i] == '=' ) 
               {
                   envdata[i] = 0;
                   break;
               }
           }
           
           // Push the key, without the HTTP_
           lua_pushstring( L, envdata + 5 );
           // Push the data
           lua_pushstring( L, envdata + strlen(envdata) + 1 );
           // Add the table entry
           lua_settable( L, -3 );

           // Free the temporary string
           free( envdata );
       }
    }
    lua_setglobal( L, "_HEADER" );
}