Merge branch 'master' into 0.8-post-fixes

This commit is contained in:
Justin Clark-Casey
2014-05-27 23:29:54 +01:00
125 changed files with 1995 additions and 674 deletions

View File

@@ -321,6 +321,8 @@ namespace OpenSim.Framework
Mac = args["mac"].AsString();
if (args["id0"] != null)
Id0 = args["id0"].AsString();
if (args["teleport_flags"] != null)
teleportFlags = args["teleport_flags"].AsUInteger();
if (args["start_pos"] != null)
Vector3.TryParse(args["start_pos"].AsString(), out startpos);

View File

@@ -35,6 +35,8 @@ using System.Threading;
using System.Web;
using log4net;
using OpenSim.Framework.ServiceAuth;
namespace OpenSim.Framework.Communications
{
/// <summary>
@@ -297,7 +299,7 @@ namespace OpenSim.Framework.Communications
/// <summary>
/// Perform a synchronous request
/// </summary>
public Stream Request()
public Stream Request(IServiceAuth auth)
{
lock (_lock)
{
@@ -307,6 +309,8 @@ namespace OpenSim.Framework.Communications
_request.Timeout = 200000;
_request.Method = RequestMethod;
_asyncException = null;
if (auth != null)
auth.AddAuthorization(_request.Headers);
// IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request);
try
@@ -358,7 +362,7 @@ namespace OpenSim.Framework.Communications
}
}
public Stream Request(Stream src)
public Stream Request(Stream src, IServiceAuth auth)
{
_request = (HttpWebRequest) WebRequest.Create(buildUri());
_request.KeepAlive = false;
@@ -367,6 +371,8 @@ namespace OpenSim.Framework.Communications
_request.Method = RequestMethod;
_asyncException = null;
_request.ContentLength = src.Length;
if (auth != null)
auth.AddAuthorization(_request.Headers);
m_log.InfoFormat("[REST]: Request Length {0}", _request.ContentLength);
m_log.InfoFormat("[REST]: Sending Web Request {0}", buildUri());
@@ -384,7 +390,22 @@ namespace OpenSim.Framework.Communications
length = src.Read(buf, 0, 1024);
}
_response = (HttpWebResponse) _request.GetResponse();
try
{
_response = (HttpWebResponse)_request.GetResponse();
}
catch (WebException e)
{
m_log.WarnFormat("[REST]: Request {0} {1} failed with status {2} and message {3}",
RequestMethod, _request.RequestUri, e.Status, e.Message);
}
catch (Exception e)
{
m_log.WarnFormat(
"[REST]: Request {0} {1} failed with exception {2} {3}",
RequestMethod, _request.RequestUri, e.Message, e.StackTrace);
}
// IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request);
@@ -423,7 +444,7 @@ namespace OpenSim.Framework.Communications
try
{
// Perform the operation; if sucessful set the result
Stream s = Request();
Stream s = Request(null);
ar.SetAsCompleted(s, false);
}
catch (Exception e)

View File

@@ -46,6 +46,11 @@ namespace OpenSim.Framework.Console
// private readonly object m_syncRoot = new object();
private const string LOGLEVEL_NONE = "(none)";
// Used to extract categories for colourization.
private Regex m_categoryRegex
= new Regex(
@"^(?<Front>.*?)\[(?<Category>[^\]]+)\]:?(?<End>.*)", RegexOptions.Singleline | RegexOptions.Compiled);
private int m_cursorYPosition = -1;
private int m_cursorXPosition = 0;
private StringBuilder m_commandLine = new StringBuilder();
@@ -280,11 +285,8 @@ namespace OpenSim.Framework.Console
string outText = text;
if (level != LOGLEVEL_NONE)
{
string regex = @"^(?<Front>.*?)\[(?<Category>[^\]]+)\]:?(?<End>.*)";
Regex RE = new Regex(regex, RegexOptions.Multiline);
MatchCollection matches = RE.Matches(text);
{
MatchCollection matches = m_categoryRegex.Matches(text);
if (matches.Count == 1)
{

View File

@@ -213,8 +213,13 @@ namespace OpenSim.Framework.Serialization.External
xtw.WriteElementString("ClaimDate", Convert.ToString(landData.ClaimDate));
xtw.WriteElementString("ClaimPrice", Convert.ToString(landData.ClaimPrice));
xtw.WriteElementString("GlobalID", landData.GlobalID.ToString());
xtw.WriteElementString("GroupID", landData.GroupID.ToString());
xtw.WriteElementString("IsGroupOwned", Convert.ToString(landData.IsGroupOwned));
UUID groupID = options.ContainsKey("wipe-owners") ? UUID.Zero : landData.GroupID;
xtw.WriteElementString("GroupID", groupID.ToString());
bool isGroupOwned = options.ContainsKey("wipe-owners") ? false : landData.IsGroupOwned;
xtw.WriteElementString("IsGroupOwned", Convert.ToString(isGroupOwned));
xtw.WriteElementString("Bitmap", Convert.ToBase64String(landData.Bitmap));
xtw.WriteElementString("Description", landData.Description);
xtw.WriteElementString("Flags", Convert.ToString((uint)landData.Flags));
@@ -227,13 +232,8 @@ namespace OpenSim.Framework.Serialization.External
xtw.WriteElementString("MediaURL", landData.MediaURL);
xtw.WriteElementString("MusicURL", landData.MusicURL);
UUID ownerIdToWrite;
if (options != null && options.ContainsKey("wipe-owners"))
ownerIdToWrite = UUID.Zero;
else
ownerIdToWrite = landData.OwnerID;
xtw.WriteElementString("OwnerID", ownerIdToWrite.ToString());
UUID ownerID = options.ContainsKey("wipe-owners") ? UUID.Zero : landData.OwnerID;
xtw.WriteElementString("OwnerID", ownerID.ToString());
xtw.WriteStartElement("ParcelAccessList");
foreach (LandAccessEntry pal in landData.ParcelAccessList)

View File

@@ -121,7 +121,8 @@ namespace OpenSim.Framework.Serialization.Tests
TestHelpers.InMethod();
// log4net.Config.XmlConfigurator.Configure();
LandData ld = LandDataSerializer.Deserialize(LandDataSerializer.Serialize(this.land, null));
Dictionary<string, object> options = new Dictionary<string, object>();
LandData ld = LandDataSerializer.Deserialize(LandDataSerializer.Serialize(this.land, options));
Assert.That(ld, Is.Not.Null, "Deserialize(string) returned null");
// Assert.That(ld.AABBMax, Is.EqualTo(land.AABBMax));
// Assert.That(ld.AABBMin, Is.EqualTo(land.AABBMin));

View File

@@ -26,6 +26,8 @@
*/
using System.IO;
using System.Net;
using OpenSim.Framework.ServiceAuth;
namespace OpenSim.Framework.Servers.HttpServer
{
@@ -37,15 +39,30 @@ namespace OpenSim.Framework.Servers.HttpServer
/// </remarks>
public abstract class BaseStreamHandler : BaseRequestHandler, IStreamedRequestHandler
{
protected BaseStreamHandler(string httpMethod, string path) : this(httpMethod, path, null, null) {}
protected IServiceAuth m_Auth;
protected BaseStreamHandler(string httpMethod, string path) : this(httpMethod, path, null, null) { }
protected BaseStreamHandler(string httpMethod, string path, string name, string description)
: base(httpMethod, path, name, description) {}
protected BaseStreamHandler(string httpMethod, string path, IServiceAuth auth)
: base(httpMethod, path, null, null)
{
m_Auth = auth;
}
public virtual byte[] Handle(
string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
{
RequestsReceived++;
if (m_Auth != null && !m_Auth.Authenticate(httpRequest.Headers, httpResponse.AddHeader))
{
httpResponse.StatusCode = (int)HttpStatusCode.Unauthorized;
httpResponse.ContentType = "text/plain";
return new byte[0];
}
byte[] result = ProcessRequest(path, request, httpRequest, httpResponse);

View File

@@ -90,14 +90,14 @@ namespace OpenSim.Framework.Servers.HttpServer
}
catch (Exception e)
{
m_log.Debug(string.Format("JsonRpc request '{0}' failed", method), e);
m_log.Debug(string.Format("JsonRpc request '{0}' to {1} failed", method, uri), e);
return false;
}
if (!response.ContainsKey("_Result"))
{
m_log.DebugFormat("JsonRpc request '{0}' returned an invalid response: {1}",
method, OSDParser.SerializeJsonString(response));
m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an invalid response: {2}",
method, uri, OSDParser.SerializeJsonString(response));
return false;
}
response = (OSDMap)response["_Result"];
@@ -107,15 +107,15 @@ namespace OpenSim.Framework.Servers.HttpServer
if (response.ContainsKey("error"))
{
data = response["error"];
m_log.DebugFormat("JsonRpc request '{0}' returned an error: {1}",
method, OSDParser.SerializeJsonString(data));
m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an error: {2}",
method, uri, OSDParser.SerializeJsonString(data));
return false;
}
if (!response.ContainsKey("result"))
{
m_log.DebugFormat("JsonRpc request '{0}' returned an invalid response: {1}",
method, OSDParser.SerializeJsonString(response));
m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an invalid response: {2}",
method, uri, OSDParser.SerializeJsonString(response));
return false;
}
@@ -161,14 +161,14 @@ namespace OpenSim.Framework.Servers.HttpServer
}
catch (Exception e)
{
m_log.Debug(string.Format("JsonRpc request '{0}' failed", method), e);
m_log.Debug(string.Format("JsonRpc request '{0}' to {1} failed", method, uri), e);
return false;
}
if (!response.ContainsKey("_Result"))
{
m_log.DebugFormat("JsonRpc request '{0}' returned an invalid response: {1}",
method, OSDParser.SerializeJsonString(response));
m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an invalid response: {2}",
method, uri, OSDParser.SerializeJsonString(response));
return false;
}
response = (OSDMap)response["_Result"];
@@ -176,8 +176,8 @@ namespace OpenSim.Framework.Servers.HttpServer
if (response.ContainsKey("error"))
{
data = response["error"];
m_log.DebugFormat("JsonRpc request '{0}' returned an error: {1}",
method, OSDParser.SerializeJsonString(data));
m_log.DebugFormat("JsonRpc request '{0}' to {1} returned an error: {2}",
method, uri, OSDParser.SerializeJsonString(data));
return false;
}

View File

@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Reflection;
using Nini.Config;
using log4net;
namespace OpenSim.Framework.ServiceAuth
{
public class BasicHttpAuthentication : IServiceAuth
{
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private string m_Username, m_Password;
private string m_CredentialsB64;
private string remove_me;
public string Credentials
{
get { return m_CredentialsB64; }
}
public BasicHttpAuthentication(IConfigSource config, string section)
{
remove_me = section;
m_Username = Util.GetConfigVarFromSections<string>(config, "HttpAuthUsername", new string[] { "Network", section }, string.Empty);
m_Password = Util.GetConfigVarFromSections<string>(config, "HttpAuthPassword", new string[] { "Network", section }, string.Empty);
string str = m_Username + ":" + m_Password;
byte[] encData_byte = Util.UTF8.GetBytes(str);
m_CredentialsB64 = Convert.ToBase64String(encData_byte);
m_log.DebugFormat("[HTTP BASIC AUTH]: {0} {1} [{2}]", m_Username, m_Password, section);
}
public void AddAuthorization(NameValueCollection headers)
{
//m_log.DebugFormat("[HTTP BASIC AUTH]: Adding authorization for {0}", remove_me);
headers["Authorization"] = "Basic " + m_CredentialsB64;
}
public bool Authenticate(string data)
{
string recovered = Util.Base64ToString(data);
if (!String.IsNullOrEmpty(recovered))
{
string[] parts = recovered.Split(new char[] { ':' });
if (parts.Length >= 2)
{
return m_Username.Equals(parts[0]) && m_Password.Equals(parts[1]);
}
}
return false;
}
public bool Authenticate(NameValueCollection requestHeaders, AddHeaderDelegate d)
{
//m_log.DebugFormat("[HTTP BASIC AUTH]: Authenticate in {0}", remove_me);
if (requestHeaders != null)
{
string value = requestHeaders.Get("Authorization");
if (value != null)
{
value = value.Trim();
if (value.StartsWith("Basic "))
{
value = value.Replace("Basic ", string.Empty);
if (Authenticate(value))
return true;
}
}
}
d("WWW-Authenticate", "Basic realm = \"Asset Server\"");
return false;
}
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace OpenSim.Framework.ServiceAuth
{
public delegate void AddHeaderDelegate(string key, string value);
public interface IServiceAuth
{
bool Authenticate(string data);
bool Authenticate(NameValueCollection headers, AddHeaderDelegate d);
void AddAuthorization(NameValueCollection headers);
}
}

View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using Nini.Config;
namespace OpenSim.Framework.ServiceAuth
{
public class ServiceAuth
{
public static IServiceAuth Create(IConfigSource config, string section)
{
string authType = Util.GetConfigVarFromSections<string>(config, "AuthType", new string[] { "Network", section }, "None");
switch (authType)
{
case "BasicHttpAuthentication":
return new BasicHttpAuthentication(config, section);
}
return null;
}
}
}

View File

@@ -45,6 +45,8 @@ using Nwc.XmlRpc;
using OpenMetaverse.StructuredData;
using XMLResponseHelper = OpenSim.Framework.SynchronousRestObjectRequester.XMLResponseHelper;
using OpenSim.Framework.ServiceAuth;
namespace OpenSim.Framework
{
/// <summary>
@@ -772,6 +774,13 @@ namespace OpenSim.Framework
public static void MakeRequest<TRequest, TResponse>(string verb,
string requestUrl, TRequest obj, Action<TResponse> action,
int maxConnections)
{
MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, action, maxConnections, null);
}
public static void MakeRequest<TRequest, TResponse>(string verb,
string requestUrl, TRequest obj, Action<TResponse> action,
int maxConnections, IServiceAuth auth)
{
int reqnum = WebUtil.RequestNumber++;
@@ -786,6 +795,10 @@ namespace OpenSim.Framework
WebRequest request = WebRequest.Create(requestUrl);
HttpWebRequest ht = (HttpWebRequest)request;
if (auth != null)
auth.AddAuthorization(ht.Headers);
if (maxConnections > 0 && ht.ServicePoint.ConnectionLimit < maxConnections)
ht.ServicePoint.ConnectionLimit = maxConnections;
@@ -969,7 +982,7 @@ namespace OpenSim.Framework
///
/// <exception cref="System.Net.WebException">Thrown if we encounter a network issue while posting
/// the request. You'll want to make sure you deal with this as they're not uncommon</exception>
public static string MakeRequest(string verb, string requestUrl, string obj, int timeoutsecs)
public static string MakeRequest(string verb, string requestUrl, string obj, int timeoutsecs, IServiceAuth auth)
{
int reqnum = WebUtil.RequestNumber++;
@@ -984,6 +997,10 @@ namespace OpenSim.Framework
request.Method = verb;
if (timeoutsecs > 0)
request.Timeout = timeoutsecs * 1000;
if (auth != null)
auth.AddAuthorization(request.Headers);
string respstring = String.Empty;
using (MemoryStream buffer = new MemoryStream())
@@ -1068,10 +1085,20 @@ namespace OpenSim.Framework
return respstring;
}
public static string MakeRequest(string verb, string requestUrl, string obj, int timeoutsecs)
{
return MakeRequest(verb, requestUrl, obj, timeoutsecs, null);
}
public static string MakeRequest(string verb, string requestUrl, string obj)
{
return MakeRequest(verb, requestUrl, obj, -1);
}
public static string MakeRequest(string verb, string requestUrl, string obj, IServiceAuth auth)
{
return MakeRequest(verb, requestUrl, obj, -1, auth);
}
}
public class SynchronousRestObjectRequester
@@ -1085,22 +1112,75 @@ namespace OpenSim.Framework
/// </summary>
/// <param name="verb"></param>
/// <param name="requestUrl"></param>
/// <param name="obj"> </param>
/// <returns></returns>
///
/// <exception cref="System.Net.WebException">Thrown if we encounter a network issue while posting
/// the request. You'll want to make sure you deal with this as they're not uncommon</exception>
/// <param name="obj"></param>
/// <returns>
/// The response. If there was an internal exception, then the default(TResponse) is returned.
/// </returns>
public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj)
{
return MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, 0);
}
public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj, IServiceAuth auth)
{
return MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, 0, auth);
}
/// <summary>
/// Perform a synchronous REST request.
/// </summary>
/// <param name="verb"></param>
/// <param name="requestUrl"></param>
/// <param name="obj"></param>
/// <param name="pTimeout">
/// Request timeout in milliseconds. Timeout.Infinite indicates no timeout. If 0 is passed then the default HttpWebRequest timeout is used (100 seconds)
/// </param>
/// <returns>
/// The response. If there was an internal exception or the request timed out,
/// then the default(TResponse) is returned.
/// </returns>
public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj, int pTimeout)
{
return MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, pTimeout, 0);
}
public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj, int pTimeout, IServiceAuth auth)
{
return MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, pTimeout, 0, auth);
}
/// Perform a synchronous REST request.
/// </summary>
/// <param name="verb"></param>
/// <param name="requestUrl"></param>
/// <param name="obj"></param>
/// <param name="pTimeout">
/// Request timeout in milliseconds. Timeout.Infinite indicates no timeout. If 0 is passed then the default HttpWebRequest timeout is used (100 seconds)
/// </param>
/// <param name="maxConnections"></param>
/// <returns>
/// The response. If there was an internal exception or the request timed out,
/// then the default(TResponse) is returned.
/// </returns>
public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj, int pTimeout, int maxConnections)
{
return MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, pTimeout, maxConnections, null);
}
/// <summary>
/// Perform a synchronous REST request.
/// </summary>
/// <param name="verb"></param>
/// <param name="requestUrl"></param>
/// <param name="obj"></param>
/// <param name="pTimeout">
/// Request timeout in milliseconds. Timeout.Infinite indicates no timeout. If 0 is passed then the default HttpWebRequest timeout is used (100 seconds)
/// </param>
/// <param name="maxConnections"></param>
/// <returns>
/// The response. If there was an internal exception or the request timed out,
/// then the default(TResponse) is returned.
/// </returns>
public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj, int pTimeout, int maxConnections, IServiceAuth auth)
{
int reqnum = WebUtil.RequestNumber++;
@@ -1116,6 +1196,13 @@ namespace OpenSim.Framework
WebRequest request = WebRequest.Create(requestUrl);
HttpWebRequest ht = (HttpWebRequest)request;
if (auth != null)
auth.AddAuthorization(ht.Headers);
if (pTimeout != 0)
ht.Timeout = pTimeout;
if (maxConnections > 0 && ht.ServicePoint.ConnectionLimit < maxConnections)
ht.ServicePoint.ConnectionLimit = maxConnections;
@@ -1191,8 +1278,18 @@ namespace OpenSim.Framework
{
using (HttpWebResponse hwr = (HttpWebResponse)e.Response)
{
if (hwr != null && hwr.StatusCode == HttpStatusCode.NotFound)
return deserial;
if (hwr != null)
{
if (hwr.StatusCode == HttpStatusCode.NotFound)
return deserial;
if (hwr.StatusCode == HttpStatusCode.Unauthorized)
{
m_log.Error(string.Format(
"[SynchronousRestObjectRequester]: Web request {0} requires authentication ",
requestUrl));
return deserial;
}
}
else
m_log.Error(string.Format(
"[SynchronousRestObjectRequester]: WebException for {0} {1} {2} ",