代码
1 /// url重写 [C#源代码来自http://www.showjim.com/]
2  using System;
3 using System.Web;
4 using System.Collections.Generic;
5 using System.Drawing;
6 using System.Drawing.Imaging;
7
8 namespace web
9 {
10 /// <summary>
11 /// url重写
12 /// </summary>
13 public static class rewriteUrl
14 {
15 #region http MIME类型
16 /// <summary>
17 /// http MIME类型
18 /// </summary>
19 public static readonly Dictionary<string, string> contentType = _contentType;
20 /// <summary>
21 /// http MIME类型
22 /// </summary>
23 private static Dictionary<string, string> _contentType
24 {
25 get
26 {
27 Dictionary<string, string> contentType = new Dictionary<string, string>();
28 contentType.Add("bmp", "application/x-bmp");
29 contentType.Add("css", "text/css");
30 contentType.Add("gif", "image/gif");
31 contentType.Add("htm", "text/html");
32 contentType.Add("jpe", "image/jpeg");
33 contentType.Add("jpeg", "image/jpeg");
34 contentType.Add("jpg", "image/jpeg");
35 contentType.Add("js", "application/x-javascript");
36 contentType.Add("swf", "application/x-shockwave-flash");
37 contentType.Add("txt", "text/plain");
38 return contentType;
39 }
40 }
41 #endregion
42
43 #region url重写入口(客户端关键字必须为小写)
44 /// <summary>
45 /// url重写入口(客户端关键字必须为小写)
46 /// </summary>
47 /// <param name="context">http请求信息</param>
48 /// <param name="ashx">是否只重写.ashx请求</param>
49 public static void init(HttpContext context, bool onlyAshx)
50 {
51 //context.Response.Cache.SetCacheability(HttpCacheability.Public);
52 string path = context.Request.Path;
53 if (sys.pub.isDebug) sys.io.file.textFile.writeDebug(path);
54 if (onlyAshx)
55 {
56 int index = path.LastIndexOf('.');
57 if (index != -1)
58 {
59 if (path.Substring(index) == ".ashx") index = (path = path.Substring(0, index)).LastIndexOf('.');
60 if (index != -1)
61 {
62 string type = path.Substring(index + 1);
63 if (contentType.ContainsKey(type))
64 {
65 context.Response.ContentType = contentType[type];
66
67 bool isRewrite = false;
68 if ((index = path.IndexOf('/', 1)) != -1)
69 {
70 string dir = path.Substring(1, index - 1);
71 if (isRewrite = rewrites.ContainsKey(dir)) rewrites[dir](context, path, index + 1);
72 }
73 if (!isRewrite)
74 {
75 try { context.Response.WriteFile(context.Server.MapPath(path)); }
76 catch { }
77 context.Response.End();
78 }
79 }
80 }
81 }
82 }
83 else
84 {
85 int index = path.IndexOf('/', 1);
86 if (index != -1)
87 {
88 string dir = path.Substring(1, index - 1);//客户端必须为小写
89 if (rewrites.ContainsKey(dir)) rewrites[dir](context, path, index + 1);
90 }
91 //if (path.EndsWith(".htm", true, null)) context.RewritePath(path.Substring(0, path.Length - 3) + "aspx");
92 }
93 }
94 #endregion
95
96 #region 重写url的委托
97 /// <summary>
98 /// 重写url的委托
99 /// </summary>
100 /// <param name="path">原始url</param>
101 private delegate void rewriteDelegate(HttpContext context, string path, int pathIndex);
102 /// <summary>
103 /// 重写url的委托集合
104 /// </summary>
105 private static readonly Dictionary<string, rewriteDelegate> rewrites = _rewrites;
106 /// <summary>
107 /// 重写url的委托集合
108 /// </summary>
109 private static Dictionary<string, rewriteDelegate> _rewrites
110 {
111 get
112 {
113 Dictionary<string, rewriteDelegate> rewrites = new Dictionary<string, rewriteDelegate>();
114 rewrites.Add("verifyimage", verifyImage);
115 rewrites.Add("image", upFile);
116 return rewrites;
117 }
118 }
119 #endregion
120
121 #region 重写验证码
122 /// <summary>
123 /// 重写验证码
124 /// </summary>
125 /// <param name="context">http请求信息</param>
126 /// <param name="path">请求页面路径</param>
127 /// <param name="pathIndex">路径有效起始位置</param>
128 private static void verifyImage(HttpContext context, string path, int pathIndex)
129 {
130 string newPath = "/verifyImage.aspx";
131 if (pathIndex != path.Length)
132 {
133 int index = path.IndexOf('.', pathIndex);
134 if (index != -1) newPath += "?temp=" + path.Substring(pathIndex, index - pathIndex);
135 }
136 context.RewritePath(newPath);
137 }
138 #endregion
139
140 #region 重写上传的文件
141 /// <summary>
142 /// 重写上传的文件
143 /// </summary>
144 /// <param name="context">http请求信息</param>
145 /// <param name="path">请求页面路径</param>
146 /// <param name="pathIndex">路径有效起始位置</param>
147 private static void upFile(HttpContext context, string path, int pathIndex)
148 {
149 sys.web.http.response.cache.check();
150 int pathLength = path.Length;
151 string defaultPath = "/upFile/noImage.gif", newPath = "/upFile/";
152 if (pathIndex == pathLength) newPath = defaultPath;
153 else
154 {
155 int index = path.IndexOf('/', pathIndex);
156 if (index - pathIndex == 1 && path[pathIndex] == 'f')
157 {
158 if ((++index) == pathLength) newPath = defaultPath;
159 else
160 {
161 pathIndex = path.IndexOf('/', index);
162 if (pathIndex == -1 || pathIndex == pathLength - 1) newPath = defaultPath;
163 else
164 {
165 string width_height = path.Substring(index, pathIndex - index);
166 int widthIndex = width_height.IndexOf('_');
167 if (widthIndex == -1 || widthIndex == 0 || widthIndex == width_height.Length - 1) newPath += path.Substring(index);
168 else
169 {
170 int width = 0, height = 0;
171 try
172 {
173 width = Int32.Parse(width_height.Substring(0,widthIndex));
174 height = Int32.Parse(width_height.Substring(widthIndex + 1));
175 newPath += path.Substring(pathIndex + 1);
176 if (width > 0 || height > 0)
177 {
178 Image oldImage = null;
179 try { oldImage = Image.FromFile(context.Server.MapPath(newPath)); }
180 catch { }
181 if (oldImage != null && oldImage.Width != 0 && oldImage.Height != 0 && (width <= 0 || oldImage.Width > width) && (height <= 0 || oldImage.Height > height))
182 {
183 if (width <= 0) width = oldImage.Width * height / oldImage.Height;
184 else if (height <= 0) height = oldImage.Height * width / oldImage.Width;
185 Image newImage = oldImage.GetThumbnailImage(width == 0 ? 1 : width, height == 0 ? 1 : height, null, new System.IntPtr());
186 newImage.Save(context.Response.OutputStream, ImageFormat.Gif); //硬盘空间足的话,应该保存到文件
187 newImage.Dispose();
188 oldImage.Dispose();
189 newPath = null;
190 context.Response.End();
191 }
192 }
193 }
194 catch { newPath += path.Substring(index); }
195 }
196 }
197 }
198 }
199 else newPath += path.Substring(pathIndex);
200 }
201 if (newPath != null) context.RewritePath(newPath);
202 }
203 #endregion
204 }
205 }
206
207

 

posted on 2010-10-08 08:27  yrScience  阅读(505)  评论(0编辑  收藏  举报