Example #1
0
/**
 * @internal
 *
 * callback for SCGI server. 
 */
void ctr_request_serve_callback() {
	ctr_argument* argumentList;
	argumentList = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
	varlistGet = CGI_get_query(NULL);
	varlistCookie = CGI_get_cookie(NULL);
	varlistPost = CGI_get_post(NULL,"/tmp/_upXXXXXX");
	ctr_block_run(CtrStdSCGICB, argumentList, CtrStdSCGICB);
	ctr_heap_free( argumentList );
	CGI_free_varlist(varlistGet);
	CGI_free_varlist(varlistPost);
	CGI_free_varlist(varlistCookie);
}
Example #2
0
/**
 * @internal
 *
 * Adds the Request object to the World.
 */
void begin(){
	ctr_object* requestObject = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT);
	requestObject->link = CtrStdObject;
	ctr_internal_create_func(requestObject, ctr_build_string_from_cstring( "get:" ), &ctr_request_get_string );
	ctr_internal_create_func(requestObject, ctr_build_string_from_cstring( "getArray:" ), &ctr_request_get_array );
	ctr_internal_create_func(requestObject, ctr_build_string_from_cstring( "cookie:" ), &ctr_request_cookie_string );
	ctr_internal_create_func(requestObject, ctr_build_string_from_cstring( "cookieArray:" ), &ctr_request_cookie_array );
	ctr_internal_create_func(requestObject, ctr_build_string_from_cstring( "post:" ), &ctr_request_post_string );
	ctr_internal_create_func(requestObject, ctr_build_string_from_cstring( "file:" ), &ctr_request_file );
	ctr_internal_create_func(requestObject, ctr_build_string_from_cstring( "postArray:" ), &ctr_request_post_array );
	ctr_internal_create_func(requestObject, ctr_build_string_from_cstring( "serverOption:is:" ), &ctr_request_server_option );
	ctr_internal_create_func(requestObject, ctr_build_string_from_cstring( "host:listen:pid:callback:" ), &ctr_request_serve );
	ctr_internal_object_add_property(CtrStdWorld, ctr_build_string_from_cstring( "Request" ), requestObject, 0);
	varlistGet = CGI_get_query(NULL);
	varlistPost = CGI_get_post(NULL,0);
	varlistCookie = CGI_get_cookie(NULL);
}
Example #3
0
bool CWRequest::ReceiveFromWeb(string* pSessionID, string* pRequestID, bool* pThisIsCookieCheck, bool* pCookieCheckOk, bool* pLongPolling, bool* pKeepAlive, string* pErrorMessage)
{
    Clear();

    CGI_varlist* query = 0;
    CGI_varlist* post = 0;
    CGI_varlist* cookie = 0;

    string file_upload_template = TempDir() + "cgi-upload-XXXXXX";

    query = CGI_get_query(NULL);
    post = CGI_get_post(NULL, file_upload_template.c_str());
    cookie = CGI_get_cookie(NULL);

    if(query != 0)
    {
        const char *name = 0;
        CGI_value  *value = 0;
        for(name = CGI_first_name(query); name != 0; name = CGI_next_name(query))
        {
            value = CGI_lookup_all(query, 0);
            for(int i = 0; value[i] != 0; i++)
            {
                string name_value = "";
                name_value = name_value + name;
                name_value = name_value + "=";
                name_value = name_value + value[i];
                Query->Add(name_value);
            }
        }
    }

    if(post != 0)
    {
        const char *name = 0;
        CGI_value  *value = 0;
        for(name = CGI_first_name(post); name != 0; name = CGI_next_name(post))
        {
            value = CGI_lookup_all(post, 0);
            for(int i = 0; value[i] != 0; i++)
            {
                string name_value = "";
                name_value = name_value + name;
                name_value = name_value + "=";
                name_value = name_value + value[i];
                Post->Add(name_value);
            }
        }
    }

    if(cookie != 0)
    {
        const char *name = 0;
        CGI_value  *value = 0;
        for(name = CGI_first_name(cookie); name != 0; name = CGI_next_name(cookie))
        {
            value = CGI_lookup_all(cookie, 0);
            for(int i = 0; value[i] != 0; i++)
            {
                string name_value = "";
                name_value = name_value + name;
                name_value = name_value + "=";
                name_value = name_value + value[i];
                Cookie->Add(name_value);
            }
        }
    }

    CGI_free_varlist(cookie);
    CGI_free_varlist(post);
    CGI_free_varlist(query);

    // environment variables
    Env->SetValue("SERVER_NAME", GetEnv("SERVER_NAME"));
    Env->SetValue("SERVER_PORT", GetEnv("SERVER_PORT"));
    Env->SetValue("SCRIPT_NAME", GetEnv("SCRIPT_NAME"));
    Env->SetValue("REQUEST_METHOD", GetEnv("REQUEST_METHOD"));
    Env->SetValue("DOCUMENT_ROOT", GetEnv("DOCUMENT_ROOT"));
    Env->SetValue("REMOTE_ADDR", GetEnv("REMOTE_ADDR"));
    Env->SetValue("HTTP_USER_AGENT", GetEnv("HTTP_USER_AGENT"));
    Env->SetValue("HTTP_REFERER", GetEnv("HTTP_REFERER"));
    Env->SetValue("HTTP_COOKIE", GetEnv("HTTP_COOKIE"));
    Env->SetValue("CONTENT_TYPE", GetEnv("CONTENT_TYPE"));
    Env->SetValue("HTTPS", GetEnv("HTTPS"));

    // read and return session_id
    string session_id = Cookie->GetValue("session_id");
    string request_id = CreateRequestID();
    bool this_is_cookie_check = GetValue("cookie_check") == "1" || GetValue("cookie_check") == "true";
    DeleteNameValue("cookie_check");
    bool cookie_check_ok = Cookie->GetValue("cookie_check_data") == "123";
    bool long_polling = GetValue("long_polling") == "1" || GetValue("long_polling") == "true";
    bool keep_alive = GetValue("keep_alive") == "1" || GetValue("keep_alive") == "true";

    if(pSessionID != NULL) *pSessionID = session_id;
    if(pRequestID != NULL) *pRequestID = request_id;
    if(pThisIsCookieCheck != NULL) *pThisIsCookieCheck = this_is_cookie_check;
    if(pCookieCheckOk != NULL) *pCookieCheckOk = cookie_check_ok;
    if(pLongPolling != NULL) *pLongPolling = long_polling;
    if(pKeepAlive != NULL) *pKeepAlive = keep_alive;

    return true;
}
Example #4
0
int main(int argc, char **argv)
{
    void portable_OS_init();
    CGI_varlist *varlist; const char *name; char namebuf[512],postbuf[65536],*remoteaddr,*str=0,*retstr,*delim,*url = 0;
    int i,j,iter,localaccess=0,doneflag=0,portflag = 0; cJSON *json; long offset; CGI_value  *value; struct destbuf urlbuf;
    portable_OS_init();
    setenv("CONTENT_TYPE", "application/x-www-form-urlencoded", 1);
    json = cJSON_CreateObject();
    if ( (remoteaddr= getenv("REMOTE_ADDR")) == 0 || strncmp("127.0.0.1",remoteaddr,strlen("127.0.0.1")) == 0 )
        remoteaddr = 0,localaccess = 1;
    else cJSON_AddItemToObject(json,"remoteaddr",cJSON_CreateString(remoteaddr));
    for (i=j=0; argv[0][i]!=0; i++)
        if ( argv[0][i] == '/' || argv[0][i] == '\\' )
            j = i+1;
    strcpy(namebuf,&argv[0][j]);
    offset = strlen(namebuf) - 4;
    if ( offset > 0 && strcmp(".exe",namebuf + offset) == 0 )
        namebuf[offset] = 0;
    if ( offset > 0 && strcmp(".cgi",namebuf + offset) == 0 )
        namebuf[offset] = 0;
    if ( strcmp(namebuf,"init") == 0 || strcmp(namebuf,"") == 0 || strcmp(namebuf,"index.cgi") == 0 )
    {
        // "http://178.63.60.131/init/?requestType=status&coin=VRC"
        //"http://78.47.115.250:7777/public?plugin=relay&method=busdata&servicename=MGW&serviceNXT=8119557380101451968&destplugin=MGW&submethod=status&coin=BTC"
        if ( strcmp(namebuf,"api") != 0 )
            cJSON_AddItemToObject(json,"agent",cJSON_CreateString(namebuf));
        cJSON_AddItemToObject(json,"plugin",cJSON_CreateString("relay"));
        cJSON_AddItemToObject(json,"method",cJSON_CreateString("busdata"));
        cJSON_AddItemToObject(json,"servicename",cJSON_CreateString("MGW"));
        cJSON_AddItemToObject(json,"serviceNXT",cJSON_CreateString("8119557380101451968"));
        cJSON_AddItemToObject(json,"destplugin",cJSON_CreateString("MGW"));
        if ( jstr(json,"requestType") != 0 )
            cJSON_AddItemToObject(json,"submethod",cJSON_CreateString(jstr(json,"requestType")));
    }
    if ( strcmp("nxt",namebuf) == 0 )
    {
        if ( setnxturl(&urlbuf) != 0 )
            url = urlbuf.buf;
        else url = "http://127.0.0.1:7876/nxt";
    }
    else if ( strcmp("nxts",namebuf) == 0 )
        url = "https://127.0.0.1:7876/nxt";
    else if ( strcmp("port",namebuf) == 0 )
        url = "http://127.0.0.1", portflag = 1;
    else if ( strcmp("ports",namebuf) == 0 )
        url = "https://127.0.0.1", portflag = 1;
    fprintf(stderr,"namebuf.(%s)\n",namebuf);
    if ( url != 0 )
         postbuf[0] = 0, delim = "";
    for (iter=0; iter<3; iter++)
    {
        if ( (varlist= ((iter==0) ? CGI_get_post(0,0) : ((iter==1) ? CGI_get_query(0) : CGI_get_cookie(0)))) != 0 )
        {
            for (name=CGI_first_name(varlist); name!=0&&doneflag==0; name=CGI_next_name(varlist))
            {
                value = CGI_lookup_all(varlist,0);
                for (i=0; value[i]!=0; i++)
                {
                fprintf(stderr,"iter.%d %s [%d] = %s\r\n",iter,name,i,value[i]);
                    if ( i == 0 )
                    {
                        if ( url == 0 )
                        {
                            if ( strcmp(name,"stringified") == 0 || strcmp(namebuf,"stringified") == 0 )
                            {
                                char *unstringify(char *str);
                                cJSON *obj;
                                if ( (obj= cJSON_Parse(name)) == 0 )
                                {
                                    str = malloc(strlen(value[i])+1);
                                    strcpy(str,value[i]);
                                    unstringify(str);
                                    printf("unstringify (%s) -> (%s)\n",value[i],str);
                                    obj= cJSON_Parse(str);
                                }
                                if ( obj != 0 )
                                {
                                    //unstringified ((null)) -> ({"stringified":{"method":"orderbook","baseid":"12071612744977229797","relid":"5527630","maxdepth":"1"},"agent":"InstantDEX"})
                                    free_json(json);
                                    if ( jobj(obj,"stringified") != 0 )
                                        json = cJSON_Duplicate(jobj(obj,"stringified"),1), free_json(obj);
                                    else json = obj;
                                    cJSON_AddItemToObject(json,"agent",cJSON_CreateString("InstantDEX"));
                                    if ( remoteaddr != 0 && remoteaddr[0] != 0 )
                                        cJSON_AddItemToObject(json,"remoteaddr",cJSON_CreateString(remoteaddr));
                                    fprintf(stderr,"unstringified (%s) -> (%s)\n",str!=0?str:"",jprint(json,0));
                                    if ( str != 0 )
                                        free(str);
                                    doneflag = 1;
                                    break;
                                }
                            }
                            cJSON_AddItemToObject(json,name,cJSON_CreateString(value[i]));
                        }
                        else
                        {
                            if ( portflag != 0 && strncmp(name,"port",strlen("port")) == 0 )
                                sprintf(urlbuf.buf,"%s:%s",url,value[i]), url = urlbuf.buf, portflag = 0;
                            else sprintf(postbuf + strlen(postbuf),"%s%s=%s",delim,name,value[i]), delim = "&";
                        }
                    }
                }
            }
        }
        CGI_free_varlist(varlist);
    }
    if ( localaccess == 0 )
        fputs("Access-Control-Allow-Origin: *\r\n",stdout);
    else fputs("Access-Control-Allow-Origin: null\r\n",stdout);
    fputs("Access-Control-Allow-Credentials: true\r\n",stdout);
    fputs("Access-Control-Allow-Headers: Authorization, Content-Type\r\n",stdout);
    fputs("Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n",stdout);
    fputs("Cache-Control: no-cache, no-store, must-revalidate\r\n",stdout);
    fputs("Content-type: text/plain\r\n",stdout);
    if ( url != 0 )
    {
fprintf(stderr,"url.(%s) (%s)\n",url,postbuf);
        if ( (retstr= issue_POST(url,postbuf)) != 0 )
        {
            //fprintf(stderr,"%s",retstr);
            printf("Content-Length: %ld\r\n\r\n",strlen(retstr)+2);
            printf("%s\r\n",retstr);
            free(retstr);
        } else printf("{\"error\":\"null return from issue_NXTPOST\"}\r\n");
    }
    else
    {
        if ( jobj(json,"agent") == 0 && strcmp(namebuf,"api") != 0 )
            cJSON_AddItemToObject(json,"agent",cJSON_CreateString(namebuf));
        fprintf(stderr,"PROCESS.(%s)\n",jprint(json,0));
        process_json(json,remoteaddr,localaccess);
    }
    free_json(json);
    return 0;
}