SvgHandler.cs 10.1 KB
Newer Older
davescriven's avatar
davescriven committed
1
using System;
2
3
4
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
davescriven's avatar
davescriven committed
5
6
7
8
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
9
using System.Threading;
davescriven's avatar
davescriven committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System.Web;

namespace Svg.Web
{
    /// <summary>
    /// A handler to asynchronously render Scalable Vector Graphics files (usually *.svg or *.xml file extensions).
    /// </summary>
    /// <remarks>
    /// <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>
    /// <para>Adding "?raw=true" to the querystring will alos force the handler to render the raw SVG.</para>
    /// </remarks>
    public class SvgHandler : IHttpAsyncHandler
    {
        Thread t;

        /// <summary>
        /// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler"/> instance.
        /// </summary>
        /// <value></value>
        /// <returns>true if the <see cref="T:System.Web.IHttpHandler"/> instance is reusable; otherwise, false.</returns>
        public bool IsReusable
        {
            get { return false; }
        }

        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface.
        /// </summary>
        /// <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>
        public void ProcessRequest(HttpContext context)
        {
            // Not used
        }

        /// <summary>
        /// Initiates an asynchronous call to the HTTP handler.
        /// </summary>
        /// <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>
        /// <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>
        /// <param name="extraData">Any extra data needed to process the request.</param>
        /// <returns>
        /// An <see cref="T:System.IAsyncResult"/> that contains information about the status of the process.
        /// </returns>
        public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
        {
            string path = context.Request.PhysicalPath;

            if (!File.Exists(path))
            {
                throw new HttpException(404, "The requested file cannot be found.");
            }

            SvgAsyncRenderState reqState = new SvgAsyncRenderState(context, cb, extraData);
            SvgAsyncRender asyncRender = new SvgAsyncRender(reqState);
            ThreadStart ts = new ThreadStart(asyncRender.RenderSvg);
            t = new Thread(ts);
            t.Start();

            return reqState;
        }

        /// <summary>
        /// Provides an asynchronous process End method when the process ends.
        /// </summary>
        /// <param name="result">An <see cref="T:System.IAsyncResult"/> that contains information about the status of the process.</param>
        public void EndProcessRequest(IAsyncResult result)
        {
            
        }

        /// <summary>
        /// The class to be used when 
        /// </summary>
        protected sealed class SvgAsyncRender
        {
            private SvgAsyncRenderState _state;

            public SvgAsyncRender(SvgAsyncRenderState state)
            {
                this._state = state;
            }

            private void RenderRawSvg()
            {
                this._state._context.Response.ContentType = "image/svg+xml";
                this._state._context.Response.WriteFile(this._state._context.Request.PhysicalPath);
                this._state._context.Response.End();
                this._state.CompleteRequest();
            }

            public void RenderSvg()
            {
                this._state._context.Response.AddFileDependency(this._state._context.Request.PhysicalPath);
                this._state._context.Response.Cache.SetLastModifiedFromFileDependencies();
                this._state._context.Response.Cache.SetETagFromFileDependencies();
105
                this._state._context.Response.Buffer = false;
davescriven's avatar
davescriven committed
106
107
108
109
110
111

                // Allow crawlers to see the raw XML - they can get more information from it that way
                if (this._state._context.Request.Browser.Crawler || !string.IsNullOrEmpty(this._state._context.Request.QueryString["raw"]))
                {
                    this.RenderRawSvg();
                }
112
                else
davescriven's avatar
davescriven committed
113
                {
114
                    try
davescriven's avatar
davescriven committed
115
                    {
116
117
118
119
120
121
122
123
124
                        Dictionary<string, string> entities = new Dictionary<string, string>();
                        NameValueCollection queryString = this._state._context.Request.QueryString;

                        for (int i = 0; i < queryString.Count; i++)
                        {
                            entities.Add(queryString.Keys[i], queryString[i]);
                        }

                        SvgDocument document = SvgDocument.Open(this._state._context.Request.PhysicalPath, entities);
125
126

                        using (Bitmap bitmap = document.Draw())
davescriven's avatar
davescriven committed
127
                        {
128
129
130
131
132
133
                            using (MemoryStream ms = new MemoryStream())
                            {
                                bitmap.Save(ms, ImageFormat.Png);
                                this._state._context.Response.ContentType = "image/png";
                                ms.WriteTo(this._state._context.Response.OutputStream);
                            }
davescriven's avatar
davescriven committed
134
135
                        }
                    }
136
137
138
139
140
141
142
143
144
                    catch (Exception exc)
                    {
                        System.Diagnostics.Trace.TraceError("An error occured while attempting to render the SVG image '" + this._state._context.Request.PhysicalPath + "': " + exc.Message);
                    }
                    finally
                    {
                        this._state._context.Response.End();
                        this._state.CompleteRequest();
                    }
davescriven's avatar
davescriven committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
                }
            }
        }

