aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorMelanie2013-01-20 02:08:38 +0100
committerMelanie2013-01-20 02:08:38 +0100
commit461ecd7cf9edebce45ab715901a7f3b02c216e7b (patch)
tree8f5ae911c4d0113a30565a6fa6ea5372118a5b60
parentRemove an extra ScriptSleep (merge artefact) from llSetLinkPrimitiveParamsFast (diff)
downloadopensim-SC-461ecd7cf9edebce45ab715901a7f3b02c216e7b.zip
opensim-SC-461ecd7cf9edebce45ab715901a7f3b02c216e7b.tar.gz
opensim-SC-461ecd7cf9edebce45ab715901a7f3b02c216e7b.tar.bz2
opensim-SC-461ecd7cf9edebce45ab715901a7f3b02c216e7b.tar.xz
Refactor scripted http request to use async calls rather than hard threads
-rw-r--r--OpenSim/Region/CoreModules/Scripting/HttpRequest/ScriptsHttpRequests.cs140
1 files changed, 87 insertions, 53 deletions
diff --git a/OpenSim/Region/CoreModules/Scripting/HttpRequest/ScriptsHttpRequests.cs b/OpenSim/Region/CoreModules/Scripting/HttpRequest/ScriptsHttpRequests.cs
index a0ae203..5b39794 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
206 m_pendingRequests.Add(reqID, htc); 206 m_pendingRequests.Add(reqID, htc);
207 } 207 }
208 208
209 htc.Process(); 209 htc.SendRequest();
210 210
211 return reqID; 211 return reqID;
212 } 212 }
@@ -340,7 +340,6 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
340 public string HttpMIMEType = "text/plain;charset=utf-8"; 340 public string HttpMIMEType = "text/plain;charset=utf-8";
341 public int HttpTimeout; 341 public int HttpTimeout;
342 public bool HttpVerifyCert = true; 342 public bool HttpVerifyCert = true;
343 private Thread httpThread;
344 343
345 // Request info 344 // Request info
346 private UUID _itemID; 345 private UUID _itemID;
@@ -374,27 +373,11 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
374 373
375 public void Process() 374 public void Process()
376 { 375 {
377 httpThread = new Thread(SendRequest); 376 SendRequest();
378 httpThread.Name = "HttpRequestThread";
379 httpThread.Priority = ThreadPriority.BelowNormal;
380 httpThread.IsBackground = true;
381 _finished = false;
382 httpThread.Start();
383 } 377 }
384 378
385 /*
386 * TODO: More work on the response codes. Right now
387 * returning 200 for success or 499 for exception
388 */
389
390 public void SendRequest() 379 public void SendRequest()
391 { 380 {
392 HttpWebResponse response = null;
393 StringBuilder sb = new StringBuilder();
394 byte[] buf = new byte[8192];
395 string tempString = null;
396 int count = 0;
397
398 try 381 try
399 { 382 {
400 Request = (HttpWebRequest) WebRequest.Create(Url); 383 Request = (HttpWebRequest) WebRequest.Create(Url);
@@ -409,13 +392,9 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
409 { 392 {
410 // We could hijack Connection Group Name to identify 393 // We could hijack Connection Group Name to identify
411 // a desired security exception. But at the moment we'll use a dummy header instead. 394 // a desired security exception. But at the moment we'll use a dummy header instead.
412// Request.ConnectionGroupName = "NoVerify";
413 Request.Headers.Add("NoVerifyCert", "true"); 395 Request.Headers.Add("NoVerifyCert", "true");
414 } 396 }
415// else 397
416// {
417// Request.ConnectionGroupName="Verify";
418// }
419 if (proxyurl != null && proxyurl.Length > 0) 398 if (proxyurl != null && proxyurl.Length > 0)
420 { 399 {
421 if (proxyexcepts != null && proxyexcepts.Length > 0) 400 if (proxyexcepts != null && proxyexcepts.Length > 0)
@@ -430,10 +409,14 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
430 } 409 }
431 410
432 foreach (KeyValuePair<string, string> entry in ResponseHeaders) 411 foreach (KeyValuePair<string, string> entry in ResponseHeaders)
412 {
433 if (entry.Key.ToLower().Equals("user-agent")) 413 if (entry.Key.ToLower().Equals("user-agent"))
434 Request.UserAgent = entry.Value; 414 Request.UserAgent = entry.Value;
435 else 415 else
436 Request.Headers[entry.Key] = entry.Value; 416 Request.Headers[entry.Key] = entry.Value;
417 }
418
419 Request.Timeout = HttpTimeout;
437 420
438 // Encode outbound data 421 // Encode outbound data
439 if (OutboundBody.Length > 0) 422 if (OutboundBody.Length > 0)
@@ -441,16 +424,78 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
441 byte[] data = Util.UTF8.GetBytes(OutboundBody); 424 byte[] data = Util.UTF8.GetBytes(OutboundBody);
442 425
443 Request.ContentLength = data.Length; 426 Request.ContentLength = data.Length;
444 Stream bstream = Request.GetRequestStream(); 427 Request.BeginGetRequestStream(new AsyncCallback(SendRequestData), this);
445 bstream.Write(data, 0, data.Length); 428 return;
446 bstream.Close();
447 } 429 }
448 430
449 Request.Timeout = HttpTimeout; 431 Request.BeginGetResponse(new AsyncCallback(ProcessResponse), this);
432 }
433 catch (Exception e)
434 {
435 // Do nothing. Abort was called.
436 }
437 }
438
439 private void SendRequestData(IAsyncResult ar)
440 {
441 try
442 {
443 byte[] data = Util.UTF8.GetBytes(OutboundBody);
444
445 Stream bstream = Request.EndGetRequestStream(ar);
446 bstream.Write(data, 0, data.Length);
447 bstream.Close();
448
449 Request.BeginGetResponse(new AsyncCallback(ProcessResponse), this);
450 }
451 catch (WebException e)
452 {
453 // Abort was called. Just go away
454 if (e.Status == WebExceptionStatus.RequestCanceled)
455 return;
456
457 // Server errored on request
458 if (e.Status == WebExceptionStatus.ProtocolError && e.Response != null)
459 {
460 HttpWebResponse webRsp = (HttpWebResponse)e.Response;
461
462 Status = (int)webRsp.StatusCode;
463
464 try
465 {
466 using (Stream responseStream = webRsp.GetResponseStream())
467 {
468 ResponseBody = responseStream.GetStreamString();
469 }
470 }
471 catch
472 {
473 ResponseBody = webRsp.StatusDescription;
474 }
475 finally
476 {
477 webRsp.Close();
478 }
479 return;
480 }
481
482 _finished = true;
483 }
484 }
485
486 private void ProcessResponse(IAsyncResult ar)
487 {
488 HttpWebResponse response = null;
489 StringBuilder sb = new StringBuilder();
490 byte[] buf = new byte[8192];
491 string tempString = null;
492 int count = 0;
493
494 try
495 {
450 try 496 try
451 { 497 {
452 // execute the request 498 response = (HttpWebResponse) Request.EndGetResponse(ar);
453 response = (HttpWebResponse) Request.GetResponse();
454 } 499 }
455 catch (WebException e) 500 catch (WebException e)
456 { 501 {
@@ -485,33 +530,22 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
485 530
486 ResponseBody = sb.ToString().Replace("\r", ""); 531 ResponseBody = sb.ToString().Replace("\r", "");
487 } 532 }
488 catch (Exception e) 533 catch (WebException e)
489 { 534 {
490 if (e is WebException && ((WebException)e).Status == WebExceptionStatus.ProtocolError) 535 // Abort was called. Just go away
491 { 536 if (e.Status == WebExceptionStatus.RequestCanceled)
492 HttpWebResponse webRsp = (HttpWebResponse)((WebException)e).Response; 537 return;
493 Status = (int)webRsp.StatusCode; 538
494 try 539 Status = (int)OSHttpStatusCode.ClientErrorJoker;
495 {
496 using (Stream responseStream = webRsp.GetResponseStream())
497 {
498 ResponseBody = responseStream.GetStreamString();
499 }
500 }
501 catch
502 {
503 ResponseBody = webRsp.StatusDescription;
504 }
505 }
506 else
507 {
508 Status = (int)OSHttpStatusCode.ClientErrorJoker;
509 ResponseBody = e.Message;
510 }
511 540
541 ResponseBody = e.Message;
512 _finished = true; 542 _finished = true;
513 return; 543 return;
514 } 544 }
545 catch (Exception e)
546 {
547 // Ignore
548 }
515 finally 549 finally
516 { 550 {
517 if (response != null) 551 if (response != null)
@@ -525,7 +559,7 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
525 { 559 {
526 try 560 try
527 { 561 {
528 httpThread.Abort(); 562 Request.Abort();
529 } 563 }
530 catch (Exception) 564 catch (Exception)
531 { 565 {