* Adding generic HttpServer to OpenMetaverse.Capabilites

* LoginResponseData can now serialize to XmlRpc
* Adding new Simian project, ultra-lightweight simulator for testing and development
* Shuffling OpenMetaverse.Capabilities around a bit in preparation for CAPS server implementation

git-svn-id: http://libopenmetaverse.googlecode.com/svn/trunk@2094 52acb1d6-8a22-11de-b505-999d5b087335
This commit is contained in:
John Hurliman
2008-08-16 02:04:20 +00:00
parent c07826e29a
commit 0bd77baba2
15 changed files with 1643 additions and 61 deletions

View File

@@ -1,39 +0,0 @@
/*
* Copyright (c) 2007-2008, openmetaverse.org
* All rights reserved.
*
* - Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Neither the name of the openmetaverse.org nor the names
* of its contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Net;
#if !PocketPC
namespace OpenMetaverse.Capabilities
{
public class EventQueueListener
{
}
}
#endif

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007-2008, openmetaverse.org
* Copyright (c) 2008, openmetaverse.org
* All rights reserved.
*
* - Redistribution and use in source and binary forms, with or without
@@ -31,7 +31,7 @@ using System.Net;
namespace OpenMetaverse.Capabilities
{
public class CapsListener
public class EventQueueServer
{
}
}

View File

@@ -0,0 +1,172 @@
/*
* Copyright (c) 2008, openmetaverse.org
* All rights reserved.
*
* - Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Neither the name of the openmetaverse.org nor the names
* of its contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Net;
using System.Text.RegularExpressions;
namespace OpenMetaverse.Capabilities
{
/// <summary>
/// Used to match incoming HTTP requests against registered handlers.
/// Matches based on any combination of HTTP Method, Content-Type header,
/// and URL path. URL path matching supports the * wildcard character
/// </summary>
public struct HttpRequestSignature : IEquatable<HttpRequestSignature>
{
private string method;
private string contentType;
private string path;
private bool pathIsWildcard;
public string Method
{
get { return method; }
set
{
if (!String.IsNullOrEmpty(value)) method = value.ToLower();
else method = String.Empty;
}
}
public string ContentType
{
get { return contentType; }
set
{
if (!String.IsNullOrEmpty(value)) contentType = value.ToLower();
else contentType = String.Empty;
}
}
public string Path
{
get { return path; }
set
{
pathIsWildcard = false;
if (!String.IsNullOrEmpty(value))
{
// Regex to tear apart URLs, used to extract just a URL
// path from any data we're given
string regexPattern =
@"^(?<s1>(?<s0>[^:/\?#]+):)?(?<a1>"
+ @"//(?<a0>[^/\?#]*))?(?<p0>[^\?#]*)"
+ @"(?<q1>\?(?<q0>[^#]*))?"
+ @"(?<f1>#(?<f0>.*))?";
Regex re = new Regex(regexPattern, RegexOptions.ExplicitCapture);
Match m = re.Match(value);
string newPath = m.Groups["p0"].Value.ToLower();
// Remove any trailing forward-slashes
if (newPath.EndsWith("/"))
newPath = newPath.Substring(0, newPath.Length - 1);
// Check if this path contains a wildcard. If so, convert it to a regular expression
if (newPath.Contains("*"))
{
pathIsWildcard = true;
newPath = String.Format("^{0}$", newPath.Replace("\\*", ".*"));
}
path = newPath;
}
else
{
path = String.Empty;
}
}
}
public bool PathIsWildcard
{
get { return pathIsWildcard; }
}
public HttpRequestSignature(HttpListenerContext context)
{
method = contentType = path = String.Empty;
pathIsWildcard = false;
Method = context.Request.HttpMethod;
ContentType = context.Request.ContentType;
Path = context.Request.RawUrl;
}
public bool ExactlyEquals(HttpRequestSignature signature)
{
return (method.Equals(signature.Method) && contentType.Equals(signature.ContentType) && path.Equals(signature.Path));
}
public override bool Equals(object obj)
{
return (obj is HttpRequestSignature) ? this == (HttpRequestSignature)obj : false;
}
public bool Equals(HttpRequestSignature signature)
{
return (this == signature);
}
public override int GetHashCode()
{
int hash = (method != null) ? method.GetHashCode() : 0;
hash ^= (contentType != null) ? contentType.GetHashCode() : 0;
hash ^= (path != null) ? path.GetHashCode() : 0;
return hash;
}
public static bool operator ==(HttpRequestSignature lhs, HttpRequestSignature rhs)
{
bool methodMatch = (String.IsNullOrEmpty(lhs.Method) || String.IsNullOrEmpty(rhs.Method) || lhs.Method.Equals(rhs.Method));
bool contentTypeMatch = (String.IsNullOrEmpty(lhs.ContentType) || String.IsNullOrEmpty(rhs.ContentType) || lhs.ContentType.Equals(rhs.ContentType));
bool pathMatch = false;
if (methodMatch && contentTypeMatch)
{
if (!String.IsNullOrEmpty(lhs.Path) && !String.IsNullOrEmpty(rhs.Path))
{
// Do wildcard matching if there is any to be done
if (lhs.PathIsWildcard)
pathMatch = Regex.IsMatch(rhs.Path, lhs.Path);
else if (rhs.PathIsWildcard)
pathMatch = Regex.IsMatch(lhs.Path, rhs.Path);
else
pathMatch = lhs.Path.Equals(rhs.Path);
}
else
{
pathMatch = true;
}
}
return (methodMatch && contentTypeMatch && pathMatch);
}
public static bool operator !=(HttpRequestSignature lhs, HttpRequestSignature rhs)
{
return !(lhs == rhs);
}
}
}

View File

@@ -0,0 +1,179 @@
/*
* Copyright (c) 2008, openmetaverse.org
* All rights reserved.
*
* - Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Neither the name of the openmetaverse.org nor the names
* of its contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Net;
namespace OpenMetaverse.Capabilities
{
public class HttpServer
{
public delegate void HttpRequestCallback(ref HttpListenerContext context);
public struct HttpRequestHandler : IEquatable<HttpRequestHandler>
{
public HttpRequestSignature Signature;
public HttpRequestCallback Callback;
public HttpRequestHandler(HttpRequestSignature signature, HttpRequestCallback callback)
{
Signature = signature;
Callback = callback;
}
public override bool Equals(object obj)
{
return (obj is HttpRequestHandler) ? this.Signature == ((HttpRequestHandler)obj).Signature : false;
}
public override int GetHashCode()
{
return Signature.GetHashCode();
}
public bool Equals(HttpRequestHandler handler)
{
return this.Signature == handler.Signature;
}
}
HttpListener server;
AsyncCallback serverCallback;
int serverPort;
bool sslEnabled;
List<HttpRequestHandler> requestHandlers;
bool isRunning;
public HttpServer(int port, bool ssl)
{
serverPort = port;
sslEnabled = ssl;
server = new HttpListener();
serverCallback = new AsyncCallback(BeginGetContextCallback);
if (ssl)
server.Prefixes.Add(String.Format("https://+:{0}/", port));
else
server.Prefixes.Add(String.Format("http://+:{0}/", port));
requestHandlers = new List<HttpRequestHandler>();
isRunning = false;
}
public void AddHandler(HttpRequestHandler handler)
{
if (!isRunning)
requestHandlers.Add(handler);
else
throw new InvalidOperationException("Cannot add HTTP request handlers while the server is running");
}
public void RemoveHandler(HttpRequestHandler handler)
{
if (!isRunning)
requestHandlers.Remove(handler);
else
throw new InvalidOperationException("Cannot add HTTP request handlers while the server is running");
}
public void Start()
{
server.Start();
server.BeginGetContext(serverCallback, server);
isRunning = true;
}
public void Stop()
{
isRunning = false;
try { server.Stop(); }
catch (ObjectDisposedException) { }
}
protected void BeginGetContextCallback(IAsyncResult result)
{
HttpListenerContext context = null;
// Retrieve the incoming request
try { context = server.EndGetContext(result); }
catch (Exception)
{
}
if (isRunning)
{
// Immediately start listening again
try { server.BeginGetContext(serverCallback, server); }
catch (Exception)
{
// Something went wrong, can't resume listening. Bail out now
// since this is a shutdown (whether it was meant to be or not)
return;
}
// Process the incoming request
if (context != null)
{
// Create a request signature
HttpRequestSignature signature = new HttpRequestSignature(context);
// Look for a signature match in our handlers
for (int i = 0; i < requestHandlers.Count; i++)
{
HttpRequestHandler handler = requestHandlers[i];
if (handler.Signature == signature)
{
// Request signature matched, handle it
try
{
handler.Callback(ref context);
}
catch (Exception e)
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.StatusDescription = e.ToString();
}
context.Response.Close();
return;
}
}
// No registered handler matched this request's signature. Send a 404
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
context.Response.StatusDescription = String.Format(
"No request handler registered for Method=\"{0}\", Content-Type=\"{1}\", Path=\"{2}\"",
signature.Method, signature.ContentType, signature.Path);
context.Response.Close();
}
}
}
}
}