/// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"></see> interface.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"></see> 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)
        {
            string etag     = context.Request.Headers["If-None-Match"];
            string modSince = context.Request.Headers["If-Modified-Since"];

            if (context.Cache[Constant.AjaxID + ".converter"] != null)
            {
                CacheInfo ci = (CacheInfo)context.Cache[Constant.AjaxID + ".converter"];

                if (etag != null)
                {
                    if (etag == ci.ETag)                                // TODO: null check
                    {
                        context.Response.StatusCode = 304;
                        return;
                    }
                }

                if (modSince != null)
                {
                    if (modSince.IndexOf(";") > 0)
                    {
                        // If-Modified-Since: Tue, 06 Jun 2006 10:13:38 GMT; length=2935
                        modSince = modSince.Split(';')[0];
                    }

                    try
                    {
                        DateTime modSinced = Convert.ToDateTime(modSince.ToString()).ToUniversalTime();
                        if (DateTime.Compare(modSinced, ci.LastModified.ToUniversalTime()) >= 0)
                        {
                            context.Response.StatusCode = 304;
                            return;
                        }
                    }
                    catch (FormatException)
                    {
                        if (context.Trace.IsEnabled)
                        {
                            context.Trace.Write(Constant.AjaxID, "The header value for If-Modified-Since = " + modSince + " could not be converted to a System.DateTime.");
                        }
                    }
                }
            }

            etag = Hash5Helper.GetHash(System.Text.Encoding.Default.GetBytes("converter"));

            DateTime now     = DateTime.Now;
            DateTime lastMod = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);             //.ToUniversalTime();

            context.Response.AddHeader("Content-Type", "application/x-javascript");
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
            context.Response.Cache.SetETag(etag);
            context.Response.Cache.SetLastModified(lastMod);

            context.Response.Write(@"//--------------------------------------------------------------
// Copyright (C) 2021 Michael Schwarz (http://www.ajaxpro.info).
// All rights reserved.
//--------------------------------------------------------------
// Converter.js

");

            if (Utility.Settings != null && Utility.Settings.Security != null)
            {
                try
                {
                    string secrityScript = Utility.Settings.Security.SecurityProvider.ClientScript;

                    if (secrityScript != null && secrityScript.Length > 0)
                    {
                        context.Response.Write(secrityScript);
                        context.Response.Write("\r\n");
                    }
                }
                catch (Exception)
                {
                }
            }

            StringCollection     convTypes = new StringCollection();
            string               convTypeName;
            IJavaScriptConverter c;
            string               script;

            IEnumerator s = Utility.Settings.SerializableConverters.Values.GetEnumerator();

            while (s.MoveNext())
            {
                c            = (IJavaScriptConverter)s.Current;
                convTypeName = c.GetType().FullName;
                if (!convTypes.Contains(convTypeName))
                {
                    script = c.GetClientScript();

                    if (script.Length > 0)
                    {
#if (NET20)
                        if (!String.IsNullOrEmpty(c.ConverterName))
#else
                        if (c.ConverterName != null && c.ConverterName.Length > 0)
#endif
                        { context.Response.Write("// " + c.ConverterName + "\r\n"); }
                        context.Response.Write(c.GetClientScript());
                        context.Response.Write("\r\n");
                    }

                    convTypes.Add(convTypeName);
                }
            }

            IEnumerator d = Utility.Settings.DeserializableConverters.Values.GetEnumerator();
            while (d.MoveNext())
            {
                c            = (IJavaScriptConverter)d.Current;
                convTypeName = c.GetType().FullName;
                if (!convTypes.Contains(convTypeName))
                {
                    script = c.GetClientScript();

                    if (script.Length > 0)
                    {
#if (NET20)
                        if (!String.IsNullOrEmpty(c.ConverterName))
#else
                        if (c.ConverterName != null && c.ConverterName.Length > 0)
#endif

                        { context.Response.Write("// " + c.ConverterName + "\r\n"); }
                        context.Response.Write(c.GetClientScript());
                        context.Response.Write("\r\n");
                    }

                    convTypes.Add(convTypeName);
                }
            }

            context.Cache.Add(Constant.AjaxID + ".converter", new CacheInfo(etag, lastMod), null,
                              System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration,
                              System.Web.Caching.CacheItemPriority.Normal, null);
        }
