SvgHandler.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Disabled cause we needn't System.Web reference
  2. //using System;
  3. //using System.Collections;
  4. //using System.Collections.Generic;
  5. //using System.Collections.Specialized;
  6. //using System.Drawing;
  7. //using System.Drawing.Imaging;
  8. //using System.IO;
  9. //using System.Text;
  10. //using System.Threading;
  11. //using System.Web;
  12. //namespace Svg.Web
  13. //{
  14. // /// <summary>
  15. // /// A handler to asynchronously render Scalable Vector Graphics files (usually *.svg or *.xml file extensions).
  16. // /// </summary>
  17. // /// <remarks>
  18. // /// <para>If a crawler requests the SVG file the raw XML will be returned rather than the image to allow crawlers to better read the image.</para>
  19. // /// <para>Adding "?raw=true" to the querystring will alos force the handler to render the raw SVG.</para>
  20. // /// </remarks>
  21. // public class SvgHandler : IHttpAsyncHandler
  22. // {
  23. // Thread t;
  24. // /// <summary>
  25. // /// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler"/> instance.
  26. // /// </summary>
  27. // /// <value></value>
  28. // /// <returns>true if the <see cref="T:System.Web.IHttpHandler"/> instance is reusable; otherwise, false.</returns>
  29. // public bool IsReusable
  30. // {
  31. // get { return false; }
  32. // }
  33. // /// <summary>
  34. // /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface.
  35. // /// </summary>
  36. // /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
  37. // public void ProcessRequest(HttpContext context)
  38. // {
  39. // // Not used
  40. // }
  41. // /// <summary>
  42. // /// Initiates an asynchronous call to the HTTP handler.
  43. // /// </summary>
  44. // /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
  45. // /// <param name="cb">The <see cref="T:System.AsyncCallback"/> to call when the asynchronous method call is complete. If <paramref name="cb"/> is null, the delegate is not called.</param>
  46. // /// <param name="extraData">Any extra data needed to process the request.</param>
  47. // /// <returns>
  48. // /// An <see cref="T:System.IAsyncResult"/> that contains information about the status of the process.
  49. // /// </returns>
  50. // public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
  51. // {
  52. // string path = context.Request.PhysicalPath;
  53. // if (!File.Exists(path))
  54. // {
  55. // throw new HttpException(404, "The requested file cannot be found.");
  56. // }
  57. // SvgAsyncRenderState reqState = new SvgAsyncRenderState(context, cb, extraData);
  58. // SvgAsyncRender asyncRender = new SvgAsyncRender(reqState);
  59. // ThreadStart ts = new ThreadStart(asyncRender.RenderSvg);
  60. // t = new Thread(ts);
  61. // t.Start();
  62. // return reqState;
  63. // }
  64. // /// <summary>
  65. // /// Provides an asynchronous process End method when the process ends.
  66. // /// </summary>
  67. // /// <param name="result">An <see cref="T:System.IAsyncResult"/> that contains information about the status of the process.</param>
  68. // public void EndProcessRequest(IAsyncResult result)
  69. // {
  70. // }
  71. // /// <summary>
  72. // /// The class to be used when
  73. // /// </summary>
  74. // protected sealed class SvgAsyncRender
  75. // {
  76. // private SvgAsyncRenderState _state;
  77. // public SvgAsyncRender(SvgAsyncRenderState state)
  78. // {
  79. // this._state = state;
  80. // }
  81. // private void RenderRawSvg()
  82. // {
  83. // this._state._context.Response.ContentType = "image/svg+xml";
  84. // this._state._context.Response.WriteFile(this._state._context.Request.PhysicalPath);
  85. // this._state._context.Response.End();
  86. // this._state.CompleteRequest();
  87. // }
  88. // public void RenderSvg()
  89. // {
  90. // this._state._context.Response.AddFileDependency(this._state._context.Request.PhysicalPath);
  91. // this._state._context.Response.Cache.SetLastModifiedFromFileDependencies();
  92. // this._state._context.Response.Cache.SetETagFromFileDependencies();
  93. // this._state._context.Response.Buffer = false;
  94. // // Allow crawlers to see the raw XML - they can get more information from it that way
  95. // if (this._state._context.Request.Browser.Crawler || !string.IsNullOrEmpty(this._state._context.Request.QueryString["raw"]))
  96. // {
  97. // this.RenderRawSvg();
  98. // }
  99. // else
  100. // {
  101. // try
  102. // {
  103. // Dictionary<string, string> entities = new Dictionary<string, string>();
  104. // NameValueCollection queryString = this._state._context.Request.QueryString;
  105. // for (int i = 0; i < queryString.Count; i++)
  106. // {
  107. // entities.Add(queryString.Keys[i], queryString[i]);
  108. // }
  109. // SvgDocument document = SvgDocument.Open<SvgDocument>(this._state._context.Request.PhysicalPath, entities);
  110. // using (Bitmap bitmap = document.Draw())
  111. // {
  112. // using (MemoryStream ms = new MemoryStream())
  113. // {
  114. // bitmap.Save(ms, ImageFormat.Png);
  115. // this._state._context.Response.ContentType = "image/png";
  116. // ms.WriteTo(this._state._context.Response.OutputStream);
  117. // }
  118. // }
  119. // }
  120. // catch (Exception exc)
  121. // {
  122. // System.Diagnostics.Trace.TraceError("An error occured while attempting to render the SVG image '" + this._state._context.Request.PhysicalPath + "': " + exc.Message);
  123. // }
  124. // finally
  125. // {
  126. // this._state._context.Response.End();
  127. // this._state.CompleteRequest();
  128. // }
  129. // }
  130. // }
  131. // }
  132. // /// <summary>
  133. // /// Represents the state of a request for SVG rendering.
  134. // /// </summary>
  135. // protected sealed class SvgAsyncRenderState : IAsyncResult
  136. // {
  137. // internal HttpContext _context;
  138. // internal AsyncCallback _callback;
  139. // internal object _extraData;
  140. // private bool _isCompleted = false;
  141. // private ManualResetEvent _callCompleteEvent = null;
  142. // /// <summary>
  143. // /// Initializes a new instance of the <see cref="SvgAsyncRenderState"/> class.
  144. // /// </summary>
  145. // /// <param name="context">The <see cref="HttpContext"/> of the request.</param>
  146. // /// <param name="callback">The delegate to be called when the rendering is complete.</param>
  147. // /// <param name="extraData">The extra data.</param>
  148. // public SvgAsyncRenderState(HttpContext context, AsyncCallback callback, object extraData)
  149. // {
  150. // _context = context;
  151. // _callback = callback;
  152. // _extraData = extraData;
  153. // }
  154. // /// <summary>
  155. // /// Indicates that the rendering is complete and the waiting thread may proceed.
  156. // /// </summary>
  157. // internal void CompleteRequest()
  158. // {
  159. // _isCompleted = true;
  160. // lock (this)
  161. // {
  162. // if (this.AsyncWaitHandle != null)
  163. // {
  164. // this._callCompleteEvent.Set();
  165. // }
  166. // }
  167. // // if a callback was registered, invoke it now
  168. // if (_callback != null)
  169. // {
  170. // _callback(this);
  171. // }
  172. // }
  173. // /// <summary>
  174. // /// Gets a user-defined object that qualifies or contains information about an asynchronous operation.
  175. // /// </summary>
  176. // /// <value></value>
  177. // /// <returns>A user-defined object that qualifies or contains information about an asynchronous operation.</returns>
  178. // public object AsyncState { get { return (_extraData); } }
  179. // /// <summary>
  180. // /// Gets an indication of whether the asynchronous operation completed synchronously.
  181. // /// </summary>
  182. // /// <value></value>
  183. // /// <returns>true if the asynchronous operation completed synchronously; otherwise, false.</returns>
  184. // public bool CompletedSynchronously { get { return (false); } }
  185. // /// <summary>
  186. // /// Gets an indication whether the asynchronous operation has completed.
  187. // /// </summary>
  188. // /// <value></value>
  189. // /// <returns>true if the operation is complete; otherwise, false.</returns>
  190. // public bool IsCompleted { get { return (_isCompleted); } }
  191. // /// <summary>
  192. // /// Gets a <see cref="T:System.Threading.WaitHandle"/> that is used to wait for an asynchronous operation to complete.
  193. // /// </summary>
  194. // /// <value></value>
  195. // /// <returns>A <see cref="T:System.Threading.WaitHandle"/> that is used to wait for an asynchronous operation to complete.</returns>
  196. // public WaitHandle AsyncWaitHandle
  197. // {
  198. // get
  199. // {
  200. // lock (this)
  201. // {
  202. // if (_callCompleteEvent == null)
  203. // {
  204. // _callCompleteEvent = new ManualResetEvent(false);
  205. // }
  206. // return _callCompleteEvent;
  207. // }
  208. // }
  209. // }
  210. // }
  211. // }
  212. //}