public void ProcessRequest(HttpContext context)
        {
            //write your handler implementation here.
            try
            {
                context.Response.Clear();
                context.Response.ClearHeaders();

                string result = "";

                if (context.Request.Url.AbsoluteUri.Length > 1024)
                {
                    err = new error();
                    err.statusCode = System.Net.HttpStatusCode.RequestUriTooLong;
                    err.parameter = "unknown";
                    err.text = "Request > 1024 characters, too long";
                }
                else
                    result = GetWidthAndHeight(context.Request["identifier"], context.Request["return"]);

                if (err != null)
                {
                    context.Response.TrySkipIisCustomErrors = true;
                    context.Response.ContentType = "application/xml";
                    context.Response.AddHeader("content-disposition", "inline;");
                    context.Response.StatusCode = (int)err.statusCode;
                    context.Response.Charset = "";
                }
                else if (context.Request["return"].Equals("json"))
                {
                    context.Response.ContentType = "application/json";
                    context.Response.AddHeader("content-disposition", "inline; filename=export.json");
                }
                else
                {
                    context.Response.ContentType = "application/xml";
                    context.Response.AddHeader("content-disposition", "inline; filename=export.xml");
                }

                context.Response.AddHeader("Link", "<http://library.stanford.edu/iiif/image-api/compliance.html#level2>;rel=\"profile\"");
                context.Response.AddHeader("content-length", (result.Length).ToString());

                context.Response.Flush();
                context.Response.Write(result);

                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }
            catch (Exception e)
            {
                context.Response.Write(e.Message);
            }
            finally
            {
                context.Response.End();
            }
        }
        private string GetWidthAndHeight(string p_identifier, string p_returnType)
        {
            try
            {
                Extractor kdu = new Extractor();

                string xmlMetadata = kdu.GetMetadata(p_identifier.EndsWith(".jp2") ? p_identifier : p_identifier + ".jp2");

                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xmlMetadata);

                Metadata data = new Metadata();
                data.profile = "http://library.stanford.edu/iiif/image-api/compliance.html#level2";
                data.identifier = p_identifier;
                data.width = int.Parse(doc.GetElementsByTagName("Width")[0].InnerText);
                data.height = int.Parse(doc.GetElementsByTagName("Height")[0].InnerText);

                scale_factors scaleFactors = new scale_factors();
                for (int i = 0; i < int.Parse(doc.GetElementsByTagName("Jp2levels")[0].InnerText); i++)
                {
                    scaleFactors.Add(Convert.ToInt32(Math.Pow(2, i)));
                }
                data.scale_factors = scaleFactors;

                formats formats = new formats();
                formats.Add("jpg");
                formats.Add("png");
                formats.Add("gif");

                data.formats = formats;

                qualities qualities = new qualities();
                qualities.Add("native");
                qualities.Add("grey");
                qualities.Add("bitonal");

                data.qualities = qualities;

                data.tile_width = int.Parse(doc.GetElementsByTagName("TileWidth")[0].InnerText);
                data.tile_height = int.Parse(doc.GetElementsByTagName("TileHeight")[0].InnerText);

                if (p_returnType.Equals("json"))
                {
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    return serializer.Serialize(data);
                }
                else
                {
                    return getSerializedXML(data);
                }
            }
            catch (Exception e)
            {
                err = new error();

                try
                {
                    err.parameter = "identifier";

                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(e.Message);

                    err.text = doc.GetElementsByTagName("Message")[0].InnerText;
                    err.statusCode = System.Net.HttpStatusCode.NotFound;
                }
                catch
                {
                    err.statusCode = System.Net.HttpStatusCode.BadRequest;
                    err.parameter = "unknown";
                    err.text = e.Message;
                }
                return getSerializedXML(err);
            }
        }