System.Web.Handlers.ScriptResourceHandler.AssemblyResourceLoader.System C# (CSharp) Method

System() private method

private System ( HttpContext context ) : void
context HttpContext
return void
		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
		}