        /// <summary>
        /// Represents the state of a request for SVG rendering.
        /// </summary>
        protected sealed class SvgAsyncRenderState : IAsyncResult
        {
            internal HttpContext _context;
            internal AsyncCallback _callback;
            internal object _extraData;
            private bool _isCompleted = false;
            private ManualResetEvent _callCompleteEvent = null;

            /// <summary>
            /// Initializes a new instance of the <see cref="SvgAsyncRenderState"/> class.
            /// </summary>
            /// <param name="context">The <see cref="HttpContext"/> of the request.</param>
            /// <param name="callback">The delegate to be called when the rendering is complete.</param>
            /// <param name="extraData">The extra data.</param>
166
            public SvgAsyncRenderState(HttpContext context, AsyncCallback callback, object extraData)
davescriven's avatar
davescriven committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
            {
                _context = context;
                _callback = callback;
                _extraData = extraData;
            }

            /// <summary>
            /// Indicates that the rendering is complete and the waiting thread may proceed.
            /// </summary>
            internal void CompleteRequest()
            {
                _isCompleted = true;
                lock (this)
                {
181
                    if (this.AsyncWaitHandle != null)
davescriven's avatar
davescriven committed
182
                    {
183
                        this._callCompleteEvent.Set();
davescriven's avatar
davescriven committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
                    }
                }
                // if a callback was registered, invoke it now
                if (_callback != null)
                {
                    _callback(this);
                }
            }

            /// <summary>
            /// Gets a user-defined object that qualifies or contains information about an asynchronous operation.
            /// </summary>
            /// <value></value>
            /// <returns>A user-defined object that qualifies or contains information about an asynchronous operation.</returns>
            public object AsyncState { get { return (_extraData); } }
            /// <summary>
            /// Gets an indication of whether the asynchronous operation completed synchronously.
            /// </summary>
            /// <value></value>
            /// <returns>true if the asynchronous operation completed synchronously; otherwise, false.</returns>
            public bool CompletedSynchronously { get { return (false); } }
            /// <summary>
            /// Gets an indication whether the asynchronous operation has completed.
            /// </summary>
            /// <value></value>
            /// <returns>true if the operation is complete; otherwise, false.</returns>
            public bool IsCompleted { get { return (_isCompleted); } }
            /// <summary>
            /// Gets a <see cref="T:System.Threading.WaitHandle"/> that is used to wait for an asynchronous operation to complete.
            /// </summary>
            /// <value></value>
            /// <returns>A <see cref="T:System.Threading.WaitHandle"/> that is used to wait for an asynchronous operation to complete.</returns>
            public WaitHandle AsyncWaitHandle
            {
                get
                {
                    lock (this)
                    {
                        if (_callCompleteEvent == null)
                        {
                            _callCompleteEvent = new ManualResetEvent(false);
                        }

                        return _callCompleteEvent;
                    }
                }
            }
        }
    }
}