diff options
Diffstat (limited to '')
-rw-r--r-- | OpenGridServices.UserServer/UserHttp.cs | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/OpenGridServices.UserServer/UserHttp.cs b/OpenGridServices.UserServer/UserHttp.cs new file mode 100644 index 0000000..ce3cfcd --- /dev/null +++ b/OpenGridServices.UserServer/UserHttp.cs | |||
@@ -0,0 +1,146 @@ | |||
1 | /* | ||
2 | Copyright (c) OpenGrid project, http://osgrid.org/ | ||
3 | |||
4 | |||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions are met: | ||
9 | * * Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * * Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * * Neither the name of the <organization> nor the | ||
15 | * names of its contributors may be used to endorse or promote products | ||
16 | * derived from this software without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY | ||
19 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
21 | * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY | ||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | using System; | ||
31 | using System.Text; | ||
32 | using Nwc.XmlRpc; | ||
33 | using System.Threading; | ||
34 | using System.Text.RegularExpressions; | ||
35 | using System.Net; | ||
36 | using System.IO; | ||
37 | using System.Collections; | ||
38 | using System.Collections.Generic; | ||
39 | using libsecondlife; | ||
40 | using ServerConsole; | ||
41 | using OpenSim.Framework.User; | ||
42 | using OpenSim.Framework.Sims; | ||
43 | using OpenSim.Framework.Inventory; | ||
44 | |||
45 | namespace OpenGridServices | ||
46 | { | ||
47 | public class UserHTTPServer { | ||
48 | public Thread HTTPD; | ||
49 | public HttpListener Listener; | ||
50 | |||
51 | public UserHTTPServer() { | ||
52 | ServerConsole.MainConsole.Instance.WriteLine("Starting up HTTP Server"); | ||
53 | HTTPD = new Thread(new ThreadStart(StartHTTP)); | ||
54 | HTTPD.Start(); | ||
55 | } | ||
56 | |||
57 | public void StartHTTP() { | ||
58 | ServerConsole.MainConsole.Instance.WriteLine("UserHttp.cs:StartHTTP() - Spawned main thread OK"); | ||
59 | Listener = new HttpListener(); | ||
60 | |||
61 | Listener.Prefixes.Add("http://+:8002/userserver/"); | ||
62 | Listener.Prefixes.Add("http://+:8002/usersessions/"); | ||
63 | Listener.Start(); | ||
64 | |||
65 | HttpListenerContext context; | ||
66 | while(true) { | ||
67 | context = Listener.GetContext(); | ||
68 | ThreadPool.QueueUserWorkItem(new WaitCallback(HandleRequest), context); | ||
69 | } | ||
70 | } | ||
71 | |||
72 | static string ParseXMLRPC(string requestBody) { | ||
73 | return OpenGridServices.OpenUser_Main.userserver._profilemanager.ParseXMLRPC(requestBody); | ||
74 | } | ||
75 | |||
76 | static string ParseREST(HttpListenerRequest www_req) { | ||
77 | Console.WriteLine("INCOMING REST - " + www_req.RawUrl); | ||
78 | |||
79 | char[] splitter = {'/'}; | ||
80 | string[] rest_params = www_req.RawUrl.Split(splitter); | ||
81 | string req_type = rest_params[1]; // First part of the URL is the type of request - usersessions/userprofiles/inventory/blabla | ||
82 | switch(req_type) { | ||
83 | case "usersessions": | ||
84 | LLUUID sessionid = new LLUUID(rest_params[2]); // get usersessions/sessionid | ||
85 | if(www_req.HttpMethod=="DELETE") { | ||
86 | foreach (libsecondlife.LLUUID UUID in OpenUser_Main.userserver._profilemanager.UserProfiles.Keys) { | ||
87 | if(OpenUser_Main.userserver._profilemanager.UserProfiles[UUID].CurrentSessionID==sessionid) { | ||
88 | OpenUser_Main.userserver._profilemanager.UserProfiles[UUID].CurrentSessionID=null; | ||
89 | OpenUser_Main.userserver._profilemanager.UserProfiles[UUID].CurrentSecureSessionID=null; | ||
90 | OpenUser_Main.userserver._profilemanager.UserProfiles[UUID].Circuits.Clear(); | ||
91 | } | ||
92 | } | ||
93 | |||
94 | } | ||
95 | return "OK"; | ||
96 | break; | ||
97 | } | ||
98 | |||
99 | return ""; | ||
100 | } | ||
101 | |||
102 | |||
103 | static void HandleRequest(Object stateinfo) { | ||
104 | HttpListenerContext context=(HttpListenerContext)stateinfo; | ||
105 | |||
106 | HttpListenerRequest request = context.Request; | ||
107 | HttpListenerResponse response = context.Response; | ||
108 | |||
109 | response.KeepAlive=false; | ||
110 | response.SendChunked=false; | ||
111 | |||
112 | System.IO.Stream body = request.InputStream; | ||
113 | System.Text.Encoding encoding = System.Text.Encoding.UTF8; | ||
114 | System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding); | ||
115 | |||
116 | string requestBody = reader.ReadToEnd(); | ||
117 | body.Close(); | ||
118 | reader.Close(); | ||
119 | |||
120 | string responseString=""; | ||
121 | switch(request.ContentType) { | ||
122 | case "text/xml": | ||
123 | // must be XML-RPC, so pass to the XML-RPC parser | ||
124 | |||
125 | responseString=ParseXMLRPC(requestBody); | ||
126 | response.AddHeader("Content-type","text/xml"); | ||
127 | break; | ||
128 | |||
129 | case "text/plaintext": | ||
130 | responseString=ParseREST(request); | ||
131 | response.AddHeader("Content-type","text/plaintext"); | ||
132 | break; | ||
133 | } | ||
134 | |||
135 | |||
136 | byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); | ||
137 | System.IO.Stream output = response.OutputStream; | ||
138 | response.SendChunked=false; | ||
139 | response.ContentLength64=buffer.Length; | ||
140 | output.Write(buffer,0,buffer.Length); | ||
141 | output.Close(); | ||
142 | } | ||
143 | } | ||
144 | |||
145 | |||
146 | } | ||