From cf4bf7432a545888e3af8f540f65092aea6f2686 Mon Sep 17 00:00:00 2001 From: Melanie Date: Sun, 20 Jan 2013 15:58:20 +0100 Subject: Revert "Refactor scripted http request to use async calls rather than hard threads" This reverts commit 461ecd7cf9edebce45ab715901a7f3b02c216e7b. --- .../Scripting/HttpRequest/ScriptsHttpRequests.cs | 140 ++++++++------------- 1 file changed, 53 insertions(+), 87 deletions(-) diff --git a/OpenSim/Region/CoreModules/Scripting/HttpRequest/ScriptsHttpRequests.cs b/OpenSim/Region/CoreModules/Scripting/HttpRequest/ScriptsHttpRequests.cs index 5b39794..a0ae203 100644 --- a/OpenSim/Region/CoreModules/Scripting/HttpRequest/ScriptsHttpRequests.cs +++ b/OpenSim/Region/CoreModules/Scripting/HttpRequest/ScriptsHttpRequests.cs @@ -206,7 +206,7 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest m_pendingRequests.Add(reqID, htc); } - htc.SendRequest(); + htc.Process(); return reqID; } @@ -340,6 +340,7 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest public string HttpMIMEType = "text/plain;charset=utf-8"; public int HttpTimeout; public bool HttpVerifyCert = true; + private Thread httpThread; // Request info private UUID _itemID; @@ -373,11 +374,27 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest 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() { + HttpWebResponse response = null; + StringBuilder sb = new StringBuilder(); + byte[] buf = new byte[8192]; + string tempString = null; + int count = 0; + try { Request = (HttpWebRequest) WebRequest.Create(Url); @@ -392,9 +409,13 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest { // We could hijack Connection Group Name to identify // a desired security exception. But at the moment we'll use a dummy header instead. +// Request.ConnectionGroupName = "NoVerify"; Request.Headers.Add("NoVerifyCert", "true"); } - +// else +// { +// Request.ConnectionGroupName="Verify"; +// } if (proxyurl != null && proxyurl.Length > 0) { if (proxyexcepts != null && proxyexcepts.Length > 0) @@ -409,14 +430,10 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest } foreach (KeyValuePair entry in ResponseHeaders) - { if (entry.Key.ToLower().Equals("user-agent")) Request.UserAgent = entry.Value; else Request.Headers[entry.Key] = entry.Value; - } - - Request.Timeout = HttpTimeout; // Encode outbound data if (OutboundBody.Length > 0) @@ -424,78 +441,16 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest byte[] data = Util.UTF8.GetBytes(OutboundBody); Request.ContentLength = data.Length; - Request.BeginGetRequestStream(new AsyncCallback(SendRequestData), this); - return; + Stream bstream = Request.GetRequestStream(); + bstream.Write(data, 0, data.Length); + bstream.Close(); } - 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.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; - - try - { - using (Stream responseStream = webRsp.GetResponseStream()) - { - 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 - { + Request.Timeout = HttpTimeout; try { - response = (HttpWebResponse) Request.EndGetResponse(ar); + // execute the request + response = (HttpWebResponse) Request.GetResponse(); } catch (WebException e) { @@ -530,22 +485,33 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest ResponseBody = sb.ToString().Replace("\r", ""); } - catch (WebException e) + catch (Exception e) { - // Abort was called. Just go away - if (e.Status == WebExceptionStatus.RequestCanceled) - return; - - Status = (int)OSHttpStatusCode.ClientErrorJoker; + 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; + } - ResponseBody = e.Message; _finished = true; return; } - catch (Exception e) - { - // Ignore - } finally { if (response != null) @@ -559,7 +525,7 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest { try { - Request.Abort(); + httpThread.Abort(); } catch (Exception) { -- cgit v1.1