Beispiel #2
0
        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"></see> interface.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"></see> 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)
        {
            string etag     = context.Request.Headers["If-None-Match"];
            string modSince = context.Request.Headers["If-Modified-Since"];

            if (context.Cache[Constant.AjaxID + "." + fileName] != null)
            {
                CacheInfo ci = (CacheInfo)context.Cache[Constant.AjaxID + "." + fileName];

                if (etag != null)
                {
                    if (etag == ci.ETag)                                // TODO: null check
                    {
                        context.Response.StatusCode = 304;
                        return;
                    }
                }

                if (modSince != null)
                {
                    if (modSince.IndexOf(";") > 0)
                    {
                        // If-Modified-Since: Tue, 06 Jun 2006 10:13:38 GMT; length=2935
                        modSince = modSince.Split(';')[0];
                    }

                    try
                    {
                        DateTime modSinced = Convert.ToDateTime(modSince.ToString()).ToUniversalTime();
                        if (DateTime.Compare(modSinced, ci.LastModified.ToUniversalTime()) >= 0)
                        {
                            context.Response.StatusCode = 304;
                            return;
                        }
                    }
                    catch (FormatException)
                    {
                        if (context.Trace.IsEnabled)
                        {
                            context.Trace.Write(Constant.AjaxID, "The header value for If-Modified-Since = " + modSince + " could not be converted to a System.DateTime.");
                        }
                    }
                }
            }

            etag = MD5Helper.GetHash(System.Text.Encoding.Default.GetBytes(fileName));

            DateTime now     = DateTime.Now;
            DateTime lastMod = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);             //.ToUniversalTime();

            context.Response.AddHeader("Content-Type", "application/x-javascript");
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
            context.Response.Cache.SetETag(etag);
            context.Response.Cache.SetLastModified(lastMod);

            context.Response.Write(@"//--------------------------------------------------------------
// Copyright (C) 2006 Michael Schwarz (http://www.ajaxpro.info).
// All rights reserved.
//--------------------------------------------------------------

");

            // Now, we want to read the JavaScript embedded source
            // from the assembly. If the filename includes any comma
            // we have to return more than one embedded JavaScript file.

            if (fileName != null && fileName.Length > 0)
            {
                string[] files    = fileName.Split(',');
                Assembly assembly = Assembly.GetExecutingAssembly();
                Stream   s;

                for (int i = 0; i < files.Length; i++)
                {
                    s = assembly.GetManifestResourceStream(Constant.AssemblyName + "." + files[i] + ".js");

                    if (s != null)
                    {
                        System.IO.StreamReader sr = new System.IO.StreamReader(s);

                        context.Response.Write("// " + files[i] + ".js\r\n");

                        context.Response.Write(sr.ReadToEnd());
                        context.Response.Write("\r\n");

                        sr.Close();

                        if (files[i] == "prototype" && AjaxPro.Utility.Settings.OldStyle.Contains("objectExtendPrototype"))
                        {
                            context.Response.Write(@"
Object.prototype.extend = function(o, override) {
	return Object.extend.apply(this, [this, o, override != false]);
}
");
                        }
                    }
                }
            }

            context.Cache.Add(Constant.AjaxID + "." + fileName, new CacheInfo(etag, lastMod), null,
                              System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration,
                              System.Web.Caching.CacheItemPriority.Normal, null);
        }
        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"></see> interface.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"></see> 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)
        {
            // The request was not a request to invoke a server-side method.
            // Now, we will render the Javascript that will be used on the
            // client to run as a proxy or wrapper for the methods marked
            // with the AjaxMethodAttribute.

            if (context.Trace.IsEnabled)
            {
                context.Trace.Write(Constant.AjaxID, "Render class proxy Javascript");
            }


            // Check wether the javascript is already rendered and cached in the
            // current context.

            string etag     = context.Request.Headers["If-None-Match"];
            string modSince = context.Request.Headers["If-Modified-Since"];

            string path = type.FullName + "," + type.Assembly.FullName.Split(',')[0];

            if (Utility.Settings.UseAssemblyQualifiedName)
            {
                path = type.AssemblyQualifiedName;
            }

            if (Utility.Settings != null && Utility.Settings.UrlNamespaceMappings.ContainsValue(path))
            {
                foreach (string key in Utility.Settings.UrlNamespaceMappings.Keys)
                {
                    if (Utility.Settings.UrlNamespaceMappings[key].ToString() == path)
                    {
                        path = key;
                        break;
                    }
                }
            }

            if (context.Cache[path] != null)
            {
                CacheInfo ci = (CacheInfo)context.Cache[path];

                if (etag != null)
                {
                    if (etag == ci.ETag)                                // TODO: null check
                    {
                        context.Response.StatusCode      = 304;
                        context.Response.SuppressContent = true;
                        return;
                    }
                }

                if (modSince != null)
                {
                    if (modSince.IndexOf(";") > 0)
                    {
                        // If-Modified-Since: Tue, 06 Jun 2006 10:13:38 GMT; length=2935
                        modSince = modSince.Split(';')[0];
                    }

                    try
                    {
                        DateTime modSinced = Convert.ToDateTime(modSince.ToString()).ToUniversalTime();
                        if (DateTime.Compare(modSinced, ci.LastModified.ToUniversalTime()) >= 0)
                        {
                            context.Response.StatusCode      = 304;
                            context.Response.SuppressContent = true;
                            return;
                        }
                    }
                    catch (FormatException)
                    {
                        if (context.Trace.IsEnabled)
                        {
                            context.Trace.Write(Constant.AjaxID, "The header value for If-Modified-Since = " + modSince + " could not be converted to a System.DateTime.");
                        }
                    }
                }
            }

            etag = type.AssemblyQualifiedName;
            etag = MD5Helper.GetHash(System.Text.Encoding.Default.GetBytes(etag));

            DateTime now     = DateTime.Now;
            DateTime lastMod = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);             // .ToUniversalTime();

            context.Response.AddHeader("Content-Type", "application/x-javascript");
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
            context.Response.Cache.SetETag(etag);
            context.Response.Cache.SetLastModified(lastMod);

            // Ok, we do not have the javascript rendered, yet.
            // Build the javascript source and save it to the current
            // Application context.


            string url = context.Request.ApplicationPath + (context.Request.ApplicationPath.EndsWith("/") ? "" : "/") + Utility.HandlerPath + "/" + AjaxPro.Utility.GetSessionUri() + path + Utility.HandlerExtension;



            // find all methods that are able to be used with AjaxPro

            MethodInfo[] mi = type.GetMethods();
            MethodInfo   method;

