Exemplo n.º 1
0
        void SendEmbeddedResource(HttpContext context, out EmbeddedResource res, out Assembly assembly)
        {
            HttpRequest         request     = context.Request;
            NameValueCollection queryString = request.QueryString;

            // val is URL-encoded, which means every + has been replaced with ' ', we
            // need to revert that or the base64 conversion will fail.
            string d = queryString ["d"];

            if (!String.IsNullOrEmpty(d))
            {
                d = d.Replace(' ', '+');
            }

            AssemblyEmbeddedResources entry;

            res = DecryptAssemblyResource(d, out entry);
            WebResourceAttribute wra = res != null ? res.Attribute : null;

            if (wra == null)
            {
                throw new HttpException(404, "Resource not found");
            }

            if (entry.AssemblyName == "s")
            {
                assembly = currAsm;
            }
            else
            {
                assembly = Assembly.Load(entry.AssemblyName);
            }

            long atime;

            if (HasCacheControl(request, queryString, out atime))
            {
                if (atime == File.GetLastWriteTimeUtc(assembly.Location).Ticks)
                {
                    RespondWithNotModified(context);
                    return;
                }
            }

            DateTime modified;

            if (HasIfModifiedSince(request, out modified))
            {
                if (File.GetLastWriteTimeUtc(assembly.Location) <= modified)
                {
                    RespondWithNotModified(context);
                    return;
                }
            }

            HttpResponse response = context.Response;

            response.ContentType = wra.ContentType;

            DateTime utcnow = DateTime.UtcNow;

            response.Headers.Add("Last-Modified", utcnow.ToString("r"));
            response.ExpiresAbsolute = utcnow.AddYears(1);
            response.CacheControl    = "public";

            Stream s = assembly.GetManifestResourceStream(res.Name);

            if (s == null)
            {
                throw new HttpException(404, "Resource " + res.Name + " not found");
            }

            if (wra.PerformSubstitution)
            {
                using (StreamReader r = new StreamReader(s)) {
                    TextWriter w = response.Output;
                    new PerformSubstitutionHelper(assembly).PerformSubstitution(r, w);
                }
            }
            else if (response.OutputStream is HttpResponseStream)
            {
                UnmanagedMemoryStream st      = (UnmanagedMemoryStream)s;
                HttpResponseStream    hstream = (HttpResponseStream)response.OutputStream;
                unsafe {
                    hstream.WritePtr(new IntPtr(st.PositionPointer), (int)st.Length);
                }
            }
            else
            {
                byte [] buf    = new byte [1024];
                Stream  output = response.OutputStream;
                int     c;
                do
                {
                    c = s.Read(buf, 0, 1024);
                    output.Write(buf, 0, c);
                } while (c > 0);
            }
        }
Exemplo n.º 2
0
        void System.Web.IHttpHandler.ProcessRequest(HttpContext context)
