Fix regression where llHTTPRequests which did not get an OK response returned 499 and the exception message in the http_response event rather than the actual response code and body.

This was a regression since commit 831e4c3 (Thu Apr 4 00:36:15 2013)
This commit also adds a regression test for this case, though this currently only works with Mono
This aims to address http://opensimulator.org/mantis/view.php?id=6704
cpu-performance
Justin Clark-Casey (justincc) 2013-07-11 23:02:30 +01:00
parent ea371a6f54
commit 44e9849ed1
4 changed files with 245 additions and 41 deletions

View File

@ -141,6 +141,11 @@ namespace OpenSim.Framework
public static FireAndForgetMethod DefaultFireAndForgetMethod = FireAndForgetMethod.SmartThreadPool; public static FireAndForgetMethod DefaultFireAndForgetMethod = FireAndForgetMethod.SmartThreadPool;
public static FireAndForgetMethod FireAndForgetMethod = DefaultFireAndForgetMethod; public static FireAndForgetMethod FireAndForgetMethod = DefaultFireAndForgetMethod;
public static bool IsPlatformMono
{
get { return Type.GetType("Mono.Runtime") != null; }
}
/// <summary> /// <summary>
/// Gets the name of the directory where the current running executable /// Gets the name of the directory where the current running executable
/// is located /// is located
@ -1326,7 +1331,7 @@ namespace OpenSim.Framework
ru = "OSX/Mono"; ru = "OSX/Mono";
else else
{ {
if (Type.GetType("Mono.Runtime") != null) if (IsPlatformMono)
ru = "Win/Mono"; ru = "Win/Mono";
else else
ru = "Win/.NET"; ru = "Win/.NET";

View File

@ -419,7 +419,7 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
get { return _reqID; } get { return _reqID; }
set { _reqID = value; } set { _reqID = value; }
} }
public HttpWebRequest Request; public WebRequest Request;
public string ResponseBody; public string ResponseBody;
public List<string> ResponseMetadata; public List<string> ResponseMetadata;
public Dictionary<string, string> ResponseHeaders; public Dictionary<string, string> ResponseHeaders;
@ -431,18 +431,11 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
SendRequest(); SendRequest();
} }
/*
* TODO: More work on the response codes. Right now
* returning 200 for success or 499 for exception
*/
public void SendRequest() public void SendRequest()
{ {
HttpWebResponse response = null;
try try
{ {
Request = (HttpWebRequest) WebRequest.Create(Url); Request = WebRequest.Create(Url);
Request.Method = HttpMethod; Request.Method = HttpMethod;
Request.ContentType = HttpMIMEType; Request.ContentType = HttpMIMEType;
@ -480,14 +473,17 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
} }
} }
if (ResponseHeaders != null)
{
foreach (KeyValuePair<string, string> entry in ResponseHeaders) foreach (KeyValuePair<string, string> entry in ResponseHeaders)
if (entry.Key.ToLower().Equals("user-agent")) if (entry.Key.ToLower().Equals("user-agent") && Request is HttpWebRequest)
Request.UserAgent = entry.Value; ((HttpWebRequest)Request).UserAgent = entry.Value;
else else
Request.Headers[entry.Key] = entry.Value; Request.Headers[entry.Key] = entry.Value;
}
// Encode outbound data // Encode outbound data
if (OutboundBody.Length > 0) if (OutboundBody != null && OutboundBody.Length > 0)
{ {
byte[] data = Util.UTF8.GetBytes(OutboundBody); byte[] data = Util.UTF8.GetBytes(OutboundBody);
@ -510,12 +506,19 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
{ {
throw; throw;
} }
response = (HttpWebResponse)e.Response;
HttpWebResponse response = (HttpWebResponse)e.Response;
Status = (int)response.StatusCode;
ResponseBody = response.StatusDescription;
_finished = true; _finished = true;
} }
} }
catch (Exception e) catch (Exception e)
{ {
// m_log.Debug(
// string.Format("[SCRIPTS HTTP REQUESTS]: Exception on request to {0} for {1} ", Url, ItemID), e);
Status = (int)OSHttpStatusCode.ClientErrorJoker; Status = (int)OSHttpStatusCode.ClientErrorJoker;
ResponseBody = e.Message; ResponseBody = e.Message;
_finished = true; _finished = true;
@ -526,36 +529,30 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
{ {
HttpWebResponse response = null; HttpWebResponse response = null;
try
{
try try
{ {
response = (HttpWebResponse)Request.EndGetResponse(ar); response = (HttpWebResponse)Request.EndGetResponse(ar);
}
catch (WebException e)
{
if (e.Status != WebExceptionStatus.ProtocolError)
{
throw;
}
response = (HttpWebResponse)e.Response;
}
Status = (int)response.StatusCode; Status = (int)response.StatusCode;
Stream resStream = response.GetResponseStream(); using (Stream stream = response.GetResponseStream())
StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];
string tempString = null;
int count = 0;
do
{ {
// fill the buffer with data StreamReader reader = new StreamReader(stream, Encoding.UTF8);
count = resStream.Read(buf, 0, buf.Length); ResponseBody = reader.ReadToEnd();
// make sure we read some data
if (count != 0)
{
// translate from bytes to ASCII text
tempString = Util.UTF8.GetString(buf, 0, count);
// continue building the string
sb.Append(tempString);
} }
} }
while (count > 0); // any more data to read?
ResponseBody = sb.ToString();
}
catch (Exception e) catch (Exception e)
{ {
Status = (int)OSHttpStatusCode.ClientErrorJoker; Status = (int)OSHttpStatusCode.ClientErrorJoker;

View File

@ -0,0 +1,201 @@
/*
* Copyright (c) Contributors, http://opensimulator.org/
* See CONTRIBUTORS.TXT for a full list of copyright holders.
*
* 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.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the OpenSimulator Project 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 DEVELOPERS ``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 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.IO;
using System.Net;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using log4net.Config;
using NUnit.Framework;
using OpenMetaverse;
using OpenMetaverse.Assets;
using OpenSim.Framework;
using OpenSim.Region.CoreModules.Scripting.HttpRequest;
using OpenSim.Region.Framework.Scenes;
using OpenSim.Tests.Common;
using OpenSim.Tests.Common.Mock;
namespace OpenSim.Region.CoreModules.Scripting.HttpRequest.Tests
{
class TestWebRequestCreate : IWebRequestCreate
{
public TestWebRequest NextRequest { get; set; }
public WebRequest Create(Uri uri)
{
// NextRequest.RequestUri = uri;
return NextRequest;
// return new TestWebRequest(new SerializationInfo(typeof(TestWebRequest), new FormatterConverter()), new StreamingContext());
}
}
class TestWebRequest : WebRequest
{
public override string ContentType { get; set; }
public override string Method { get; set; }
public Func<IAsyncResult, WebResponse> OnEndGetResponse { get; set; }
public TestWebRequest() : base()
{
// Console.WriteLine("created");
}
// public TestWebRequest(SerializationInfo serializationInfo, StreamingContext streamingContext)
// : base(serializationInfo, streamingContext)
// {
// Console.WriteLine("created");
// }
public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state)
{
// Console.WriteLine("bish");
TestAsyncResult tasr = new TestAsyncResult();
callback(tasr);
return tasr;
}
public override WebResponse EndGetResponse(IAsyncResult asyncResult)
{
// Console.WriteLine("bosh");
return OnEndGetResponse(asyncResult);
}
}
class TestHttpWebResponse : HttpWebResponse
{
public string Response { get; set; }
public TestHttpWebResponse(SerializationInfo serializationInfo, StreamingContext streamingContext)
: base(serializationInfo, streamingContext) {}
public override Stream GetResponseStream()
{
return new MemoryStream(Encoding.UTF8.GetBytes(Response));
}
}
class TestAsyncResult : IAsyncResult
{
WaitHandle m_wh = new ManualResetEvent(true);
object IAsyncResult.AsyncState
{
get {
throw new System.NotImplementedException ();
}
}
WaitHandle IAsyncResult.AsyncWaitHandle
{
get { return m_wh; }
}
bool IAsyncResult.CompletedSynchronously
{
get { return false; }
}
bool IAsyncResult.IsCompleted
{
get { return true; }
}
}
/// <summary>
/// Test script http request code.
/// </summary>
/// <remarks>
/// This class uses some very hacky workarounds in order to mock HttpWebResponse which are Mono dependent (though
/// alternative code can be written to make this work for Windows). However, the value of being able to
/// regression test this kind of code is very high.
/// </remarks>
[TestFixture]
public class ScriptsHttpRequestsTests : OpenSimTestCase
{
/// <summary>
/// Test what happens when we get a 404 response from a call.
/// </summary>
[Test]
public void Test404Response()
{
TestHelpers.InMethod();
TestHelpers.EnableLogging();
if (!Util.IsPlatformMono)
Assert.Ignore("Ignoring test since can only currently run on Mono");
string rawResponse = "boom";
TestWebRequestCreate twrc = new TestWebRequestCreate();
TestWebRequest twr = new TestWebRequest();
//twr.OnEndGetResponse += ar => new TestHttpWebResponse(null, new StreamingContext());
twr.OnEndGetResponse += ar =>
{
SerializationInfo si = new SerializationInfo(typeof(HttpWebResponse), new FormatterConverter());
StreamingContext sc = new StreamingContext();
// WebHeaderCollection headers = new WebHeaderCollection();
// si.AddValue("m_HttpResponseHeaders", headers);
si.AddValue("uri", new Uri("test://arrg"));
// si.AddValue("m_Certificate", null);
si.AddValue("version", HttpVersion.Version11);
si.AddValue("statusCode", HttpStatusCode.NotFound);
si.AddValue("contentLength", 0);
si.AddValue("method", "GET");
si.AddValue("statusDescription", "Not Found");
si.AddValue("contentType", null);
si.AddValue("cookieCollection", new CookieCollection());
TestHttpWebResponse thwr = new TestHttpWebResponse(si, sc);
thwr.Response = rawResponse;
throw new WebException("no message", null, WebExceptionStatus.ProtocolError, thwr);
};
twrc.NextRequest = twr;
WebRequest.RegisterPrefix("test", twrc);
HttpRequestClass hr = new HttpRequestClass();
hr.Url = "test://something";
hr.SendRequest();
while (!hr.Finished)
Thread.Sleep(100);
Assert.That(hr.Status, Is.EqualTo((int)HttpStatusCode.NotFound));
Assert.That(hr.ResponseBody, Is.EqualTo(rawResponse));
}
}
}

View File

@ -3102,6 +3102,7 @@
<Match path="Avatar/Inventory/Transfer/Tests" pattern="*.cs" recurse="true"/> <Match path="Avatar/Inventory/Transfer/Tests" pattern="*.cs" recurse="true"/>
<Match path="Framework/InventoryAccess/Tests" pattern="*.cs" recurse="true"/> <Match path="Framework/InventoryAccess/Tests" pattern="*.cs" recurse="true"/>
<Match path="Framework/UserManagement/Tests" pattern="*.cs" recurse="true"/> <Match path="Framework/UserManagement/Tests" pattern="*.cs" recurse="true"/>
<Match path="Scripting/HttpRequest/Tests" pattern="*.cs" recurse="true"/>
<Match path="Scripting/VectorRender/Tests" pattern="*.cs" recurse="true"/> <Match path="Scripting/VectorRender/Tests" pattern="*.cs" recurse="true"/>
<Match path="World/Archiver/Tests" pattern="*.cs" recurse="true"/> <Match path="World/Archiver/Tests" pattern="*.cs" recurse="true"/>
<Match buildAction="EmbeddedResource" path="World/Archiver/Tests/Resources" pattern="*"/> <Match buildAction="EmbeddedResource" path="World/Archiver/Tests/Resources" pattern="*"/>