#if (NET20)
            List <MethodInfo> methods = new List <MethodInfo>();
#else
            MethodInfo[] methods = new MethodInfo[mi.Length];                           // maximum length
            int          mc      = 0;
#endif

            for (int y = 0; y < mi.Length; y++)
            {
                method = mi[y];

                if (!method.IsPublic)
                {
                    continue;
                }

                AjaxMethodAttribute[] ma = (AjaxMethodAttribute[])method.GetCustomAttributes(typeof(AjaxMethodAttribute), true);

                if (ma.Length == 0)
                {
                    continue;
                }

                PrincipalPermissionAttribute[] ppa = (PrincipalPermissionAttribute[])method.GetCustomAttributes(typeof(PrincipalPermissionAttribute), true);
                if (ppa.Length > 0)
                {
                    bool permissionDenied = true;
                    for (int p = 0; p < ppa.Length && permissionDenied; p++)
                    {
#if (_____NET20)
                        if (Roles.Enabled)
                        {
                            try
                            {
                                if (!String.IsNullOrEmpty(ppa[p].Role) && !Roles.IsUserInRole(ppa[p].Role))
                                {
                                    continue;
                                }
                            }
                            catch (Exception)
                            {
                                // Should we disable this AjaxMethod of there is an exception?
                                continue;
                            }
                        }
                        else
#endif
                        if (ppa[p].Role != null && ppa[p].Role.Length > 0 && context.User != null && context.User.Identity.IsAuthenticated && !context.User.IsInRole(ppa[p].Role))
                        {
                            continue;
                        }

                        permissionDenied = false;
                    }

                    if (permissionDenied)
                    {
                        continue;
                    }
                }

#if (NET20)
                methods.Add(method);
#else
                methods[mc++] = method;
#endif
            }



            // render client-side proxy file

            System.Text.StringBuilder sb  = new System.Text.StringBuilder();
            TypeJavaScriptProvider    jsp = null;

            if (Utility.Settings.TypeJavaScriptProvider != null)
            {
                try
                {
                    Type jspt = Type.GetType(Utility.Settings.TypeJavaScriptProvider);
                    if (jspt != null && typeof(TypeJavaScriptProvider).IsAssignableFrom(jspt))
                    {
                        jsp = (TypeJavaScriptProvider)Activator.CreateInstance(jspt, new object[3] {
                            type, url, sb
                        });
                    }
                }
                catch (Exception)
                {
                }
            }

            if (jsp == null)
            {
                jsp = new TypeJavaScriptProvider(type, url, sb);
            }

            jsp.RenderNamespace();
            jsp.RenderClassBegin();
#if (NET20)
            jsp.RenderMethods(methods.ToArray());
#else
            jsp.RenderMethods(methods);
#endif
            jsp.RenderClassEnd();

            context.Response.Write(sb.ToString());
            context.Response.Write("\r\n");


            // save the javascript in current Application context to
            // speed up the next requests.

            // TODO: was können wir hier machen??
            // System.Web.Caching.CacheDependency fileDepend = new System.Web.Caching.CacheDependency(type.Assembly.Location);

            context.Cache.Add(path, new CacheInfo(etag, lastMod), null,
                              System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration,
                              System.Web.Caching.CacheItemPriority.Normal, null);

            if (context.Trace.IsEnabled)
            {
                context.Trace.Write(Constant.AjaxID, "End ProcessRequest");
            }
        }