CGI与WEB服务器的响应头
时间:2005-05-03 来源:笨狗
perl
CGI与WEB服务器的响应头
我们知道 CGI 除了文本外还可以输出其他不同的资料例如图片、声音等数据流,为了让
WEBSEVER能辨认不同的数据型态,所以CGI 的输出包括两部分,前面的是相应头告诉
Server要输出的信息属于何种MIME类型,后面才是我们要输出的数据;具体上响应头与
输出正文、响应头与响应头之间是用两个空行作为分隔的,通常大部分的CGI程序都会有
一行 print "Content-type: text/html "; 下面跟着才是要输出的信息。这个响应
头地意思是告诉Server要输出的信息的MIME类型是文本/超文本。如果你是要输出的是一
张图片的print "Content-Type: image/gif "; 跟着是图片的数据流就可以了。
当然响应头的作用不仅仅是这些,利用响应头我们在编写CGI时大有文章可作,下面有几
个重要的响应头是在我们写程序时也是很有用的:
print "Pragma:no-cache ";
这个响应头是通知浏览器不要把该页面保存在缓冲区中
print "Location: URL"
返回一个指定的URL,有时如果你想让程序动态得返回一个URL给来访者就得用到这个响应
头,但每次只能返回一个URL,举个应用的例子,当你主页访问量较大而想减轻服务器的
负担,那么可以建立多个镜像站点,通过CGI随机发送镜像站点的URL从而减轻服务器的负担。
print "Set-Cookie:name1=value;name2=value2;exprires=DATE;PATH=PATH;domiam=DOMAIN_NAME;SECURE ";
CGI这个响应头就是利用这个响应头在来访者的计算机上设置Cookie的,如果你想知道如何
具体用CGI在来访者的计算机上设置Cookie那么请看本站的关于CGI读写COOKIE的编程。
如果CGI程式所送出的结果不希望经过 Server 的 包 装,而希望直接送到客户端的浏览器,
则CGI必须负责送出完整的超本文传输协定(HTTP)讯息 封包(Message Packaged)。当Server
收到CGI程式输出的结果时,会先检查是否有 "HTTP/1.0" 的讯 息。如果有则Server会认为
以下的讯息已 经包 含 了完整的超本文传输协定(HTTP)回应,并将其直接送至客户端的浏
览程式(Browser)。以下是一简短的范例 ∶
print "HTTP/1.0 200 OK "
print "Date: Tuesday, 31-May-94 19:04:30 GMT ";
print "erver: WebSite 2.0 ";
print "MIME-version: 1.0 "
print "Content-type: text/html";
print "Last-modified: Sunday, 15-May-94 02:12:32 GMT ";
print "Content-length: 4109 ";
print " ";
print "";
为了让WEBSERVER知道不必为CGI生成响应头,CGI程序的文件明必须有前缀 "HPH-" 或 "nph-",
例如nph-mycgi.cgi
Status: 说明CGI执行的况状,其值必须是超本文传输协定(HTTP)所认可的。
(http://www.fanqiang.com)
============================================
============================================
doc => 'application/msword',
ppt => 'application/mspowerpoint',
oda => 'application/oda',
pdf => 'application/pdf',
eds => 'application/postscript',
ai => 'application/postscript',
ps => 'application/postscript',
rtf => 'application/rtf',
dvi => 'application/x-dvi',
hdf => 'application/x-hdf',
latex => 'application/x-latex',
nc => 'application/x-netcdf',
cdf => 'application/x-netcdf',
tex => 'application/x-tex',
texinfo => 'application/x-texinfo',
texi => 'application/x-texinfo',
t => 'application/x-troff',
tr => 'application/x-troff',
roff => 'application/x-troff',
man => 'application/x-troff-man',
me => 'application/x-troff-me',
ms => 'application/x-troff-ms',
src => 'application/x-wais-source',
wsrc => 'application/x-wais-source',
zip => 'application/zip',
bcpio => 'application/x-bcpio',
cpio => 'application/x-cpio',
gtar => 'application/x-gtar',
sh => 'application/x-shar',
shar => 'application/x-shar',
sv4cpio => 'application/x-sv4cpio',
sv4crc => 'application/x-sv4crc',
tar => 'application/x-tar',
ustar => 'application/x-ustar',
snd => 'audio/basic',
au => 'audio/basic',
aifc => 'audio/x-aiff',
aif => 'audio/x-aiff',
aiff => 'audio/x-aiff',
wav => 'audio/x-wav',
gif => 'image/gif',
jpe => 'image/jpeg',
jpg => 'image/jpeg',
jpeg => 'image/jpeg',
tif => 'image/tiff',
tiff => 'image/tiff',
fpx => 'image/vnd.fpx',
fpix => 'image/vnd.fpx',
ras => 'image/x-cmu-rast',
pnm => 'image/x-portable-anymap',
pbn => 'image/x-portable-bitmap',
pgm => 'image/x-portable-graymap',
ppm => 'image/x-portable-pixmap',
rgb => 'image/x-rgb',
xbm => 'image/x-xbitmap',
xpm => 'image/x-xbitmap',
xwd => 'image/x-xwindowdump',
png => 'image/png',
css => 'text/css',
htm => 'text/html',
html => 'text/html',
shtml => 'text/html',
text => 'text/plain',
c => 'text/plain',
cc => 'text/plain',
'c++' => 'text/plain',
h => 'text/plain',
asp => 'text/plain',
php => 'text/plain',
php3 => 'text/plain',
pl => 'text/plain',
pm => 'text/plain',
cgi => 'text/plain',
txt => 'text/plain',
java => 'text/plain',
el => 'text/plain',
tsv => 'text/tab-separated-values',
etx => 'text/x-setext',
mpg => 'video/mpeg',
mpe => 'video/mpeg',
mpeg => 'video/mpeg',
mov => 'video/quicktime',
qt => 'video/quicktime',
avi => 'application/x-troff-msvideo',
movie => 'video/x-sgi-movie',
mv => 'video/x-sgi-movie',
mime => 'message/rfc822',
xml => 'application/xml'