diff options
author | Melanie | 2013-01-20 15:58:20 +0100 |
---|---|---|
committer | Melanie | 2013-01-20 15:58:20 +0100 |
commit | cf4bf7432a545888e3af8f540f65092aea6f2686 (patch) | |
tree | e6ccfdceb8b7ec1ab82c273e89b5b465798756ee /OpenSim/Region/CoreModules | |
parent | Refactor scripted http request to use async calls rather than hard threads (diff) | |
download | opensim-SC_OLD-cf4bf7432a545888e3af8f540f65092aea6f2686.zip opensim-SC_OLD-cf4bf7432a545888e3af8f540f65092aea6f2686.tar.gz opensim-SC_OLD-cf4bf7432a545888e3af8f540f65092aea6f2686.tar.bz2 opensim-SC_OLD-cf4bf7432a545888e3af8f540f65092aea6f2686.tar.xz |
Revert "Refactor scripted http request to use async calls rather than hard threads"
This reverts commit 461ecd7cf9edebce45ab715901a7f3b02c216e7b.
Diffstat (limited to 'OpenSim/Region/CoreModules')
-rw-r--r-- | OpenSim/Region/CoreModules/Scripting/HttpRequest/ScriptsHttpRequests.cs | 140 |
1 files 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 | |||
206 | m_pendingRequests.Add(reqID, htc); | 206 | m_pendingRequests.Add(reqID, htc); |
207 | } | 207 | } |
208 | 208 | ||
209 | htc.SendRequest(); | 209 | htc.Process(); |
210 | 210 | ||
211 | return reqID; | 211 | return reqID; |
212 | } | 212 | } |
@@ -340,6 +340,7 @@ 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; | ||
343 | 344 | ||
344 | // Request info | 345 | // Request info |
345 | private UUID _itemID; | 346 | private UUID _itemID; |
@@ -373,11 +374,27 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest | |||
373 | 374 | ||
374 | public void Process() | 375 | public void Process() |
375 | { | 376 | { |
376 | SendRequest(); | 377 | httpThread = new Thread(SendRequest); |
378 | httpThread.Name = "HttpRequestThread"; | ||
379 | httpThread.Priority = ThreadPriority.BelowNormal; | ||
380 | httpThread.IsBackground = true; | ||
381 | _finished = false; | ||
382 | httpThread.Start(); | ||
377 | } | 383 | } |
378 | 384 | ||
385 | /* | ||
386 | * TODO: More work on the response codes. Right now | ||
387 | * returning 200 for success or 499 for exception | ||
388 | */ | ||
389 | |||
379 | public void SendRequest() | 390 | public void SendRequest() |
380 | { | 391 | { |
392 | HttpWebResponse response = null; | ||
393 | StringBuilder sb = new StringBuilder(); | ||
394 | byte[] buf = new byte[8192]; | ||
395 | string tempString = null; | ||
396 | int count = 0; | ||
397 | |||
381 | try | 398 | try |
382 | { | 399 | { |
383 | Request = (HttpWebRequest) WebRequest.Create(Url); | 400 | Request = (HttpWebRequest) WebRequest.Create(Url); |
@@ -392,9 +409,13 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest | |||
392 | { | 409 | { |
393 | // We could hijack Connection Group Name to identify | 410 | // We could hijack Connection Group Name to identify |
394 | // a desired security exception. But at the moment we'll use a dummy header instead. | 411 | // a desired security exception. But at the moment we'll use a dummy header instead. |
412 | // Request.ConnectionGroupName = "NoVerify"; | ||
395 | Request.Headers.Add("NoVerifyCert", "true"); | 413 | Request.Headers.Add("NoVerifyCert", "true"); |
396 | } | 414 | } |
397 | 415 | // else | |
416 | // { | ||
417 | // Request.ConnectionGroupName="Verify"; | ||
418 | // } | ||
398 | if (proxyurl != null && proxyurl.Length > 0) | 419 | if (proxyurl != null && proxyurl.Length > 0) |
399 | { | 420 | { |
400 | if (proxyexcepts != null && proxyexcepts.Length > 0) | 421 | if (proxyexcepts != null && proxyexcepts.Length > 0) |
@@ -409,14 +430,10 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest | |||
409 | } | 430 | } |
410 | 431 | ||
411 | foreach (KeyValuePair<string, string> entry in ResponseHeaders) | 432 | foreach (KeyValuePair<string, string> entry in ResponseHeaders) |
412 | { | ||
413 | if (entry.Key.ToLower().Equals("user-agent")) | 433 | if (entry.Key.ToLower().Equals("user-agent")) |
414 | Request.UserAgent = entry.Value; | 434 | Request.UserAgent = entry.Value; |
415 | else | 435 | else |
416 | Request.Headers[entry.Key] = entry.Value; | 436 | Request.Headers[entry.Key] = entry.Value; |
417 | } | ||
418 | |||
419 | Request.Timeout = HttpTimeout; | ||
420 | 437 | ||
421 | // Encode outbound data | 438 | // Encode outbound data |
422 | if (OutboundBody.Length > 0) | 439 | if (OutboundBody.Length > 0) |
@@ -424,78 +441,16 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest | |||
424 | byte[] data = Util.UTF8.GetBytes(OutboundBody); | 441 | byte[] data = Util.UTF8.GetBytes(OutboundBody); |
425 | 442 | ||
426 | Request.ContentLength = data.Length; | 443 | Request.ContentLength = data.Length; |
427 | Request.BeginGetRequestStream(new AsyncCallback(SendRequestData), this); | 444 | Stream bstream = Request.GetRequestStream(); |
428 | return; | 445 | bstream.Write(data, 0, data.Length); |
446 | bstream.Close(); | ||
429 | } | 447 | } |
430 | 448 | ||
431 | Request.BeginGetResponse(new AsyncCallback(ProcessResponse), this); | 449 | Request.Timeout = HttpTimeout; |
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 | { | ||
496 | try | 450 | try |
497 | { | 451 | { |
498 | response = (HttpWebResponse) Request.EndGetResponse(ar); | 452 | // execute the request |
453 | response = (HttpWebResponse) Request.GetResponse(); | ||
499 | } | 454 | } |
500 | catch (WebException e) | 455 | catch (WebException e) |
501 | { | 456 | { |
@@ -530,22 +485,33 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest | |||
530 | 485 | ||
531 | ResponseBody = sb.ToString().Replace("\r", ""); | 486 | ResponseBody = sb.ToString().Replace("\r", ""); |
532 | } | 487 | } |
533 | catch (WebException e) | 488 | catch (Exception e) |
534 | { | 489 | { |
535 | // Abort was called. Just go away | 490 | if (e is WebException && ((WebException)e).Status == WebExceptionStatus.ProtocolError) |
536 | if (e.Status == WebExceptionStatus.RequestCanceled) | 491 | { |
537 | return; | 492 | HttpWebResponse webRsp = (HttpWebResponse)((WebException)e).Response; |
538 | 493 | Status = (int)webRsp.StatusCode; | |
539 | Status = (int)OSHttpStatusCode.ClientErrorJoker; | 494 | try |
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 | } | ||
540 | 511 | ||
541 | ResponseBody = e.Message; | ||
542 | _finished = true; | 512 | _finished = true; |
543 | return; | 513 | return; |
544 | } | 514 | } |
545 | catch (Exception e) | ||
546 | { | ||
547 | // Ignore | ||
548 | } | ||
549 | finally | 515 | finally |
550 | { | 516 | { |
551 | if (response != null) | 517 | if (response != null) |
@@ -559,7 +525,7 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest | |||
559 | { | 525 | { |
560 | try | 526 | try |
561 | { | 527 | { |
562 | Request.Abort(); | 528 | httpThread.Abort(); |
563 | } | 529 | } |
564 | catch (Exception) | 530 | catch (Exception) |
565 | { | 531 | { |