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