Added REST-handler interface for the http server and changed the Admin Web front end to one.

This commit is contained in:
MW
2007-03-27 09:35:03 +00:00
parent a4fc6b5fbb
commit 39c7fe5ec7
9 changed files with 258 additions and 184 deletions

View File

@@ -48,17 +48,13 @@ namespace OpenSim.CAPS
{
public Thread HTTPD;
public HttpListener Listener;
private string AdminPage;
private string NewAccountForm;
private string LoginForm;
private string passWord = "Admin";
private Dictionary<string, IRestHandler> restHandlers = new Dictionary<string, IRestHandler>();
public SimCAPSHTTPServer()
{
OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Starting up HTTP Server");
HTTPD = new Thread(new ThreadStart(StartHTTP));
HTTPD.Start();
LoadAdminPage();
}
public void StartHTTP()
@@ -84,7 +80,18 @@ namespace OpenSim.CAPS
}
}
private string ParseXMLRPC(string requestBody)
public bool AddRestHandler(string path, IRestHandler handler)
{
if (!this.restHandlers.ContainsKey(path))
{
this.restHandlers.Add(path, handler);
return true;
}
//must already have a handler for that path so return false
return false;
}
protected virtual string ParseXMLRPC(string requestBody)
{
try
{
@@ -116,104 +123,34 @@ namespace OpenSim.CAPS
return "";
}
private string ParseREST(string requestBody, string requestURL, string requestMethod)
protected virtual string ParseREST(string requestBody, string requestURL, string requestMethod)
{
string[] path;
string pathDelimStr = "/";
char[] pathDelimiter = pathDelimStr.ToCharArray();
path = requestURL.Split(pathDelimiter);
string responseString = "";
try
//path[0] should be empty so we are interested in path[1]
if (path.Length > 1)
{
switch (requestURL)
if ((path[1] != "") && (this.restHandlers.ContainsKey(path[1])))
{
case "/Admin/Accounts":
if (requestMethod == "GET")
{
responseString = "<p> Account management </p>";
responseString += "<br> ";
responseString += "<p> Create New Account </p>";
responseString += NewAccountForm;
}
break;
case "/Admin/Clients":
if (requestMethod == "GET")
{
responseString = " <p> Listing connected Clients </p>";
OpenSim.world.Avatar TempAv;
foreach (libsecondlife.LLUUID UUID in OpenSimRoot.Instance.LocalWorld.Entities.Keys)
{
if (OpenSimRoot.Instance.LocalWorld.Entities[UUID].ToString() == "OpenSim.world.Avatar")
{
TempAv = (OpenSim.world.Avatar)OpenSimRoot.Instance.LocalWorld.Entities[UUID];
responseString += "<p>";
responseString += String.Format("{0,-16}{1,-16}{2,-25}{3,-25}{4,-16},{5,-16}", TempAv.firstname, TempAv.lastname, UUID, TempAv.ControllingClient.SessionID, TempAv.ControllingClient.CircuitCode, TempAv.ControllingClient.userEP.ToString());
responseString += "</p>";
}
}
}
break;
case "/Admin/NewAccount":
if (requestMethod == "POST")
{
string[] comp = new string[10];
string[] passw = new string[3];
string delimStr = "&";
char[] delimiter = delimStr.ToCharArray();
string delimStr2 = "=";
char[] delimiter2 = delimStr2.ToCharArray();
//Console.WriteLine(requestBody);
comp = requestBody.Split(delimiter);
passw = comp[3].Split(delimiter2);
if (passw[1] == passWord)
{
responseString = "<p> New Account created </p>";
}
else
{
responseString = "<p> Admin password is incorrect, please login with the correct password</p>";
responseString += "<br><br>" + LoginForm;
}
}
break;
case "/Admin/Login":
if (requestMethod == "POST")
{
// Console.WriteLine(requestBody);
if (requestBody == passWord)
{
responseString = "<p> Login Successful </p>";
}
else
{
responseString = "<p> Password Error </p>";
responseString += "<p> Please Login with the correct password </p>";
responseString += "<br><br> " + LoginForm;
}
}
break;
case "/Admin/Welcome":
if (requestMethod == "GET")
{
responseString = "Welcome to the OpenSim Admin Page";
responseString += "<br><br><br> " + LoginForm;
}
break;
responseString = this.restHandlers[path[1]].HandleREST(requestBody, requestURL, requestMethod);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
return responseString;
}
private string ParseLLSDXML(string requestBody)
protected virtual string ParseLLSDXML(string requestBody)
{
// dummy function for now - IMPLEMENT ME!
return "";
}
public void HandleRequest(Object stateinfo)
public virtual void HandleRequest(Object stateinfo)
{
// Console.WriteLine("new http incoming");
HttpListenerContext context = (HttpListenerContext)stateinfo;
@@ -258,17 +195,9 @@ namespace OpenSim.CAPS
break;
case null:
if ((request.HttpMethod == "GET") && (request.RawUrl == "/Admin"))
{
responseString = AdminPage;
response.AddHeader("Content-type", "text/html");
}
else
{
// must be REST or invalid crap, so pass to the REST parser
responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod);
response.AddHeader("Content-type", "text/html");
}
// must be REST or invalid crap, so pass to the REST parser
responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod);
response.AddHeader("Content-type", "text/html");
break;
}
@@ -280,52 +209,6 @@ namespace OpenSim.CAPS
output.Write(buffer, 0, buffer.Length);
output.Close();
}
private void LoadAdminPage()
{
try
{
StreamReader SR;
string lines;
AdminPage = "";
NewAccountForm = "";
LoginForm = "";
SR = File.OpenText("testadmin.htm");
while (!SR.EndOfStream)
{
lines = SR.ReadLine();
AdminPage += lines + "\n";
}
SR.Close();
SR = File.OpenText("newaccountform.htm");
while (!SR.EndOfStream)
{
lines = SR.ReadLine();
NewAccountForm += lines + "\n";
}
SR.Close();
SR = File.OpenText("login.htm");
while (!SR.EndOfStream)
{
lines = SR.ReadLine();
LoginForm += lines + "\n";
}
SR.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}