2007-03-27 21:42:14 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2007-03-29 11:08:42 +00:00
|
|
|
using System.Net;
|
2007-03-27 21:42:14 +00:00
|
|
|
using System.Text;
|
2007-03-29 11:08:42 +00:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using System.Threading;
|
2007-03-30 10:14:15 +00:00
|
|
|
//using OpenSim.CAPS;
|
2007-03-29 11:08:42 +00:00
|
|
|
using Nwc.XmlRpc;
|
|
|
|
using System.Collections;
|
2007-05-12 15:19:03 +00:00
|
|
|
using OpenSim.Framework.Console;
|
2007-03-27 21:42:14 +00:00
|
|
|
|
|
|
|
namespace OpenSim.Servers
|
|
|
|
{
|
|
|
|
public class BaseHttpServer
|
|
|
|
{
|
2007-04-11 14:14:19 +00:00
|
|
|
protected class RestMethodEntry
|
|
|
|
{
|
|
|
|
private string m_path;
|
|
|
|
public string Path
|
|
|
|
{
|
|
|
|
get { return m_path; }
|
|
|
|
}
|
|
|
|
|
|
|
|
private RestMethod m_restMethod;
|
|
|
|
public RestMethod RestMethod
|
|
|
|
{
|
|
|
|
get { return m_restMethod; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public RestMethodEntry( string path, RestMethod restMethod )
|
|
|
|
{
|
|
|
|
m_path = path;
|
|
|
|
m_restMethod = restMethod;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-29 11:08:42 +00:00
|
|
|
protected Thread m_workerThread;
|
|
|
|
protected HttpListener m_httpListener;
|
2007-04-11 14:14:19 +00:00
|
|
|
protected Dictionary<string, RestMethodEntry> m_restHandlers = new Dictionary<string, RestMethodEntry>();
|
2007-03-29 11:08:42 +00:00
|
|
|
protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>();
|
|
|
|
protected int m_port;
|
|
|
|
|
|
|
|
public BaseHttpServer(int port)
|
|
|
|
{
|
|
|
|
m_port = port;
|
|
|
|
}
|
|
|
|
|
2007-03-29 19:05:34 +00:00
|
|
|
public bool AddRestHandler(string method, string path, RestMethod handler)
|
2007-03-29 11:08:42 +00:00
|
|
|
{
|
2007-03-29 19:05:34 +00:00
|
|
|
string methodKey = String.Format("{0}: {1}", method, path);
|
|
|
|
|
|
|
|
if (!this.m_restHandlers.ContainsKey(methodKey))
|
2007-03-29 11:08:42 +00:00
|
|
|
{
|
2007-04-11 14:14:19 +00:00
|
|
|
this.m_restHandlers.Add(methodKey, new RestMethodEntry( path, handler ));
|
2007-03-29 11:08:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//must already have a handler for that path so return false
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool AddXmlRPCHandler(string method, XmlRpcMethod handler)
|
|
|
|
{
|
|
|
|
if (!this.m_rpcHandlers.ContainsKey(method))
|
|
|
|
{
|
|
|
|
this.m_rpcHandlers.Add(method, handler);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//must already have a handler for that path so return false
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual string ProcessXMLRPCMethod(string methodName, XmlRpcRequest request)
|
|
|
|
{
|
|
|
|
XmlRpcResponse response;
|
|
|
|
|
|
|
|
XmlRpcMethod method;
|
|
|
|
if( this.m_rpcHandlers.TryGetValue( methodName, out method ) )
|
|
|
|
{
|
|
|
|
response = method(request);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response = new XmlRpcResponse();
|
|
|
|
Hashtable unknownMethodError = new Hashtable();
|
|
|
|
unknownMethodError["reason"] = "XmlRequest"; ;
|
|
|
|
unknownMethodError["message"] = "Unknown Rpc request";
|
|
|
|
unknownMethodError["login"] = "false";
|
|
|
|
response.Value = unknownMethodError;
|
|
|
|
}
|
|
|
|
|
|
|
|
return XmlRpcResponseSerializer.Singleton.Serialize(response);
|
|
|
|
}
|
|
|
|
|
2007-03-29 19:05:34 +00:00
|
|
|
protected virtual string ParseREST(string request, string path, string method)
|
2007-03-29 11:08:42 +00:00
|
|
|
{
|
2007-03-29 19:05:34 +00:00
|
|
|
string response;
|
|
|
|
|
2007-03-29 19:22:01 +00:00
|
|
|
string requestKey = String.Format("{0}: {1}", method, path);
|
|
|
|
|
|
|
|
string bestMatch = String.Empty;
|
|
|
|
foreach( string currentKey in m_restHandlers.Keys )
|
|
|
|
{
|
|
|
|
if( requestKey.StartsWith( currentKey ))
|
|
|
|
{
|
|
|
|
if(currentKey.Length > bestMatch.Length )
|
|
|
|
{
|
|
|
|
bestMatch = currentKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-04-11 14:14:19 +00:00
|
|
|
|
|
|
|
RestMethodEntry restMethodEntry;
|
|
|
|
if (m_restHandlers.TryGetValue(bestMatch, out restMethodEntry))
|
2007-03-29 19:05:34 +00:00
|
|
|
{
|
2007-04-11 14:14:19 +00:00
|
|
|
RestMethod restMethod = restMethodEntry.RestMethod;
|
|
|
|
|
|
|
|
string param = path.Substring( restMethodEntry.Path.Length );
|
|
|
|
response = restMethod(request, path, param);
|
2007-03-29 19:05:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
2007-03-29 11:08:42 +00:00
|
|
|
{
|
2007-03-29 19:05:34 +00:00
|
|
|
response = String.Empty;
|
2007-03-29 11:08:42 +00:00
|
|
|
}
|
|
|
|
|
2007-03-29 19:05:34 +00:00
|
|
|
return response;
|
2007-03-29 11:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual string ParseLLSDXML(string requestBody)
|
|
|
|
{
|
|
|
|
// dummy function for now - IMPLEMENT ME!
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual string ParseXMLRPC(string requestBody)
|
|
|
|
{
|
|
|
|
string responseString = String.Empty;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
XmlRpcRequest request = (XmlRpcRequest)(new XmlRpcRequestDeserializer()).Deserialize(requestBody);
|
|
|
|
|
|
|
|
string methodName = request.MethodName;
|
|
|
|
|
|
|
|
responseString = ProcessXMLRPCMethod(methodName, request );
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Console.WriteLine(e.ToString());
|
|
|
|
}
|
|
|
|
return responseString;
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void HandleRequest(Object stateinfo)
|
|
|
|
{
|
2007-05-15 14:54:53 +00:00
|
|
|
try {
|
2007-03-29 11:08:42 +00:00
|
|
|
HttpListenerContext context = (HttpListenerContext)stateinfo;
|
|
|
|
|
|
|
|
HttpListenerRequest request = context.Request;
|
|
|
|
HttpListenerResponse response = context.Response;
|
|
|
|
|
|
|
|
response.KeepAlive = false;
|
|
|
|
response.SendChunked = false;
|
|
|
|
|
|
|
|
System.IO.Stream body = request.InputStream;
|
|
|
|
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
|
|
|
|
System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
|
|
|
|
|
|
|
|
string requestBody = reader.ReadToEnd();
|
|
|
|
body.Close();
|
|
|
|
reader.Close();
|
|
|
|
|
|
|
|
//Console.WriteLine(request.HttpMethod + " " + request.RawUrl + " Http/" + request.ProtocolVersion.ToString() + " content type: " + request.ContentType);
|
|
|
|
//Console.WriteLine(requestBody);
|
|
|
|
|
|
|
|
string responseString = "";
|
|
|
|
switch (request.ContentType)
|
|
|
|
{
|
|
|
|
case "text/xml":
|
|
|
|
// must be XML-RPC, so pass to the XML-RPC parser
|
|
|
|
|
|
|
|
responseString = ParseXMLRPC(requestBody);
|
|
|
|
responseString = Regex.Replace(responseString, "utf-16", "utf-8");
|
|
|
|
|
|
|
|
response.AddHeader("Content-type", "text/xml");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "application/xml":
|
|
|
|
// probably LLSD we hope, otherwise it should be ignored by the parser
|
|
|
|
responseString = ParseLLSDXML(requestBody);
|
|
|
|
response.AddHeader("Content-type", "application/xml");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "application/x-www-form-urlencoded":
|
|
|
|
// a form data POST so send to the REST parser
|
|
|
|
responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod);
|
|
|
|
response.AddHeader("Content-type", "text/html");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case null:
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
|
|
|
|
System.IO.Stream output = response.OutputStream;
|
|
|
|
response.SendChunked = false;
|
|
|
|
response.ContentLength64 = buffer.Length;
|
|
|
|
output.Write(buffer, 0, buffer.Length);
|
2007-05-15 14:54:53 +00:00
|
|
|
output.Close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
Console.WriteLine(e.ToString());
|
2007-04-13 15:14:21 +00:00
|
|
|
}
|
2007-03-29 11:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
{
|
2007-05-12 15:19:03 +00:00
|
|
|
OpenSim.Framework.Console.MainConsole.Instance.WriteLine(LogPriority.LOW,"BaseHttpServer.cs: Starting up HTTP Server");
|
2007-03-29 11:08:42 +00:00
|
|
|
|
2007-03-30 10:14:15 +00:00
|
|
|
m_workerThread = new Thread(new ThreadStart(StartHTTP));
|
|
|
|
m_workerThread.IsBackground = true;
|
|
|
|
m_workerThread.Start();
|
2007-03-29 11:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void StartHTTP()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2007-05-12 15:19:03 +00:00
|
|
|
OpenSim.Framework.Console.MainConsole.Instance.WriteLine(LogPriority.LOW,"BaseHttpServer.cs: StartHTTP() - Spawned main thread OK");
|
2007-03-29 11:08:42 +00:00
|
|
|
m_httpListener = new HttpListener();
|
|
|
|
|
|
|
|
m_httpListener.Prefixes.Add("http://+:" + m_port + "/");
|
|
|
|
m_httpListener.Start();
|
|
|
|
|
|
|
|
HttpListenerContext context;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
context = m_httpListener.GetContext();
|
|
|
|
ThreadPool.QueueUserWorkItem(new WaitCallback(HandleRequest), context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2007-05-12 15:19:03 +00:00
|
|
|
OpenSim.Framework.Console.MainConsole.Instance.WriteLine(LogPriority.MEDIUM,e.Message);
|
2007-03-29 11:08:42 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-27 21:42:14 +00:00
|
|
|
}
|
|
|
|
}
|