aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorJustin Clark-Casey (justincc)2014-02-27 22:52:43 +0000
committerJustin Clark-Casey (justincc)2014-02-27 22:52:43 +0000
commit5fafea6631ec1da18803c6a599d9bc912cc15053 (patch)
treef840cdd2d9c3375d2369b7a77c5fe2cf684386f4
parentAdd more details to InvalidOperationException logging in SychronousRestFormsR... (diff)
downloadopensim-SC_OLD-5fafea6631ec1da18803c6a599d9bc912cc15053.zip
opensim-SC_OLD-5fafea6631ec1da18803c6a599d9bc912cc15053.tar.gz
opensim-SC_OLD-5fafea6631ec1da18803c6a599d9bc912cc15053.tar.bz2
opensim-SC_OLD-5fafea6631ec1da18803c6a599d9bc912cc15053.tar.xz
refactor: More consistently use using construct within WebUtil to match other existing code there.
-rw-r--r--OpenSim/Framework/WebUtil.cs443
1 files changed, 218 insertions, 225 deletions
diff --git a/OpenSim/Framework/WebUtil.cs b/OpenSim/Framework/WebUtil.cs
index 07c9e3f..6b39345 100644
--- a/OpenSim/Framework/WebUtil.cs
+++ b/OpenSim/Framework/WebUtil.cs
@@ -794,172 +794,166 @@ namespace OpenSim.Framework
794 if (maxConnections > 0 && ht.ServicePoint.ConnectionLimit < maxConnections) 794 if (maxConnections > 0 && ht.ServicePoint.ConnectionLimit < maxConnections)
795 ht.ServicePoint.ConnectionLimit = maxConnections; 795 ht.ServicePoint.ConnectionLimit = maxConnections;
796 796
797 WebResponse response = null;
798 TResponse deserial = default(TResponse); 797 TResponse deserial = default(TResponse);
799 XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); 798 XmlSerializer deserializer = new XmlSerializer(typeof(TResponse));
800 799
801 request.Method = verb; 800 request.Method = verb;
801
802 MemoryStream buffer = null; 802 MemoryStream buffer = null;
803 803
804 if (verb == "POST") 804 try
805 { 805 {
806 request.ContentType = "text/xml"; 806 if (verb == "POST")
807
808 buffer = new MemoryStream();
809
810 XmlWriterSettings settings = new XmlWriterSettings();
811 settings.Encoding = Encoding.UTF8;
812
813 using (XmlWriter writer = XmlWriter.Create(buffer, settings))
814 { 807 {
815 XmlSerializer serializer = new XmlSerializer(type); 808 request.ContentType = "text/xml";
816 serializer.Serialize(writer, obj);
817 writer.Flush();
818 }
819 809
820 int length = (int)buffer.Length; 810 buffer = new MemoryStream();
821 request.ContentLength = length;
822 811
823 if (WebUtil.DebugLevel >= 5) 812 XmlWriterSettings settings = new XmlWriterSettings();
824 WebUtil.LogOutgoingDetail(buffer); 813 settings.Encoding = Encoding.UTF8;
825 814
826 request.BeginGetRequestStream(delegate(IAsyncResult res) 815 using (XmlWriter writer = XmlWriter.Create(buffer, settings))
827 { 816 {
828 Stream requestStream = request.EndGetRequestStream(res); 817 XmlSerializer serializer = new XmlSerializer(type);
818 serializer.Serialize(writer, obj);
819 writer.Flush();
820 }
829 821
830 requestStream.Write(buffer.ToArray(), 0, length); 822 int length = (int)buffer.Length;
831 requestStream.Close(); 823 request.ContentLength = length;
832 824
833 // capture how much time was spent writing 825 if (WebUtil.DebugLevel >= 5)
834 tickdata = Util.EnvironmentTickCountSubtract(tickstart); 826 WebUtil.LogOutgoingDetail(buffer);
835 827
836 request.BeginGetResponse(delegate(IAsyncResult ar) 828 request.BeginGetRequestStream(delegate(IAsyncResult res)
837 { 829 {
838 response = request.EndGetResponse(ar); 830 using (Stream requestStream = request.EndGetRequestStream(res))
839 Stream respStream = null; 831 requestStream.Write(buffer.ToArray(), 0, length);
840 try 832
841 { 833 // capture how much time was spent writing
842 respStream = response.GetResponseStream(); 834 tickdata = Util.EnvironmentTickCountSubtract(tickstart);
843 deserial = (TResponse)deserializer.Deserialize( 835
844 respStream); 836 request.BeginGetResponse(delegate(IAsyncResult ar)
845 }
846 catch (System.InvalidOperationException)
847 {
848 }
849 finally
850 { 837 {
851 // Let's not close this 838 using (WebResponse response = request.EndGetResponse(ar))
852 //buffer.Close(); 839 {
853 respStream.Close(); 840 try
854 response.Close(); 841 {
855 } 842 using (Stream respStream = response.GetResponseStream())
843 deserial = (TResponse)deserializer.Deserialize(respStream);
844 }
845 catch (System.InvalidOperationException)
846 {
847 }
848 }
856 849
857 action(deserial); 850 action(deserial);
858 851
852 }, null);
859 }, null); 853 }, null);
860 }, null); 854 }
861 } 855 else
862 else
863 {
864 request.BeginGetResponse(delegate(IAsyncResult res2)
865 { 856 {
866 try 857 request.BeginGetResponse(delegate(IAsyncResult res2)
867 { 858 {
868 // If the server returns a 404, this appears to trigger a System.Net.WebException even though that isn't
869 // documented in MSDN
870 response = request.EndGetResponse(res2);
871
872 Stream respStream = null;
873 try 859 try
874 { 860 {
875 respStream = response.GetResponseStream(); 861 // If the server returns a 404, this appears to trigger a System.Net.WebException even though that isn't
876 deserial = (TResponse)deserializer.Deserialize(respStream); 862 // documented in MSDN
877 } 863 using (WebResponse response = request.EndGetResponse(res2))
878 catch (System.InvalidOperationException) 864 {
879 { 865 try
880 } 866 {
881 finally 867 using (Stream respStream = response.GetResponseStream())
882 { 868 deserial = (TResponse)deserializer.Deserialize(respStream);
883 respStream.Close(); 869 }
884 response.Close(); 870 catch (System.InvalidOperationException)
871 {
872 }
873 }
885 } 874 }
886 } 875 catch (WebException e)
887 catch (WebException e)
888 {
889 if (e.Status == WebExceptionStatus.ProtocolError)
890 { 876 {
891 if (e.Response is HttpWebResponse) 877 if (e.Status == WebExceptionStatus.ProtocolError)
892 { 878 {
893 using (HttpWebResponse httpResponse = (HttpWebResponse)e.Response) 879 if (e.Response is HttpWebResponse)
894 { 880 {
895 if (httpResponse.StatusCode != HttpStatusCode.NotFound) 881 using (HttpWebResponse httpResponse = (HttpWebResponse)e.Response)
896 { 882 {
897 // We don't appear to be handling any other status codes, so log these feailures to that 883 if (httpResponse.StatusCode != HttpStatusCode.NotFound)
898 // people don't spend unnecessary hours hunting phantom bugs. 884 {
899 m_log.DebugFormat( 885 // We don't appear to be handling any other status codes, so log these feailures to that
900 "[ASYNC REQUEST]: Request {0} {1} failed with unexpected status code {2}", 886 // people don't spend unnecessary hours hunting phantom bugs.
901 verb, requestUrl, httpResponse.StatusCode); 887 m_log.DebugFormat(
888 "[ASYNC REQUEST]: Request {0} {1} failed with unexpected status code {2}",
889 verb, requestUrl, httpResponse.StatusCode);
890 }
902 } 891 }
903 } 892 }
904 } 893 }
894 else
895 {
896 m_log.ErrorFormat(
897 "[ASYNC REQUEST]: Request {0} {1} failed with status {2} and message {3}",
898 verb, requestUrl, e.Status, e.Message);
899 }
905 } 900 }
906 else 901 catch (Exception e)
907 { 902 {
908 m_log.ErrorFormat( 903 m_log.ErrorFormat(
909 "[ASYNC REQUEST]: Request {0} {1} failed with status {2} and message {3}", 904 "[ASYNC REQUEST]: Request {0} {1} failed with exception {2}{3}",
910 verb, requestUrl, e.Status, e.Message); 905 verb, requestUrl, e.Message, e.StackTrace);
911 } 906 }
912 } 907
913 catch (Exception e) 908 // m_log.DebugFormat("[ASYNC REQUEST]: Received {0}", deserial.ToString());
914 {
915 m_log.ErrorFormat(
916 "[ASYNC REQUEST]: Request {0} {1} failed with exception {2}{3}",
917 verb, requestUrl, e.Message, e.StackTrace);
918 }
919
920 // m_log.DebugFormat("[ASYNC REQUEST]: Received {0}", deserial.ToString());
921 909
922 try 910 try
923 { 911 {
924 action(deserial); 912 action(deserial);
925 } 913 }
926 catch (Exception e) 914 catch (Exception e)
915 {
916 m_log.ErrorFormat(
917 "[ASYNC REQUEST]: Request {0} {1} callback failed with exception {2}{3}",
918 verb, requestUrl, e.Message, e.StackTrace);
919 }
920
921 }, null);
922 }
923
924 int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
925 if (tickdiff > WebUtil.LongCallTime)
926 {
927 string originalRequest = null;
928
929 if (buffer != null)
927 { 930 {
928 m_log.ErrorFormat( 931 originalRequest = Encoding.UTF8.GetString(buffer.ToArray());
929 "[ASYNC REQUEST]: Request {0} {1} callback failed with exception {2}{3}",
930 verb, requestUrl, e.Message, e.StackTrace);
931 }
932
933 }, null);
934 }
935 932
936 int tickdiff = Util.EnvironmentTickCountSubtract(tickstart); 933 if (originalRequest.Length > WebUtil.MaxRequestDiagLength)
937 if (tickdiff > WebUtil.LongCallTime) 934 originalRequest = originalRequest.Remove(WebUtil.MaxRequestDiagLength);
938 { 935 }
939 string originalRequest = null;
940 936
941 if (buffer != null) 937 m_log.InfoFormat(
938 "[ASYNC REQUEST]: Slow request {0} {1} {2} took {3}ms, {4}ms writing, {5}",
939 reqnum,
940 verb,
941 requestUrl,
942 tickdiff,
943 tickdata,
944 originalRequest);
945 }
946 else if (WebUtil.DebugLevel >= 4)
942 { 947 {
943 originalRequest = Encoding.UTF8.GetString(buffer.ToArray()); 948 m_log.DebugFormat(
944 949 "[WEB UTIL]: HTTP OUT {0} took {1}ms, {2}ms writing",
945 if (originalRequest.Length > WebUtil.MaxRequestDiagLength) 950 reqnum, tickdiff, tickdata);
946 originalRequest = originalRequest.Remove(WebUtil.MaxRequestDiagLength);
947 } 951 }
948
949 m_log.InfoFormat(
950 "[ASYNC REQUEST]: Slow request {0} {1} {2} took {3}ms, {4}ms writing, {5}",
951 reqnum,
952 verb,
953 requestUrl,
954 tickdiff,
955 tickdata,
956 originalRequest);
957 } 952 }
958 else if (WebUtil.DebugLevel >= 4) 953 finally
959 { 954 {
960 m_log.DebugFormat( 955 if (buffer != null)
961 "[WEB UTIL]: HTTP OUT {0} took {1}ms, {2}ms writing", 956 buffer.Dispose();
962 reqnum, tickdiff, tickdata);
963 } 957 }
964 } 958 }
965 } 959 }
@@ -1056,11 +1050,6 @@ namespace OpenSim.Framework
1056 "[FORMS]: Exception occured on receiving {0} {1}: {2}{3}", 1050 "[FORMS]: Exception occured on receiving {0} {1}: {2}{3}",
1057 verb, requestUrl, e.Message, e.StackTrace); 1051 verb, requestUrl, e.Message, e.StackTrace);
1058 } 1052 }
1059 finally
1060 {
1061 if (respStream != null)
1062 respStream.Close();
1063 }
1064 } 1053 }
1065 } 1054 }
1066 } 1055 }
@@ -1145,128 +1134,132 @@ namespace OpenSim.Framework
1145 request.Method = verb; 1134 request.Method = verb;
1146 MemoryStream buffer = null; 1135 MemoryStream buffer = null;
1147 1136
1148 if ((verb == "POST") || (verb == "PUT")) 1137 try
1149 { 1138 {
1150 request.ContentType = "text/xml"; 1139 if ((verb == "POST") || (verb == "PUT"))
1140 {
1141 request.ContentType = "text/xml";
1151 1142
1152 buffer = new MemoryStream(); 1143 buffer = new MemoryStream();
1153 1144
1154 XmlWriterSettings settings = new XmlWriterSettings(); 1145 XmlWriterSettings settings = new XmlWriterSettings();
1155 settings.Encoding = Encoding.UTF8; 1146 settings.Encoding = Encoding.UTF8;
1156 1147
1157 using (XmlWriter writer = XmlWriter.Create(buffer, settings)) 1148 using (XmlWriter writer = XmlWriter.Create(buffer, settings))
1158 { 1149 {
1159 XmlSerializer serializer = new XmlSerializer(type); 1150 XmlSerializer serializer = new XmlSerializer(type);
1160 serializer.Serialize(writer, obj); 1151 serializer.Serialize(writer, obj);
1161 writer.Flush(); 1152 writer.Flush();
1162 } 1153 }
1163
1164 int length = (int)buffer.Length;
1165 request.ContentLength = length;
1166 1154
1167 if (WebUtil.DebugLevel >= 5) 1155 int length = (int)buffer.Length;
1168 WebUtil.LogOutgoingDetail(buffer); 1156 request.ContentLength = length;
1169 1157
1170 Stream requestStream = null; 1158 if (WebUtil.DebugLevel >= 5)
1171 try 1159 WebUtil.LogOutgoingDetail(buffer);
1172 {
1173 requestStream = request.GetRequestStream();
1174 requestStream.Write(buffer.ToArray(), 0, length);
1175 }
1176 catch (Exception e)
1177 {
1178 m_log.DebugFormat(
1179 "[SynchronousRestObjectRequester]: Exception in making request {0} {1}: {2}{3}",
1180 verb, requestUrl, e.Message, e.StackTrace);
1181 1160
1182 return deserial; 1161 try
1183 } 1162 {
1184 finally 1163 using (Stream requestStream = request.GetRequestStream())
1185 { 1164 requestStream.Write(buffer.ToArray(), 0, length);
1186 if (requestStream != null) 1165 }
1187 requestStream.Dispose(); 1166 catch (Exception e)
1167 {
1168 m_log.DebugFormat(
1169 "[SynchronousRestObjectRequester]: Exception in making request {0} {1}: {2}{3}",
1170 verb, requestUrl, e.Message, e.StackTrace);
1188 1171
1189 // capture how much time was spent writing 1172 return deserial;
1190 tickdata = Util.EnvironmentTickCountSubtract(tickstart); 1173 }
1174 finally
1175 {
1176 // capture how much time was spent writing
1177 tickdata = Util.EnvironmentTickCountSubtract(tickstart);
1178 }
1191 } 1179 }
1192 }
1193 1180
1194 try 1181 try
1195 {
1196 using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
1197 { 1182 {
1198 if (resp.ContentLength != 0) 1183 using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
1199 { 1184 {
1200 using (Stream respStream = resp.GetResponseStream()) 1185 if (resp.ContentLength != 0)
1186 {
1187 using (Stream respStream = resp.GetResponseStream())
1188 {
1189 XmlSerializer deserializer = new XmlSerializer(typeof(TResponse));
1190 deserial = (TResponse)deserializer.Deserialize(respStream);
1191 }
1192 }
1193 else
1201 { 1194 {
1202 XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); 1195 m_log.DebugFormat(
1203 deserial = (TResponse)deserializer.Deserialize(respStream); 1196 "[SynchronousRestObjectRequester]: Oops! no content found in response stream from {0} {1}",
1197 verb, requestUrl);
1204 } 1198 }
1205 } 1199 }
1206 else 1200 }
1201 catch (WebException e)
1202 {
1203 using (HttpWebResponse hwr = (HttpWebResponse)e.Response)
1207 { 1204 {
1208 m_log.DebugFormat( 1205 if (hwr != null && hwr.StatusCode == HttpStatusCode.NotFound)
1209 "[SynchronousRestObjectRequester]: Oops! no content found in response stream from {0} {1}", 1206 return deserial;
1210 verb, requestUrl); 1207 else
1208 m_log.ErrorFormat(
1209 "[SynchronousRestObjectRequester]: WebException for {0} {1} {2}: {3} {4}",
1210 verb, requestUrl, typeof(TResponse).ToString(), e.Message, e.StackTrace);
1211 } 1211 }
1212 } 1212 }
1213 } 1213 catch (System.InvalidOperationException)
1214 catch (WebException e)
1215 {
1216 using (HttpWebResponse hwr = (HttpWebResponse)e.Response)
1217 { 1214 {
1218 if (hwr != null && hwr.StatusCode == HttpStatusCode.NotFound) 1215 // This is what happens when there is invalid XML
1219 return deserial; 1216 m_log.DebugFormat(
1220 else 1217 "[SynchronousRestObjectRequester]: Invalid XML from {0} {1} {2}",
1221 m_log.ErrorFormat( 1218 verb, requestUrl, typeof(TResponse).ToString());
1222 "[SynchronousRestObjectRequester]: WebException for {0} {1} {2}: {3} {4}", 1219 }
1223 verb, requestUrl, typeof(TResponse).ToString(), e.Message, e.StackTrace); 1220 catch (Exception e)
1221 {
1222 m_log.DebugFormat(
1223 "[SynchronousRestObjectRequester]: Exception on response from {0} {1}: {2}{3}",
1224 verb, requestUrl, e.Message, e.StackTrace);
1224 } 1225 }
1225 }
1226 catch (System.InvalidOperationException)
1227 {
1228 // This is what happens when there is invalid XML
1229 m_log.DebugFormat(
1230 "[SynchronousRestObjectRequester]: Invalid XML from {0} {1} {2}",
1231 verb, requestUrl, typeof(TResponse).ToString());
1232 }
1233 catch (Exception e)
1234 {
1235 m_log.DebugFormat(
1236 "[SynchronousRestObjectRequester]: Exception on response from {0} {1}: {2}{3}",
1237 verb, requestUrl, e.Message, e.StackTrace);
1238 }
1239
1240 int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
1241 if (tickdiff > WebUtil.LongCallTime)
1242 {
1243 string originalRequest = null;
1244 1226
1245 if (buffer != null) 1227 int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
1228 if (tickdiff > WebUtil.LongCallTime)
1246 { 1229 {
1247 originalRequest = Encoding.UTF8.GetString(buffer.ToArray()); 1230 string originalRequest = null;
1248 1231
1249 if (originalRequest.Length > WebUtil.MaxRequestDiagLength) 1232 if (buffer != null)
1250 originalRequest = originalRequest.Remove(WebUtil.MaxRequestDiagLength); 1233 {
1251 } 1234 originalRequest = Encoding.UTF8.GetString(buffer.ToArray());
1252 1235
1253 m_log.InfoFormat( 1236 if (originalRequest.Length > WebUtil.MaxRequestDiagLength)
1254 "[SynchronousRestObjectRequester]: Slow request {0} {1} {2} took {3}ms, {4}ms writing, {5}", 1237 originalRequest = originalRequest.Remove(WebUtil.MaxRequestDiagLength);
1255 reqnum, 1238 }
1256 verb, 1239
1257 requestUrl, 1240 m_log.InfoFormat(
1258 tickdiff, 1241 "[SynchronousRestObjectRequester]: Slow request {0} {1} {2} took {3}ms, {4}ms writing, {5}",
1259 tickdata, 1242 reqnum,
1260 originalRequest); 1243 verb,
1244 requestUrl,
1245 tickdiff,
1246 tickdata,
1247 originalRequest);
1248 }
1249 else if (WebUtil.DebugLevel >= 4)
1250 {
1251 m_log.DebugFormat(
1252 "[WEB UTIL]: HTTP OUT {0} took {1}ms, {2}ms writing",
1253 reqnum, tickdiff, tickdata);
1254 }
1261 } 1255 }
1262 else if (WebUtil.DebugLevel >= 4) 1256 finally
1263 { 1257 {
1264 m_log.DebugFormat( 1258 if (buffer != null)
1265 "[WEB UTIL]: HTTP OUT {0} took {1}ms, {2}ms writing", 1259 buffer.Dispose();
1266 reqnum, tickdiff, tickdata);
1267 } 1260 }
1268 1261
1269 return deserial; 1262 return deserial;
1270 } 1263 }
1271 } 1264 }
1272} 1265} \ No newline at end of file