aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/CoreModules/Scripting/HttpRequest/ScriptsHttpRequests.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/CoreModules/Scripting/HttpRequest/ScriptsHttpRequests.cs')
-rw-r--r--OpenSim/Region/CoreModules/Scripting/HttpRequest/ScriptsHttpRequests.cs77
1 files changed, 37 insertions, 40 deletions
diff --git a/OpenSim/Region/CoreModules/Scripting/HttpRequest/ScriptsHttpRequests.cs b/OpenSim/Region/CoreModules/Scripting/HttpRequest/ScriptsHttpRequests.cs
index 6793fc8..1a62405 100644
--- a/OpenSim/Region/CoreModules/Scripting/HttpRequest/ScriptsHttpRequests.cs
+++ b/OpenSim/Region/CoreModules/Scripting/HttpRequest/ScriptsHttpRequests.cs
@@ -419,7 +419,7 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
419 get { return _reqID; } 419 get { return _reqID; }
420 set { _reqID = value; } 420 set { _reqID = value; }
421 } 421 }
422 public HttpWebRequest Request; 422 public WebRequest Request;
423 public string ResponseBody; 423 public string ResponseBody;
424 public List<string> ResponseMetadata; 424 public List<string> ResponseMetadata;
425 public Dictionary<string, string> ResponseHeaders; 425 public Dictionary<string, string> ResponseHeaders;
@@ -431,18 +431,11 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
431 SendRequest(); 431 SendRequest();
432 } 432 }
433 433
434 /*
435 * TODO: More work on the response codes. Right now
436 * returning 200 for success or 499 for exception
437 */
438
439 public void SendRequest() 434 public void SendRequest()
440 { 435 {
441 HttpWebResponse response = null;
442
443 try 436 try
444 { 437 {
445 Request = (HttpWebRequest) WebRequest.Create(Url); 438 Request = WebRequest.Create(Url);
446 Request.Method = HttpMethod; 439 Request.Method = HttpMethod;
447 Request.ContentType = HttpMIMEType; 440 Request.ContentType = HttpMIMEType;
448 441
@@ -480,14 +473,17 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
480 } 473 }
481 } 474 }
482 475
483 foreach (KeyValuePair<string, string> entry in ResponseHeaders) 476 if (ResponseHeaders != null)
484 if (entry.Key.ToLower().Equals("user-agent")) 477 {
485 Request.UserAgent = entry.Value; 478 foreach (KeyValuePair<string, string> entry in ResponseHeaders)
486 else 479 if (entry.Key.ToLower().Equals("user-agent") && Request is HttpWebRequest)
487 Request.Headers[entry.Key] = entry.Value; 480 ((HttpWebRequest)Request).UserAgent = entry.Value;
481 else
482 Request.Headers[entry.Key] = entry.Value;
483 }
488 484
489 // Encode outbound data 485 // Encode outbound data
490 if (OutboundBody.Length > 0) 486 if (OutboundBody != null && OutboundBody.Length > 0)
491 { 487 {
492 byte[] data = Util.UTF8.GetBytes(OutboundBody); 488 byte[] data = Util.UTF8.GetBytes(OutboundBody);
493 489
@@ -510,12 +506,19 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
510 { 506 {
511 throw; 507 throw;
512 } 508 }
513 response = (HttpWebResponse)e.Response; 509
510 HttpWebResponse response = (HttpWebResponse)e.Response;
511
512 Status = (int)response.StatusCode;
513 ResponseBody = response.StatusDescription;
514 _finished = true; 514 _finished = true;
515 } 515 }
516 } 516 }
517 catch (Exception e) 517 catch (Exception e)
518 { 518 {
519// m_log.Debug(
520// string.Format("[SCRIPTS HTTP REQUESTS]: Exception on request to {0} for {1} ", Url, ItemID), e);
521
519 Status = (int)OSHttpStatusCode.ClientErrorJoker; 522 Status = (int)OSHttpStatusCode.ClientErrorJoker;
520 ResponseBody = e.Message; 523 ResponseBody = e.Message;
521 _finished = true; 524 _finished = true;
@@ -528,33 +531,27 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
528 531
529 try 532 try
530 { 533 {
531 response = (HttpWebResponse)Request.EndGetResponse(ar); 534 try
532 Status = (int)response.StatusCode;
533
534 Stream resStream = response.GetResponseStream();
535 StringBuilder sb = new StringBuilder();
536 byte[] buf = new byte[8192];
537 string tempString = null;
538 int count = 0;
539
540 do
541 { 535 {
542 // fill the buffer with data 536 response = (HttpWebResponse)Request.EndGetResponse(ar);
543 count = resStream.Read(buf, 0, buf.Length); 537 }
544 538 catch (WebException e)
545 // make sure we read some data 539 {
546 if (count != 0) 540 if (e.Status != WebExceptionStatus.ProtocolError)
547 { 541 {
548 // translate from bytes to ASCII text 542 throw;
549 tempString = Util.UTF8.GetString(buf, 0, count);
550
551 // continue building the string
552 sb.Append(tempString);
553 } 543 }
554 }
555 while (count > 0); // any more data to read?
556 544
557 ResponseBody = sb.ToString(); 545 response = (HttpWebResponse)e.Response;
546 }
547
548 Status = (int)response.StatusCode;
549
550 using (Stream stream = response.GetResponseStream())
551 {
552 StreamReader reader = new StreamReader(stream, Encoding.UTF8);
553 ResponseBody = reader.ReadToEnd();
554 }
558 } 555 }
559 catch (Exception e) 556 catch (Exception e)
560 { 557 {
@@ -587,4 +584,4 @@ namespace OpenSim.Region.CoreModules.Scripting.HttpRequest
587 Request.Abort(); 584 Request.Abort();
588 } 585 }
589 } 586 }
590} 587} \ No newline at end of file