WebUtil.cs 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Collections.Specialized;
  31. using System.Globalization;
  32. using System.IO;
  33. using System.IO.Compression;
  34. using System.Net;
  35. using System.Net.Security;
  36. using System.Reflection;
  37. using System.Text;
  38. using System.Web;
  39. using System.Xml;
  40. using System.Xml.Serialization;
  41. using System.Xml.Linq;
  42. using log4net;
  43. using Nwc.XmlRpc;
  44. using OpenMetaverse.StructuredData;
  45. using XMLResponseHelper = OpenSim.Framework.SynchronousRestObjectRequester.XMLResponseHelper;
  46. using OpenSim.Framework.ServiceAuth;
  47. namespace OpenSim.Framework
  48. {
  49. /// <summary>
  50. /// Miscellaneous static methods and extension methods related to the web
  51. /// </summary>
  52. public static class WebUtil
  53. {
  54. private static readonly ILog m_log =
  55. LogManager.GetLogger(
  56. MethodBase.GetCurrentMethod().DeclaringType);
  57. /// <summary>
  58. /// Control the printing of certain debug messages.
  59. /// </summary>
  60. /// <remarks>
  61. /// If DebugLevel >= 3 then short notices about outgoing HTTP requests are logged.
  62. /// </remarks>
  63. public static int DebugLevel { get; set; }
  64. /// <summary>
  65. /// Request number for diagnostic purposes.
  66. /// </summary>
  67. public static int RequestNumber { get; set; }
  68. /// <summary>
  69. /// this is the header field used to communicate the local request id
  70. /// used for performance and debugging
  71. /// </summary>
  72. public const string OSHeaderRequestID = "opensim-request-id";
  73. /// <summary>
  74. /// Number of milliseconds a call can take before it is considered
  75. /// a "long" call for warning & debugging purposes
  76. /// </summary>
  77. public const int LongCallTime = 3000;
  78. /// <summary>
  79. /// The maximum length of any data logged because of a long request time.
  80. /// </summary>
  81. /// <remarks>
  82. /// This is to truncate any really large post data, such as an asset. In theory, the first section should
  83. /// give us useful information about the call (which agent it relates to if applicable, etc.).
  84. /// This is also used to truncate messages when using DebugLevel 5.
  85. /// </remarks>
  86. public const int MaxRequestDiagLength = 200;
  87. #region JSONRequest
  88. /// <summary>
  89. /// PUT JSON-encoded data to a web service that returns LLSD or
  90. /// JSON data
  91. /// </summary>
  92. public static OSDMap PutToServiceCompressed(string url, OSDMap data, int timeout)
  93. {
  94. return ServiceOSDRequest(url,data, "PUT", timeout, true, false);
  95. }
  96. public static OSDMap PutToService(string url, OSDMap data, int timeout)
  97. {
  98. return ServiceOSDRequest(url,data, "PUT", timeout, false, false);
  99. }
  100. public static OSDMap PostToService(string url, OSDMap data, int timeout, bool rpc)
  101. {
  102. return ServiceOSDRequest(url, data, "POST", timeout, false, rpc);
  103. }
  104. public static OSDMap PostToServiceCompressed(string url, OSDMap data, int timeout)
  105. {
  106. return ServiceOSDRequest(url, data, "POST", timeout, true, false);
  107. }
  108. public static OSDMap GetFromService(string url, int timeout)
  109. {
  110. return ServiceOSDRequest(url, null, "GET", timeout, false, false);
  111. }
  112. public static void LogOutgoingDetail(Stream outputStream)
  113. {
  114. LogOutgoingDetail("", outputStream);
  115. }
  116. public static void LogOutgoingDetail(string context, Stream outputStream)
  117. {
  118. using (Stream stream = Util.Copy(outputStream))
  119. using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
  120. {
  121. string output;
  122. if (DebugLevel == 5)
  123. {
  124. char[] chars = new char[WebUtil.MaxRequestDiagLength + 1]; // +1 so we know to add "..." only if needed
  125. int len = reader.Read(chars, 0, WebUtil.MaxRequestDiagLength + 1);
  126. output = new string(chars, 0, len);
  127. }
  128. else
  129. {
  130. output = reader.ReadToEnd();
  131. }
  132. LogOutgoingDetail(context, output);
  133. }
  134. }
  135. public static void LogOutgoingDetail(string type, int reqnum, string output)
  136. {
  137. LogOutgoingDetail(string.Format("{0} {1}: ", type, reqnum), output);
  138. }
  139. public static void LogOutgoingDetail(string context, string output)
  140. {
  141. if (DebugLevel == 5)
  142. {
  143. if (output.Length > MaxRequestDiagLength)
  144. output = output.Substring(0, MaxRequestDiagLength) + "...";
  145. }
  146. m_log.DebugFormat("[LOGHTTP]: {0}{1}", context, Util.BinaryToASCII(output));
  147. }
  148. public static void LogResponseDetail(int reqnum, Stream inputStream)
  149. {
  150. LogOutgoingDetail(string.Format("RESPONSE {0}: ", reqnum), inputStream);
  151. }
  152. public static void LogResponseDetail(int reqnum, string input)
  153. {
  154. LogOutgoingDetail(string.Format("RESPONSE {0}: ", reqnum), input);
  155. }
  156. public static OSDMap ServiceOSDRequest(string url, OSDMap data, string method, int timeout, bool compressed, bool rpc)
  157. {
  158. int reqnum = RequestNumber++;
  159. if (DebugLevel >= 3)
  160. m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} JSON-RPC {1} to {2}",
  161. reqnum, method, url);
  162. string errorMessage = "unknown error";
  163. int tickstart = Util.EnvironmentTickCount();
  164. int tickdata = 0;
  165. int tickcompressdata = 0;
  166. int tickJsondata = 0;
  167. int compsize = 0;
  168. string strBuffer = null;
  169. try
  170. {
  171. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  172. request.Method = method;
  173. request.Timeout = timeout;
  174. request.KeepAlive = false;
  175. request.MaximumAutomaticRedirections = 10;
  176. request.ReadWriteTimeout = timeout / 4;
  177. request.Headers[OSHeaderRequestID] = reqnum.ToString();
  178. // If there is some input, write it into the request
  179. if (data != null)
  180. {
  181. strBuffer = OSDParser.SerializeJsonString(data);
  182. tickJsondata = Util.EnvironmentTickCountSubtract(tickstart);
  183. if (DebugLevel >= 5)
  184. LogOutgoingDetail("SEND", reqnum, strBuffer);
  185. byte[] buffer = System.Text.Encoding.UTF8.GetBytes(strBuffer);
  186. request.ContentType = rpc ? "application/json-rpc" : "application/json";
  187. if (compressed)
  188. {
  189. request.Headers["X-Content-Encoding"] = "gzip"; // can't set "Content-Encoding" because old OpenSims fail if they get an unrecognized Content-Encoding
  190. using (MemoryStream ms = new MemoryStream())
  191. {
  192. using (GZipStream comp = new GZipStream(ms, CompressionMode.Compress, true))
  193. {
  194. comp.Write(buffer, 0, buffer.Length);
  195. // We need to close the gzip stream before we write it anywhere
  196. // because apparently something important related to gzip compression
  197. // gets written on the stream upon Dispose()
  198. }
  199. byte[] buf = ms.ToArray();
  200. tickcompressdata = Util.EnvironmentTickCountSubtract(tickstart);
  201. request.ContentLength = buf.Length; //Count bytes to send
  202. compsize = buf.Length;
  203. using (Stream requestStream = request.GetRequestStream())
  204. requestStream.Write(buf, 0, (int)buf.Length);
  205. }
  206. }
  207. else
  208. {
  209. compsize = buffer.Length;
  210. request.ContentLength = buffer.Length; //Count bytes to send
  211. using (Stream requestStream = request.GetRequestStream())
  212. requestStream.Write(buffer, 0, buffer.Length); //Send it
  213. }
  214. }
  215. // capture how much time was spent writing, this may seem silly
  216. // but with the number concurrent requests, this often blocks
  217. tickdata = Util.EnvironmentTickCountSubtract(tickstart);
  218. using (WebResponse response = request.GetResponse())
  219. {
  220. using (Stream responseStream = response.GetResponseStream())
  221. {
  222. using (StreamReader reader = new StreamReader(responseStream))
  223. {
  224. string responseStr = reader.ReadToEnd();
  225. if (WebUtil.DebugLevel >= 5)
  226. WebUtil.LogResponseDetail(reqnum, responseStr);
  227. return CanonicalizeResults(responseStr);
  228. }
  229. }
  230. }
  231. }
  232. catch (WebException we)
  233. {
  234. errorMessage = we.Message;
  235. if (we.Status == WebExceptionStatus.ProtocolError)
  236. {
  237. using (HttpWebResponse webResponse = (HttpWebResponse)we.Response)
  238. errorMessage = String.Format("[{0}] {1}", webResponse.StatusCode, webResponse.StatusDescription);
  239. }
  240. }
  241. catch (Exception ex)
  242. {
  243. errorMessage = ex.Message;
  244. m_log.Debug("[WEB UTIL]: Exception making request: " + ex.ToString());
  245. }
  246. finally
  247. {
  248. int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
  249. if (tickdiff > LongCallTime)
  250. {
  251. m_log.InfoFormat(
  252. "[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}",
  253. reqnum,
  254. method,
  255. url,
  256. tickdiff,
  257. tickdata,
  258. tickJsondata,
  259. tickcompressdata,
  260. compsize,
  261. strBuffer != null ? strBuffer.Length : 0,
  262. strBuffer != null
  263. ? (strBuffer.Length > MaxRequestDiagLength ? strBuffer.Remove(MaxRequestDiagLength) : strBuffer)
  264. : "");
  265. }
  266. else if (DebugLevel >= 4)
  267. {
  268. m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} took {1}ms, {2}ms writing",
  269. reqnum, tickdiff, tickdata);
  270. }
  271. }
  272. m_log.DebugFormat(
  273. "[LOGHTTP]: JSON-RPC request {0} {1} to {2} FAILED: {3}", reqnum, method, url, errorMessage);
  274. return ErrorResponseMap(errorMessage);
  275. }
  276. /// <summary>
  277. /// Since there are no consistencies in the way web requests are
  278. /// formed, we need to do a little guessing about the result format.
  279. /// Keys:
  280. /// Success|success == the success fail of the request
  281. /// _RawResult == the raw string that came back
  282. /// _Result == the OSD unpacked string
  283. /// </summary>
  284. private static OSDMap CanonicalizeResults(string response)
  285. {
  286. OSDMap result = new OSDMap();
  287. // Default values
  288. result["Success"] = OSD.FromBoolean(true);
  289. result["success"] = OSD.FromBoolean(true);
  290. result["_RawResult"] = OSD.FromString(response);
  291. result["_Result"] = new OSDMap();
  292. if (response.Equals("true",System.StringComparison.OrdinalIgnoreCase))
  293. return result;
  294. if (response.Equals("false",System.StringComparison.OrdinalIgnoreCase))
  295. {
  296. result["Success"] = OSD.FromBoolean(false);
  297. result["success"] = OSD.FromBoolean(false);
  298. return result;
  299. }
  300. try
  301. {
  302. OSD responseOSD = OSDParser.Deserialize(response);
  303. if (responseOSD.Type == OSDType.Map)
  304. {
  305. result["_Result"] = (OSDMap)responseOSD;
  306. return result;
  307. }
  308. }
  309. catch
  310. {
  311. // don't need to treat this as an error... we're just guessing anyway
  312. // m_log.DebugFormat("[WEB UTIL] couldn't decode <{0}>: {1}",response,e.Message);
  313. }
  314. return result;
  315. }
  316. #endregion JSONRequest
  317. #region FormRequest
  318. /// <summary>
  319. /// POST URL-encoded form data to a web service that returns LLSD or
  320. /// JSON data
  321. /// </summary>
  322. public static OSDMap PostToService(string url, NameValueCollection data)
  323. {
  324. return ServiceFormRequest(url,data, 30000);
  325. }
  326. public static OSDMap ServiceFormRequest(string url, NameValueCollection data, int timeout)
  327. {
  328. int reqnum = RequestNumber++;
  329. string method = (data != null && data["RequestMethod"] != null) ? data["RequestMethod"] : "unknown";
  330. if (DebugLevel >= 3)
  331. m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} ServiceForm '{1}' to {2}",
  332. reqnum, method, url);
  333. string errorMessage = "unknown error";
  334. int tickstart = Util.EnvironmentTickCount();
  335. int tickdata = 0;
  336. string queryString = null;
  337. try
  338. {
  339. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
  340. request.Method = "POST";
  341. request.Timeout = timeout;
  342. request.KeepAlive = false;
  343. request.MaximumAutomaticRedirections = 10;
  344. request.ReadWriteTimeout = timeout / 4;
  345. request.Headers[OSHeaderRequestID] = reqnum.ToString();
  346. if (data != null)
  347. {
  348. queryString = BuildQueryString(data);
  349. if (DebugLevel >= 5)
  350. LogOutgoingDetail("SEND", reqnum, queryString);
  351. byte[] buffer = System.Text.Encoding.UTF8.GetBytes(queryString);
  352. request.ContentLength = buffer.Length;
  353. request.ContentType = "application/x-www-form-urlencoded";
  354. using (Stream requestStream = request.GetRequestStream())
  355. requestStream.Write(buffer, 0, buffer.Length);
  356. }
  357. // capture how much time was spent writing, this may seem silly
  358. // but with the number concurrent requests, this often blocks
  359. tickdata = Util.EnvironmentTickCountSubtract(tickstart);
  360. using (WebResponse response = request.GetResponse())
  361. {
  362. using (Stream responseStream = response.GetResponseStream())
  363. {
  364. using (StreamReader reader = new StreamReader(responseStream))
  365. {
  366. string responseStr = reader.ReadToEnd();
  367. if (WebUtil.DebugLevel >= 5)
  368. WebUtil.LogResponseDetail(reqnum, responseStr);
  369. OSD responseOSD = OSDParser.Deserialize(responseStr);
  370. if (responseOSD.Type == OSDType.Map)
  371. return (OSDMap)responseOSD;
  372. }
  373. }
  374. }
  375. }
  376. catch (WebException we)
  377. {
  378. errorMessage = we.Message;
  379. if (we.Status == WebExceptionStatus.ProtocolError)
  380. {
  381. using (HttpWebResponse webResponse = (HttpWebResponse)we.Response)
  382. errorMessage = String.Format("[{0}] {1}",webResponse.StatusCode,webResponse.StatusDescription);
  383. }
  384. }
  385. catch (Exception ex)
  386. {
  387. errorMessage = ex.Message;
  388. }
  389. finally
  390. {
  391. int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
  392. if (tickdiff > LongCallTime)
  393. {
  394. m_log.InfoFormat(
  395. "[LOGHTTP]: Slow ServiceForm request {0} '{1}' to {2} took {3}ms, {4}ms writing, {5}",
  396. reqnum, method, url, tickdiff, tickdata,
  397. queryString != null
  398. ? (queryString.Length > MaxRequestDiagLength) ? queryString.Remove(MaxRequestDiagLength) : queryString
  399. : "");
  400. }
  401. else if (DebugLevel >= 4)
  402. {
  403. m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} took {1}ms, {2}ms writing",
  404. reqnum, tickdiff, tickdata);
  405. }
  406. }
  407. m_log.WarnFormat("[LOGHTTP]: ServiceForm request {0} '{1}' to {2} failed: {3}", reqnum, method, url, errorMessage);
  408. return ErrorResponseMap(errorMessage);
  409. }
  410. /// <summary>
  411. /// Create a response map for an error, trying to keep
  412. /// the result formats consistent
  413. /// </summary>
  414. private static OSDMap ErrorResponseMap(string msg)
  415. {
  416. OSDMap result = new OSDMap();
  417. result["Success"] = "False";
  418. result["Message"] = OSD.FromString("Service request failed: " + msg);
  419. return result;
  420. }
  421. #endregion FormRequest
  422. #region Uri
  423. /// <summary>
  424. /// Combines a Uri that can contain both a base Uri and relative path
  425. /// with a second relative path fragment
  426. /// </summary>
  427. /// <param name="uri">Starting (base) Uri</param>
  428. /// <param name="fragment">Relative path fragment to append to the end
  429. /// of the Uri</param>
  430. /// <returns>The combined Uri</returns>
  431. /// <remarks>This is similar to the Uri constructor that takes a base
  432. /// Uri and the relative path, except this method can append a relative
  433. /// path fragment on to an existing relative path</remarks>
  434. public static Uri Combine(this Uri uri, string fragment)
  435. {
  436. string fragment1 = uri.Fragment;
  437. string fragment2 = fragment;
  438. if (!fragment1.EndsWith("/"))
  439. fragment1 = fragment1 + '/';
  440. if (fragment2.StartsWith("/"))
  441. fragment2 = fragment2.Substring(1);
  442. return new Uri(uri, fragment1 + fragment2);
  443. }
  444. /// <summary>
  445. /// Combines a Uri that can contain both a base Uri and relative path
  446. /// with a second relative path fragment. If the fragment is absolute,
  447. /// it will be returned without modification
  448. /// </summary>
  449. /// <param name="uri">Starting (base) Uri</param>
  450. /// <param name="fragment">Relative path fragment to append to the end
  451. /// of the Uri, or an absolute Uri to return unmodified</param>
  452. /// <returns>The combined Uri</returns>
  453. public static Uri Combine(this Uri uri, Uri fragment)
  454. {
  455. if (fragment.IsAbsoluteUri)
  456. return fragment;
  457. string fragment1 = uri.Fragment;
  458. string fragment2 = fragment.ToString();
  459. if (!fragment1.EndsWith("/"))
  460. fragment1 = fragment1 + '/';
  461. if (fragment2.StartsWith("/"))
  462. fragment2 = fragment2.Substring(1);
  463. return new Uri(uri, fragment1 + fragment2);
  464. }
  465. /// <summary>
  466. /// Appends a query string to a Uri that may or may not have existing
  467. /// query parameters
  468. /// </summary>
  469. /// <param name="uri">Uri to append the query to</param>
  470. /// <param name="query">Query string to append. Can either start with ?
  471. /// or just containg key/value pairs</param>
  472. /// <returns>String representation of the Uri with the query string
  473. /// appended</returns>
  474. public static string AppendQuery(this Uri uri, string query)
  475. {
  476. if (String.IsNullOrEmpty(query))
  477. return uri.ToString();
  478. if (query[0] == '?' || query[0] == '&')
  479. query = query.Substring(1);
  480. string uriStr = uri.ToString();
  481. if (uriStr.Contains("?"))
  482. return uriStr + '&' + query;
  483. else
  484. return uriStr + '?' + query;
  485. }
  486. #endregion Uri
  487. #region NameValueCollection
  488. /// <summary>
  489. /// Convert a NameValueCollection into a query string. This is the
  490. /// inverse of HttpUtility.ParseQueryString()
  491. /// </summary>
  492. /// <param name="parameters">Collection of key/value pairs to convert</param>
  493. /// <returns>A query string with URL-escaped values</returns>
  494. public static string BuildQueryString(NameValueCollection parameters)
  495. {
  496. List<string> items = new List<string>(parameters.Count);
  497. foreach (string key in parameters.Keys)
  498. {
  499. string[] values = parameters.GetValues(key);
  500. if (values != null)
  501. {
  502. foreach (string value in values)
  503. items.Add(String.Concat(key, "=", HttpUtility.UrlEncode(value ?? String.Empty)));
  504. }
  505. }
  506. return String.Join("&", items.ToArray());
  507. }
  508. /// <summary>
  509. ///
  510. /// </summary>
  511. /// <param name="collection"></param>
  512. /// <param name="key"></param>
  513. /// <returns></returns>
  514. public static string GetOne(this NameValueCollection collection, string key)
  515. {
  516. string[] values = collection.GetValues(key);
  517. if (values != null && values.Length > 0)
  518. return values[0];
  519. return null;
  520. }
  521. #endregion NameValueCollection
  522. #region Stream
  523. /// <summary>
  524. /// Copies the contents of one stream to another, starting at the
  525. /// current position of each stream
  526. /// </summary>
  527. /// <param name="copyFrom">The stream to copy from, at the position
  528. /// where copying should begin</param>
  529. /// <param name="copyTo">The stream to copy to, at the position where
  530. /// bytes should be written</param>
  531. /// <param name="maximumBytesToCopy">The maximum bytes to copy</param>
  532. /// <returns>The total number of bytes copied</returns>
  533. /// <remarks>
  534. /// Copying begins at the streams' current positions. The positions are
  535. /// NOT reset after copying is complete.
  536. /// NOTE!! .NET 4.0 adds the method 'Stream.CopyTo(stream, bufferSize)'.
  537. /// This function could be replaced with that method once we move
  538. /// totally to .NET 4.0. For versions before, this routine exists.
  539. /// This routine used to be named 'CopyTo' but the int parameter has
  540. /// a different meaning so this method was renamed to avoid any confusion.
  541. /// </remarks>
  542. public static int CopyStream(this Stream copyFrom, Stream copyTo, int maximumBytesToCopy)
  543. {
  544. byte[] buffer = new byte[4096];
  545. int readBytes;
  546. int totalCopiedBytes = 0;
  547. while ((readBytes = copyFrom.Read(buffer, 0, Math.Min(4096, maximumBytesToCopy))) > 0)
  548. {
  549. int writeBytes = Math.Min(maximumBytesToCopy, readBytes);
  550. copyTo.Write(buffer, 0, writeBytes);
  551. totalCopiedBytes += writeBytes;
  552. maximumBytesToCopy -= writeBytes;
  553. }
  554. return totalCopiedBytes;
  555. }
  556. #endregion Stream
  557. public class QBasedComparer : IComparer
  558. {
  559. public int Compare(Object x, Object y)
  560. {
  561. float qx = GetQ(x);
  562. float qy = GetQ(y);
  563. return qy.CompareTo(qx); // descending order
  564. }
  565. private float GetQ(Object o)
  566. {
  567. // Example: image/png;q=0.9
  568. float qvalue = 1F;
  569. if (o is String)
  570. {
  571. string mime = (string)o;
  572. string[] parts = mime.Split(';');
  573. if (parts.Length > 1)
  574. {
  575. string[] kvp = parts[1].Split('=');
  576. if (kvp.Length == 2 && kvp[0] == "q")
  577. float.TryParse(kvp[1], NumberStyles.Number, CultureInfo.InvariantCulture, out qvalue);
  578. }
  579. }
  580. return qvalue;
  581. }
  582. }
  583. /// <summary>
  584. /// Takes the value of an Accept header and returns the preferred types
  585. /// ordered by q value (if it exists).
  586. /// Example input: image/jpg;q=0.7, image/png;q=0.8, image/jp2
  587. /// Exmaple output: ["jp2", "png", "jpg"]
  588. /// NOTE: This doesn't handle the semantics of *'s...
  589. /// </summary>
  590. /// <param name="accept"></param>
  591. /// <returns></returns>
  592. public static string[] GetPreferredImageTypes(string accept)
  593. {
  594. if (string.IsNullOrEmpty(accept))
  595. return new string[0];
  596. string[] types = accept.Split(new char[] { ',' });
  597. if (types.Length > 0)
  598. {
  599. List<string> list = new List<string>(types);
  600. list.RemoveAll(delegate(string s) { return !s.ToLower().StartsWith("image"); });
  601. ArrayList tlist = new ArrayList(list);
  602. tlist.Sort(new QBasedComparer());
  603. string[] result = new string[tlist.Count];
  604. for (int i = 0; i < tlist.Count; i++)
  605. {
  606. string mime = (string)tlist[i];
  607. string[] parts = mime.Split(new char[] { ';' });
  608. string[] pair = parts[0].Split(new char[] { '/' });
  609. if (pair.Length == 2)
  610. result[i] = pair[1].ToLower();
  611. else // oops, we don't know what this is...
  612. result[i] = pair[0];
  613. }
  614. return result;
  615. }
  616. return new string[0];
  617. }
  618. }
  619. public static class AsynchronousRestObjectRequester
  620. {
  621. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  622. /// <summary>
  623. /// Perform an asynchronous REST request.
  624. /// </summary>
  625. /// <param name="verb">GET or POST</param>
  626. /// <param name="requestUrl"></param>
  627. /// <param name="obj"></param>
  628. /// <param name="action"></param>
  629. /// <returns></returns>
  630. ///
  631. /// <exception cref="System.Net.WebException">Thrown if we encounter a
  632. /// network issue while posting the request. You'll want to make
  633. /// sure you deal with this as they're not uncommon</exception>
  634. //
  635. public static void MakeRequest<TRequest, TResponse>(string verb,
  636. string requestUrl, TRequest obj, Action<TResponse> action)
  637. {
  638. MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, action, 0);
  639. }
  640. public static void MakeRequest<TRequest, TResponse>(string verb,
  641. string requestUrl, TRequest obj, Action<TResponse> action,
  642. int maxConnections)
  643. {
  644. MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, action, maxConnections, null);
  645. }
  646. /// <summary>
  647. /// Perform a synchronous REST request.
  648. /// </summary>
  649. /// <param name="verb"></param>
  650. /// <param name="requestUrl"></param>
  651. /// <param name="obj"></param>
  652. /// <param name="pTimeout">
  653. /// Request timeout in seconds. Timeout.Infinite indicates no timeout. If 0 is passed then the default HttpWebRequest timeout is used (100 seconds)
  654. /// </param>
  655. /// <param name="maxConnections"></param>
  656. /// <returns>
  657. /// The response. If there was an internal exception or the request timed out,
  658. /// then the default(TResponse) is returned.
  659. /// </returns>
  660. public static void MakeRequest<TRequest, TResponse>(string verb,
  661. string requestUrl, TRequest obj, Action<TResponse> action,
  662. int maxConnections, IServiceAuth auth)
  663. {
  664. int reqnum = WebUtil.RequestNumber++;
  665. if (WebUtil.DebugLevel >= 3)
  666. m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} AsynchronousRequestObject {1} to {2}",
  667. reqnum, verb, requestUrl);
  668. int tickstart = Util.EnvironmentTickCount();
  669. int tickdata = 0;
  670. int tickdiff = 0;
  671. Type type = typeof(TRequest);
  672. WebRequest request = WebRequest.Create(requestUrl);
  673. HttpWebRequest ht = (HttpWebRequest)request;
  674. if (auth != null)
  675. auth.AddAuthorization(ht.Headers);
  676. if (maxConnections > 0 && ht.ServicePoint.ConnectionLimit < maxConnections)
  677. ht.ServicePoint.ConnectionLimit = maxConnections;
  678. TResponse deserial = default(TResponse);
  679. request.Method = verb;
  680. MemoryStream buffer = null;
  681. try
  682. {
  683. if (verb == "POST")
  684. {
  685. request.ContentType = "text/xml";
  686. buffer = new MemoryStream();
  687. XmlWriterSettings settings = new XmlWriterSettings();
  688. settings.Encoding = Encoding.UTF8;
  689. using (XmlWriter writer = XmlWriter.Create(buffer, settings))
  690. {
  691. XmlSerializer serializer = new XmlSerializer(type);
  692. serializer.Serialize(writer, obj);
  693. writer.Flush();
  694. }
  695. int length = (int)buffer.Length;
  696. request.ContentLength = length;
  697. byte[] data = buffer.ToArray();
  698. if (WebUtil.DebugLevel >= 5)
  699. WebUtil.LogOutgoingDetail("SEND", reqnum, System.Text.Encoding.UTF8.GetString(data));
  700. request.BeginGetRequestStream(delegate(IAsyncResult res)
  701. {
  702. using (Stream requestStream = request.EndGetRequestStream(res))
  703. requestStream.Write(data, 0, length);
  704. // capture how much time was spent writing
  705. tickdata = Util.EnvironmentTickCountSubtract(tickstart);
  706. request.BeginGetResponse(delegate(IAsyncResult ar)
  707. {
  708. using (WebResponse response = request.EndGetResponse(ar))
  709. {
  710. try
  711. {
  712. using (Stream respStream = response.GetResponseStream())
  713. {
  714. deserial = XMLResponseHelper.LogAndDeserialize<TRequest, TResponse>(
  715. reqnum, respStream, response.ContentLength);
  716. }
  717. }
  718. catch (System.InvalidOperationException)
  719. {
  720. }
  721. }
  722. action(deserial);
  723. }, null);
  724. }, null);
  725. }
  726. else
  727. {
  728. request.BeginGetResponse(delegate(IAsyncResult res2)
  729. {
  730. try
  731. {
  732. // If the server returns a 404, this appears to trigger a System.Net.WebException even though that isn't
  733. // documented in MSDN
  734. using (WebResponse response = request.EndGetResponse(res2))
  735. {
  736. try
  737. {
  738. using (Stream respStream = response.GetResponseStream())
  739. {
  740. deserial = XMLResponseHelper.LogAndDeserialize<TRequest, TResponse>(
  741. reqnum, respStream, response.ContentLength);
  742. }
  743. }
  744. catch (System.InvalidOperationException)
  745. {
  746. }
  747. }
  748. }
  749. catch (WebException e)
  750. {
  751. if (e.Status == WebExceptionStatus.ProtocolError)
  752. {
  753. if (e.Response is HttpWebResponse)
  754. {
  755. using (HttpWebResponse httpResponse = (HttpWebResponse)e.Response)
  756. {
  757. if (httpResponse.StatusCode != HttpStatusCode.NotFound)
  758. {
  759. // We don't appear to be handling any other status codes, so log these feailures to that
  760. // people don't spend unnecessary hours hunting phantom bugs.
  761. m_log.DebugFormat(
  762. "[ASYNC REQUEST]: Request {0} {1} failed with unexpected status code {2}",
  763. verb, requestUrl, httpResponse.StatusCode);
  764. }
  765. }
  766. }
  767. }
  768. else
  769. {
  770. m_log.ErrorFormat(
  771. "[ASYNC REQUEST]: Request {0} {1} failed with status {2} and message {3}",
  772. verb, requestUrl, e.Status, e.Message);
  773. }
  774. }
  775. catch (Exception e)
  776. {
  777. m_log.ErrorFormat(
  778. "[ASYNC REQUEST]: Request {0} {1} failed with exception {2}{3}",
  779. verb, requestUrl, e.Message, e.StackTrace);
  780. }
  781. // m_log.DebugFormat("[ASYNC REQUEST]: Received {0}", deserial.ToString());
  782. try
  783. {
  784. action(deserial);
  785. }
  786. catch (Exception e)
  787. {
  788. m_log.ErrorFormat(
  789. "[ASYNC REQUEST]: Request {0} {1} callback failed with exception {2}{3}",
  790. verb, requestUrl, e.Message, e.StackTrace);
  791. }
  792. }, null);
  793. }
  794. tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
  795. if (tickdiff > WebUtil.LongCallTime)
  796. {
  797. string originalRequest = null;
  798. if (buffer != null)
  799. {
  800. originalRequest = Encoding.UTF8.GetString(buffer.ToArray());
  801. if (originalRequest.Length > WebUtil.MaxRequestDiagLength)
  802. originalRequest = originalRequest.Remove(WebUtil.MaxRequestDiagLength);
  803. }
  804. m_log.InfoFormat(
  805. "[LOGHTTP]: Slow AsynchronousRequestObject request {0} {1} to {2} took {3}ms, {4}ms writing, {5}",
  806. reqnum, verb, requestUrl, tickdiff, tickdata,
  807. originalRequest);
  808. }
  809. else if (WebUtil.DebugLevel >= 4)
  810. {
  811. m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} took {1}ms, {2}ms writing",
  812. reqnum, tickdiff, tickdata);
  813. }
  814. }
  815. finally
  816. {
  817. if (buffer != null)
  818. buffer.Dispose();
  819. }
  820. }
  821. }
  822. public static class SynchronousRestFormsRequester
  823. {
  824. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  825. /// <summary>
  826. /// Perform a synchronous REST request.
  827. /// </summary>
  828. /// <param name="verb"></param>
  829. /// <param name="requestUrl"></param>
  830. /// <param name="obj"> </param>
  831. /// <param name="timeoutsecs"> </param>
  832. /// <returns></returns>
  833. ///
  834. /// <exception cref="System.Net.WebException">Thrown if we encounter a network issue while posting
  835. /// the request. You'll want to make sure you deal with this as they're not uncommon</exception>
  836. public static string MakeRequest(string verb, string requestUrl, string obj, int timeoutsecs = -1,
  837. IServiceAuth auth = null, bool keepalive = true)
  838. {
  839. int reqnum = WebUtil.RequestNumber++;
  840. if (WebUtil.DebugLevel >= 3)
  841. m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} SynchronousRestForms {1} to {2}",
  842. reqnum, verb, requestUrl);
  843. int tickstart = Util.EnvironmentTickCount();
  844. int tickdata = 0;
  845. WebRequest request = WebRequest.Create(requestUrl);
  846. request.Method = verb;
  847. if (timeoutsecs > 0)
  848. request.Timeout = timeoutsecs * 1000;
  849. if(!keepalive && request is HttpWebRequest)
  850. ((HttpWebRequest)request).KeepAlive = false;
  851. if (auth != null)
  852. auth.AddAuthorization(request.Headers);
  853. string respstring = String.Empty;
  854. int tickset = Util.EnvironmentTickCountSubtract(tickstart);
  855. using (MemoryStream buffer = new MemoryStream())
  856. {
  857. if ((verb == "POST") || (verb == "PUT"))
  858. {
  859. request.ContentType = "application/x-www-form-urlencoded";
  860. int length = 0;
  861. using (StreamWriter writer = new StreamWriter(buffer))
  862. {
  863. writer.Write(obj);
  864. writer.Flush();
  865. }
  866. length = (int)obj.Length;
  867. request.ContentLength = length;
  868. byte[] data = buffer.ToArray();
  869. if (WebUtil.DebugLevel >= 5)
  870. WebUtil.LogOutgoingDetail("SEND", reqnum, System.Text.Encoding.UTF8.GetString(data));
  871. try
  872. {
  873. using(Stream requestStream = request.GetRequestStream())
  874. requestStream.Write(data,0,length);
  875. }
  876. catch (Exception e)
  877. {
  878. m_log.InfoFormat("[FORMS]: Error sending request to {0}: {1}. Request: {2}", requestUrl, e.Message,
  879. obj.Length > WebUtil.MaxRequestDiagLength ? obj.Remove(WebUtil.MaxRequestDiagLength) : obj);
  880. throw e;
  881. }
  882. finally
  883. {
  884. // capture how much time was spent writing
  885. tickdata = Util.EnvironmentTickCountSubtract(tickstart);
  886. }
  887. }
  888. try
  889. {
  890. using (WebResponse resp = request.GetResponse())
  891. {
  892. if (resp.ContentLength != 0)
  893. {
  894. using (Stream respStream = resp.GetResponseStream())
  895. using (StreamReader reader = new StreamReader(respStream))
  896. respstring = reader.ReadToEnd();
  897. }
  898. }
  899. }
  900. catch (Exception e)
  901. {
  902. m_log.InfoFormat("[FORMS]: Error receiving response from {0}: {1}. Request: {2}", requestUrl, e.Message,
  903. obj.Length > WebUtil.MaxRequestDiagLength ? obj.Remove(WebUtil.MaxRequestDiagLength) : obj);
  904. throw e;
  905. }
  906. }
  907. int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
  908. if (tickdiff > WebUtil.LongCallTime)
  909. {
  910. m_log.InfoFormat(
  911. "[FORMS]: Slow request {0} {1} {2} took {3}ms, {4}ms writing, {5}",
  912. reqnum,
  913. verb,
  914. requestUrl,
  915. tickdiff,
  916. tickset,
  917. tickdata,
  918. obj.Length > WebUtil.MaxRequestDiagLength ? obj.Remove(WebUtil.MaxRequestDiagLength) : obj);
  919. }
  920. else if (WebUtil.DebugLevel >= 4)
  921. {
  922. m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} took {1}ms, {2}ms writing",
  923. reqnum, tickdiff, tickdata);
  924. }
  925. if (WebUtil.DebugLevel >= 5)
  926. WebUtil.LogResponseDetail(reqnum, respstring);
  927. return respstring;
  928. }
  929. public static string MakeRequest(string verb, string requestUrl, string obj, IServiceAuth auth)
  930. {
  931. return MakeRequest(verb, requestUrl, obj, -1, auth);
  932. }
  933. }
  934. public class SynchronousRestObjectRequester
  935. {
  936. private static readonly ILog m_log =
  937. LogManager.GetLogger(
  938. MethodBase.GetCurrentMethod().DeclaringType);
  939. /// <summary>
  940. /// Perform a synchronous REST request.
  941. /// </summary>
  942. /// <param name="verb"></param>
  943. /// <param name="requestUrl"></param>
  944. /// <param name="obj"></param>
  945. /// <returns>
  946. /// The response. If there was an internal exception, then the default(TResponse) is returned.
  947. /// </returns>
  948. public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj)
  949. {
  950. return MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, 0);
  951. }
  952. public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj, IServiceAuth auth)
  953. {
  954. return MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, 0, auth);
  955. }
  956. /// <summary>
  957. /// Perform a synchronous REST request.
  958. /// </summary>
  959. /// <param name="verb"></param>
  960. /// <param name="requestUrl"></param>
  961. /// <param name="obj"></param>
  962. /// <param name="pTimeout">
  963. /// Request timeout in milliseconds. Timeout.Infinite indicates no timeout. If 0 is passed then the default HttpWebRequest timeout is used (100 seconds)
  964. /// </param>
  965. /// <returns>
  966. /// The response. If there was an internal exception or the request timed out,
  967. /// then the default(TResponse) is returned.
  968. /// </returns>
  969. public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj, int pTimeout)
  970. {
  971. return MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, pTimeout, 0);
  972. }
  973. public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj, int pTimeout, IServiceAuth auth)
  974. {
  975. return MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, pTimeout, 0, auth);
  976. }
  977. /// Perform a synchronous REST request.
  978. /// </summary>
  979. /// <param name="verb"></param>
  980. /// <param name="requestUrl"></param>
  981. /// <param name="obj"></param>
  982. /// <param name="pTimeout">
  983. /// Request timeout in milliseconds. Timeout.Infinite indicates no timeout. If 0 is passed then the default HttpWebRequest timeout is used (100 seconds)
  984. /// </param>
  985. /// <param name="maxConnections"></param>
  986. /// <returns>
  987. /// The response. If there was an internal exception or the request timed out,
  988. /// then the default(TResponse) is returned.
  989. /// </returns>
  990. public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj, int pTimeout, int maxConnections)
  991. {
  992. return MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, pTimeout, maxConnections, null);
  993. }
  994. /// <summary>
  995. /// Perform a synchronous REST request.
  996. /// </summary>
  997. /// <param name="verb"></param>
  998. /// <param name="requestUrl"></param>
  999. /// <param name="obj"></param>
  1000. /// <param name="pTimeout">
  1001. /// Request timeout in milliseconds. Timeout.Infinite indicates no timeout. If 0 is passed then the default HttpWebRequest timeout is used (100 seconds)
  1002. /// </param>
  1003. /// <param name="maxConnections"></param>
  1004. /// <returns>
  1005. /// The response. If there was an internal exception or the request timed out,
  1006. /// then the default(TResponse) is returned.
  1007. /// </returns>
  1008. public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj, int pTimeout, int maxConnections, IServiceAuth auth)
  1009. {
  1010. int reqnum = WebUtil.RequestNumber++;
  1011. if (WebUtil.DebugLevel >= 3)
  1012. m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} SynchronousRestObject {1} to {2}",
  1013. reqnum, verb, requestUrl);
  1014. int tickstart = Util.EnvironmentTickCount();
  1015. int tickdata = 0;
  1016. Type type = typeof(TRequest);
  1017. TResponse deserial = default(TResponse);
  1018. WebRequest request = WebRequest.Create(requestUrl);
  1019. HttpWebRequest ht = (HttpWebRequest)request;
  1020. if (auth != null)
  1021. auth.AddAuthorization(ht.Headers);
  1022. if (pTimeout != 0)
  1023. request.Timeout = pTimeout;
  1024. if (maxConnections > 0 && ht.ServicePoint.ConnectionLimit < maxConnections)
  1025. ht.ServicePoint.ConnectionLimit = maxConnections;
  1026. request.Method = verb;
  1027. MemoryStream buffer = null;
  1028. try
  1029. {
  1030. if ((verb == "POST") || (verb == "PUT"))
  1031. {
  1032. request.ContentType = "text/xml";
  1033. buffer = new MemoryStream();
  1034. XmlWriterSettings settings = new XmlWriterSettings();
  1035. settings.Encoding = Encoding.UTF8;
  1036. using (XmlWriter writer = XmlWriter.Create(buffer, settings))
  1037. {
  1038. XmlSerializer serializer = new XmlSerializer(type);
  1039. serializer.Serialize(writer, obj);
  1040. writer.Flush();
  1041. }
  1042. int length = (int)buffer.Length;
  1043. request.ContentLength = length;
  1044. byte[] data = buffer.ToArray();
  1045. if (WebUtil.DebugLevel >= 5)
  1046. WebUtil.LogOutgoingDetail("SEND", reqnum, System.Text.Encoding.UTF8.GetString(data));
  1047. try
  1048. {
  1049. using (Stream requestStream = request.GetRequestStream())
  1050. requestStream.Write(data, 0, length);
  1051. }
  1052. catch (Exception e)
  1053. {
  1054. m_log.DebugFormat(
  1055. "[SynchronousRestObjectRequester]: Exception in making request {0} {1}: {2}{3}",
  1056. verb, requestUrl, e.Message, e.StackTrace);
  1057. return deserial;
  1058. }
  1059. finally
  1060. {
  1061. // capture how much time was spent writing
  1062. tickdata = Util.EnvironmentTickCountSubtract(tickstart);
  1063. }
  1064. }
  1065. try
  1066. {
  1067. using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
  1068. {
  1069. if (resp.ContentLength != 0)
  1070. {
  1071. using (Stream respStream = resp.GetResponseStream())
  1072. {
  1073. deserial = XMLResponseHelper.LogAndDeserialize<TRequest, TResponse>(
  1074. reqnum, respStream, resp.ContentLength);
  1075. }
  1076. }
  1077. else
  1078. {
  1079. m_log.DebugFormat(
  1080. "[SynchronousRestObjectRequester]: Oops! no content found in response stream from {0} {1}",
  1081. verb, requestUrl);
  1082. }
  1083. }
  1084. }
  1085. catch (WebException e)
  1086. {
  1087. using (HttpWebResponse hwr = (HttpWebResponse)e.Response)
  1088. {
  1089. if (hwr != null)
  1090. {
  1091. if (hwr.StatusCode == HttpStatusCode.NotFound)
  1092. return deserial;
  1093. if (hwr.StatusCode == HttpStatusCode.Unauthorized)
  1094. {
  1095. m_log.ErrorFormat("[SynchronousRestObjectRequester]: Web request {0} requires authentication",
  1096. requestUrl);
  1097. }
  1098. else
  1099. {
  1100. m_log.WarnFormat("[SynchronousRestObjectRequester]: Web request {0} returned error: {1}",
  1101. requestUrl, hwr.StatusCode);
  1102. }
  1103. }
  1104. else
  1105. m_log.ErrorFormat(
  1106. "[SynchronousRestObjectRequester]: WebException for {0} {1} {2} {3}",
  1107. verb, requestUrl, typeof(TResponse).ToString(), e.Message);
  1108. return deserial;
  1109. }
  1110. }
  1111. catch (System.InvalidOperationException)
  1112. {
  1113. // This is what happens when there is invalid XML
  1114. m_log.DebugFormat(
  1115. "[SynchronousRestObjectRequester]: Invalid XML from {0} {1} {2}",
  1116. verb, requestUrl, typeof(TResponse).ToString());
  1117. }
  1118. catch (Exception e)
  1119. {
  1120. m_log.Debug(string.Format(
  1121. "[SynchronousRestObjectRequester]: Exception on response from {0} {1} ",
  1122. verb, requestUrl), e);
  1123. }
  1124. int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
  1125. if (tickdiff > WebUtil.LongCallTime)
  1126. {
  1127. string originalRequest = null;
  1128. if (buffer != null)
  1129. {
  1130. originalRequest = Encoding.UTF8.GetString(buffer.ToArray());
  1131. if (originalRequest.Length > WebUtil.MaxRequestDiagLength)
  1132. originalRequest = originalRequest.Remove(WebUtil.MaxRequestDiagLength);
  1133. }
  1134. m_log.InfoFormat(
  1135. "[LOGHTTP]: Slow SynchronousRestObject request {0} {1} to {2} took {3}ms, {4}ms writing, {5}",
  1136. reqnum, verb, requestUrl, tickdiff, tickdata,
  1137. originalRequest);
  1138. }
  1139. else if (WebUtil.DebugLevel >= 4)
  1140. {
  1141. m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} took {1}ms, {2}ms writing",
  1142. reqnum, tickdiff, tickdata);
  1143. }
  1144. }
  1145. finally
  1146. {
  1147. if (buffer != null)
  1148. buffer.Dispose();
  1149. }
  1150. return deserial;
  1151. }
  1152. public static class XMLResponseHelper
  1153. {
  1154. public static TResponse LogAndDeserialize<TRequest, TResponse>(int reqnum, Stream respStream, long contentLength)
  1155. {
  1156. XmlSerializer deserializer = new XmlSerializer(typeof(TResponse));
  1157. if (WebUtil.DebugLevel >= 5)
  1158. {
  1159. byte[] data = new byte[contentLength];
  1160. Util.ReadStream(respStream, data);
  1161. WebUtil.LogResponseDetail(reqnum, System.Text.Encoding.UTF8.GetString(data));
  1162. using (MemoryStream temp = new MemoryStream(data))
  1163. return (TResponse)deserializer.Deserialize(temp);
  1164. }
  1165. else
  1166. {
  1167. return (TResponse)deserializer.Deserialize(respStream);
  1168. }
  1169. }
  1170. }
  1171. }
  1172. public static class XMLRPCRequester
  1173. {
  1174. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  1175. public static Hashtable SendRequest(Hashtable ReqParams, string method, string url)
  1176. {
  1177. int reqnum = WebUtil.RequestNumber++;
  1178. if (WebUtil.DebugLevel >= 3)
  1179. m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} XML-RPC '{1}' to {2}",
  1180. reqnum, method, url);
  1181. int tickstart = Util.EnvironmentTickCount();
  1182. string responseStr = null;
  1183. try
  1184. {
  1185. ArrayList SendParams = new ArrayList();
  1186. SendParams.Add(ReqParams);
  1187. XmlRpcRequest Req = new XmlRpcRequest(method, SendParams);
  1188. if (WebUtil.DebugLevel >= 5)
  1189. {
  1190. string str = Req.ToString();
  1191. str = XElement.Parse(str).ToString(SaveOptions.DisableFormatting);
  1192. WebUtil.LogOutgoingDetail("SEND", reqnum, str);
  1193. }
  1194. XmlRpcResponse Resp = Req.Send(url, 30000);
  1195. try
  1196. {
  1197. responseStr = Resp.ToString();
  1198. responseStr = XElement.Parse(responseStr).ToString(SaveOptions.DisableFormatting);
  1199. if (WebUtil.DebugLevel >= 5)
  1200. WebUtil.LogResponseDetail(reqnum, responseStr);
  1201. }
  1202. catch (Exception e)
  1203. {
  1204. m_log.Error("Error parsing XML-RPC response", e);
  1205. }
  1206. if (Resp.IsFault)
  1207. {
  1208. m_log.DebugFormat(
  1209. "[LOGHTTP]: XML-RPC request {0} '{1}' to {2} FAILED: FaultCode={3}, FaultMessage={4}",
  1210. reqnum, method, url, Resp.FaultCode, Resp.FaultString);
  1211. return null;
  1212. }
  1213. Hashtable RespData = (Hashtable)Resp.Value;
  1214. return RespData;
  1215. }
  1216. finally
  1217. {
  1218. int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
  1219. if (tickdiff > WebUtil.LongCallTime)
  1220. {
  1221. m_log.InfoFormat(
  1222. "[LOGHTTP]: Slow XML-RPC request {0} '{1}' to {2} took {3}ms, {4}",
  1223. reqnum, method, url, tickdiff,
  1224. responseStr != null
  1225. ? (responseStr.Length > WebUtil.MaxRequestDiagLength ? responseStr.Remove(WebUtil.MaxRequestDiagLength) : responseStr)
  1226. : "");
  1227. }
  1228. else if (WebUtil.DebugLevel >= 4)
  1229. {
  1230. m_log.DebugFormat("[LOGHTTP]: HTTP OUT {0} took {1}ms", reqnum, tickdiff);
  1231. }
  1232. }
  1233. }
  1234. }
  1235. }