/** * Grabs a page from the internet * * @param WorldContextObject The current context * @param url The URL to request * * @return A pointer to the newly created post data */ UJsonFieldData* UJsonFieldData::GetRequest(UObject* WorldContextObject, const FString &url) { // Create new page data for the response UJsonFieldData* dataObj = Create(WorldContextObject); // Create the HTTP request TSharedRef< IHttpRequest > HttpRequest = FHttpModule::Get().CreateRequest(); HttpRequest->SetVerb("GET"); HttpRequest->SetURL(CreateURL(url)); HttpRequest->OnProcessRequestComplete().BindUObject(dataObj, &UJsonFieldData::OnReady); // Execute the request HttpRequest->ProcessRequest(); // Return the page data return dataObj; }
char *SSL_Open(URL *Url) { char *msg=NULL; char *proxy=NULL,*sproxy=NULL; int socksremotedns=0; socksdata_t *socksdata=NULL,socksbuf; /* Sort out the host. */ if(!IsLocalNetHost(Url->host)) { proxy=ConfigStringURL(SSLProxy,Url); sproxy=ConfigStringURL(SocksProxy,Url); socksremotedns=ConfigBooleanURL(SocksRemoteDNS,Url); } if(proxyUrl) { FreeURL(proxyUrl); proxyUrl=NULL; } if(proxy) Url=proxyUrl=CreateURL("http",proxy,"/",NULL,NULL,NULL); /* Open the connection. */ server=-1; if(Url->portnum) { if((sproxy && !(socksdata= MakeSocksData(sproxy,socksremotedns,&socksbuf))) || (server=OpenUrlSocket(Url,socksdata))==-1) { msg=GetPrintMessage(Warning,"Cannot open the https (SSL) connection to %s port %d; [%!s].",Url->host,Url->portnum); } else { init_io(server); configure_io_timeout_rw(server,ConfigInteger(SocketTimeout)); } } else msg=GetPrintMessage(Warning,"No port given for the https (SSL) connection to %s.",Url->host); return(msg); }
char *HTTP_Open(URL *Url) { char *msg=NULL; char *proxy=NULL; char *server_host=NULL; int server_port=-1; /* Sort out the host. */ if(!IsLocalNetHost(Url->host)) proxy=ConfigStringURL(Proxies,Url); if(proxy) { if(proxyUrl) FreeURL(proxyUrl); proxyUrl=NULL; proxyUrl=CreateURL("http",proxy,"/",NULL,NULL,NULL); server_host=proxyUrl->host; server_port=proxyUrl->port; } else { server_host=Url->host; server_port=Url->port; } if(server_port==-1) server_port=DefaultPort(Url); /* Open the connection. */ server=OpenClientSocket(server_host,server_port); if(server==-1) msg=GetPrintMessage(Warning,"Cannot open the HTTP connection to %s port %d; [%!s].",server_host,server_port); else { init_io(server); configure_io_timeout(server,ConfigInteger(SocketTimeout),ConfigInteger(SocketTimeout)); } return(msg); }
/** * Posts the current request data to the internet * * @param WorldContextObject The current context * @param url The URL to post to * */ void UJsonFieldData::PostRequest(UObject* WorldContextObject, const FString &url) { FString outStr; TSharedRef<TJsonWriter<TCHAR>> JsonWriter = TJsonWriterFactory<TCHAR>::Create(&outStr); // Start writing the response WriteObject(JsonWriter, "", new FJsonValueObject(Data)); JsonWriter->Close(); // Log the post data for the user (OPTIONAL) UE_LOG(LogTemp, Warning, TEXT("Post data: %s"), *outStr); // Create the post request with the generated data TSharedRef< IHttpRequest > HttpRequest = FHttpModule::Get().CreateRequest(); HttpRequest->SetVerb("POST"); HttpRequest->SetURL(CreateURL(url)); HttpRequest->SetHeader("Content-Type", "application/json"); HttpRequest->SetContentAsString(outStr); HttpRequest->OnProcessRequestComplete().BindUObject(this, &UJsonFieldData::OnReady); // Execute the request HttpRequest->ProcessRequest(); }
URL *SplitURL(const char *url) { URL *Url; char *proto,*user,*pass,*hostport,*path,*args; char *copyurl,*mallocurl=malloc(strlen(url)+2); char *colon,*slash,*at,*ques,*hash; copyurl=mallocurl; strcpy(copyurl,url); /* Remove any fragment identifiers */ hash=strchr(copyurl,'#'); if(hash) *hash=0; /* Protocol = Url->proto */ colon=strchr(copyurl,':'); slash=strchr(copyurl,'/'); at =strchr(copyurl,'@'); if(slash==copyurl) /* /dir/... (local) */ { proto="http"; } else if(colon && slash && (colon+1)==slash) /* http:/[/]... */ { *colon=0; proto=copyurl; copyurl=slash+1; if(*copyurl=='/') copyurl++; colon=strchr(copyurl,':'); slash=strchr(copyurl,'/'); } else if(colon && !isdigit(*(colon+1)) && (!slash || colon<slash) && (!at || (slash && at>slash))) /* http:www.foo/...[:@]... */ { *colon=0; proto=copyurl; copyurl=colon+1; colon=strchr(copyurl,':'); } else /* www.foo:80/... */ { proto="http"; } /* Username, Password = Url->user, Url->pass */ if(at && at>copyurl && (!slash || slash>at)) { char *at2; if(colon && at>colon) /* user:[email protected]...[/]... */ { *colon=0; user=copyurl; *at=0; pass=colon+1; copyurl=at+1; } else if(colon && (at2=strchr(at+1,'@')) && /* user@host:[email protected]...[/]... */ at2>colon && at2<slash) /* [not actually valid, but common] */ { *colon=0; user=copyurl; *at2=0; pass=colon+1; copyurl=at2+1; } else /* [email protected]...[:/]... */ { *at=0; user=copyurl; pass=NULL; copyurl=at+1; } } else { if(at==copyurl) /* @www.foo... */ copyurl++; user=NULL; pass=NULL; } /* Arguments = Url->args */ ques=strchr(copyurl,'?'); if(ques) /* ../path?... */ { *ques++=0; args=ques; } else args=NULL; /* Pathname = Url->path */ slash=strchr(copyurl,'/'); if(slash) /* /path/... (local) */ /* www.foo/...[?]... */ { path=(char*)malloc(strlen(slash)+1); strcpy(path,slash); *slash=0; } else /* www.foo[?]... */ path="/"; /* Hostname:port = Url->hostport */ if(*copyurl) /* www.foo */ hostport=copyurl; else /* /path/... (local) */ hostport=GetLocalHostPort(); /* Create the URL */ Url=CreateURL(proto,hostport,path,args,user,pass); /* Tidy up */ free(mallocurl); if(hostport!=copyurl) free(hostport); if(slash) free(path); return(Url); }