//1.0 HttpApplication
using System;
using System.Collections.Generic;using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 小型IIS服務器
{
/// <summary>
/// 存放19個管道事件
/// 繼承IHttpHandler 接口 實現其中的PR方法
/// </summary>
public class HttpApplication : IHttpHandler
{
/// <summary>
/// 按照流程以此執行19個管道事件
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
//開始執行1-19個管道事件
//執行到第8個事件時:判斷url請求頁面的擴展名
// 01靜態文件,則直接從服務器讀取其內容存入到response中
// 02動態文件(.ashx,.aspx),則實例化其類的對象(動態頁面是有一個頁面對象存儲信息的),並將此對象存入httpcontext的remaphander屬性中
//傳入context,以獲取請求url
IHttpHandler handler = PageFactoryer.GetPageInstance(context);
//調用ProcessRequest給context.RemapHandler 賦值
//如果是靜態文件或圖片的,直接給返回靜態頁面的內容,給content.contentBody即可
//如果是動態文件的,將頁面對象存入上下文的RemapHandler屬性中,提供給第11-12管道之間的事件調用
handler.ProcessRequest(context);
//第11個事件
//如果是動態文件,RemapHandler!=null
if (context.RemapHandler != null)
{
context.RemapHandler.ProcessRequest(context);
}
//第12個事件
}
}
}
//2.0 PageFactoryer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 小型IIS服務器
{
public class PageFactoryer
{
/// <summary>
/// 根據url後綴的不同,調用不同的處理方法
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static IHttpHandler GetPageInstance(HttpContext context)
{
//1.0 獲取request對象封裝好的Extention
string extention = context.Request.UrlExtention; //web/2.htm
IHttpHandler handler = null;
switch (extention)
{
case ".html":
case ".htm":
//實例化處理靜態文件的類對象
handler = new ProcessStatic();
break;
case ".jpg":
case ".png":
case ".gif":
//實例化處理圖片文件的類對象
handler = new ProcessImage();
break;
case ".aspx":
case ".ashx":
//實例化處理動態文件的類對象
handler = new ProcessDyn();
break;
default:
break;
}
return handler;
}
}
}
//3.0 ProcessDyn:IHttpHandler
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection; //反射
namespace 小型IIS服務器
{
/// <summary>
/// 創建根據url來解析出請求頁面的類文件,通過反射的方式動態創建頁面類的實例
/// 存入上下文的remapHandlder中
/// index.aspx -> index_aspx
/// </summary>
public class ProcessDyn:IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
//1.0 獲取當前url中動態頁面對應的類名
string className = System.IO.Path.GetFileNameWithoutExtension(context.Request.RawUrl);
//2.0 通過反射,動態創建動態頁面的類的對象實體
Assembly ass = typeof(ProcessDyn).Assembly;
object instance = ass.CreateInstance("小型IIS服務器.page." + className, true); //true,忽略大小寫
if (instance != null)
{
IHttpHandler handler = instance as IHttpHandler;
//將頁面對象存入上下文的RemapHandler屬性中,提供給第11-12管道之間的事件調用
context.RemapHandler = handler;
}
else
{
//當前動態頁面不存在
context.Response.StateCode = 404;
}
}
catch
{
context.Response.StateCode = 500;
}
}
}
}
//4.0 ProcessImage
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 小型IIS服務器
{
/// <summary>
/// 處理圖片文件
/// </summary>
public class ProcessImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
//1.0 獲取物理路徑
string phyPath = context.Server.MapPath(context.Request.RawUrl);
if (System.IO.File.Exists(phyPath))
{
//2.0 根據五里路徑獲取圖片內容,此時獲取到的直接是字符串,所以直接給context.Response屬性賦值
context.Response.ContentBody = System.IO.File.ReadAllBytes(phyPath);
}
else
{
context.Response.StateCode = 404;
}
}
catch
{
context.Response.StateCode = 500;
}
}
}
}
//4.0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 小型IIS服務器
{
/// <summary>
/// 處理圖片文件
/// </summary>
public class ProcessImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
//1.0 獲取物理路徑
string phyPath = context.Server.MapPath(context.Request.RawUrl);
if (System.IO.File.Exists(phyPath))
{
//2.0 根據五里路徑獲取圖片內容,此時獲取到的直接是字符串,所以直接給context.Response屬性賦值
context.Response.ContentBody = System.IO.File.ReadAllBytes(phyPath);
}
else
{
context.Response.StateCode = 404;
}
}
catch
{
context.Response.StateCode = 500;
}
}
}
}
//5.0 ProcessStatic:IHttpHandler
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 小型IIS服務器
{
/// <summary>
/// 處理靜態文件,根據url中的虛擬路徑去服務器硬盤上找到相對應的內容,並存入response
/// </summary>
public class ProcessStatic:IHttpHandler
{
/*
*01 根據url虛擬路徑獲取物理路徑
*02 根據物理路徑找到相應的文件
* 首先判斷文件是否存在,不存在就響應404
* 找到文件,並將文件的所有內容轉爲字符串類型
* 將文件內容送至response.contentBody屬性存儲
*/
/// <summary>
/// 根據url中的虛擬路徑去服務器硬盤上找到相對應的內容,並存入response
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
try
{
//1.0 根據虛擬路徑獲取物理路徑
string vitPath = context.Request.RawUrl; //web/1.html
string phyPath = context.Server.MapPath(vitPath);
//如果文件存在,則執行
if (System.IO.File.Exists(phyPath))
{
//2.0 根據物理路徑讀取文件內容
string contentBody = System.IO.File.ReadAllText(phyPath);
//3.0 將文件內容,存入response對象的ContentBody屬性中
context.Response.Write(contentBody);
}
else
{
//文件不存在,設置響應代碼404,表示文件不存在
context.Response.StateCode = 404;
}
}
//如果服務器沒有響應,就顯示響應代碼500,服務器異常
catch
{
context.Response.StateCode = 500;
}
}
}
}