aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim.Servers/BaseHttpServer.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim.Servers/BaseHttpServer.cs')
-rw-r--r--OpenSim.Servers/BaseHttpServer.cs167
1 files changed, 85 insertions, 82 deletions
diff --git a/OpenSim.Servers/BaseHttpServer.cs b/OpenSim.Servers/BaseHttpServer.cs
index a14e5c3..38f4370 100644
--- a/OpenSim.Servers/BaseHttpServer.cs
+++ b/OpenSim.Servers/BaseHttpServer.cs
@@ -26,14 +26,14 @@ namespace OpenSim.Servers
26 { 26 {
27 get { return m_restMethod; } 27 get { return m_restMethod; }
28 } 28 }
29 29
30 public RestMethodEntry( string path, RestMethod restMethod ) 30 public RestMethodEntry(string path, RestMethod restMethod)
31 { 31 {
32 m_path = path; 32 m_path = path;
33 m_restMethod = restMethod; 33 m_restMethod = restMethod;
34 } 34 }
35 } 35 }
36 36
37 protected Thread m_workerThread; 37 protected Thread m_workerThread;
38 protected HttpListener m_httpListener; 38 protected HttpListener m_httpListener;
39 protected Dictionary<string, RestMethodEntry> m_restHandlers = new Dictionary<string, RestMethodEntry>(); 39 protected Dictionary<string, RestMethodEntry> m_restHandlers = new Dictionary<string, RestMethodEntry>();
@@ -48,10 +48,10 @@ namespace OpenSim.Servers
48 public bool AddRestHandler(string method, string path, RestMethod handler) 48 public bool AddRestHandler(string method, string path, RestMethod handler)
49 { 49 {
50 string methodKey = String.Format("{0}: {1}", method, path); 50 string methodKey = String.Format("{0}: {1}", method, path);
51 51
52 if (!this.m_restHandlers.ContainsKey(methodKey)) 52 if (!this.m_restHandlers.ContainsKey(methodKey))
53 { 53 {
54 this.m_restHandlers.Add(methodKey, new RestMethodEntry( path, handler )); 54 this.m_restHandlers.Add(methodKey, new RestMethodEntry(path, handler));
55 return true; 55 return true;
56 } 56 }
57 57
@@ -74,9 +74,9 @@ namespace OpenSim.Servers
74 protected virtual string ProcessXMLRPCMethod(string methodName, XmlRpcRequest request) 74 protected virtual string ProcessXMLRPCMethod(string methodName, XmlRpcRequest request)
75 { 75 {
76 XmlRpcResponse response; 76 XmlRpcResponse response;
77 77
78 XmlRpcMethod method; 78 XmlRpcMethod method;
79 if( this.m_rpcHandlers.TryGetValue( methodName, out method ) ) 79 if (this.m_rpcHandlers.TryGetValue(methodName, out method))
80 { 80 {
81 response = method(request); 81 response = method(request);
82 } 82 }
@@ -96,15 +96,15 @@ namespace OpenSim.Servers
96 protected virtual string ParseREST(string request, string path, string method) 96 protected virtual string ParseREST(string request, string path, string method)
97 { 97 {
98 string response; 98 string response;
99 99
100 string requestKey = String.Format("{0}: {1}", method, path); 100 string requestKey = String.Format("{0}: {1}", method, path);
101 101
102 string bestMatch = String.Empty; 102 string bestMatch = String.Empty;
103 foreach( string currentKey in m_restHandlers.Keys ) 103 foreach (string currentKey in m_restHandlers.Keys)
104 { 104 {
105 if( requestKey.StartsWith( currentKey )) 105 if (requestKey.StartsWith(currentKey))
106 { 106 {
107 if(currentKey.Length > bestMatch.Length ) 107 if (currentKey.Length > bestMatch.Length)
108 { 108 {
109 bestMatch = currentKey; 109 bestMatch = currentKey;
110 } 110 }
@@ -116,9 +116,9 @@ namespace OpenSim.Servers
116 { 116 {
117 RestMethod restMethod = restMethodEntry.RestMethod; 117 RestMethod restMethod = restMethodEntry.RestMethod;
118 118
119 string param = path.Substring( restMethodEntry.Path.Length ); 119 string param = path.Substring(restMethodEntry.Path.Length);
120 response = restMethod(request, path, param); 120 response = restMethod(request, path, param);
121 121
122 } 122 }
123 else 123 else
124 { 124 {
@@ -144,7 +144,7 @@ namespace OpenSim.Servers
144 144
145 string methodName = request.MethodName; 145 string methodName = request.MethodName;
146 146
147 responseString = ProcessXMLRPCMethod(methodName, request ); 147 responseString = ProcessXMLRPCMethod(methodName, request);
148 } 148 }
149 catch (Exception e) 149 catch (Exception e)
150 { 150 {
@@ -155,83 +155,86 @@ namespace OpenSim.Servers
155 155
156 public virtual void HandleRequest(Object stateinfo) 156 public virtual void HandleRequest(Object stateinfo)
157 { 157 {
158 try { 158 try
159 HttpListenerContext context = (HttpListenerContext)stateinfo; 159 {
160 160 HttpListenerContext context = (HttpListenerContext)stateinfo;
161 HttpListenerRequest request = context.Request; 161
162 HttpListenerResponse response = context.Response; 162 HttpListenerRequest request = context.Request;
163 163 HttpListenerResponse response = context.Response;
164 response.KeepAlive = false; 164
165 response.SendChunked = false; 165 response.KeepAlive = false;
166 166 response.SendChunked = false;
167 System.IO.Stream body = request.InputStream; 167
168 System.Text.Encoding encoding = System.Text.Encoding.UTF8; 168 System.IO.Stream body = request.InputStream;
169 System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding); 169 System.Text.Encoding encoding = System.Text.Encoding.UTF8;
170 170 System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
171 string requestBody = reader.ReadToEnd(); 171
172 body.Close(); 172 string requestBody = reader.ReadToEnd();
173 reader.Close(); 173 body.Close();
174 174 reader.Close();
175 //Console.WriteLine(request.HttpMethod + " " + request.RawUrl + " Http/" + request.ProtocolVersion.ToString() + " content type: " + request.ContentType); 175
176 //Console.WriteLine(requestBody); 176 //Console.WriteLine(request.HttpMethod + " " + request.RawUrl + " Http/" + request.ProtocolVersion.ToString() + " content type: " + request.ContentType);
177 177 //Console.WriteLine(requestBody);
178 string responseString = ""; 178
179 switch (request.ContentType) 179 string responseString = "";
180 { 180 switch (request.ContentType)
181 case "text/xml": 181 {
182 // must be XML-RPC, so pass to the XML-RPC parser 182 case "text/xml":
183 183 // must be XML-RPC, so pass to the XML-RPC parser
184 responseString = ParseXMLRPC(requestBody); 184
185 responseString = Regex.Replace(responseString, "utf-16", "utf-8"); 185 responseString = ParseXMLRPC(requestBody);
186 186 responseString = Regex.Replace(responseString, "utf-16", "utf-8");
187 response.AddHeader("Content-type", "text/xml"); 187
188 break; 188 response.AddHeader("Content-type", "text/xml");
189 189 break;
190 case "application/xml": 190
191 // probably LLSD we hope, otherwise it should be ignored by the parser 191 case "application/xml":
192 responseString = ParseLLSDXML(requestBody); 192 // probably LLSD we hope, otherwise it should be ignored by the parser
193 response.AddHeader("Content-type", "application/xml"); 193 responseString = ParseLLSDXML(requestBody);
194 break; 194 response.AddHeader("Content-type", "application/xml");
195 195 break;
196 case "application/x-www-form-urlencoded": 196
197 // a form data POST so send to the REST parser 197 case "application/x-www-form-urlencoded":
198 responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); 198 // a form data POST so send to the REST parser
199 response.AddHeader("Content-type", "text/html"); 199 responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod);
200 break; 200 response.AddHeader("Content-type", "text/html");
201 201 break;
202 case null: 202
203 // must be REST or invalid crap, so pass to the REST parser 203 case null:
204 responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); 204 // must be REST or invalid crap, so pass to the REST parser
205 response.AddHeader("Content-type", "text/html"); 205 responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod);
206 break; 206 response.AddHeader("Content-type", "text/html");
207 207 break;
208 } 208
209 209 }
210 byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); 210
211 System.IO.Stream output = response.OutputStream; 211 byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
212 response.SendChunked = false; 212 System.IO.Stream output = response.OutputStream;
213 response.ContentLength64 = buffer.Length; 213 response.SendChunked = false;
214 output.Write(buffer, 0, buffer.Length); 214 response.ContentLength64 = buffer.Length;
215 output.Close(); 215 output.Write(buffer, 0, buffer.Length);
216 } catch (Exception e) { 216 output.Close();
217 Console.WriteLine(e.ToString()); 217 }
218 } 218 catch (Exception e)
219 {
220 Console.WriteLine(e.ToString());
221 }
219 } 222 }
220 223
221 public void Start() 224 public void Start()
222 { 225 {
223 OpenSim.Framework.Console.MainConsole.Instance.WriteLine(LogPriority.LOW,"BaseHttpServer.cs: Starting up HTTP Server"); 226 OpenSim.Framework.Console.MainConsole.Instance.WriteLine(LogPriority.LOW, "BaseHttpServer.cs: Starting up HTTP Server");
224 227
225 m_workerThread = new Thread(new ThreadStart(StartHTTP)); 228 m_workerThread = new Thread(new ThreadStart(StartHTTP));
226 m_workerThread.IsBackground = true; 229 m_workerThread.IsBackground = true;
227 m_workerThread.Start(); 230 m_workerThread.Start();
228 } 231 }
229 232
230 private void StartHTTP() 233 private void StartHTTP()
231 { 234 {
232 try 235 try
233 { 236 {
234 OpenSim.Framework.Console.MainConsole.Instance.WriteLine(LogPriority.LOW,"BaseHttpServer.cs: StartHTTP() - Spawned main thread OK"); 237 OpenSim.Framework.Console.MainConsole.Instance.WriteLine(LogPriority.LOW, "BaseHttpServer.cs: StartHTTP() - Spawned main thread OK");
235 m_httpListener = new HttpListener(); 238 m_httpListener = new HttpListener();
236 239
237 m_httpListener.Prefixes.Add("http://+:" + m_port + "/"); 240 m_httpListener.Prefixes.Add("http://+:" + m_port + "/");
@@ -246,7 +249,7 @@ namespace OpenSim.Servers
246 } 249 }
247 catch (Exception e) 250 catch (Exception e)
248 { 251 {
249 OpenSim.Framework.Console.MainConsole.Instance.WriteLine(LogPriority.MEDIUM,e.Message); 252 OpenSim.Framework.Console.MainConsole.Instance.WriteLine(LogPriority.MEDIUM, e.Message);
250 } 253 }
251 } 254 }
252 } 255 }