我們都知道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)-ipos-8) call printline(strline,line_check(strline,cgi_type),cgi_type) case else response.write("error") end select end sub %> <html> <head> <title> view cgi_code(.asp or .php) </title> </head> <body> <form action="view_code.asp" method="POST"> 請輸入路徑:<input type=text name="code_path"> 請選擇類型:<select name="cgi_type"> <option value="asp">asp</option> <option value="php">php</option> </select> <input type=submit> </form> <hr> <% if vartype(request.servervariables("HTTP_REFERER")) then '判斷該頁面是否是由其他的頁面申請提交,若用戶是直接在瀏覽器中輸入地址 而來的,則HTTP_REFERER環境變量應該沒有被初始化 if request.servervariables("REQUEST_METHOD")="POST" then code_path=request.form("code_path") cgi_type=request.form("cgi_type") response.write("下面的代碼來自表格的提交:"&"<br>") response.write("路徑為:"&code_path&"<br>") elseif request.servervariables("REQUEST_METHOD")="GET" then code_path=request.querystring("code_path") cgi_type=request.querystring("cgi_type") response.write("下面的代碼來自"&code_path&"的提交:"&"<br>") response.write("路徑為:"&code_path&"<br>") end if '根據提交方式的不同顯示不同的提示 set fileobject=server.createobject("Scripting.FileSystemObject") if fileobject.fileexists(code_path) then '檢查要打開的文件是否存在 set stream=fileobject.opentextfile(code_path,1,false,0) while not stream.atendofstream stroutput=stream.readline call printline(stroutput,line_check(stroutput,cgi_type),cgi_type) '將該文件的每一行都分別交給printline來處理 response.write("<br>") wend set stream=nothing else response.write("不能打開文件"&"<br>") end if end if %> </body> </html> <% '下面的代碼為統一的錯誤處理段,它根據程序運行時產生的錯誤代碼來分別處理 if err.number<>0 then response.write("error"&"<br>") response.write("錯誤代碼:"&err.number&"<br>") response.write("錯誤描述:"&err.description) end if %>
最后,我在給出一個引用該程序的測試頁面 <html> <head> <title>顯示代碼的測試頁面</title> </head> <body> <a href="view_code.asp?code_path=<%=server.mappath(request.servervariables ("PATH_INFO"))%>&cgi_type=asp">點擊此處查看該頁的源碼</a> </body> </html>(出處:熱點網絡)
|