aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorlbsa712007-03-29 19:05:34 +0000
committerlbsa712007-03-29 19:05:34 +0000
commit514a3230563e19708588945f90e81760377f1265 (patch)
tree89c2b0c9d05b10b4a470bc9834cc7f4aef89047a
parent* Tried to make the sandbox/useraccount sim setup a bit simpler (diff)
downloadopensim-SC_OLD-514a3230563e19708588945f90e81760377f1265.zip
opensim-SC_OLD-514a3230563e19708588945f90e81760377f1265.tar.gz
opensim-SC_OLD-514a3230563e19708588945f90e81760377f1265.tar.bz2
opensim-SC_OLD-514a3230563e19708588945f90e81760377f1265.tar.xz
* RestMethod now uses same pattern as XmlRpcMethod
* Made /Admin use RestMethod * HttpServer is now a mini-webapp-server yay!
-rw-r--r--OpenSim.RegionServer/CAPS/AdminWebFront.cs221
-rw-r--r--OpenSim.RegionServer/OpenSimMain.cs3
-rw-r--r--Servers/BaseHttpServer.cs37
-rw-r--r--Servers/IRestHandler.cs5
4 files changed, 132 insertions, 134 deletions
diff --git a/OpenSim.RegionServer/CAPS/AdminWebFront.cs b/OpenSim.RegionServer/CAPS/AdminWebFront.cs
index 8224050..3596ffa 100644
--- a/OpenSim.RegionServer/CAPS/AdminWebFront.cs
+++ b/OpenSim.RegionServer/CAPS/AdminWebFront.cs
@@ -4,10 +4,11 @@ using System.Text;
4using System.IO; 4using System.IO;
5using OpenSim.world; 5using OpenSim.world;
6using OpenSim.UserServer; 6using OpenSim.UserServer;
7using OpenSim.Servers;
7 8
8namespace OpenSim.CAPS 9namespace OpenSim.CAPS
9{ 10{
10 public class AdminWebFront : IRestHandler 11 public class AdminWebFront
11 { 12 {
12 private string AdminPage; 13 private string AdminPage;
13 private string NewAccountForm; 14 private string NewAccountForm;
@@ -24,126 +25,124 @@ namespace OpenSim.CAPS
24 LoadAdminPage(); 25 LoadAdminPage();
25 } 26 }
26 27
27 public string HandleREST(string requestBody, string requestURL, string requestMethod) 28 public void LoadMethods( BaseHttpServer server )
28 { 29 {
29 string responseString = ""; 30 server.AddRestHandler("GET", "/Admin", GetAdminPage);
30 try 31 server.AddRestHandler("GET", "/Admin/Welcome", GetWelcomePage);
32 server.AddRestHandler("GET", "/Admin/Accounts", GetAccountsPage );
33 server.AddRestHandler("GET", "/Admin/Clients", GetConnectedClientsPage );
34
35 server.AddRestHandler("POST", "/Admin/NewAccount", PostNewAccount );
36 server.AddRestHandler("POST", "/Admin/Login", PostLogin );
37 }
38
39 private string GetWelcomePage( string request )
40 {
41 string responseString;
42 responseString = "Welcome to the OpenSim Admin Page";
43 responseString += "<br><br><br> " + LoginForm;
44 return responseString;
45 }
46
47 private string PostLogin(string requestBody)
48 {
49 string responseString;
50// Console.WriteLine(requestBody);
51 if (requestBody == passWord)
52 {
53 responseString = "<p> Login Successful </p>";
54 }
55 else
56 {
57 responseString = "<p> Password Error </p>";
58 responseString += "<p> Please Login with the correct password </p>";
59 responseString += "<br><br> " + LoginForm;
60 }
61 return responseString;
62 }
63
64 private string PostNewAccount(string requestBody)
65 {
66 string responseString;
67 string firstName = "";
68 string secondName = "";
69 string userPasswd = "";
70 string[] comp;
71 string[] passw;
72 string[] line;
73 string delimStr = "&";
74 char[] delimiter = delimStr.ToCharArray();
75 string delimStr2 = "=";
76 char[] delimiter2 = delimStr2.ToCharArray();
77
78 //Console.WriteLine(requestBody);
79 comp = requestBody.Split(delimiter);
80 passw = comp[3].Split(delimiter2);
81 if (passw[1] == passWord)
31 { 82 {
32 switch (requestURL)
33 {
34 case "/Admin":
35 if (requestMethod == "GET")
36 {
37 responseString = AdminPage;
38 }
39 break;
40 case "/Admin/Accounts":
41 if (requestMethod == "GET")
42 {
43 responseString = "<p> Account management </p>";
44 responseString += "<br> ";
45 responseString += "<p> Create New Account </p>";
46 responseString += NewAccountForm;
47 }
48 break;
49 case "/Admin/Clients":
50 if (requestMethod == "GET")
51 {
52 responseString = " <p> Listing connected Clients </p>";
53 OpenSim.world.Avatar TempAv;
54 foreach (libsecondlife.LLUUID UUID in m_world.Entities.Keys)
55 {
56 if (m_world.Entities[UUID].ToString() == "OpenSim.world.Avatar")
57 {
58 TempAv = (OpenSim.world.Avatar)m_world.Entities[UUID];
59 responseString += "<p>";
60 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());
61 responseString += "</p>";
62 }
63 }
64 }
65 break;
66 case "/Admin/NewAccount":
67 if (requestMethod == "POST")
68 {
69 string firstName = "";
70 string secondName = "";
71 string userPasswd = "";
72 string[] comp;
73 string[] passw;
74 string[] line;
75 string delimStr = "&";
76 char[] delimiter = delimStr.ToCharArray();
77 string delimStr2 = "=";
78 char[] delimiter2 = delimStr2.ToCharArray();
79
80 //Console.WriteLine(requestBody);
81 comp = requestBody.Split(delimiter);
82 passw = comp[3].Split(delimiter2);
83 if (passw[1] == passWord)
84 {
85 83
86 line = comp[0].Split(delimiter2); //split firstname 84 line = comp[0].Split(delimiter2); //split firstname
87 if (line.Length > 1) 85 if (line.Length > 1)
88 { 86 {
89 firstName = line[1]; 87 firstName = line[1];
90 }
91 line = comp[1].Split(delimiter2); //split secondname
92 if (line.Length > 1)
93 {
94 secondName = line[1];
95 }
96 line = comp[2].Split(delimiter2); //split user password
97 if (line.Length > 1)
98 {
99 userPasswd = line[1];
100 }
101 if (this._userServer != null)
102 {
103 this._userServer.CreateUserAccount(firstName, secondName, userPasswd);
104 }
105 responseString = "<p> New Account created </p>";
106 }
107 else
108 {
109 responseString = "<p> Admin password is incorrect, please login with the correct password</p>";
110 responseString += "<br><br>" + LoginForm;
111 }
112 }
113 break;
114 case "/Admin/Login":
115 if (requestMethod == "POST")
116 {
117 // Console.WriteLine(requestBody);
118 if (requestBody == passWord)
119 {
120 responseString = "<p> Login Successful </p>";
121 }
122 else
123 {
124 responseString = "<p> Password Error </p>";
125 responseString += "<p> Please Login with the correct password </p>";
126 responseString += "<br><br> " + LoginForm;
127 }
128 }
129 break;
130 case "/Admin/Welcome":
131 if (requestMethod == "GET")
132 {
133 responseString = "Welcome to the OpenSim Admin Page";
134 responseString += "<br><br><br> " + LoginForm;
135
136 }
137 break;
138 } 88 }
89 line = comp[1].Split(delimiter2); //split secondname
90 if (line.Length > 1)
91 {
92 secondName = line[1];
93 }
94 line = comp[2].Split(delimiter2); //split user password
95 if (line.Length > 1)
96 {
97 userPasswd = line[1];
98 }
99 if (this._userServer != null)
100 {
101 this._userServer.CreateUserAccount(firstName, secondName, userPasswd);
102 }
103 responseString = "<p> New Account created </p>";
139 } 104 }
140 catch (Exception e) 105 else
141 { 106 {
142 Console.WriteLine(e.ToString()); 107 responseString = "<p> Admin password is incorrect, please login with the correct password</p>";
108 responseString += "<br><br>" + LoginForm;
109 }
110 return responseString;
111 }
112
113 private string GetConnectedClientsPage( string request )
114 {
115 string responseString;
116 responseString = " <p> Listing connected Clients </p>";
117 OpenSim.world.Avatar TempAv;
118 foreach (libsecondlife.LLUUID UUID in m_world.Entities.Keys)
119 {
120 if (m_world.Entities[UUID].ToString() == "OpenSim.world.Avatar")
121 {
122 TempAv = (OpenSim.world.Avatar)m_world.Entities[UUID];
123 responseString += "<p>";
124 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());
125 responseString += "</p>";
126 }
143 } 127 }
144 return responseString; 128 return responseString;
145 } 129 }
146 130
131 private string GetAccountsPage( string request )
132 {
133 string responseString;
134 responseString = "<p> Account management </p>";
135 responseString += "<br> ";
136 responseString += "<p> Create New Account </p>";
137 responseString += NewAccountForm;
138 return responseString;
139 }
140
141 private string GetAdminPage( string request )
142 {
143 return AdminPage;
144 }
145
147 private void LoadAdminPage() 146 private void LoadAdminPage()
148 { 147 {
149 try 148 try
diff --git a/OpenSim.RegionServer/OpenSimMain.cs b/OpenSim.RegionServer/OpenSimMain.cs
index 51ec12b..36e1817 100644
--- a/OpenSim.RegionServer/OpenSimMain.cs
+++ b/OpenSim.RegionServer/OpenSimMain.cs
@@ -199,7 +199,8 @@ namespace OpenSim
199 } 199 }
200 } 200 }
201 201
202 HttpServer.AddRestHandler("Admin", new AdminWebFront("Admin", LocalWorld, adminLoginServer )); 202 AdminWebFront adminWebFront = new AdminWebFront("Admin", LocalWorld, adminLoginServer);
203 adminWebFront.LoadMethods( HttpServer );
203 204
204 m_console.WriteLine("Main.cs:Startup() - Starting HTTP server"); 205 m_console.WriteLine("Main.cs:Startup() - Starting HTTP server");
205 HttpServer.Start(); 206 HttpServer.Start();
diff --git a/Servers/BaseHttpServer.cs b/Servers/BaseHttpServer.cs
index bac7e86..7c2a195 100644
--- a/Servers/BaseHttpServer.cs
+++ b/Servers/BaseHttpServer.cs
@@ -14,7 +14,7 @@ namespace OpenSim.Servers
14 { 14 {
15 protected Thread m_workerThread; 15 protected Thread m_workerThread;
16 protected HttpListener m_httpListener; 16 protected HttpListener m_httpListener;
17 protected Dictionary<string, IRestHandler> m_restHandlers = new Dictionary<string, IRestHandler>(); 17 protected Dictionary<string, RestMethod> m_restHandlers = new Dictionary<string, RestMethod>();
18 protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>(); 18 protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>();
19 protected int m_port; 19 protected int m_port;
20 20
@@ -23,11 +23,13 @@ namespace OpenSim.Servers
23 m_port = port; 23 m_port = port;
24 } 24 }
25 25
26 public bool AddRestHandler(string path, IRestHandler handler) 26 public bool AddRestHandler(string method, string path, RestMethod handler)
27 { 27 {
28 if (!this.m_restHandlers.ContainsKey(path)) 28 string methodKey = String.Format("{0}: {1}", method, path);
29
30 if (!this.m_restHandlers.ContainsKey(methodKey))
29 { 31 {
30 this.m_restHandlers.Add(path, handler); 32 this.m_restHandlers.Add(methodKey, handler);
31 return true; 33 return true;
32 } 34 }
33 35
@@ -69,25 +71,24 @@ namespace OpenSim.Servers
69 return XmlRpcResponseSerializer.Singleton.Serialize(response); 71 return XmlRpcResponseSerializer.Singleton.Serialize(response);
70 } 72 }
71 73
72 protected virtual string ParseREST(string requestBody, string requestURL, string requestMethod) 74 protected virtual string ParseREST(string request, string path, string method)
73 { 75 {
74 string[] path; 76 string response;
75 string pathDelimStr = "/"; 77 RestMethod handler;
76 char[] pathDelimiter = pathDelimStr.ToCharArray(); 78
77 path = requestURL.Split(pathDelimiter); 79 string methodKey = String.Format("{0}: {1}", method, path);
78
79 string responseString = "";
80 80
81 //path[0] should be empty so we are interested in path[1] 81 if (m_restHandlers.TryGetValue(methodKey, out handler))
82 if (path.Length > 1)
83 { 82 {
84 if ((path[1] != "") && (this.m_restHandlers.ContainsKey(path[1]))) 83 response = handler(request);
85 { 84
86 responseString = this.m_restHandlers[path[1]].HandleREST(requestBody, requestURL, requestMethod); 85 }
87 } 86 else
87 {
88 response = String.Empty;
88 } 89 }
89 90
90 return responseString; 91 return response;
91 } 92 }
92 93
93 protected virtual string ParseLLSDXML(string requestBody) 94 protected virtual string ParseLLSDXML(string requestBody)
diff --git a/Servers/IRestHandler.cs b/Servers/IRestHandler.cs
index f269600..deb77d8 100644
--- a/Servers/IRestHandler.cs
+++ b/Servers/IRestHandler.cs
@@ -4,8 +4,5 @@ using System.Text;
4 4
5namespace OpenSim.CAPS 5namespace OpenSim.CAPS
6{ 6{
7 public interface IRestHandler 7 public delegate string RestMethod( string request );
8 {
9 string HandleREST(string requestBody, string requestURL, string requestMethod);
10 }
11} 8}