20-755- The InternetLecture 10- Web Services III.ppt
《20-755- The InternetLecture 10- Web Services III.ppt》由会员分享,可在线阅读,更多相关《20-755- The InternetLecture 10- Web Services III.ppt(41页珍藏版)》请在麦多课文档分享上搜索。
1、20-755: The Internet Lecture 10: Web Services III,David OHallaron School of Computer Science and Department of Electrical and Computer Engineering Carnegie Mellon UniversityInstitute for eCommerce, Summer 1999,Todays lecture,Anatomy of a simple Web server (40 min) Break (10 min) Advanced server feat
2、ures (45 min),Anatomy of Tiny: A simple Web server,#!/usr/local/bin/perl5 -w use IO:Socket; # # tiny.pl - The Tiny HTTP server #,Tiny: configuration,# # Configuration # $port = 8000; # the port we listen on $htmldir = “./html/“; # the base html directory $cgidir = “./cgi-bin/“; # the base cgi direct
3、ory $server = “Tiny Web server 1.0“; # server info,Tiny: error messages,# # Error messages # # Terse error messages go in the response header %terse_errors = ( “403“, “Forbidden“, “404“, “Not Found“, “501“, “Not Implemented“, ); # Verbose error messages go in the response message body %verbose_error
4、s = ( “403“, “You are not allowed to access this item“, “404“, “Tiny couldnt find the requested item on the server“, “501“, “Tiny does not support the given request type“, );,Tiny: Create a listening socket,# # Create a TCP listening socket file descriptor # # LocalPort: list on port $port # Type :
5、use TCP # Resuse : reuse address right away # Listen : buffer at most 10 requests # $listenfd = IO:Socket:INET-new(LocalPort = $port, Type = SOCK_STREAM, Reuse = 1, Listen = 10) or die “Couldnt listen on port $port: $n“;,Tiny: main loop structure,# # Loop forever waiting for HTTP requests # while(1)
6、 # Wait for a connection request from a client $connfd = $listenfd-accept(); # Determine the domain name and IP address of this client # Parse the request line (after stripping the newline) # Parse the URI# Parse the request headers# OPTIONS method# HEAD method# GET method# misc: POST, PUT, DELETE,
7、and TRACE methods ,Tiny: error procedure,# # error - send an error message back to the client # $_0: the error number # $_1: the method or URI that caused the error # sub error local($errno) = $_0; local($errmsg) = “$errno $terse_errors$errno“; print $connfd $errmsg $errmsg $verbose_errors$errno: $_
8、1 The Tiny Web Server EndOfMessage ,Tiny: get clients name and address,# Determine the domain name and IP address of this client $client_sockaddr = getpeername($connfd); ($client_port, $client_iaddr) = unpack_sockaddr_in($client_sockaddr); $client_port = $client_port; # so -w wont complain $client_n
9、ame = gethostbyaddr($client_iaddr, AF_INET); ($a1, $a2, $a3, $a4) = unpack(C4, $client_iaddr); print “Opened connection with $client_name ($a1.$a2.$a3.$a4)n“;,Tiny: parsing the request line,# Parse the request line (after stripping the newline) chomp($line = ); ($method, $uri, $version) = split(/s+/
10、, $line); print “received $linen“;,Tiny: parsing the URI,# # Parse the URI # # Either the URI refers to a CGI program. if ($uri = m:/cgi-bin/:) $is_static = 0; # extract the program name and its arguments ($filename, $cgiargs) = split(/?/, $uri); if (!defined($cgiargs) $cgiargs = “; # replace /cgi-b
11、in with the default cgi directory $filename = s:/cgi-bin/:$cgidir:o; ,Tiny: Parsing the URI,# . or the URI refers to a file else $is_static = 1; # static content $cgiargs = “; # replace the first / with the default html directory $filename = $uri; $filename = s:/:$htmldir:o; # use index.html for the
12、 default file $filename = s:/$:/index.html:; # debug statements like this will help you a lot print “parsed URI: is_static=$is_static, filename=$filename, cgiargs=$cgiargsn“;,Tiny: parsig the request headers,# # Parse the request headers # $content_length = 0; $content_type = “text/html“; while () #
13、 read request header into $_ # Delete CR and NL chars s/n|r/g; # delete CRLF and CR chars from $_ # Determine the length of the message body # search for “Content-Length:“ at beginning of string $_ # ignore the case if (/Content-Length: (S*)/i) $content_length = $1; ,Tiny: parse the command line (co
14、nt),# determine the type of content (if any) in msg body # search for “Content-Type:“ at beginning of string $_ # ignore the case if (/Content-Type: (S*)/i) $content_type = $1; # If $_ was a blank line, exit the loop if (length = 0) last; ,Tiny: OPTIONS,# # OPTIONS method # if ($method eq “OPTIONS“)
15、 $today = gmtime().“ GMT“; $connfd-print(“$version 200 OKn“); $connfd-print(“Date: $todayn“); $connfd-print(“Server: $servern“); $connfd-print(“Content-length: 0n“); $connfd-print(“Allow: OPTIONS HEAD GETn“); $connfd-print(“n“); ,Tiny: HEAD,# # HEAD method # elsif ($method eq “HEAD“) # were dissallo
16、wing HEAD methods on scripts if (!$is_static) error(403, $filename); else $today = gmtime().“ GMT“; head_method($filename, $uri, $today, $server); ,Tiny: HEAD (cont),# # process the HEAD method on static content # $_0 : the file to be processed # $_1 : the uri # $_2 : todays date # $_3 : server name
17、 # sub head_method local ($filename) = $_0; local ($uri) = $_1; local ($today) = $_2; local ($server) = $_3; local $modified; local $filesize; local $filetype;,Tiny: HEAD (cont),# make sure the requested file exists if (!(-e $filename) error(404, $uri); # make sure the requested is readable elsif (!
18、(-r $filename) error(403, $uri); ,Tiny: HEAD (cont),# serve the response header but not the file else # determine file modifcation date $modified = gmtime(stat($filename)9).“ GMT“; # determine filesize in bytes $filesize = (stat($filename)7; # determin filetype (default is text) if ($filename = /.ht
19、ml$/) $filetype = “text/html“; elsif ($filename = /.gif$/) $filetype = “image/gif“; elsif ($filename = /.jpg$/) $filetype = “image/jpeg“; else $filetype = “text/plain“; ,Tiny: HEAD (cont),# print the response header $connfd-print(“HTTP/1.1 200 OKn“); $connfd-print(“Date: $todayn“); $connfd-print(“Se
20、rver: $servern“); $connfd- print(“Last-modified: $modifiedn“); $connfd- print(“Content-length: $filesizen“); $connfd-print(“Content-type: $filetypen“); print(“n“); # CRLF required by HTTP standard # end of else # end of procedure,Some Tiny issues,How would you serve static and dynamic content with G
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 20755 THEINTERNETLECTURE10WEBSERVICESIIIPPT

链接地址:http://www.mydoc123.com/p-380269.html