//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;
}
}
}
}