cleaning up OSHttpRequest removing old Http stuff. also adding test

case for OSHttpRequest (not very fancy yet, but still).
0.6.0-stable
Dr Scofield 2008-10-06 19:42:03 +00:00
parent 600721d480
commit a1f3409032
6 changed files with 127 additions and 318 deletions

View File

@ -36,6 +36,7 @@
<test>
<assemblies>
<include name="../bin/OpenSim.Framework.Tests.dll" />
<include name="../bin/OpenSim.Framework.Servers.Tests.dll" />
<include name="../bin/OpenSim.Region.ScriptEngine.Common.Tests.dll" />
<include name="../bin/OpenSim.Region.ScriptEngine.Shared.Tests.dll" />
<include name="../bin/OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests.dll" />

View File

@ -35,6 +35,7 @@
<test>
<assemblies>
<include name="./bin/OpenSim.Framework.Tests.dll" />
<include name="./bin/OpenSim.Framework.Servers.Tests.dll" />
<include name="./bin/OpenSim.Region.ScriptEngine.Shared.Tests.dll" />
<include name="./bin/OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests.dll" />
<include name="./bin/OpenSim.Region.Environment.Tests.dll" />
@ -51,6 +52,7 @@
<test>
<assemblies>
<include name="./bin/OpenSim.Framework.Tests.dll" />
<include name="./bin/OpenSim.Framework.Servers.Tests.dll" />
<include name="./bin/OpenSim.Region.ScriptEngine.Shared.Tests.dll" />
<include name="./bin/OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests.dll" />
<include name="./bin/OpenSim.Region.Environment.Tests.dll" />

View File

