WebUtil.cs 59 KB

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