Revert "Refactor scripted http request to use async calls rather than hard threads"

This reverts commit 461ecd7cf9.
avinationmerge
Melanie 2013-01-20 15:58:20 +01:00
parent 461ecd7cf9
commit cf4bf7432a
1 changed files with 55 additions and 89 deletions

View File

@ -206,7 +206,7 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
m_pendingRequests.Add(reqID, htc); m_pendingRequests.Add(reqID, htc);
} }
htc.SendRequest(); htc.Process();
return reqID; return reqID;
} }
@ -340,6 +340,7 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
public string HttpMIMEType = "text/plain;charset=utf-8"; public string HttpMIMEType = "text/plain;charset=utf-8";
public int HttpTimeout; public int HttpTimeout;
public bool HttpVerifyCert = true; public bool HttpVerifyCert = true;
private Thread httpThread;
// Request info // Request info
private UUID _itemID; private UUID _itemID;
@ -373,11 +374,27 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
public void Process() public void Process()
{ {
SendRequest(); httpThread = new Thread(SendRequest);
httpThread.Name = "HttpRequestThread";
httpThread.Priority = ThreadPriority.BelowNormal;
httpThread.IsBackground = true;
_finished = false;
httpThread.Start();
} }
/*
* 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;
StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];
string tempString = null;
int count = 0;
try try
{ {
Request = (HttpWebRequest) WebRequest.Create(Url); Request = (HttpWebRequest) WebRequest.Create(Url);
@ -392,9 +409,13 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
{ {
// We could hijack Connection Group Name to identify // We could hijack Connection Group Name to identify
// a desired security exception. But at the moment we'll use a dummy header instead. // a desired security exception. But at the moment we'll use a dummy header instead.
// Request.ConnectionGroupName = "NoVerify";
Request.Headers.Add("NoVerifyCert", "true"); Request.Headers.Add("NoVerifyCert", "true");
} }
// else
// {
// Request.ConnectionGroupName="Verify";
// }
if (proxyurl != null && proxyurl.Length > 0) if (proxyurl != null && proxyurl.Length > 0)
{ {
if (proxyexcepts != null && proxyexcepts.Length > 0) if (proxyexcepts != null && proxyexcepts.Length > 0)
@ -409,14 +430,10 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
} }
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.UserAgent = entry.Value; Request.UserAgent = entry.Value;
else else
Request.Headers[entry.Key] = entry.Value; Request.Headers[entry.Key] = entry.Value;
}
Request.Timeout = HttpTimeout;
// Encode outbound data // Encode outbound data
if (OutboundBody.Length > 0) if (OutboundBody.Length > 0)
@ -424,78 +441,16 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
byte[] data = Util.UTF8.GetBytes(OutboundBody); byte[] data = Util.UTF8.GetBytes(OutboundBody);
Request.ContentLength = data.Length; Request.ContentLength = data.Length;
Request.BeginGetRequestStream(new AsyncCallback(SendRequestData), this); Stream bstream = Request.GetRequestStream();
return;
}
Request.BeginGetResponse(new AsyncCallback(ProcessResponse), this);
}
catch (Exception e)
{
// Do nothing. Abort was called.
}
}
private void SendRequestData(IAsyncResult ar)
{
try
{
byte[] data = Util.UTF8.GetBytes(OutboundBody);
Stream bstream = Request.EndGetRequestStream(ar);
bstream.Write(data, 0, data.Length); bstream.Write(data, 0, data.Length);
bstream.Close(); bstream.Close();
Request.BeginGetResponse(new AsyncCallback(ProcessResponse), this);
} }
catch (WebException e)
{
// Abort was called. Just go away
if (e.Status == WebExceptionStatus.RequestCanceled)
return;
// Server errored on request
if (e.Status == WebExceptionStatus.ProtocolError && e.Response != null)
{
HttpWebResponse webRsp = (HttpWebResponse)e.Response;
Status = (int)webRsp.StatusCode;
Request.Timeout = HttpTimeout;
try try
{ {
using (Stream responseStream = webRsp.GetResponseStream()) // execute the request
{ response = (HttpWebResponse) Request.GetResponse();
ResponseBody = responseStream.GetStreamString();
}
}
catch
{
ResponseBody = webRsp.StatusDescription;
}
finally
{
webRsp.Close();
}
return;
}
_finished = true;
}
}
private void ProcessResponse(IAsyncResult ar)
{
HttpWebResponse response = null;
StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];
string tempString = null;
int count = 0;
try
{
try
{
response = (HttpWebResponse) Request.EndGetResponse(ar);
} }
catch (WebException e) catch (WebException e)
{ {
@ -530,21 +485,32 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
ResponseBody = sb.ToString().Replace("\r", ""); ResponseBody = sb.ToString().Replace("\r", "");
} }
catch (WebException e)
{
// Abort was called. Just go away
if (e.Status == WebExceptionStatus.RequestCanceled)
return;
Status = (int)OSHttpStatusCode.ClientErrorJoker;
ResponseBody = e.Message;
_finished = true;
return;
}
catch (Exception e) catch (Exception e)
{ {
// Ignore if (e is WebException && ((WebException)e).Status == WebExceptionStatus.ProtocolError)
{
HttpWebResponse webRsp = (HttpWebResponse)((WebException)e).Response;
Status = (int)webRsp.StatusCode;
try
{
using (Stream responseStream = webRsp.GetResponseStream())
{
ResponseBody = responseStream.GetStreamString();
}
}
catch
{
ResponseBody = webRsp.StatusDescription;
}
}
else
{
Status = (int)OSHttpStatusCode.ClientErrorJoker;
ResponseBody = e.Message;
}
_finished = true;
return;
} }
finally finally
{ {
@ -559,7 +525,7 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
{ {
try try
{ {
Request.Abort(); httpThread.Abort();
} }
catch (Exception) catch (Exception)
{ {