@ -103,120 +103,13 @@ namespace OpenSim.Framework.Servers
{
m_ssl = ssl;
m_port = port;
if (m_ssl)
{
//SetupSsl((int)sslport, CN);
m_sslport = sslport;
}
}
/*
*
public bool SetupSsl(int port, string CN)
{
string searchCN = Environment.MachineName.ToUpper();
if (CN.Length > 0)
searchCN = CN.ToUpper();
m_SSLCommonName = searchCN;
Type t = Type.GetType("Mono.Runtime");
if (t != null)
{
// TODO Mono User Friendly HTTPS setup
// if this doesn't exist, then mono people can still manually use httpcfg
}
else
{
// Windows.
// Search through the store for a certificate with a Common name specified in OpenSim.ini.
// We need to find it's hash so we can pass it to httpcfg
X509Store store = new X509Store(StoreLocation.LocalMachine);
//Use the first cert to configure Ssl
store.Open(OpenFlags.ReadOnly);
//Assumption is we have certs. If not then this call will fail :(
try
{
bool found = false;
//X509Certificate2.CreateFromCertFile("testCert.cer");
foreach (X509Certificate2 cert in store.Certificates)
{
String certHash = cert.GetCertHashString();
//Only install certs issued for the machine and has the name as the machine name
if (cert.Subject.ToUpper().IndexOf(searchCN) >= 0)
{
string httpcfgparams = String.Format("set ssl -i 0.0.0.0:{1} -c \"MY\" -h {0}", certHash, port);
try
{
found = true;
ExecuteHttpcfgCommand(httpcfgparams);
break;
}
catch (Exception e)
{
m_log.WarnFormat("[HTTPS]: Automatic HTTPS setup failed. Do you have httpcfg.exe in your path? If not, you can download it in the windowsXP Service Pack 2 Support Tools, here: http://www.microsoft.com/downloads/details.aspx?FamilyID=49ae8576-9bb9-4126-9761-ba8011fabf38&displaylang=en. When you get it installed type, httpcfg {0} - {1}", httpcfgparams, e);
return false;
}
}
}
if (!found)
{
m_log.WarnFormat("[HTTPS]: We didn't find a certificate that matched the common name {0}. Automatic HTTPS setup failed, you may have certificate errors. To fix this, make sure you generate a certificate request(CSR) using OpenSSL or the IIS snap-in with the common name you specified in opensim.ini. Then get it signed by a certification authority or sign it yourself with OpenSSL and the junkCA. Finally, be sure to import the cert to the 'MY' store(StoreLocation.LocalMachine)", searchCN);
return false;
}
}
catch (Exception e)
{
m_log.WarnFormat("[HTTPS]: We didn't any certificates in your LocalMachine certificate store. Automatic HTTPS setup failed, you may have certificate errors. To fix this, make sure you generate a certificate request(CSR) using OpenSSL or the IIS snap-inwith the common name you specified in opensim.ini. Then get it signed by a certification authority or sign it yourself with OpenSSL and the junkCA. Finally, be sure to import the cert to the 'MY' store(StoreLocation.LocalMachine). The configured common name is {0} - {1}", searchCN, e);
return false;
}
finally
{
if (store != null)
{
store.Close();
}
}
}
return true;
}
private void ExecuteHttpcfgCommand(string p)
{
string file = "httpcfg";
ProcessStartInfo info = new ProcessStartInfo(file, p);
// Redirect output so we can read it.
info.RedirectStandardOutput = true;
// To redirect, we must not use shell execute.
info.UseShellExecute = false;
// Create and execute the process.
Process httpcfgprocess = Process.Start(info);
httpcfgprocess.Start();
string result = httpcfgprocess.StandardOutput.ReadToEnd();
if (result.Contains("HttpSetServiceConfiguration completed with"))
{
//success
}
else
{
//fail
m_log.WarnFormat("[HTTPS]:Error binding certificate with the requested port. Message:{0}", result);
}
}
*/
/// <summary>
/// Add a stream handler to the http server. If the handler already exists, then nothing happens.
/// </summary>
@ -311,81 +204,6 @@ namespace OpenSim.Framework.Servers
return true;
}
/// <summary>
/// HttpListener Handle an individual http request. This method is given to a worker in the thread pool.
/// </summary>
/// <param name="stateinfo"></param>
public virtual void HandleRequest(Object stateinfo)
{
// force the culture to en-US
// If we don't catch the exception here it will just disappear into the thread pool and we'll be none the wiser
try
{
HttpListenerContext context = (HttpListenerContext)stateinfo;
OSHttpRequest request = new OSHttpRequest(context.Request);
OSHttpResponse response = new OSHttpResponse(context.Response);
HandleRequest(request, response);
}
catch (SocketException e)
{
// At least on linux, it appears that if the client makes a request without requiring the response,
// an unconnected socket exception is thrown when we close the response output stream. There's no
// obvious way to tell if the client didn't require the response, so instead we'll catch and ignore
// the exception instead.
//
// An alternative may be to turn off all response write exceptions on the HttpListener, but let's go
// with the minimum first
m_log.WarnFormat("[BASE HTTP SERVER]: HandleRequest threw {0}.\nNOTE: this may be spurious on Linux", e);
}
catch (Exception e)
{
m_log.ErrorFormat("[BASE HTTP SERVER]: HandleRequest() threw {0}", e);
}
}
/*
/// <summary>
/// HttpListener Handle an individual http request. This method is given to a worker in the thread pool.
/// </summary>
/// <param name="stateinfo"></param>
public virtual void HandleRequestHttpServer(Object stateinfo)
{
// force the culture to en-US
// If we don't catch the exception here it will just disappear into the thread pool and we'll be none the wiser
try
{
HttpServerContextObj context = (HttpServerContextObj)stateinfo;
OSHttpRequest request = new OSHttpRequest(context.Request);
OSHttpResponse response = new OSHttpResponse(context.Response);
HandleRequest(request, response);
}
catch (SocketException e)
{
// At least on linux, it appears that if the client makes a request without requiring the response,
// an unconnected socket exception is thrown when we close the response output stream. There's no
// obvious way to tell if the client didn't require the response, so instead we'll catch and ignore
// the exception instead.
//
// An alternative may be to turn off all response write exceptions on the HttpListener, but let's go
// with the minimum first
m_log.WarnFormat("[BASE HTTP SERVER]: HandleRequest threw {0}.\nNOTE: this may be spurious on Linux", e);
}
catch (Exception e)
{
m_log.ErrorFormat("[BASE HTTP SERVER]: HandleRequest() threw {0}", e);
}
}
*/
public void OnHandleRequestIOThread(IHttpClientContext context, IHttpRequest request)
{
OSHttpRequest req = new OSHttpRequest(context, request);
@ -845,7 +663,7 @@ namespace OpenSim.Framework.Servers
if (TryGetLLSDHandler(request.RawUrl, out llsdhandler) && !LegacyLLSDLoginLibOMV)
{
// we found a registered llsd handler to service this request
llsdResponse = llsdhandler(request.RawUrl, llsdRequest, (request.RemoteIPEndPoint == null)? "" : request.RemoteIPEndPoint.ToString());
llsdResponse = llsdhandler(request.RawUrl, llsdRequest, request.RemoteIPEndPoint.ToString());
}
else
{

View File

@ -31,18 +31,25 @@ using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.IO;
using System.Reflection;
using System.Text;
using HttpServer;
using log4net;
namespace OpenSim.Framework.Servers
{
public class OSHttpRequest
{
private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
protected HttpServer.IHttpRequest _request = null;
protected HttpServer.IHttpClientContext _context = null;
public string[] AcceptTypes
{
get { return _acceptTypes; }
get { return _request.AcceptTypes; }
}
private string[] _acceptTypes;
public Encoding ContentEncoding
{
@ -52,9 +59,8 @@ namespace OpenSim.Framework.Servers
public long ContentLength
{
get { return _contentLength64; }
get { return _request.ContentLength; }
}
private long _contentLength64;
public long ContentLength64
{
@ -67,71 +73,35 @@ namespace OpenSim.Framework.Servers
}
private string _contentType;
// public CookieCollection Cookies
// {
// get { return _cookies; }
// }
// private CookieCollection _cookies;
public bool HasEntityBody
{
get { return _request.ContentLength != 0; }
}
public NameValueCollection Headers
{
get { return _headers; }
get { return _request.Headers; }
}
private NameValueCollection _headers;
public string HttpMethod
{
get { return _httpMethod; }
get { return _request.Method; }
}
private string _httpMethod;
public Stream InputStream
{
get { return _inputStream; }
get { return _request.Body; }
}
private Stream _inputStream;
// public bool IsSecureConnection
// {
// get { return _isSecureConnection; }
// }
// private bool _isSecureConnection;
// public bool IsAuthenticated
// {
// get { return _isAuthenticated; }
// }
// private bool _isAuthenticated;
public bool HasEntityBody
public bool IsSecured
{
get { return _hasbody; }
get { return _context.Secured; }
}
private bool _hasbody;
public bool KeepAlive
{
get { return _keepAlive; }
get { return ConnectionType.KeepAlive == _request.Connection; }
}
private bool _keepAlive;
public string RawUrl
{
get { return _rawUrl; }
}
private string _rawUrl;
public Uri Url
{
get { return _url; }
}
private Uri _url;
public string UserAgent
{
get { return _userAgent; }
}
private string _userAgent;
public NameValueCollection QueryString
{
@ -145,23 +115,30 @@ namespace OpenSim.Framework.Servers
}
private Hashtable _query;
public string RawUrl
{
get { return _request.Uri.AbsolutePath; }
}
public IPEndPoint RemoteIPEndPoint
{
get { return _ipEndPoint; }
get { return _remoteIPEndPoint; }
}
private IPEndPoint _ipEndPoint;
private IPEndPoint _remoteIPEndPoint;
public Uri Url
{
get { return _request.Uri; }
}
public string UserAgent
{
get { return _userAgent; }
}
private string _userAgent;
// internal HttpRequest HttpRequest
// {
// get { return _request; }
// }
// private HttpRequest _request;
// internal HttpClientContext HttpClientContext
// {
// get { return _context; }
// }
// private HttpClientContext _context;
/// <summary>
/// Internal whiteboard for handlers to store temporary stuff
@ -173,52 +150,37 @@ namespace OpenSim.Framework.Servers
}
private Dictionary<string, object> _whiteboard = new Dictionary<string, object>();
public OSHttpRequest()
{
}
public OSHttpRequest(HttpListenerRequest req)
{
_acceptTypes = req.AcceptTypes;
_contentEncoding = req.ContentEncoding;
_contentLength64 = req.ContentLength64;
_contentType = req.ContentType;
_headers = req.Headers;
_httpMethod = req.HttpMethod;
_hasbody = req.HasEntityBody;
_inputStream = req.InputStream;
_keepAlive = req.KeepAlive;
_rawUrl = req.RawUrl;
_url = req.Url;
_queryString = req.QueryString;
_userAgent = req.UserAgent;
_ipEndPoint = req.RemoteEndPoint;
// _cookies = req.Cookies;
// _isSecureConnection = req.IsSecureConnection;
// _isAuthenticated = req.IsAuthenticated;
}
public OSHttpRequest(HttpServer.IHttpClientContext context, HttpServer.IHttpRequest req)
{
//_context = context;
HttpServer.IHttpRequest _request = req;
_request = req;
_context = context;
_acceptTypes = req.AcceptTypes;
if (null != req.Headers["content-encoding"])
_contentEncoding = Encoding.GetEncoding(_request.Headers["content-encoding"]);
_contentLength64 = req.ContentLength;
if (null != req.Headers["content-type"])
_contentType = _request.Headers["content-type"];
_headers = req.Headers;
_httpMethod = req.Method;
_hasbody = req.ContentLength != 0;
_inputStream = req.Body;
_keepAlive = ConnectionType.KeepAlive == req.Connection;
_rawUrl = req.Uri.AbsolutePath;
_url = req.Uri;
if (null != req.Headers["user-agent"])
_userAgent = req.Headers["user-agent"];
if (null != req.Headers["remote_addr"])
{
try
{
IPAddress addr = IPAddress.Parse(req.Headers["remote_addr"]);
int port = Int32.Parse(req.Headers["remote_port"]);
_remoteIPEndPoint = new IPEndPoint(addr, port);
}
catch (FormatException)
{
_log.ErrorFormat("[OSHttpRequest]: format exception on addr/port {0}:{1}, ignoring",
req.Headers["remote_addr"], req.Headers["remote_port"]);
}
}
_queryString = new NameValueCollection();
_query = new Hashtable();
try
@ -232,21 +194,15 @@ namespace OpenSim.Framework.Servers
}
catch (InvalidCastException)
{
System.Console.WriteLine("[OSHttpRequest]: Errror parsing querystring.. but it was recoverable.. skipping on to the next one");
_log.DebugFormat("[OSHttpRequest]: error parsing {0} query item, skipping it", item.Name);
continue;
}
}
}
catch (Exception)
{
System.Console.WriteLine("[OSHttpRequest]: Errror parsing querystring");
_log.ErrorFormat("[OSHttpRequest]: Error parsing querystring");
}
// TODO: requires change to HttpServer.HttpRequest
_ipEndPoint = null;
// _cookies = req.Cookies;
// _isSecureConnection = req.IsSecureConnection;
// _isAuthenticated = req.IsAuthenticated;
}
public override string ToString()
@ -259,7 +215,7 @@ namespace OpenSim.Framework.Servers
}
if (null != RemoteIPEndPoint)
{
me.Append(String.Format(" IP: {0}\n", RemoteIPEndPoint.ToString()));
me.Append(String.Format(" IP: {0}\n", RemoteIPEndPoint));
}
return me.ToString();

