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.cs860
1 files changed, 575 insertions, 285 deletions
diff --git a/OpenSim/Framework/WebUtil.cs b/OpenSim/Framework/WebUtil.cs
index 3625a1f..94b5230 100644
--- a/OpenSim/Framework/WebUtil.cs
+++ b/OpenSim/Framework/WebUtil.cs
@@ -39,8 +39,13 @@ using System.Text;
39using System.Web; 39using System.Web;
40using System.Xml; 40using System.Xml;
41using System.Xml.Serialization; 41using System.Xml.Serialization;
42using System.Xml.Linq;
42using log4net; 43using log4net;
44using Nwc.XmlRpc;
43using OpenMetaverse.StructuredData; 45using OpenMetaverse.StructuredData;
46using XMLResponseHelper = OpenSim.Framework.SynchronousRestObjectRequester.XMLResponseHelper;
47
48using OpenSim.Framework.ServiceAuth;
44 49
45namespace OpenSim.Framework 50namespace OpenSim.Framework
46{ 51{
@@ -64,7 +69,7 @@ namespace OpenSim.Framework
64 /// <summary> 69 /// <summary>
65 /// Request number for diagnostic purposes. 70 /// Request number for diagnostic purposes.
66 /// </summary> 71 /// </summary>
67 public static int RequestNumber { get; internal set; } 72 public static int RequestNumber { get; set; }
68 73
69 /// <summary> 74 /// <summary>
70 /// Control where OSD requests should be serialized per endpoint. 75 /// Control where OSD requests should be serialized per endpoint.
@@ -89,8 +94,9 @@ namespace OpenSim.Framework
89 /// <remarks> 94 /// <remarks>
90 /// This is to truncate any really large post data, such as an asset. In theory, the first section should 95 /// This is to truncate any really large post data, such as an asset. In theory, the first section should
91 /// give us useful information about the call (which agent it relates to if applicable, etc.). 96 /// give us useful information about the call (which agent it relates to if applicable, etc.).
97 /// This is also used to truncate messages when using DebugLevel 5.
92 /// </remarks> 98 /// </remarks>
93 public const int MaxRequestDiagLength = 100; 99 public const int MaxRequestDiagLength = 200;
94 100
95 /// <summary> 101 /// <summary>
96 /// Dictionary of end points 102 /// Dictionary of end points
@@ -125,88 +131,112 @@ namespace OpenSim.Framework
125 /// </summary> 131 /// </summary>
126 public static OSDMap PutToServiceCompressed(string url, OSDMap data, int timeout) 132 public static OSDMap PutToServiceCompressed(string url, OSDMap data, int timeout)
127 { 133 {
128 return ServiceOSDRequest(url,data, "PUT", timeout, true); 134 return ServiceOSDRequest(url,data, "PUT", timeout, true, false);
129 } 135 }
130 136
131 public static OSDMap PutToService(string url, OSDMap data, int timeout) 137 public static OSDMap PutToService(string url, OSDMap data, int timeout)
132 { 138 {
133 return ServiceOSDRequest(url,data, "PUT", timeout, false); 139 return ServiceOSDRequest(url,data, "PUT", timeout, false, false);
134 } 140 }
135 141
136 public static OSDMap PostToService(string url, OSDMap data, int timeout) 142 public static OSDMap PostToService(string url, OSDMap data, int timeout, bool rpc)
137 { 143 {
138 return ServiceOSDRequest(url, data, "POST", timeout, false); 144 return ServiceOSDRequest(url, data, "POST", timeout, false, rpc);
139 } 145 }
140 146
141 public static OSDMap PostToServiceCompressed(string url, OSDMap data, int timeout) 147 public static OSDMap PostToServiceCompressed(string url, OSDMap data, int timeout)
142 { 148 {
143 return ServiceOSDRequest(url, data, "POST", timeout, true); 149 return ServiceOSDRequest(url, data, "POST", timeout, true, false);
144 } 150 }
145 151
146 public static OSDMap GetFromService(string url, int timeout) 152 public static OSDMap GetFromService(string url, int timeout)
147 { 153 {
148 return ServiceOSDRequest(url, null, "GET", timeout, false); 154 return ServiceOSDRequest(url, null, "GET", timeout, false, false);
149 } 155 }
150 156
151 public static OSDMap ServiceOSDRequest(string url, OSDMap data, string method, int timeout, bool compressed) 157 public static OSDMap ServiceOSDRequest(string url, OSDMap data, string method, int timeout, bool compressed, bool rpc)
152 { 158 {
153 if (SerializeOSDRequestsPerEndpoint) 159 if (SerializeOSDRequestsPerEndpoint)
154 { 160 {
155 lock (EndPointLock(url)) 161 lock (EndPointLock(url))
156 { 162 {
157 return ServiceOSDRequestWorker(url, data, method, timeout, compressed); 163 return ServiceOSDRequestWorker(url, data, method, timeout, compressed, rpc);
158 } 164 }
159 } 165 }
160 else 166 else
161 { 167 {
162 return ServiceOSDRequestWorker(url, data, method, timeout, compressed); 168 return ServiceOSDRequestWorker(url, data, method, timeout, compressed, rpc);
163 } 169 }
164 } 170 }
165 171
166 public static void LogOutgoingDetail(Stream outputStream) 172 public static void LogOutgoingDetail(Stream outputStream)
167 { 173 {
168 using (StreamReader reader = new StreamReader(Util.Copy(outputStream), Encoding.UTF8)) 174 LogOutgoingDetail("", outputStream);
175 }
176
177 public static void LogOutgoingDetail(string context, Stream outputStream)
178 {
179 using (Stream stream = Util.Copy(outputStream))
180 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
169 { 181 {
170 string output; 182 string output;
171 183
172 if (DebugLevel == 5) 184 if (DebugLevel == 5)
173 { 185 {
174 const int sampleLength = 80; 186 char[] chars = new char[WebUtil.MaxRequestDiagLength + 1]; // +1 so we know to add "..." only if needed
175 char[] sampleChars = new char[sampleLength]; 187 int len = reader.Read(chars, 0, WebUtil.MaxRequestDiagLength + 1);
176 reader.Read(sampleChars, 0, sampleLength); 188 output = new string(chars, 0, len);
177 output = new string(sampleChars);
178 } 189 }
179 else 190 else
180 { 191 {
181 output = reader.ReadToEnd(); 192 output = reader.ReadToEnd();
182 } 193 }
183 194
184 LogOutgoingDetail(output); 195 LogOutgoingDetail(context, output);
185 } 196 }
186 } 197 }
187 198
188 public static void LogOutgoingDetail(string output) 199 public static void LogOutgoingDetail(string type, int reqnum, string output)
200 {
201 LogOutgoingDetail(string.Format("{0} {1}: ", type, reqnum), output);
202 }
203
204 public static void LogOutgoingDetail(string context, string output)
189 { 205 {
190 if (DebugLevel == 5) 206 if (DebugLevel == 5)
191 { 207 {
208<<<<<<< HEAD
209 if (output.Length > MaxRequestDiagLength)
210 output = output.Substring(0, MaxRequestDiagLength) + "...";
211=======
192 int len = output.Length; 212 int len = output.Length;
193 if(len > 80) 213 if(len > 80)
194 len = 80; 214 len = 80;
195 output = output.Substring(0, len); 215 output = output.Substring(0, len);
196 output = output + "..."; 216 output = output + "...";
217>>>>>>> avn/ubitvar
197 } 218 }
198 219
199 m_log.DebugFormat("[WEB UTIL]: {0}", output.Replace("\n", @"\n")); 220 m_log.DebugFormat("[LOGHTTP]: {0}{1}", context, Util.BinaryToASCII(output));
221 }
222
223 public static void LogResponseDetail(int reqnum, Stream inputStream)
224 {
225 LogOutgoingDetail(string.Format("RESPONSE {0}: ", reqnum), inputStream);
200 } 226 }
201 227
202 private static OSDMap ServiceOSDRequestWorker(string url, OSDMap data, string method, int timeout, bool compressed) 228 public static void LogResponseDetail(int reqnum, string input)
229 {
230 LogOutgoingDetail(string.Format("RESPONSE {0}: ", reqnum), input);
231 }
232
233 private static OSDMap ServiceOSDRequestWorker(string url, OSDMap data, string method, int timeout, bool compressed, bool rpc)
203 { 234 {
204 int reqnum = RequestNumber++; 235 int reqnum = RequestNumber++;
205 236
206 if (DebugLevel >= 3) 237 if (DebugLevel >= 3)
207 m_log.DebugFormat( 238 m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} JSON-RPC {1} to {2}",
208 "[WEB UTIL]: HTTP OUT {0} ServiceOSD {1} {2} (timeout {3}, compressed {4})", 239 reqnum, method, url);
209 reqnum, method, url, timeout, compressed);
210 240
211 string errorMessage = "unknown error"; 241 string errorMessage = "unknown error";
212 int tickstart = Util.EnvironmentTickCount(); 242 int tickstart = Util.EnvironmentTickCount();
@@ -234,21 +264,24 @@ namespace OpenSim.Framework
234 tickJsondata = Util.EnvironmentTickCountSubtract(tickstart); 264 tickJsondata = Util.EnvironmentTickCountSubtract(tickstart);
235 265
236 if (DebugLevel >= 5) 266 if (DebugLevel >= 5)
237 LogOutgoingDetail(strBuffer); 267 LogOutgoingDetail("SEND", reqnum, strBuffer);
238 268
239 byte[] buffer = System.Text.Encoding.UTF8.GetBytes(strBuffer); 269 byte[] buffer = System.Text.Encoding.UTF8.GetBytes(strBuffer);
240 270
271 request.ContentType = rpc ? "application/json-rpc" : "application/json";
272
241 if (compressed) 273 if (compressed)
242 { 274 {
243 request.ContentType = "application/x-gzip"; 275 request.Headers["X-Content-Encoding"] = "gzip"; // can't set "Content-Encoding" because old OpenSims fail if they get an unrecognized Content-Encoding
276
244 using (MemoryStream ms = new MemoryStream()) 277 using (MemoryStream ms = new MemoryStream())
245 { 278 {
246 using (GZipStream comp = new GZipStream(ms, CompressionMode.Compress)) 279 using (GZipStream comp = new GZipStream(ms, CompressionMode.Compress, true))
247 { 280 {
248 comp.Write(buffer, 0, buffer.Length); 281 comp.Write(buffer, 0, buffer.Length);
249 // We need to close the gzip stream before we write it anywhere 282 // We need to close the gzip stream before we write it anywhere
250 // because apparently something important related to gzip compression 283 // because apparently something important related to gzip compression
251 // gets written on the strteam upon Dispose() 284 // gets written on the stream upon Dispose()
252 } 285 }
253 byte[] buf = ms.ToArray(); 286 byte[] buf = ms.ToArray();
254 287
@@ -262,12 +295,15 @@ namespace OpenSim.Framework
262 } 295 }
263 else 296 else
264 { 297 {
298<<<<<<< HEAD
299=======
265 tickcompressdata = tickJsondata; 300 tickcompressdata = tickJsondata;
266 compsize = buffer.Length; 301 compsize = buffer.Length;
267 request.ContentType = "application/json"; 302 request.ContentType = "application/json";
303>>>>>>> avn/ubitvar
268 request.ContentLength = buffer.Length; //Count bytes to send 304 request.ContentLength = buffer.Length; //Count bytes to send
269 using (Stream requestStream = request.GetRequestStream()) 305 using (Stream requestStream = request.GetRequestStream())
270 requestStream.Write(buffer, 0, buffer.Length); //Send it 306 requestStream.Write(buffer, 0, buffer.Length); //Send it
271 } 307 }
272 } 308 }
273 309
@@ -279,10 +315,20 @@ namespace OpenSim.Framework
279 { 315 {
280 using (Stream responseStream = response.GetResponseStream()) 316 using (Stream responseStream = response.GetResponseStream())
281 { 317 {
318<<<<<<< HEAD
319 using (StreamReader reader = new StreamReader(responseStream))
320 {
321 string responseStr = reader.ReadToEnd();
322 if (WebUtil.DebugLevel >= 5)
323 WebUtil.LogResponseDetail(reqnum, responseStr);
324 return CanonicalizeResults(responseStr);
325 }
326=======
282 string responseStr = null; 327 string responseStr = null;
283 responseStr = responseStream.GetStreamString(); 328 responseStr = responseStream.GetStreamString();
284 //m_log.DebugFormat("[WEB UTIL]: <{0}> response is <{1}>",reqnum,responseStr); 329 //m_log.DebugFormat("[WEB UTIL]: <{0}> response is <{1}>",reqnum,responseStr);
285 return CanonicalizeResults(responseStr); 330 return CanonicalizeResults(responseStr);
331>>>>>>> avn/ubitvar
286 } 332 }
287 } 333 }
288 } 334 }
@@ -304,7 +350,12 @@ namespace OpenSim.Framework
304 { 350 {
305 int tickdiff = Util.EnvironmentTickCountSubtract(tickstart); 351 int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
306 if (tickdiff > LongCallTime) 352 if (tickdiff > LongCallTime)
353 {
307 m_log.InfoFormat( 354 m_log.InfoFormat(
355<<<<<<< HEAD
356 "[LOGHTTP]: Slow JSON-RPC request {0} {1} to {2} took {3}ms, {4}ms writing, {5}",
357 reqnum, method, url, tickdiff, tickdata,
358=======
308 "[WEB UTIL]: Slow ServiceOSD request {0} {1} {2} took {3}ms, {4}ms writing({5} at Json; {6} at comp), {7} bytes ({8} uncomp): {9}", 359 "[WEB UTIL]: Slow ServiceOSD request {0} {1} {2} took {3}ms, {4}ms writing({5} at Json; {6} at comp), {7} bytes ({8} uncomp): {9}",
309 reqnum, 360 reqnum,
310 method, 361 method,
@@ -315,17 +366,20 @@ namespace OpenSim.Framework
315 tickcompressdata, 366 tickcompressdata,
316 compsize, 367 compsize,
317 strBuffer != null ? strBuffer.Length : 0, 368 strBuffer != null ? strBuffer.Length : 0,
369>>>>>>> avn/ubitvar
318 strBuffer != null 370 strBuffer != null
319 ? (strBuffer.Length > MaxRequestDiagLength ? strBuffer.Remove(MaxRequestDiagLength) : strBuffer) 371 ? (strBuffer.Length > MaxRequestDiagLength ? strBuffer.Remove(MaxRequestDiagLength) : strBuffer)
320 : ""); 372 : "");
373 }
321 else if (DebugLevel >= 4) 374 else if (DebugLevel >= 4)
322 m_log.DebugFormat( 375 {
323 "[WEB UTIL]: HTTP OUT {0} took {1}ms, {2}ms writing", 376 m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} took {1}ms, {2}ms writing",
324 reqnum, tickdiff, tickdata); 377 reqnum, tickdiff, tickdata);
378 }
325 } 379 }
326 380
327 m_log.DebugFormat( 381 m_log.DebugFormat(
328 "[WEB UTIL]: ServiceOSD request {0} {1} {2} FAILED: {3}", reqnum, url, method, errorMessage); 382 "[LOGHTTP]: JSON-RPC request {0} {1} to {2} FAILED: {3}", reqnum, method, url, errorMessage);
329 383
330 return ErrorResponseMap(errorMessage); 384 return ErrorResponseMap(errorMessage);
331 } 385 }
@@ -403,9 +457,8 @@ namespace OpenSim.Framework
403 string method = (data != null && data["RequestMethod"] != null) ? data["RequestMethod"] : "unknown"; 457 string method = (data != null && data["RequestMethod"] != null) ? data["RequestMethod"] : "unknown";
404 458
405 if (DebugLevel >= 3) 459 if (DebugLevel >= 3)
406 m_log.DebugFormat( 460 m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} ServiceForm '{1}' to {2}",
407 "[WEB UTIL]: HTTP OUT {0} ServiceForm {1} {2} (timeout {3})", 461 reqnum, method, url);
408 reqnum, method, url, timeout);
409 462
410 string errorMessage = "unknown error"; 463 string errorMessage = "unknown error";
411 int tickstart = Util.EnvironmentTickCount(); 464 int tickstart = Util.EnvironmentTickCount();
@@ -427,7 +480,7 @@ namespace OpenSim.Framework
427 queryString = BuildQueryString(data); 480 queryString = BuildQueryString(data);
428 481
429 if (DebugLevel >= 5) 482 if (DebugLevel >= 5)
430 LogOutgoingDetail(queryString); 483 LogOutgoingDetail("SEND", reqnum, queryString);
431 484
432 byte[] buffer = System.Text.Encoding.UTF8.GetBytes(queryString); 485 byte[] buffer = System.Text.Encoding.UTF8.GetBytes(queryString);
433 486
@@ -445,12 +498,16 @@ namespace OpenSim.Framework
445 { 498 {
446 using (Stream responseStream = response.GetResponseStream()) 499 using (Stream responseStream = response.GetResponseStream())
447 { 500 {
448 string responseStr = null; 501 using (StreamReader reader = new StreamReader(responseStream))
502 {
503 string responseStr = reader.ReadToEnd();
504 if (WebUtil.DebugLevel >= 5)
505 WebUtil.LogResponseDetail(reqnum, responseStr);
506 OSD responseOSD = OSDParser.Deserialize(responseStr);
449 507
450 responseStr = responseStream.GetStreamString(); 508 if (responseOSD.Type == OSDType.Map)
451 OSD responseOSD = OSDParser.Deserialize(responseStr); 509 return (OSDMap)responseOSD;
452 if (responseOSD.Type == OSDType.Map) 510 }
453 return (OSDMap)responseOSD;
454 } 511 }
455 } 512 }
456 } 513 }
@@ -471,23 +528,22 @@ namespace OpenSim.Framework
471 { 528 {
472 int tickdiff = Util.EnvironmentTickCountSubtract(tickstart); 529 int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
473 if (tickdiff > LongCallTime) 530 if (tickdiff > LongCallTime)
531 {
474 m_log.InfoFormat( 532 m_log.InfoFormat(
475 "[WEB UTIL]: Slow ServiceForm request {0} {1} {2} took {3}ms, {4}ms writing, {5}", 533 "[LOGHTTP]: Slow ServiceForm request {0} '{1}' to {2} took {3}ms, {4}ms writing, {5}",
476 reqnum, 534 reqnum, method, url, tickdiff, tickdata,
477 method,
478 url,
479 tickdiff,
480 tickdata,
481 queryString != null 535 queryString != null
482 ? (queryString.Length > MaxRequestDiagLength) ? queryString.Remove(MaxRequestDiagLength) : queryString 536 ? (queryString.Length > MaxRequestDiagLength) ? queryString.Remove(MaxRequestDiagLength) : queryString
483 : ""); 537 : "");
538 }
484 else if (DebugLevel >= 4) 539 else if (DebugLevel >= 4)
485 m_log.DebugFormat( 540 {
486 "[WEB UTIL]: HTTP OUT {0} took {1}ms, {2}ms writing", 541 m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} took {1}ms, {2}ms writing",
487 reqnum, tickdiff, tickdata); 542 reqnum, tickdiff, tickdata);
543 }
488 } 544 }
489 545
490 m_log.WarnFormat("[WEB UTIL]: ServiceForm request {0} {1} {2} failed: {2}", reqnum, method, url, errorMessage); 546 m_log.WarnFormat("[LOGHTTP]: ServiceForm request {0} '{1}' to {2} failed: {3}", reqnum, method, url, errorMessage);
491 547
492 return ErrorResponseMap(errorMessage); 548 return ErrorResponseMap(errorMessage);
493 } 549 }
@@ -664,38 +720,6 @@ namespace OpenSim.Framework
664 return totalCopiedBytes; 720 return totalCopiedBytes;
665 } 721 }
666 722
667 /// <summary>
668 /// Converts an entire stream to a string, regardless of current stream
669 /// position
670 /// </summary>
671 /// <param name="stream">The stream to convert to a string</param>
672 /// <returns></returns>
673 /// <remarks>When this method is done, the stream position will be
674 /// reset to its previous position before this method was called</remarks>
675 public static string GetStreamString(this Stream stream)
676 {
677 string value = null;
678
679 if (stream != null && stream.CanRead)
680 {
681 long rewindPos = -1;
682
683 if (stream.CanSeek)
684 {
685 rewindPos = stream.Position;
686 stream.Seek(0, SeekOrigin.Begin);
687 }
688
689 StreamReader reader = new StreamReader(stream);
690 value = reader.ReadToEnd();
691
692 if (rewindPos >= 0)
693 stream.Seek(rewindPos, SeekOrigin.Begin);
694 }
695
696 return value;
697 }
698
699 #endregion Stream 723 #endregion Stream
700 724
701 public class QBasedComparer : IComparer 725 public class QBasedComparer : IComparer
@@ -796,11 +820,17 @@ namespace OpenSim.Framework
796 string requestUrl, TRequest obj, Action<TResponse> action, 820 string requestUrl, TRequest obj, Action<TResponse> action,
797 int maxConnections) 821 int maxConnections)
798 { 822 {
823 MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, action, maxConnections, null);
824 }
825
826 public static void MakeRequest<TRequest, TResponse>(string verb,
827 string requestUrl, TRequest obj, Action<TResponse> action,
828 int maxConnections, IServiceAuth auth)
829 {
799 int reqnum = WebUtil.RequestNumber++; 830 int reqnum = WebUtil.RequestNumber++;
800 831
801 if (WebUtil.DebugLevel >= 3) 832 if (WebUtil.DebugLevel >= 3)
802 m_log.DebugFormat( 833 m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} AsynchronousRequestObject {1} to {2}",
803 "[WEB UTIL]: HTTP OUT {0} AsynchronousRequestObject {1} {2}",
804 reqnum, verb, requestUrl); 834 reqnum, verb, requestUrl);
805 835
806 int tickstart = Util.EnvironmentTickCount(); 836 int tickstart = Util.EnvironmentTickCount();
@@ -811,125 +841,170 @@ namespace OpenSim.Framework
811 841
812 WebRequest request = WebRequest.Create(requestUrl); 842 WebRequest request = WebRequest.Create(requestUrl);
813 HttpWebRequest ht = (HttpWebRequest)request; 843 HttpWebRequest ht = (HttpWebRequest)request;
844
845 if (auth != null)
846 auth.AddAuthorization(ht.Headers);
847
814 if (maxConnections > 0 && ht.ServicePoint.ConnectionLimit < maxConnections) 848 if (maxConnections > 0 && ht.ServicePoint.ConnectionLimit < maxConnections)
815 ht.ServicePoint.ConnectionLimit = maxConnections; 849 ht.ServicePoint.ConnectionLimit = maxConnections;
816 850
817 WebResponse response = null;
818 TResponse deserial = default(TResponse); 851 TResponse deserial = default(TResponse);
819 XmlSerializer deserializer = new XmlSerializer(typeof(TResponse));
820 852
821 request.Method = verb; 853 request.Method = verb;
854
822 MemoryStream buffer = null; 855 MemoryStream buffer = null;
823 856
824 if (verb == "POST") 857 try
825 { 858 {
826 request.ContentType = "text/xml"; 859 if (verb == "POST")
827
828 buffer = new MemoryStream();
829
830 XmlWriterSettings settings = new XmlWriterSettings();
831 settings.Encoding = Encoding.UTF8;
832
833 using (XmlWriter writer = XmlWriter.Create(buffer, settings))
834 { 860 {
835 XmlSerializer serializer = new XmlSerializer(type); 861 request.ContentType = "text/xml";
836 serializer.Serialize(writer, obj);
837 writer.Flush();
838 }
839 862
840 int length = (int)buffer.Length; 863 buffer = new MemoryStream();
841 request.ContentLength = length;
842 864
843 if (WebUtil.DebugLevel >= 5) 865 XmlWriterSettings settings = new XmlWriterSettings();
844 WebUtil.LogOutgoingDetail(buffer); 866 settings.Encoding = Encoding.UTF8;
845 867
846 request.BeginGetRequestStream(delegate(IAsyncResult res) 868 using (XmlWriter writer = XmlWriter.Create(buffer, settings))
847 { 869 {
848 Stream requestStream = request.EndGetRequestStream(res); 870 XmlSerializer serializer = new XmlSerializer(type);
871 serializer.Serialize(writer, obj);
872 writer.Flush();
873 }
874
875 int length = (int)buffer.Length;
876 request.ContentLength = length;
877 byte[] data = buffer.ToArray();
849 878
850 requestStream.Write(buffer.ToArray(), 0, length); 879<<<<<<< HEAD
851 requestStream.Close(); 880 if (WebUtil.DebugLevel >= 5)
881 WebUtil.LogOutgoingDetail("SEND", reqnum, System.Text.Encoding.UTF8.GetString(data));
852 882
883 request.BeginGetRequestStream(delegate(IAsyncResult res)
884=======
853 // capture how much time was spent writing 885 // capture how much time was spent writing
854 // useless in this async 886 // useless in this async
855// tickdata = Util.EnvironmentTickCountSubtract(tickstart); 887// tickdata = Util.EnvironmentTickCountSubtract(tickstart);
856 request.BeginGetResponse(delegate(IAsyncResult ar) 888 request.BeginGetResponse(delegate(IAsyncResult ar)
889>>>>>>> avn/ubitvar
857 { 890 {
858 response = request.EndGetResponse(ar); 891 using (Stream requestStream = request.EndGetRequestStream(res))
859 Stream respStream = null; 892 requestStream.Write(data, 0, length);
860 try 893
861 { 894 // capture how much time was spent writing
862 respStream = response.GetResponseStream(); 895 tickdata = Util.EnvironmentTickCountSubtract(tickstart);
863 deserial = (TResponse)deserializer.Deserialize( 896
864 respStream); 897 request.BeginGetResponse(delegate(IAsyncResult ar)
865 }
866 catch (System.InvalidOperationException)
867 {
868 }
869 finally
870 { 898 {
899<<<<<<< HEAD
900 using (WebResponse response = request.EndGetResponse(ar))
901 {
902 try
903 {
904 using (Stream respStream = response.GetResponseStream())
905 {
906 deserial = XMLResponseHelper.LogAndDeserialize<TRequest, TResponse>(
907 reqnum, respStream, response.ContentLength);
908 }
909 }
910 catch (System.InvalidOperationException)
911 {
912 }
913 }
914=======
871 // Let's not close this 915 // Let's not close this
872 // yes do close it 916 // yes do close it
873 buffer.Close(); 917 buffer.Close();
874 respStream.Close(); 918 respStream.Close();
875 response.Close(); 919 response.Close();
876 } 920 }
921>>>>>>> avn/ubitvar
877 922
878 action(deserial); 923 action(deserial);
879 924
925 }, null);
880 }, null); 926 }, null);
881 }, null); 927 }
882 } 928 else
883 else
884 {
885 request.BeginGetResponse(delegate(IAsyncResult res2)
886 { 929 {
887 try 930 request.BeginGetResponse(delegate(IAsyncResult res2)
888 { 931 {
889 // If the server returns a 404, this appears to trigger a System.Net.WebException even though that isn't
890 // documented in MSDN
891 response = request.EndGetResponse(res2);
892
893 Stream respStream = null;
894 try 932 try
895 { 933 {
896 respStream = response.GetResponseStream(); 934 // If the server returns a 404, this appears to trigger a System.Net.WebException even though that isn't
897 deserial = (TResponse)deserializer.Deserialize(respStream); 935 // documented in MSDN
898 } 936 using (WebResponse response = request.EndGetResponse(res2))
899 catch (System.InvalidOperationException) 937 {
900 { 938 try
901 } 939 {
902 finally 940 using (Stream respStream = response.GetResponseStream())
903 { 941 {
904 respStream.Close(); 942 deserial = XMLResponseHelper.LogAndDeserialize<TRequest, TResponse>(
905 response.Close(); 943 reqnum, respStream, response.ContentLength);
944 }
945 }
946 catch (System.InvalidOperationException)
947 {
948 }
949 }
906 } 950 }
907 } 951 catch (WebException e)
908 catch (WebException e)
909 {
910 if (e.Status == WebExceptionStatus.ProtocolError)
911 { 952 {
912 if (e.Response is HttpWebResponse) 953 if (e.Status == WebExceptionStatus.ProtocolError)
913 { 954 {
914 using (HttpWebResponse httpResponse = (HttpWebResponse)e.Response) 955 if (e.Response is HttpWebResponse)
915 { 956 {
916 if (httpResponse.StatusCode != HttpStatusCode.NotFound) 957 using (HttpWebResponse httpResponse = (HttpWebResponse)e.Response)
917 { 958 {
918 // We don't appear to be handling any other status codes, so log these feailures to that 959 if (httpResponse.StatusCode != HttpStatusCode.NotFound)
919 // people don't spend unnecessary hours hunting phantom bugs. 960 {
920 m_log.DebugFormat( 961 // We don't appear to be handling any other status codes, so log these feailures to that
921 "[ASYNC REQUEST]: Request {0} {1} failed with unexpected status code {2}", 962 // people don't spend unnecessary hours hunting phantom bugs.
922 verb, requestUrl, httpResponse.StatusCode); 963 m_log.DebugFormat(
964 "[ASYNC REQUEST]: Request {0} {1} failed with unexpected status code {2}",
965 verb, requestUrl, httpResponse.StatusCode);
966 }
923 } 967 }
924 } 968 }
925 } 969 }
970 else
971 {
972 m_log.ErrorFormat(
973 "[ASYNC REQUEST]: Request {0} {1} failed with status {2} and message {3}",
974 verb, requestUrl, e.Status, e.Message);
975 }
926 } 976 }
927 else 977 catch (Exception e)
978 {
979 m_log.ErrorFormat(
980 "[ASYNC REQUEST]: Request {0} {1} failed with exception {2}{3}",
981 verb, requestUrl, e.Message, e.StackTrace);
982 }
983<<<<<<< HEAD
984
985 // m_log.DebugFormat("[ASYNC REQUEST]: Received {0}", deserial.ToString());
986
987 try
988 {
989 action(deserial);
990 }
991 catch (Exception e)
928 { 992 {
929 m_log.ErrorFormat( 993 m_log.ErrorFormat(
930 "[ASYNC REQUEST]: Request {0} {1} failed with status {2} and message {3}", 994 "[ASYNC REQUEST]: Request {0} {1} callback failed with exception {2}{3}",
931 verb, requestUrl, e.Status, e.Message); 995 verb, requestUrl, e.Message, e.StackTrace);
932 } 996 }
997
998 }, null);
999 }
1000
1001 int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
1002 if (tickdiff > WebUtil.LongCallTime)
1003 {
1004 string originalRequest = null;
1005
1006 if (buffer != null)
1007=======
933 } 1008 }
934 catch (Exception e) 1009 catch (Exception e)
935 { 1010 {
@@ -944,28 +1019,39 @@ namespace OpenSim.Framework
944 action(deserial); 1019 action(deserial);
945 } 1020 }
946 catch (Exception e) 1021 catch (Exception e)
1022>>>>>>> avn/ubitvar
947 { 1023 {
948 m_log.ErrorFormat( 1024 originalRequest = Encoding.UTF8.GetString(buffer.ToArray());
949 "[ASYNC REQUEST]: Request {0} {1} callback failed with exception {2}{3}",
950 verb, requestUrl, e.Message, e.StackTrace);
951 }
952
953 }, null);
954 }
955 1025
1026<<<<<<< HEAD
1027 if (originalRequest.Length > WebUtil.MaxRequestDiagLength)
1028 originalRequest = originalRequest.Remove(WebUtil.MaxRequestDiagLength);
1029 }
1030=======
956 tickdiff = Util.EnvironmentTickCountSubtract(tickstart); 1031 tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
957 if (tickdiff > WebUtil.LongCallTime) 1032 if (tickdiff > WebUtil.LongCallTime)
958 { 1033 {
959/* 1034/*
960 string originalRequest = null; 1035 string originalRequest = null;
1036>>>>>>> avn/ubitvar
961 1037
962 if (buffer != null) 1038 m_log.InfoFormat(
1039 "[LOGHTTP]: Slow AsynchronousRequestObject request {0} {1} to {2} took {3}ms, {4}ms writing, {5}",
1040 reqnum, verb, requestUrl, tickdiff, tickdata,
1041 originalRequest);
1042 }
1043 else if (WebUtil.DebugLevel >= 4)
963 { 1044 {
964 originalRequest = Encoding.UTF8.GetString(buffer.ToArray()); 1045 m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} took {1}ms, {2}ms writing",
965 1046 reqnum, tickdiff, tickdata);
966 if (originalRequest.Length > WebUtil.MaxRequestDiagLength)
967 originalRequest = originalRequest.Remove(WebUtil.MaxRequestDiagLength);
968 } 1047 }
1048<<<<<<< HEAD
1049 }
1050 finally
1051 {
1052 if (buffer != null)
1053 buffer.Dispose();
1054=======
969 1055
970 m_log.InfoFormat( 1056 m_log.InfoFormat(
971 "[ASYNC REQUEST]: Slow request {0} {1} {2} took {3}ms, {4}ms writing, {5}", 1057 "[ASYNC REQUEST]: Slow request {0} {1} {2} took {3}ms, {4}ms writing, {5}",
@@ -988,6 +1074,7 @@ namespace OpenSim.Framework
988 m_log.DebugFormat( 1074 m_log.DebugFormat(
989 "[WEB UTIL]: HTTP OUT {0} took {1}ms", 1075 "[WEB UTIL]: HTTP OUT {0} took {1}ms",
990 reqnum, tickdiff); 1076 reqnum, tickdiff);
1077>>>>>>> avn/ubitvar
991 } 1078 }
992 } 1079 }
993 } 1080 }
@@ -1007,13 +1094,12 @@ namespace OpenSim.Framework
1007 /// 1094 ///
1008 /// <exception cref="System.Net.WebException">Thrown if we encounter a network issue while posting 1095 /// <exception cref="System.Net.WebException">Thrown if we encounter a network issue while posting
1009 /// the request. You'll want to make sure you deal with this as they're not uncommon</exception> 1096 /// the request. You'll want to make sure you deal with this as they're not uncommon</exception>
1010 public static string MakeRequest(string verb, string requestUrl, string obj, int timeoutsecs) 1097 public static string MakeRequest(string verb, string requestUrl, string obj, int timeoutsecs, IServiceAuth auth)
1011 { 1098 {
1012 int reqnum = WebUtil.RequestNumber++; 1099 int reqnum = WebUtil.RequestNumber++;
1013 1100
1014 if (WebUtil.DebugLevel >= 3) 1101 if (WebUtil.DebugLevel >= 3)
1015 m_log.DebugFormat( 1102 m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} SynchronousRestForms {1} to {2}",
1016 "[WEB UTIL]: HTTP OUT {0} SynchronousRestForms {1} {2}",
1017 reqnum, verb, requestUrl); 1103 reqnum, verb, requestUrl);
1018 1104
1019 int tickstart = Util.EnvironmentTickCount(); 1105 int tickstart = Util.EnvironmentTickCount();
@@ -1023,6 +1109,10 @@ namespace OpenSim.Framework
1023 request.Method = verb; 1109 request.Method = verb;
1024 if (timeoutsecs > 0) 1110 if (timeoutsecs > 0)
1025 request.Timeout = timeoutsecs * 1000; 1111 request.Timeout = timeoutsecs * 1000;
1112
1113 if (auth != null)
1114 auth.AddAuthorization(request.Headers);
1115
1026 string respstring = String.Empty; 1116 string respstring = String.Empty;
1027 1117
1028 int tickset = Util.EnvironmentTickCountSubtract(tickstart); 1118 int tickset = Util.EnvironmentTickCountSubtract(tickstart);
@@ -1044,23 +1134,30 @@ namespace OpenSim.Framework
1044 1134
1045 length = (int)obj.Length; 1135 length = (int)obj.Length;
1046 request.ContentLength = length; 1136 request.ContentLength = length;
1137 byte[] data = buffer.ToArray();
1047 1138
1139<<<<<<< HEAD
1140 if (WebUtil.DebugLevel >= 5)
1141 WebUtil.LogOutgoingDetail("SEND", reqnum, System.Text.Encoding.UTF8.GetString(data));
1142=======
1143>>>>>>> avn/ubitvar
1048 1144
1049 Stream requestStream = null; 1145 Stream requestStream = null;
1050 try 1146 try
1051 { 1147 {
1052 requestStream = request.GetRequestStream(); 1148 requestStream = request.GetRequestStream();
1053 requestStream.Write(buffer.ToArray(), 0, length); 1149 requestStream.Write(data, 0, length);
1054 } 1150 }
1055 catch (Exception e) 1151 catch (Exception e)
1056 { 1152 {
1057 m_log.DebugFormat( 1153 m_log.InfoFormat("[FORMS]: Error sending request to {0}: {1}. Request: {2}", requestUrl, e.Message,
1058 "[FORMS]: exception occured {0} {1}: {2}{3}", verb, requestUrl, e.Message, e.StackTrace); 1154 obj.Length > WebUtil.MaxRequestDiagLength ? obj.Remove(WebUtil.MaxRequestDiagLength) : obj);
1155 throw e;
1059 } 1156 }
1060 finally 1157 finally
1061 { 1158 {
1062 if (requestStream != null) 1159 if (requestStream != null)
1063 requestStream.Close(); 1160 requestStream.Dispose();
1064 1161
1065 // capture how much time was spent writing 1162 // capture how much time was spent writing
1066 tickdata = Util.EnvironmentTickCountSubtract(tickstart); 1163 tickdata = Util.EnvironmentTickCountSubtract(tickstart);
@@ -1073,37 +1170,28 @@ namespace OpenSim.Framework
1073 { 1170 {
1074 if (resp.ContentLength != 0) 1171 if (resp.ContentLength != 0)
1075 { 1172 {
1076 Stream respStream = null; 1173 using (Stream respStream = resp.GetResponseStream())
1077 try 1174 using (StreamReader reader = new StreamReader(respStream))
1078 { 1175 respstring = reader.ReadToEnd();
1079 using (respStream = resp.GetResponseStream())
1080 using (StreamReader reader = new StreamReader(respStream))
1081 respstring = reader.ReadToEnd();
1082 }
1083 catch (Exception e)
1084 {
1085 m_log.DebugFormat(
1086 "[FORMS]: Exception occured on receiving {0} {1}: {2}{3}",
1087 verb, requestUrl, e.Message, e.StackTrace);
1088 }
1089 finally
1090 {
1091 if (respStream != null)
1092 respStream.Close();
1093 }
1094 } 1176 }
1095 } 1177 }
1096 } 1178 }
1097 catch (System.InvalidOperationException) 1179 catch (Exception e)
1098 { 1180 {
1099 // This is what happens when there is invalid XML 1181 m_log.InfoFormat("[FORMS]: Error receiving response from {0}: {1}. Request: {2}", requestUrl, e.Message,
1100 m_log.DebugFormat("[FORMS]: InvalidOperationException on receiving {0} {1}", verb, requestUrl); 1182 obj.Length > WebUtil.MaxRequestDiagLength ? obj.Remove(WebUtil.MaxRequestDiagLength) : obj);
1183 throw e;
1101 } 1184 }
1102 } 1185 }
1103 1186
1104 int tickdiff = Util.EnvironmentTickCountSubtract(tickstart); 1187 int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
1105 if (tickdiff > WebUtil.LongCallTime) 1188 if (tickdiff > WebUtil.LongCallTime)
1189 {
1106 m_log.InfoFormat( 1190 m_log.InfoFormat(
1191<<<<<<< HEAD
1192 "[LOGHTTP]: Slow SynchronousRestForms request {0} {1} to {2} took {3}ms, {4}ms writing, {5}",
1193 reqnum, verb, requestUrl, tickdiff, tickdata,
1194=======
1107 "[FORMS]: Slow request {0} {1} {2} took {3}ms, {4}ms writing, {5}", 1195 "[FORMS]: Slow request {0} {1} {2} took {3}ms, {4}ms writing, {5}",
1108 reqnum, 1196 reqnum,
1109 verb, 1197 verb,
@@ -1111,19 +1199,35 @@ namespace OpenSim.Framework
1111 tickdiff, 1199 tickdiff,
1112 tickset, 1200 tickset,
1113 tickdata, 1201 tickdata,
1202>>>>>>> avn/ubitvar
1114 obj.Length > WebUtil.MaxRequestDiagLength ? obj.Remove(WebUtil.MaxRequestDiagLength) : obj); 1203 obj.Length > WebUtil.MaxRequestDiagLength ? obj.Remove(WebUtil.MaxRequestDiagLength) : obj);
1204 }
1115 else if (WebUtil.DebugLevel >= 4) 1205 else if (WebUtil.DebugLevel >= 4)
1116 m_log.DebugFormat( 1206 {
1117 "[WEB UTIL]: HTTP OUT {0} took {1}ms, {2}ms writing", 1207 m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} took {1}ms, {2}ms writing",
1118 reqnum, tickdiff, tickdata); 1208 reqnum, tickdiff, tickdata);
1209 }
1210
1211 if (WebUtil.DebugLevel >= 5)
1212 WebUtil.LogResponseDetail(reqnum, respstring);
1119 1213
1120 return respstring; 1214 return respstring;
1121 } 1215 }
1122 1216
1217 public static string MakeRequest(string verb, string requestUrl, string obj, int timeoutsecs)
1218 {
1219 return MakeRequest(verb, requestUrl, obj, timeoutsecs, null);
1220 }
1221
1123 public static string MakeRequest(string verb, string requestUrl, string obj) 1222 public static string MakeRequest(string verb, string requestUrl, string obj)
1124 { 1223 {
1125 return MakeRequest(verb, requestUrl, obj, -1); 1224 return MakeRequest(verb, requestUrl, obj, -1);
1126 } 1225 }
1226
1227 public static string MakeRequest(string verb, string requestUrl, string obj, IServiceAuth auth)
1228 {
1229 return MakeRequest(verb, requestUrl, obj, -1, auth);
1230 }
1127 } 1231 }
1128 1232
1129 public class SynchronousRestObjectRequester 1233 public class SynchronousRestObjectRequester
@@ -1137,28 +1241,80 @@ namespace OpenSim.Framework
1137 /// </summary> 1241 /// </summary>
1138 /// <param name="verb"></param> 1242 /// <param name="verb"></param>
1139 /// <param name="requestUrl"></param> 1243 /// <param name="requestUrl"></param>
1140 /// <param name="obj"> </param> 1244 /// <param name="obj"></param>
1141 /// <returns></returns> 1245 /// <returns>
1142 /// 1246 /// The response. If there was an internal exception, then the default(TResponse) is returned.
1143 /// <exception cref="System.Net.WebException">Thrown if we encounter a network issue while posting 1247 /// </returns>
1144 /// the request. You'll want to make sure you deal with this as they're not uncommon</exception>
1145 public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj) 1248 public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj)
1146 { 1249 {
1147 return MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, 0); 1250 return MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, 0);
1148 } 1251 }
1149 1252
1253 public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj, IServiceAuth auth)
1254 {
1255 return MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, 0, auth);
1256 }
1257 /// <summary>
1258 /// Perform a synchronous REST request.
1259 /// </summary>
1260 /// <param name="verb"></param>
1261 /// <param name="requestUrl"></param>
1262 /// <param name="obj"></param>
1263 /// <param name="pTimeout">
1264 /// Request timeout in milliseconds. Timeout.Infinite indicates no timeout. If 0 is passed then the default HttpWebRequest timeout is used (100 seconds)
1265 /// </param>
1266 /// <returns>
1267 /// The response. If there was an internal exception or the request timed out,
1268 /// then the default(TResponse) is returned.
1269 /// </returns>
1150 public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj, int pTimeout) 1270 public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj, int pTimeout)
1151 { 1271 {
1152 return MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, pTimeout, 0); 1272 return MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, pTimeout, 0);
1153 } 1273 }
1154 1274
1275 public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj, int pTimeout, IServiceAuth auth)
1276 {
1277 return MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, pTimeout, 0, auth);
1278 }
1279
1280 /// Perform a synchronous REST request.
1281 /// </summary>
1282 /// <param name="verb"></param>
1283 /// <param name="requestUrl"></param>
1284 /// <param name="obj"></param>
1285 /// <param name="pTimeout">
1286 /// Request timeout in milliseconds. Timeout.Infinite indicates no timeout. If 0 is passed then the default HttpWebRequest timeout is used (100 seconds)
1287 /// </param>
1288 /// <param name="maxConnections"></param>
1289 /// <returns>
1290 /// The response. If there was an internal exception or the request timed out,
1291 /// then the default(TResponse) is returned.
1292 /// </returns>
1155 public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj, int pTimeout, int maxConnections) 1293 public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj, int pTimeout, int maxConnections)
1156 { 1294 {
1295 return MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, pTimeout, maxConnections, null);
1296 }
1297
1298 /// <summary>
1299 /// Perform a synchronous REST request.
1300 /// </summary>
1301 /// <param name="verb"></param>
1302 /// <param name="requestUrl"></param>
1303 /// <param name="obj"></param>
1304 /// <param name="pTimeout">
1305 /// Request timeout in milliseconds. Timeout.Infinite indicates no timeout. If 0 is passed then the default HttpWebRequest timeout is used (100 seconds)
1306 /// </param>
1307 /// <param name="maxConnections"></param>
1308 /// <returns>
1309 /// The response. If there was an internal exception or the request timed out,
1310 /// then the default(TResponse) is returned.
1311 /// </returns>
1312 public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj, int pTimeout, int maxConnections, IServiceAuth auth)
1313 {
1157 int reqnum = WebUtil.RequestNumber++; 1314 int reqnum = WebUtil.RequestNumber++;
1158 1315
1159 if (WebUtil.DebugLevel >= 3) 1316 if (WebUtil.DebugLevel >= 3)
1160 m_log.DebugFormat( 1317 m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} SynchronousRestObject {1} to {2}",
1161 "[WEB UTIL]: HTTP OUT {0} SynchronousRestObject {1} {2}",
1162 reqnum, verb, requestUrl); 1318 reqnum, verb, requestUrl);
1163 1319
1164 int tickstart = Util.EnvironmentTickCount(); 1320 int tickstart = Util.EnvironmentTickCount();
@@ -1169,6 +1325,13 @@ namespace OpenSim.Framework
1169 1325
1170 WebRequest request = WebRequest.Create(requestUrl); 1326 WebRequest request = WebRequest.Create(requestUrl);
1171 HttpWebRequest ht = (HttpWebRequest)request; 1327 HttpWebRequest ht = (HttpWebRequest)request;
1328
1329 if (auth != null)
1330 auth.AddAuthorization(ht.Headers);
1331
1332 if (pTimeout != 0)
1333 ht.Timeout = pTimeout;
1334
1172 if (maxConnections > 0 && ht.ServicePoint.ConnectionLimit < maxConnections) 1335 if (maxConnections > 0 && ht.ServicePoint.ConnectionLimit < maxConnections)
1173 ht.ServicePoint.ConnectionLimit = maxConnections; 1336 ht.ServicePoint.ConnectionLimit = maxConnections;
1174 1337
@@ -1177,15 +1340,25 @@ namespace OpenSim.Framework
1177 request.Timeout = pTimeout * 1000; 1340 request.Timeout = pTimeout * 1000;
1178 MemoryStream buffer = null; 1341 MemoryStream buffer = null;
1179 1342
1180 if ((verb == "POST") || (verb == "PUT")) 1343 try
1181 { 1344 {
1182 request.ContentType = "text/xml"; 1345 if ((verb == "POST") || (verb == "PUT"))
1346 {
1347 request.ContentType = "text/xml";
1183 1348
1184 buffer = new MemoryStream(); 1349 buffer = new MemoryStream();
1185 1350
1186 XmlWriterSettings settings = new XmlWriterSettings(); 1351 XmlWriterSettings settings = new XmlWriterSettings();
1187 settings.Encoding = Encoding.UTF8; 1352 settings.Encoding = Encoding.UTF8;
1188 1353
1354<<<<<<< HEAD
1355 using (XmlWriter writer = XmlWriter.Create(buffer, settings))
1356 {
1357 XmlSerializer serializer = new XmlSerializer(type);
1358 serializer.Serialize(writer, obj);
1359 writer.Flush();
1360 }
1361=======
1189 using (XmlWriter writer = XmlWriter.Create(buffer, settings)) 1362 using (XmlWriter writer = XmlWriter.Create(buffer, settings))
1190 { 1363 {
1191 XmlSerializer serializer = new XmlSerializer(type); 1364 XmlSerializer serializer = new XmlSerializer(type);
@@ -1194,110 +1367,227 @@ namespace OpenSim.Framework
1194 if (WebUtil.DebugLevel >= 5) 1367 if (WebUtil.DebugLevel >= 5)
1195 WebUtil.LogOutgoingDetail(buffer); 1368 WebUtil.LogOutgoingDetail(buffer);
1196 } 1369 }
1370>>>>>>> avn/ubitvar
1371
1372 int length = (int)buffer.Length;
1373 request.ContentLength = length;
1374 byte[] data = buffer.ToArray();
1375
1376<<<<<<< HEAD
1377 if (WebUtil.DebugLevel >= 5)
1378 WebUtil.LogOutgoingDetail("SEND", reqnum, System.Text.Encoding.UTF8.GetString(data));
1379
1380 try
1381 {
1382 using (Stream requestStream = request.GetRequestStream())
1383 requestStream.Write(data, 0, length);
1384 }
1385 catch (Exception e)
1386 {
1387 m_log.DebugFormat(
1388 "[SynchronousRestObjectRequester]: Exception in making request {0} {1}: {2}{3}",
1389 verb, requestUrl, e.Message, e.StackTrace);
1197 1390
1198 int length = (int)buffer.Length; 1391 return deserial;
1199 request.ContentLength = length; 1392 }
1393 finally
1394 {
1395 // capture how much time was spent writing
1396 tickdata = Util.EnvironmentTickCountSubtract(tickstart);
1397 }
1398 }
1200 1399
1400=======
1201 Stream requestStream = null; 1401 Stream requestStream = null;
1402>>>>>>> avn/ubitvar
1202 try 1403 try
1203 { 1404 {
1204 requestStream = request.GetRequestStream(); 1405 using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
1205 requestStream.Write(buffer.ToArray(), 0, length); 1406 {
1407 if (resp.ContentLength != 0)
1408 {
1409 using (Stream respStream = resp.GetResponseStream())
1410 {
1411 deserial = XMLResponseHelper.LogAndDeserialize<TRequest, TResponse>(
1412 reqnum, respStream, resp.ContentLength);
1413 }
1414 }
1415 else
1416 {
1417 m_log.DebugFormat(
1418 "[SynchronousRestObjectRequester]: Oops! no content found in response stream from {0} {1}",
1419 verb, requestUrl);
1420 }
1421 }
1206 } 1422 }
1207 catch (Exception e) 1423 catch (WebException e)
1424 {
1425 using (HttpWebResponse hwr = (HttpWebResponse)e.Response)
1426 {
1427 if (hwr != null)
1428 {
1429 if (hwr.StatusCode == HttpStatusCode.NotFound)
1430 return deserial;
1431 if (hwr.StatusCode == HttpStatusCode.Unauthorized)
1432 {
1433 m_log.Error(string.Format(
1434 "[SynchronousRestObjectRequester]: Web request {0} requires authentication ",
1435 requestUrl));
1436 return deserial;
1437 }
1438 }
1439 else
1440 m_log.Error(string.Format(
1441 "[SynchronousRestObjectRequester]: WebException for {0} {1} {2} ",
1442 verb, requestUrl, typeof(TResponse).ToString()), e);
1443 }
1444 }
1445 catch (System.InvalidOperationException)
1208 { 1446 {
1447 // This is what happens when there is invalid XML
1209 m_log.DebugFormat( 1448 m_log.DebugFormat(
1210 "[SynchronousRestObjectRequester]: Exception in making request {0} {1}: {2}{3}", 1449 "[SynchronousRestObjectRequester]: Invalid XML from {0} {1} {2}",
1211 verb, requestUrl, e.Message, e.StackTrace); 1450 verb, requestUrl, typeof(TResponse).ToString());
1212
1213 return deserial;
1214 } 1451 }
1215 finally 1452 catch (Exception e)
1216 { 1453 {
1217 if (requestStream != null) 1454 m_log.Debug(string.Format(
1218 requestStream.Close(); 1455 "[SynchronousRestObjectRequester]: Exception on response from {0} {1} ",
1219 1456 verb, requestUrl), e);
1220 // capture how much time was spent writing
1221 tickdata = Util.EnvironmentTickCountSubtract(tickstart);
1222 } 1457 }
1223 }
1224 1458
1225 try 1459 int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
1226 { 1460 if (tickdiff > WebUtil.LongCallTime)
1227 using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
1228 { 1461 {
1229 if (resp.ContentLength != 0) 1462 string originalRequest = null;
1230 { 1463
1231 using (Stream respStream = resp.GetResponseStream()) 1464 if (buffer != null)
1232 {
1233 XmlSerializer deserializer = new XmlSerializer(typeof(TResponse));
1234 deserial = (TResponse)deserializer.Deserialize(respStream);
1235 }
1236 }
1237 else
1238 { 1465 {
1239 m_log.DebugFormat( 1466 originalRequest = Encoding.UTF8.GetString(buffer.ToArray());
1240 "[SynchronousRestObjectRequester]: Oops! no content found in response stream from {0} {1}", 1467
1241 verb, requestUrl); 1468 if (originalRequest.Length > WebUtil.MaxRequestDiagLength)
1469 originalRequest = originalRequest.Remove(WebUtil.MaxRequestDiagLength);
1242 } 1470 }
1471
1472 m_log.InfoFormat(
1473 "[LOGHTTP]: Slow SynchronousRestObject request {0} {1} to {2} took {3}ms, {4}ms writing, {5}",
1474 reqnum, verb, requestUrl, tickdiff, tickdata,
1475 originalRequest);
1243 } 1476 }
1244 } 1477 else if (WebUtil.DebugLevel >= 4)
1245 catch (WebException e)
1246 {
1247 using (HttpWebResponse hwr = (HttpWebResponse)e.Response)
1248 { 1478 {
1249 if (hwr != null && hwr.StatusCode == HttpStatusCode.NotFound) 1479 m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} took {1}ms, {2}ms writing",
1250 return deserial; 1480 reqnum, tickdiff, tickdata);
1251 else
1252 m_log.ErrorFormat(
1253 "[SynchronousRestObjectRequester]: WebException for {0} {1} {2}: {3} {4}",
1254 verb, requestUrl, typeof(TResponse).ToString(), e.Message, e.StackTrace);
1255 } 1481 }
1256 } 1482 }
1257 catch (System.InvalidOperationException) 1483 finally
1258 { 1484 {
1259 // This is what happens when there is invalid XML 1485 if (buffer != null)
1260 m_log.DebugFormat( 1486 buffer.Dispose();
1261 "[SynchronousRestObjectRequester]: Invalid XML from {0} {1} {2}",
1262 verb, requestUrl, typeof(TResponse).ToString());
1263 } 1487 }
1264 catch (Exception e) 1488
1489 return deserial;
1490 }
1491
1492
1493 public static class XMLResponseHelper
1494 {
1495 public static TResponse LogAndDeserialize<TRequest, TResponse>(int reqnum, Stream respStream, long contentLength)
1265 { 1496 {
1266 m_log.DebugFormat( 1497 XmlSerializer deserializer = new XmlSerializer(typeof(TResponse));
1267 "[SynchronousRestObjectRequester]: Exception on response from {0} {1}: {2}{3}", 1498
1268 verb, requestUrl, e.Message, e.StackTrace); 1499 if (WebUtil.DebugLevel >= 5)
1500 {
1501 byte[] data = new byte[contentLength];
1502 Util.ReadStream(respStream, data);
1503
1504 WebUtil.LogResponseDetail(reqnum, System.Text.Encoding.UTF8.GetString(data));
1505
1506 using (MemoryStream temp = new MemoryStream(data))
1507 return (TResponse)deserializer.Deserialize(temp);
1508 }
1509 else
1510 {
1511 return (TResponse)deserializer.Deserialize(respStream);
1512 }
1269 } 1513 }
1514 }
1515 }
1270 1516
1271 int tickdiff = Util.EnvironmentTickCountSubtract(tickstart); 1517
1272 if (tickdiff > WebUtil.LongCallTime) 1518 public static class XMLRPCRequester
1519 {
1520 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
1521
1522 public static Hashtable SendRequest(Hashtable ReqParams, string method, string url)
1523 {
1524 int reqnum = WebUtil.RequestNumber++;
1525
1526 if (WebUtil.DebugLevel >= 3)
1527 m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} XML-RPC '{1}' to {2}",
1528 reqnum, method, url);
1529
1530 int tickstart = Util.EnvironmentTickCount();
1531 string responseStr = null;
1532
1533 try
1273 { 1534 {
1274 string originalRequest = null; 1535 ArrayList SendParams = new ArrayList();
1536 SendParams.Add(ReqParams);
1275 1537
1276 if (buffer != null) 1538 XmlRpcRequest Req = new XmlRpcRequest(method, SendParams);
1539
1540 if (WebUtil.DebugLevel >= 5)
1277 { 1541 {
1278 originalRequest = Encoding.UTF8.GetString(buffer.ToArray()); 1542 string str = Req.ToString();
1543 str = XElement.Parse(str).ToString(SaveOptions.DisableFormatting);
1544 WebUtil.LogOutgoingDetail("SEND", reqnum, str);
1545 }
1279 1546
1280 if (originalRequest.Length > WebUtil.MaxRequestDiagLength) 1547 XmlRpcResponse Resp = Req.Send(url, 30000);
1281 originalRequest = originalRequest.Remove(WebUtil.MaxRequestDiagLength); 1548
1549 try
1550 {
1551 responseStr = Resp.ToString();
1552 responseStr = XElement.Parse(responseStr).ToString(SaveOptions.DisableFormatting);
1553
1554 if (WebUtil.DebugLevel >= 5)
1555 WebUtil.LogResponseDetail(reqnum, responseStr);
1556 }
1557 catch (Exception e)
1558 {
1559 m_log.Error("Error parsing XML-RPC response", e);
1560 }
1561
1562 if (Resp.IsFault)
1563 {
1564 m_log.DebugFormat(
1565 "[LOGHTTP]: XML-RPC request {0} '{1}' to {2} FAILED: FaultCode={3}, FaultMessage={4}",
1566 reqnum, method, url, Resp.FaultCode, Resp.FaultString);
1567 return null;
1282 } 1568 }
1283 1569
1284 m_log.InfoFormat( 1570 Hashtable RespData = (Hashtable)Resp.Value;
1285 "[SynchronousRestObjectRequester]: Slow request {0} {1} {2} took {3}ms, {4}ms writing, {5}", 1571 return RespData;
1286 reqnum,
1287 verb,
1288 requestUrl,
1289 tickdiff,
1290 tickdata,
1291 originalRequest);
1292 } 1572 }
1293 else if (WebUtil.DebugLevel >= 4) 1573 finally
1294 { 1574 {
1295 m_log.DebugFormat( 1575 int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
1296 "[WEB UTIL]: HTTP OUT {0} took {1}ms, {2}ms writing", 1576 if (tickdiff > WebUtil.LongCallTime)
1297 reqnum, tickdiff, tickdata); 1577 {
1578 m_log.InfoFormat(
1579 "[LOGHTTP]: Slow XML-RPC request {0} '{1}' to {2} took {3}ms, {4}",
1580 reqnum, method, url, tickdiff,
1581 responseStr != null
1582 ? (responseStr.Length > WebUtil.MaxRequestDiagLength ? responseStr.Remove(WebUtil.MaxRequestDiagLength) : responseStr)
1583 : "");
1584 }
1585 else if (WebUtil.DebugLevel >= 4)
1586 {
1587 m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} took {1}ms", reqnum, tickdiff);
1588 }
1298 } 1589 }
1299
1300 return deserial;
1301 } 1590 }
1591
1302 } 1592 }
1303} \ No newline at end of file 1593}