aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/WebUtil.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework/WebUtil.cs')
-rw-r--r--OpenSim/Framework/WebUtil.cs346
1 files changed, 344 insertions, 2 deletions
diff --git a/OpenSim/Framework/WebUtil.cs b/OpenSim/Framework/WebUtil.cs
index f59c8f8..d04a3df 100644
--- a/OpenSim/Framework/WebUtil.cs
+++ b/OpenSim/Framework/WebUtil.cs
@@ -36,6 +36,9 @@ using System.Net.Security;
36using System.Reflection; 36using System.Reflection;
37using System.Text; 37using System.Text;
38using System.Web; 38using System.Web;
39using System.Xml;
40using System.Xml.Serialization;
41
39using log4net; 42using log4net;
40using OpenSim.Framework.Servers.HttpServer; 43using OpenSim.Framework.Servers.HttpServer;
41using OpenMetaverse.StructuredData; 44using OpenMetaverse.StructuredData;
@@ -224,8 +227,8 @@ namespace OpenSim.Framework
224 m_log.InfoFormat("[WEB UTIL]: osd request <{0}> (URI:{1}, METHOD:{2}) took {3}ms overall, {4}ms writing", 227 m_log.InfoFormat("[WEB UTIL]: osd request <{0}> (URI:{1}, METHOD:{2}) took {3}ms overall, {4}ms writing",
225 reqnum,url,method,tickdiff,tickdata); 228 reqnum,url,method,tickdiff,tickdata);
226 } 229 }
227 230
228 m_log.WarnFormat("[WEB UTIL] <{0}> osd request failed: {1}",reqnum,errorMessage); 231 m_log.WarnFormat("[WEB UTIL]: <{0}> osd request for {1}, method {2} FAILED: {3}", reqnum, url, method, errorMessage);
229 return ErrorResponseMap(errorMessage); 232 return ErrorResponseMap(errorMessage);
230 } 233 }
231 234
@@ -630,4 +633,343 @@ namespace OpenSim.Framework
630 633
631 634
632 } 635 }
636
637 public static class AsynchronousRestObjectRequester
638 {
639 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
640
641 /// <summary>
642 /// Perform an asynchronous REST request.
643 /// </summary>
644 /// <param name="verb">GET or POST</param>
645 /// <param name="requestUrl"></param>
646 /// <param name="obj"></param>
647 /// <param name="action"></param>
648 /// <returns></returns>
649 ///
650 /// <exception cref="System.Net.WebException">Thrown if we encounter a
651 /// network issue while posting the request. You'll want to make
652 /// sure you deal with this as they're not uncommon</exception>
653 //
654 public static void MakeRequest<TRequest, TResponse>(string verb,
655 string requestUrl, TRequest obj, Action<TResponse> action)
656 {
657 // m_log.DebugFormat("[ASYNC REQUEST]: Starting {0} {1}", verb, requestUrl);
658
659 Type type = typeof(TRequest);
660
661 WebRequest request = WebRequest.Create(requestUrl);
662 WebResponse response = null;
663 TResponse deserial = default(TResponse);
664 XmlSerializer deserializer = new XmlSerializer(typeof(TResponse));
665
666 request.Method = verb;
667
668 if (verb == "POST")
669 {
670 request.ContentType = "text/xml";
671
672 MemoryStream buffer = new MemoryStream();
673
674 XmlWriterSettings settings = new XmlWriterSettings();
675 settings.Encoding = Encoding.UTF8;
676
677 using (XmlWriter writer = XmlWriter.Create(buffer, settings))
678 {
679 XmlSerializer serializer = new XmlSerializer(type);
680 serializer.Serialize(writer, obj);
681 writer.Flush();
682 }
683
684 int length = (int)buffer.Length;
685 request.ContentLength = length;
686
687 request.BeginGetRequestStream(delegate(IAsyncResult res)
688 {
689 Stream requestStream = request.EndGetRequestStream(res);
690
691 requestStream.Write(buffer.ToArray(), 0, length);
692 requestStream.Close();
693
694 request.BeginGetResponse(delegate(IAsyncResult ar)
695 {
696 response = request.EndGetResponse(ar);
697 Stream respStream = null;
698 try
699 {
700 respStream = response.GetResponseStream();
701 deserial = (TResponse)deserializer.Deserialize(
702 respStream);
703 }
704 catch (System.InvalidOperationException)
705 {
706 }
707 finally
708 {
709 // Let's not close this
710 //buffer.Close();
711 respStream.Close();
712 response.Close();
713 }
714
715 action(deserial);
716
717 }, null);
718 }, null);
719
720
721 return;
722 }
723
724 request.BeginGetResponse(delegate(IAsyncResult res2)
725 {
726 try
727 {
728 // If the server returns a 404, this appears to trigger a System.Net.WebException even though that isn't
729 // documented in MSDN
730 response = request.EndGetResponse(res2);
731
732 Stream respStream = null;
733 try
734 {
735 respStream = response.GetResponseStream();
736 deserial = (TResponse)deserializer.Deserialize(respStream);
737 }
738 catch (System.InvalidOperationException)
739 {
740 }
741 finally
742 {
743 respStream.Close();
744 response.Close();
745 }
746 }
747 catch (WebException e)
748 {
749 if (e.Status == WebExceptionStatus.ProtocolError)
750 {
751 if (e.Response is HttpWebResponse)
752 {
753 HttpWebResponse httpResponse = (HttpWebResponse)e.Response;
754
755 if (httpResponse.StatusCode != HttpStatusCode.NotFound)
756 {
757 // We don't appear to be handling any other status codes, so log these feailures to that
758 // people don't spend unnecessary hours hunting phantom bugs.
759 m_log.DebugFormat(
760 "[ASYNC REQUEST]: Request {0} {1} failed with unexpected status code {2}",
761 verb, requestUrl, httpResponse.StatusCode);
762 }
763 }
764 }
765 else
766 {
767 m_log.ErrorFormat("[ASYNC REQUEST]: Request {0} {1} failed with status {2} and message {3}", verb, requestUrl, e.Status, e.Message);
768 }
769 }
770 catch (Exception e)
771 {
772 m_log.ErrorFormat("[ASYNC REQUEST]: Request {0} {1} failed with exception {2}", verb, requestUrl, e);
773 }
774
775 // m_log.DebugFormat("[ASYNC REQUEST]: Received {0}", deserial.ToString());
776
777 try
778 {
779 action(deserial);
780 }
781 catch (Exception e)
782 {
783 m_log.ErrorFormat(
784 "[ASYNC REQUEST]: Request {0} {1} callback failed with exception {2}", verb, requestUrl, e);
785 }
786
787 }, null);
788 }
789 }
790
791 public static class SynchronousRestFormsRequester
792 {
793 private static readonly ILog m_log =
794 LogManager.GetLogger(
795 MethodBase.GetCurrentMethod().DeclaringType);
796
797 /// <summary>
798 /// Perform a synchronous REST request.
799 /// </summary>
800 /// <param name="verb"></param>
801 /// <param name="requestUrl"></param>
802 /// <param name="obj"> </param>
803 /// <returns></returns>
804 ///
805 /// <exception cref="System.Net.WebException">Thrown if we encounter a network issue while posting
806 /// the request. You'll want to make sure you deal with this as they're not uncommon</exception>
807 public static string MakeRequest(string verb, string requestUrl, string obj)
808 {
809 WebRequest request = WebRequest.Create(requestUrl);
810 request.Method = verb;
811 string respstring = String.Empty;
812
813 using (MemoryStream buffer = new MemoryStream())
814 {
815 if ((verb == "POST") || (verb == "PUT"))
816 {
817 request.ContentType = "text/www-form-urlencoded";
818
819 int length = 0;
820 using (StreamWriter writer = new StreamWriter(buffer))
821 {
822 writer.Write(obj);
823 writer.Flush();
824 }
825
826 length = (int)obj.Length;
827 request.ContentLength = length;
828
829 Stream requestStream = null;
830 try
831 {
832 requestStream = request.GetRequestStream();
833 requestStream.Write(buffer.ToArray(), 0, length);
834 }
835 catch (Exception e)
836 {
837 m_log.DebugFormat("[FORMS]: exception occured on sending request to {0}: " + e.ToString(), requestUrl);
838 }
839 finally
840 {
841 if (requestStream != null)
842 requestStream.Close();
843 }
844 }
845
846 try
847 {
848 using (WebResponse resp = request.GetResponse())
849 {
850 if (resp.ContentLength != 0)
851 {
852 Stream respStream = null;
853 try
854 {
855 respStream = resp.GetResponseStream();
856 using (StreamReader reader = new StreamReader(respStream))
857 {
858 respstring = reader.ReadToEnd();
859 }
860 }
861 catch (Exception e)
862 {
863 m_log.DebugFormat("[FORMS]: exception occured on receiving reply " + e.ToString());
864 }
865 finally
866 {
867 if (respStream != null)
868 respStream.Close();
869 }
870 }
871 }
872 }
873 catch (System.InvalidOperationException)
874 {
875 // This is what happens when there is invalid XML
876 m_log.DebugFormat("[FORMS]: InvalidOperationException on receiving request");
877 }
878 }
879 return respstring;
880 }
881 }
882
883 public class SynchronousRestObjectPoster
884 {
885 [Obsolete]
886 public static TResponse BeginPostObject<TRequest, TResponse>(string verb, string requestUrl, TRequest obj)
887 {
888 return SynchronousRestObjectRequester.MakeRequest<TRequest, TResponse>(verb, requestUrl, obj);
889 }
890 }
891
892 public class SynchronousRestObjectRequester
893 {
894 /// <summary>
895 /// Perform a synchronous REST request.
896 /// </summary>
897 /// <param name="verb"></param>
898 /// <param name="requestUrl"></param>
899 /// <param name="obj"> </param>
900 /// <returns></returns>
901 ///
902 /// <exception cref="System.Net.WebException">Thrown if we encounter a network issue while posting
903 /// the request. You'll want to make sure you deal with this as they're not uncommon</exception>
904 public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj)
905 {
906 return MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, 0);
907 }
908
909 public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj, int pTimeout)
910 {
911 Type type = typeof(TRequest);
912 TResponse deserial = default(TResponse);
913
914 WebRequest request = WebRequest.Create(requestUrl);
915 request.Method = verb;
916 if (pTimeout != 0)
917 request.Timeout = pTimeout * 1000;
918
919 if ((verb == "POST") || (verb == "PUT"))
920 {
921 request.ContentType = "text/xml";
922
923 MemoryStream buffer = new MemoryStream();
924
925 XmlWriterSettings settings = new XmlWriterSettings();
926 settings.Encoding = Encoding.UTF8;
927
928 using (XmlWriter writer = XmlWriter.Create(buffer, settings))
929 {
930 XmlSerializer serializer = new XmlSerializer(type);
931 serializer.Serialize(writer, obj);
932 writer.Flush();
933 }
934
935 int length = (int)buffer.Length;
936 request.ContentLength = length;
937
938 Stream requestStream = null;
939 try
940 {
941 requestStream = request.GetRequestStream();
942 requestStream.Write(buffer.ToArray(), 0, length);
943 }
944 catch (Exception)
945 {
946 return deserial;
947 }
948 finally
949 {
950 if (requestStream != null)
951 requestStream.Close();
952 }
953 }
954
955 try
956 {
957 using (WebResponse resp = request.GetResponse())
958 {
959 if (resp.ContentLength > 0)
960 {
961 Stream respStream = resp.GetResponseStream();
962 XmlSerializer deserializer = new XmlSerializer(typeof(TResponse));
963 deserial = (TResponse)deserializer.Deserialize(respStream);
964 respStream.Close();
965 }
966 }
967 }
968 catch (System.InvalidOperationException)
969 {
970 // This is what happens when there is invalid XML
971 }
972 return deserial;
973 }
974 }
633} 975}