View File

@ -45,9 +45,10 @@ namespace OpenSim.Framework.Servers
private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
/// <summary>
/// An OSHttpHandler that matches on the "content-type" header can
/// supply an OSHttpContentTypeChecker delegate which will be
/// invoked by the request matcher in OSHttpRequestPump.
/// XmlRpcMethodMatch tries to reify (deserialize) an incoming
/// XmlRpc request (and posts it to the "whiteboard") and
/// checks whether the method name is one we are interested
/// in.
/// </summary>
/// <returns>true if the handler is interested in the content;
/// false otherwise</returns>

View File

@ -466,6 +466,8 @@
<Files>
<Match pattern="*.cs" recurse="true">
<Exclude name="Tests" pattern="Tests" />
<!-- on temporary suspension -->
<Exclude pattern="OSHttpHandler\.cs" />
<Exclude pattern="OSHttpHttpHandler\.cs" />
<Exclude pattern="OSHttpRequestPump\.cs" />
@ -476,6 +478,35 @@
</Files>
</Project>
<Project name="OpenSim.Framework.Servers.Tests" path="OpenSim/Framework/Servers/Tests" type="Library">
<Configuration name="Debug">
<Options>
<OutputPath>../../../../bin/</OutputPath>
</Options>
</Configuration>
<Configuration name="Release">
<Options>
<OutputPath>../../../../bin/</OutputPath>
</Options>
</Configuration>
<ReferencePath>../../../../bin/</ReferencePath>
<Reference name="System"/>
<Reference name="System.IO"/>
<Reference name="System.Net"/>
<Reference name="OpenSim.Framework"/>
<Reference name="OpenSim.Framework.Servers"/>
<Reference name="log4net.dll"/>
<Reference name="HttpServer.dll"/>
<Reference name="nunit.framework.dll" />
<Files>
<Match pattern="*.cs" recurse="true">
</Match>
</Files>
</Project>
<Project name="OpenSim.Region.Physics.Manager" path="OpenSim/Region/Physics/Manager" type="Library">
<Configuration name="Debug">
<Options>