#endif
        {
            HttpRequest  request  = context.Request;
            HttpResponse response = context.Response;
            string       resourceName;
            string       asmName;
            Assembly     assembly;

            DecryptAssemblyResource(request.QueryString ["d"], out asmName, out resourceName);
            if (resourceName == null)
            {
                throw new HttpException(404, "No resource name given");
            }

            if (asmName == null || asmName == "s")
            {
                assembly = currAsm;
            }
            else
            {
                assembly = Assembly.Load(asmName);
            }

            WebResourceAttribute wra = null;

            WebResourceAttribute [] attrs = (WebResourceAttribute [])assembly.GetCustomAttributes(typeof(WebResourceAttribute), false);
            for (int i = 0; i < attrs.Length; i++)
            {
                if (attrs [i].WebResource == resourceName)
                {
                    wra = attrs [i];
                    break;
                }
            }
#if SYSTEM_WEB_EXTENSIONS
            if (wra == null && resourceName.Length > 9 && resourceName.EndsWith(".debug.js", StringComparison.OrdinalIgnoreCase))
            {
                resourceName = String.Concat(resourceName.Substring(0, resourceName.Length - 9), ".js");
                for (int i = 0; i < attrs.Length; i++)
                {
                    if (attrs [i].WebResource == resourceName)
                    {
                        wra = attrs [i];
                        break;
                    }
                }
            }
#endif
            if (wra == null)
            {
                throw new HttpException(404, String.Concat("Resource ", resourceName, " not found"));
            }

            string req_cache = request.Headers ["Cache-Control"];
            if (req_cache == "max-age=0")
            {
                long atime;
#if NET_2_0
                if (Int64.TryParse(request.QueryString ["t"], out atime))
                {
#else
                atime = -1;
                try {
                    atime = Int64.Parse(request.QueryString ["t"]);
                }
                catch {}
                if (atime > -1)
                {
#endif
                    if (atime == File.GetLastWriteTimeUtc(assembly.Location).Ticks)
                    {
                        response.Clear();
                        response.StatusCode   = 304;
                        response.ContentType  = null;
                        response.CacheControl = "public";                         // easier to set it to public as MS than remove it
                        context.ApplicationInstance.CompleteRequest();
                        return;
                    }
                }
            }
            string modif_since = request.Headers ["If-Modified-Since"];
            if (modif_since != null && modif_since != "")
            {
                try {
                    DateTime modif;
#if NET_2_0
                    if (DateTime.TryParseExact(modif_since, "r", null, 0, out modif))
#else
                    modif = DateTime.MinValue;
                    try {
                        modif = DateTime.ParseExact(modif_since, "r", null, 0);
                    } catch { }
                    if (modif != DateTime.MinValue)
#endif
                    { if (File.GetLastWriteTimeUtc(assembly.Location) <= modif)
                      {
                          response.Clear();
                          response.StatusCode   = 304;
                          response.ContentType  = null;
                          response.CacheControl = "public";                               // easier to set it to public as MS than remove it
                          context.ApplicationInstance.CompleteRequest();
                          return;
                      }
                    }
                } catch {}
            }

            response.ContentType = wra.ContentType;

            DateTime utcnow = DateTime.UtcNow;
            response.Headers.Add("Last-Modified", utcnow.ToString("r"));
            response.ExpiresAbsolute = utcnow.AddYears(1);
            response.CacheControl    = "public";

            Stream s = assembly.GetManifestResourceStream(resourceName);
            if (s == null)
            {
                throw new HttpException(404, String.Concat("Resource ", resourceName, " not found"));
            }

            if (wra.PerformSubstitution)
            {
                using (StreamReader r = new StreamReader(s)) {
                    TextWriter w = response.Output;
                    new PerformSubstitutionHelper(assembly).PerformSubstitution(r, w);
                }
#if NET_2_0
            }
            else if (response.OutputStream is HttpResponseStream)
            {
                UnmanagedMemoryStream st      = (UnmanagedMemoryStream)s;
                HttpResponseStream    hstream = (HttpResponseStream)response.OutputStream;
                unsafe {
                    hstream.WritePtr(new IntPtr(st.PositionPointer), (int)st.Length);
                }
#endif
            }
            else
            {
                byte [] buf    = new byte [1024];
                Stream  output = response.OutputStream;
                int     c;
                do
                {
                    c = s.Read(buf, 0, 1024);
                    output.Write(buf, 0, c);
                } while (c > 0);
            }
#if SYSTEM_WEB_EXTENSIONS
            TextWriter writer = response.Output;
            foreach (ScriptResourceAttribute sra in assembly.GetCustomAttributes(typeof(ScriptResourceAttribute), false))
            {
                if (sra.ScriptName == resourceName)
                {
                    string      scriptResourceName = sra.ScriptResourceName;
                    ResourceSet rset = null;
                    try {
                        rset = new ResourceManager(scriptResourceName, assembly).GetResourceSet(Threading.Thread.CurrentThread.CurrentUICulture, true, true);
                    }
                    catch (MissingManifestResourceException) {
#if TARGET_JVM // GetResourceSet does not throw  MissingManifestResourceException if ressource is not exists
                    }
                    if (rset == null)
                    {
#endif
                        if (scriptResourceName.EndsWith(".resources"))
                        {
                            scriptResourceName = scriptResourceName.Substring(0, scriptResourceName.Length - 10);
                            rset = new ResourceManager(scriptResourceName, assembly).GetResourceSet(Threading.Thread.CurrentThread.CurrentUICulture, true, true);
                        }
#if !TARGET_JVM
                        else
                        {
                            throw;
                        }
#endif
                    }
                    if (rset == null)
                    {
                        break;
                    }
                    writer.WriteLine();
                    string ns   = sra.TypeName;
                    int    indx = ns.LastIndexOf('.');
                    if (indx > 0)
                    {
                        writer.WriteLine("Type.registerNamespace('" + ns.Substring(0, indx) + "')");
                    }
                    writer.Write("{0}={{", sra.TypeName);
                    bool first = true;
                    foreach (DictionaryEntry entry in rset)
                    {
                        string value = entry.Value as string;
                        if (value != null)
                        {
                            if (first)
                            {
                                first = false;
                            }
                            else
                            {
                                writer.Write(',');
                            }
                            writer.WriteLine();
                            writer.Write("{0}:{1}", GetScriptStringLiteral((string)entry.Key), GetScriptStringLiteral(value));
                        }
                    }
                    writer.WriteLine();
                    writer.WriteLine("};");
                    break;
                }
            }

            bool notifyScriptLoaded = request.QueryString ["n"] == "t";
            if (notifyScriptLoaded)
            {
                writer.WriteLine();
                writer.WriteLine("if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();");
            }
#endif
        }
Exemplo n.º 3
0
        void System.Web.IHttpHandler.ProcessRequest(HttpContext context)
#endif
        {
            HttpRequest request = context.Request;
            // val is URL-encoded, which means every + has been replaced with ' ', we
            // need to revert that or the base64 conversion will fail.
            string d = request.QueryString ["d"];

            if (!String.IsNullOrEmpty(d))
            {
                d = d.Replace(' ', '+');
            }

            AssemblyEmbeddedResources entry;
            EmbeddedResource          res = DecryptAssemblyResource(d, out entry);
            WebResourceAttribute      wra = res != null ? res.Attribute : null;

            if (wra == null)
            {
                throw new HttpException(404, "Resource not found");
            }

            Assembly assembly;

            if (entry.AssemblyName == "s")
            {
                assembly = currAsm;
            }
            else
            {
                assembly = Assembly.Load(entry.AssemblyName);
            }

            HttpResponse response  = context.Response;
            string       req_cache = request.Headers ["Cache-Control"];

            if (String.Compare(req_cache, "max-age=0", StringComparison.Ordinal) == 0)
            {
                long atime;
                if (Int64.TryParse(request.QueryString ["t"], out atime))
                {
                    if (atime == File.GetLastWriteTimeUtc(assembly.Location).Ticks)
                    {
                        response.Clear();
                        response.StatusCode   = 304;
                        response.ContentType  = null;
                        response.CacheControl = "public";                         // easier to set it to public as MS than remove it
                        context.ApplicationInstance.CompleteRequest();
                        return;
                    }
                }
            }
            string modif_since = request.Headers ["If-Modified-Since"];

            if (!String.IsNullOrEmpty(modif_since))
            {
                try {
                    DateTime modif;
                    if (DateTime.TryParseExact(modif_since, "r", null, 0, out modif))
                    {
                        if (File.GetLastWriteTimeUtc(assembly.Location) <= modif)
                        {
                            response.Clear();
                            response.StatusCode   = 304;
                            response.ContentType  = null;
                            response.CacheControl = "public";                             // easier to set it to public as MS than remove it
                            context.ApplicationInstance.CompleteRequest();
                            return;
                        }
                    }
                } catch {}
            }

            response.ContentType = wra.ContentType;

            DateTime utcnow = DateTime.UtcNow;

            response.Headers.Add("Last-Modified", utcnow.ToString("r"));
            response.ExpiresAbsolute = utcnow.AddYears(1);
            response.CacheControl    = "public";

            Stream s = assembly.GetManifestResourceStream(res.Name);

            if (s == null)
            {
                throw new HttpException(404, "Resource " + res.Name + " not found");
            }

            if (wra.PerformSubstitution)
            {
                using (StreamReader r = new StreamReader(s)) {
                    TextWriter w = response.Output;
                    new PerformSubstitutionHelper(assembly).PerformSubstitution(r, w);
                }
            }
            else if (response.OutputStream is HttpResponseStream)
            {
                UnmanagedMemoryStream st      = (UnmanagedMemoryStream)s;
                HttpResponseStream    hstream = (HttpResponseStream)response.OutputStream;
                unsafe {
                    hstream.WritePtr(new IntPtr(st.PositionPointer), (int)st.Length);
                }
            }
            else
            {
                byte [] buf    = new byte [1024];
                Stream  output = response.OutputStream;
                int     c;
                do
                {
                    c = s.Read(buf, 0, 1024);
                    output.Write(buf, 0, c);
                } while (c > 0);
            }
#if SYSTEM_WEB_EXTENSIONS
            TextWriter writer = response.Output;
            foreach (ScriptResourceAttribute sra in assembly.GetCustomAttributes(typeof(ScriptResourceAttribute), false))
            {
                if (String.Compare(sra.ScriptName, res.Name, StringComparison.Ordinal) == 0)
                {
                    string      scriptResourceName = sra.ScriptResourceName;
                    ResourceSet rset = null;
                    try {
                        rset = new ResourceManager(scriptResourceName, assembly).GetResourceSet(Threading.Thread.CurrentThread.CurrentUICulture, true, true);
                    }
                    catch (MissingManifestResourceException) {
#if TARGET_JVM // GetResourceSet does not throw  MissingManifestResourceException if ressource is not exists
                    }
                    if (rset == null)
                    {
#endif
                        if (scriptResourceName.EndsWith(".resources"))
                        {
                            scriptResourceName = scriptResourceName.Substring(0, scriptResourceName.Length - 10);
                            rset = new ResourceManager(scriptResourceName, assembly).GetResourceSet(Threading.Thread.CurrentThread.CurrentUICulture, true, true);
                        }
#if !TARGET_JVM
                        else
                        {
                            throw;
                        }
#endif
                    }
                    if (rset == null)
                    {
                        break;
                    }
                    writer.WriteLine();
                    string ns   = sra.TypeName;
                    int    indx = ns.LastIndexOf('.');
                    if (indx > 0)
                    {
                        writer.WriteLine("Type.registerNamespace('" + ns.Substring(0, indx) + "')");
                    }
                    writer.Write("{0}={{", sra.TypeName);
                    bool first = true;
                    foreach (DictionaryEntry de in rset)
                    {
                        string value = de.Value as string;
                        if (value != null)
                        {
                            if (first)
                            {
                                first = false;
                            }
                            else
                            {
                                writer.Write(',');
                            }
                            writer.WriteLine();
                            writer.Write("{0}:{1}", GetScriptStringLiteral((string)de.Key), GetScriptStringLiteral(value));
                        }
                    }
                    writer.WriteLine();
                    writer.WriteLine("};");
                    break;
                }
            }

            bool notifyScriptLoaded = request.QueryString ["n"] == "t";
            if (notifyScriptLoaded)
            {
                writer.WriteLine();
                writer.WriteLine("if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();");
            }
#endif
        }