UBrowser
Class UBrowserHTTPClient

source: e:\games\UnrealTournament\UBrowser\Classes\UBrowserHTTPClient.uc
Core.Object
   |
   +--Engine.Actor
      |
      +--Engine.Info
         |
         +--Engine.InternetInfo
            |
            +--IpDrv.InternetLink
               |
               +--IpDrv.TcpLink
                  |
                  +--UBrowser.UBrowserBufferedTcpLink
                     |
                     +--UBrowser.UBrowserHTTPClient
Direct Known Subclasses:UBrowserUpdateServerLink

class UBrowserHTTPClient
extends UBrowser.UBrowserBufferedTcpLink


Variables
 int CurrentState
 int ErrorCode
 string ProxyServerAddress
 int ProxyServerPort
 string ServerAddress
 IpAddr ServerIpAddr
 int ServerPort
 string ServerURI
 bool bClosed


Function Summary
 void Browse(string InAddress, string InURI, optional int, optional int)
 void DoBind()
 void HTTPError(int Code)
 void HTTPReceivedData(string Data)
 void PostBeginPlay()
 void Resolved(IpAddr Addr)
 void SetError(int Code)



Source Code


00001	class UBrowserHTTPClient extends UBrowserBufferedTcpLink;
00002	
00003	var IpAddr		ServerIpAddr;
00004	var string		ServerAddress;
00005	var string		ServerURI;
00006	var int			ServerPort;
00007	var int			CurrentState;
00008	var int			ErrorCode;
00009	var bool		bClosed;
00010	
00011	var globalconfig string	ProxyServerAddress;
00012	var globalconfig int	ProxyServerPort;
00013	
00014	const Connecting		= 0;
00015	const WaitingForHeader	= 1;
00016	const ReceivingHeader	= 2;
00017	const ReceivingData		= 3;
00018	const HadError			= 4;
00019	
00020	function PostBeginPlay()
00021	{
00022		Super.PostBeginPlay();
00023		ServerIpAddr.Addr = 0;
00024		Disable('Tick');
00025	}
00026	
00027	function Browse(string InAddress, string InURI, optional int InPort, optional int InTimeout)
00028	{
00029		CurrentState = Connecting;
00030	
00031		ServerAddress = InAddress;
00032		ServerURI = InURI;
00033		if(InPort == 0)
00034			ServerPort = 80;
00035		else
00036			ServerPort = InPort;
00037		
00038		if(InTimeout > 0 )
00039			SetTimer(InTimeout, False);
00040	
00041		ResetBuffer();
00042	
00043		if(ProxyServerAddress != "")
00044		{
00045			ServerIpAddr.Port = ProxyServerPort;
00046			if(ServerIpAddr.Addr == 0)
00047				Resolve( ProxyServerAddress );
00048			else
00049				DoBind();
00050		}
00051		else
00052		{
00053			ServerIpAddr.Port = ServerPort;
00054			if(ServerIpAddr.Addr == 0)
00055				Resolve( ServerAddress );
00056			else
00057				DoBind();
00058		}
00059	}
00060	
00061	function Resolved( IpAddr Addr )
00062	{
00063		// Set the address
00064		ServerIpAddr.Addr = Addr.Addr;
00065	
00066		if( ServerIpAddr.Addr == 0 )
00067		{
00068			Log( "UBrowserHTTPClient: Invalid server address" );
00069			SetError(-1);
00070			return;
00071		}
00072		
00073		DoBind();
00074	}
00075	
00076	function DoBind()
00077	{
00078		if( BindPort() == 0 )
00079		{
00080			Log( "UBrowserHTTPLink: Error binding local port." );
00081			SetError(-2);
00082			return;
00083		}
00084	
00085		Open( ServerIpAddr );
00086		bClosed = False;
00087	}
00088	
00089	event Timer()
00090	{
00091		SetError(-3);	
00092	}
00093	
00094	event Opened()
00095	{
00096		Enable('Tick');
00097		if(ProxyServerAddress != "")
00098			SendBufferedData("GET http://"$ServerAddress$":"$string(ServerPort)$ServerURI$" HTTP/1.1"$CR$LF);
00099		else
00100			SendBufferedData("GET "$ServerURI$" HTTP/1.1"$CR$LF);
00101		SendBufferedData("User-Agent: Unreal"$CR$LF);
00102		SendBufferedData("Connection: close"$CR$LF);
00103		SendBufferedData("Host: "$ServerAddress$":"$ServerPort$CR$LF$CR$LF);
00104	
00105		CurrentState = WaitingForHeader;
00106	}
00107	
00108	function SetError(int Code)
00109	{
00110		Disable('Tick');
00111		SetTimer(0, False);
00112		ResetBuffer();
00113	
00114		CurrentState = HadError;
00115		ErrorCode = Code;
00116	
00117		if(!IsConnected() || !Close())
00118			HTTPError(ErrorCode);
00119	}
00120	
00121	event Closed()
00122	{
00123		bClosed = True;
00124	}
00125	
00126	function HTTPReceivedData(string Data)
00127	{
00128	}
00129	
00130	function HTTPError(int Code)
00131	{
00132	}
00133	
00134	event Tick(float DeltaTime)
00135	{
00136		local string Line;
00137		local bool bGotData;
00138		local int NextState;
00139		local int i;
00140		local int Result;
00141	
00142		Super.Tick(DeltaTime);
00143		DoBufferQueueIO();
00144	
00145		do
00146		{
00147			NextState = CurrentState;
00148			switch(CurrentState)
00149			{
00150			case WaitingForHeader:
00151				bGotData = ReadBufferedLine(Line);
00152				if(bGotData)
00153				{
00154					i = InStr(Line, " ");
00155					Result = Int(Mid(Line, i+1));
00156					if(Result != 200)
00157					{
00158						SetError(Result);
00159						return;
00160					}
00161						
00162					NextState = ReceivingHeader;
00163				}	
00164				break;
00165			case ReceivingHeader:
00166				bGotData = ReadBufferedLine(Line);
00167				if(bGotData)
00168				{
00169					if(Line == "")
00170						NextState = ReceivingData;
00171				}	
00172				break;
00173			case ReceivingData:
00174				bGotData = False;
00175				break;
00176			default:
00177				bGotData = False;
00178				break;
00179			}
00180			CurrentState = NextState;
00181		} until(!bGotData);
00182	
00183		if(bClosed)
00184		{
00185			Disable('Tick');
00186			if(CurrentState == ReceivingData)
00187				HTTPReceivedData(InputBuffer);
00188	
00189			if(CurrentState == HadError)
00190				HTTPError(ErrorCode);
00191		}
00192	}
00193	
00194	defaultproperties
00195	{
00196	}

End Source Code