* RestMethod now uses same pattern as XmlRpcMethod

* Made /Admin use RestMethod
* HttpServer is now a mini-webapp-server yay!
0.1-prestable
lbsa71 2007-03-29 19:05:34 +00:00
parent 761db20c5f
commit 514a323056
4 changed files with 134 additions and 136 deletions

View File

@ -4,10 +4,11 @@ using System.Text;
using System.IO; using System.IO;
using OpenSim.world; using OpenSim.world;
using OpenSim.UserServer; using OpenSim.UserServer;
using OpenSim.Servers;
namespace OpenSim.CAPS namespace OpenSim.CAPS
{ {
public class AdminWebFront : IRestHandler public class AdminWebFront
{ {
private string AdminPage; private string AdminPage;
private string NewAccountForm; private string NewAccountForm;
@ -24,48 +25,45 @@ namespace OpenSim.CAPS
LoadAdminPage(); LoadAdminPage();
} }
public string HandleREST(string requestBody, string requestURL, string requestMethod) public void LoadMethods( BaseHttpServer server )
{ {
string responseString = ""; server.AddRestHandler("GET", "/Admin", GetAdminPage);
try server.AddRestHandler("GET", "/Admin/Welcome", GetWelcomePage);
{ server.AddRestHandler("GET", "/Admin/Accounts", GetAccountsPage );
switch (requestURL) server.AddRestHandler("GET", "/Admin/Clients", GetConnectedClientsPage );
{
case "/Admin": server.AddRestHandler("POST", "/Admin/NewAccount", PostNewAccount );
if (requestMethod == "GET") server.AddRestHandler("POST", "/Admin/Login", PostLogin );
{
responseString = AdminPage;
} }
break;
case "/Admin/Accounts": private string GetWelcomePage( string request )
if (requestMethod == "GET")
{ {
responseString = "<p> Account management </p>"; string responseString;
responseString += "<br> "; responseString = "Welcome to the OpenSim Admin Page";
responseString += "<p> Create New Account </p>"; responseString += "<br><br><br> " + LoginForm;
responseString += NewAccountForm; return responseString;
} }
break;
case "/Admin/Clients": private string PostLogin(string requestBody)
if (requestMethod == "GET")
{ {
responseString = " <p> Listing connected Clients </p>"; string responseString;
OpenSim.world.Avatar TempAv; // Console.WriteLine(requestBody);
foreach (libsecondlife.LLUUID UUID in m_world.Entities.Keys) if (requestBody == passWord)
{ {
if (m_world.Entities[UUID].ToString() == "OpenSim.world.Avatar") responseString = "<p> Login Successful </p>";
{
TempAv = (OpenSim.world.Avatar)m_world.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>";
} }
} else
}
break;
case "/Admin/NewAccount":
if (requestMethod == "POST")
{ {
responseString = "<p> Password Error </p>";
responseString += "<p> Please Login with the correct password </p>";
responseString += "<br><br> " + LoginForm;
}
return responseString;
}
private string PostNewAccount(string requestBody)
{
string responseString;
string firstName = ""; string firstName = "";
string secondName = ""; string secondName = "";
string userPasswd = ""; string userPasswd = "";
@ -109,41 +107,42 @@ namespace OpenSim.CAPS
responseString = "<p> Admin password is incorrect, please login with the correct password</p>"; responseString = "<p> Admin password is incorrect, please login with the correct password</p>";
responseString += "<br><br>" + LoginForm; responseString += "<br><br>" + LoginForm;
} }
return responseString;
} }
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;
} private string GetConnectedClientsPage( string request )
break;
}
}
catch (Exception e)
{ {
Console.WriteLine(e.ToString()); string responseString;
responseString = " <p> Listing connected Clients </p>";
OpenSim.world.Avatar TempAv;
foreach (libsecondlife.LLUUID UUID in m_world.Entities.Keys)
{
if (m_world.Entities[UUID].ToString() == "OpenSim.world.Avatar")
{
TempAv = (OpenSim.world.Avatar)m_world.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>";
}
} }
return responseString; return responseString;
} }
private string GetAccountsPage( string request )
{
string responseString;
responseString = "<p> Account management </p>";
responseString += "<br> ";
responseString += "<p> Create New Account </p>";
responseString += NewAccountForm;
return responseString;
}
private string GetAdminPage( string request )
{
return AdminPage;
}
private void LoadAdminPage() private void LoadAdminPage()
{ {
try try

View File

@ -199,7 +199,8 @@ namespace OpenSim
} }
} }
HttpServer.AddRestHandler("Admin", new AdminWebFront("Admin", LocalWorld, adminLoginServer )); AdminWebFront adminWebFront = new AdminWebFront("Admin", LocalWorld, adminLoginServer);
adminWebFront.LoadMethods( HttpServer );
m_console.WriteLine("Main.cs:Startup() - Starting HTTP server"); m_console.WriteLine("Main.cs:Startup() - Starting HTTP server");
HttpServer.Start(); HttpServer.Start();

View File

@ -14,7 +14,7 @@ namespace OpenSim.Servers
{ {
protected Thread m_workerThread; protected Thread m_workerThread;
protected HttpListener m_httpListener; protected HttpListener m_httpListener;
protected Dictionary<string, IRestHandler> m_restHandlers = new Dictionary<string, IRestHandler>(); protected Dictionary<string, RestMethod> m_restHandlers = new Dictionary<string, RestMethod>();
protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>(); protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>();
protected int m_port; protected int m_port;
@ -23,11 +23,13 @@ namespace OpenSim.Servers
m_port = port; m_port = port;
} }
public bool AddRestHandler(string path, IRestHandler handler) public bool AddRestHandler(string method, string path, RestMethod handler)
{ {
if (!this.m_restHandlers.ContainsKey(path)) string methodKey = String.Format("{0}: {1}", method, path);
if (!this.m_restHandlers.ContainsKey(methodKey))
{ {
this.m_restHandlers.Add(path, handler); this.m_restHandlers.Add(methodKey, handler);
return true; return true;
} }
@ -69,25 +71,24 @@ namespace OpenSim.Servers
return XmlRpcResponseSerializer.Singleton.Serialize(response); return XmlRpcResponseSerializer.Singleton.Serialize(response);
} }
protected virtual string ParseREST(string requestBody, string requestURL, string requestMethod) protected virtual string ParseREST(string request, string path, string method)
{ {
string[] path; string response;
string pathDelimStr = "/"; RestMethod handler;
char[] pathDelimiter = pathDelimStr.ToCharArray();
path = requestURL.Split(pathDelimiter);
string responseString = ""; string methodKey = String.Format("{0}: {1}", method, path);
//path[0] should be empty so we are interested in path[1] if (m_restHandlers.TryGetValue(methodKey, out handler))
if (path.Length > 1)
{ {
if ((path[1] != "") && (this.m_restHandlers.ContainsKey(path[1]))) response = handler(request);
{
responseString = this.m_restHandlers[path[1]].HandleREST(requestBody, requestURL, requestMethod);
} }
else
{
response = String.Empty;
} }
return responseString; return response;
} }
protected virtual string ParseLLSDXML(string requestBody) protected virtual string ParseLLSDXML(string requestBody)

View File

@ -4,8 +4,5 @@ using System.Text;
namespace OpenSim.CAPS namespace OpenSim.CAPS
{ {
public interface IRestHandler public delegate string RestMethod( string request );
{
string HandleREST(string requestBody, string requestURL, string requestMethod);
}
} }