日期:2013-07-11  浏览次数:20777 次

我们都知道asp这一类的服务器端处理的程序,其好处之一就是只向客户端输出
标准的Html流。因此可以起到向客户隐藏细节的作用。也就是说当我们在浏览器中
键入asp程序的网址后只能看见标准的Html文件,而不能看见asp的内容。但有时,
例如在一个asp的教学站点,我们有必要显示asp文件的内容,或者你愿意将你的原
代码与人享,通过一个程序将代码显示出来。
下面是我编写的一个asp程序,view_code.asp,它提供两种提交方式:一种是用
表格提交,即你知道了该源文件的物理地址(类似于:c:\asp_source\test.asp的
形式)。一种是采用get方式提交(类似于:<a href="view_code.asp?code_path=
<%=server.mappath(request.servervariables("PATH_INFO"))%>&cgi_type=asp">
点击此处查看原代码</a>)。另外它还支持两种cgi脚本,一种是asp,一种是php。

代码段:
<%
on error resume next
'忽略程序执行中的错误,在程序的最后统一处理。
%>
<%
function rt_min(num1,num2)
'该子程序用于返回两数中不等于零的最小数。
if num1=0 and num2=0 then
rt_min=-1
elseif num1=0 then
rt_min=num2
elseif num2=0 then
rt_min=num1
elseif num1<num2 then
rt_min=num1
else
rt_min=num2
end if
end function
%>
<%
function line_check(strline,cgi_type)
'该子程序用于检查输入段中是否包含有"<%、%>、<script或</script的特殊字符
dim cgi_flag
if cgi_type="php" then
cgi_flag="?"
else
cgi_flag="%"
end if
'定义的cgi_flag用于代表php和asp的不同标识符
line_check=0
itemp=0
ipos=instr(strline,"<"&cgi_flag)
if rt_min(ipos,itemp)=ipos then
itemp=ipos
line_check=1
end if
ipos=instr(strline,cgi_flag&">")
if rt_min(ipos,itemp)=ipos then
itemp=ipos
line_check=2
end if
ipos=instr(1,strline,"<"&"script",1)
if rt_min(ipos,itemp)=ipos then
itemp=ipos
line_check=3
end if
ipos=instr(1,strline,"<"&"/script",1)
if rt_min(ipos,itemp)=ipos then
itemp=ipos
line_check=4
end if
end function
%>
<%
sub printhtml(strline)
'该子过程用于打印不含有上述四种特殊标记的行
ispace=len(strline)-len(ltrim(strline))
i=1
while(mid(strline,i,1))=chr(9)
ispace=ispace+5
i=i+1
wend
'统计空白的数量
if ispace>0 then
for i=1 to ispace
response.write(" ")
next
end if
ipos=instr(strline,"<")
if ipos then
response.write(left(strline,ipos-1))
response.write("<")
'用<来替代<,使浏览器不解释<>中的标记
strline=right(strline,len(strline)-ipos)
call printhtml(strline)
'自调用,直到没有<的出现
else
response.write(strline)
end if
end sub
%>
<%
sub printline(strline,iflag,cgi_type)
'该自过程用于根据line_check的返回值分别处理
dim cgi_flag
if cgi_type="php" then
cgi_flag="?"
else
cgi_flag="%"
end if
select case iflag
case 0
call printhtml(strline)
case 1
ipos=instr(strline,"<"&cgi_flag)
call printhtml(left(strline,ipos-1))
response.write("<font color=#ff0000>")
response.write("<"&cgi_flag)
strline=right(strline,len(strline)-ipos-1)
call printline(strline,line_check(strline,cgi_type),cgi_type)
'自调用,直到没有四种特殊标记的出现
case 2
ipos=instr(strline,cgi_flag&">")
call printhtml(left(strline,ipos-1))
response.write(cgi_flag&">")
response.write("</font>")
strline=right(strline,len(strline)-ipos-1)
call printline(strline,line_check(strline,cgi_type),cgi_type)
case 3
ipos=instr(1,strline,"<"&"script",1)
call printhtml(left(strline,ipos-1))
response.write("<font color=#00ff00>")
response.write("<"&"script")
strline=right(strline,len(strline)-ipos-6)
call printline(strline,line_check(strline.cgi_type),cgi_type)
case 4
ipos=instr(1,strline,"<"&"/script>",1)
call printhtml(left(strline,ipos-1))
response.write("lt;"&"/script"&">")
response.write("</font>")
strline=right(strline,len(strline)-ipo