Core.Object | +--Engine.Actor | +--Engine.Info | +--Engine.InternetInfo | +--IpDrv.InternetLink | +--IpDrv.TcpLink | +--UWeb.WebServer
WebApplication
ApplicationObjects[10]
string
ApplicationPaths[10]
Applications[10]
int
ConnectionCount
DefaultApplication
ListenPort
MaxConnections
ServerName
ServerURL
bool
bEnabled
void
BeginPlay()
GetApplication(string URI, out string)
00001 class WebServer expands TcpLink; 00002 00003 var config string Applications[10]; 00004 var config string ApplicationPaths[10]; 00005 var config int ListenPort; 00006 var config int MaxConnections; 00007 var config string ServerName; 00008 var config bool bEnabled; 00009 var config int DefaultApplication; 00010 00011 var string ServerURL; 00012 var WebApplication ApplicationObjects[10]; 00013 00014 var int ConnectionCount; 00015 00016 function BeginPlay() 00017 { 00018 local int i; 00019 local class<WebApplication> ApplicationClass; 00020 local IpAddr l; 00021 local string s; 00022 00023 if(!bEnabled) 00024 { 00025 Log("Webserver is not enabled. Set bEnabled to True in Advanced Options."); 00026 Destroy(); 00027 return; 00028 } 00029 00030 Super.BeginPlay(); 00031 00032 for(i=0;i<10;i++) 00033 { 00034 if(Applications[i] == "") 00035 break; 00036 00037 ApplicationClass = class<WebApplication>(DynamicLoadObject(Applications[i], class'Class')); 00038 if(ApplicationClass != None) 00039 { 00040 ApplicationObjects[i] = New(None) ApplicationClass; 00041 ApplicationObjects[i].Level = Level; 00042 ApplicationObjects[i].WebServer = Self; 00043 ApplicationObjects[i].Path = ApplicationPaths[i]; 00044 ApplicationObjects[i].Init(); 00045 } 00046 } 00047 00048 if(ServerName == "") 00049 { 00050 GetLocalIP(l); 00051 s = IpAddrToString(l); 00052 i = InStr(s, ":"); 00053 if(i != -1) 00054 s = Left(s, i); 00055 ServerURL = "http://"$s; 00056 } 00057 else 00058 ServerURL = "http://"$ServerName; 00059 00060 if(ListenPort != 80) 00061 ServerURL = ServerURL $ ":"$string(ListenPort); 00062 00063 BindPort( ListenPort ); 00064 Listen(); 00065 } 00066 00067 event Destroyed() 00068 { 00069 local int i; 00070 00071 for(i=0;i<10;i++) 00072 if(ApplicationObjects[i] != None) 00073 { 00074 ApplicationObjects[i].Cleanup(); 00075 ApplicationObjects[i].Level = None; 00076 ApplicationObjects[i].WebServer = None; 00077 ApplicationObjects[i] = None; 00078 } 00079 00080 Super.Destroyed(); 00081 } 00082 00083 event GainedChild( Actor C ) 00084 { 00085 Super.GainedChild(C); 00086 ConnectionCount++; 00087 00088 // if too many connections, close down listen. 00089 if(MaxConnections > 0 && ConnectionCount > MaxConnections && LinkState == STATE_Listening) 00090 { 00091 Log("WebServer: Too many connections - closing down Listen."); 00092 Close(); 00093 } 00094 } 00095 00096 event LostChild( Actor C ) 00097 { 00098 Super.LostChild(C); 00099 ConnectionCount--; 00100 00101 // if closed due to too many connections, start listening again. 00102 if(ConnectionCount <= MaxConnections && LinkState != STATE_Listening) 00103 { 00104 Log("WebServer: Listening again - connections have been closed."); 00105 Listen(); 00106 } 00107 } 00108 00109 function WebApplication GetApplication(string URI, out string SubURI) 00110 { 00111 local int i, l; 00112 00113 SubURI = ""; 00114 for(i=0;i<10;i++) 00115 { 00116 if(ApplicationPaths[i] != "") 00117 { 00118 l = Len(ApplicationPaths[i]); 00119 if(Left(URI, l) == ApplicationPaths[i] && (Len(URI) == l || Mid(URI, l, 1) == "/")) 00120 { 00121 SubURI = Mid(URI, l); 00122 return ApplicationObjects[i]; 00123 } 00124 } 00125 } 00126 return None; 00127 } 00128 00129 defaultproperties 00130 { 00131 Applications(0)="UTServerAdmin.UTServerAdmin" 00132 Applications(1)="UTServerAdmin.UTImageServer" 00133 ApplicationPaths(0)="/ServerAdmin" 00134 ApplicationPaths(1)="/images" 00135 ListenPort=80 00136 MaxConnections=30 00137 AcceptClass=Class'UWeb.WebConnection' 00138 }