Fun Fun Project OneWhat is a Web Server?A Simplified Web ServerWhat Does “Connect” Mean?How Do You Identify MachinesHow Do You Identify Files?What’s In A Request Message?What Do You Care About?What Could They Want?What’s In A Response Message?What’s a Minimal Response?Response when…How to Test Your Server?More Test CasesWhat is Content-Type?Need more info?Why open instead of fopen?What’s a File Descriptor?General StepsWhat Am I Given?Help! I’m Lost!Why Are We Doing This?Wrap UpFun Fun Project One 1Fun Fun Project OneBuilding Your Very Own Web ServerFun Fun Project One 2What is a Web Server?Program that understands the HTTP protocol and generates appropriate responsesClients “connect” to the machineClients send a “request”Server reads request, generates “response”Client interprets response appropriatelyFun Fun Project One 3A Simplified Web ServerClient asks for fileServer finds appropriate fileServer sends back a response header followed by the requested file’s dataServer closes connectionFun Fun Project One 4What Does “Connect” Mean?For all practical purposes, it looks like there’s data available via a file descriptorStream of bytesCan be treated like any other file descriptorNot a FILE * (like stdio, stderr)Must use read() and write() system callsFun Fun Project One 5How Do You Identify MachinesDomain names/IP address and portshttp://www.cs.princeton.edu implies a machine named www.cs.princeton.edu and a default port of 80http://127.0.0.1:8080/index.htmlRefers to local box (127.0.0.1 is me)Port # is 8080 (used for this project)File is named index.htmlFun Fun Project One 6How Do You Identify Files?File name is specified in Request MessageServer maps that name to a real fileMapping can be done in whichever way server wantsFor example, /~vivek/index.html is actually /n/fs/fac/vivek/public_html/index.htmlIn your web server, you can choose your ownFun Fun Project One 7What’s In A Request Message?GET /index.html HTTP/1.0\r\nConnection: Keep-Alive\r\nUser-Agent: Mozilla/4.72 [en] (X11..)\r\nHost: 127.0.0.1:31415\r\nAccept: image/gif, image/jpeg, */*\r\nAccept-Encoding: gzip\r\nAccept-Language: en\r\nAccept-Charset: iso-8859-1,*,utf-8\r\n\r\nFun Fun Project One 8What Do You Care About?GET /index.html HTTP/1.0In particular, just index.htmlAssume “/” means “/index.html”Fun Fun Project One 9What Could They Want?An honest-to-goodness file (me.jpg)An indirect request for such a file (such as “/” meaning index.html)An implied directory with index (/home/vivek instead of /home/vivek/)Just a directory listingA query (we don’t care about these)An invalid/nonexistent fileFun Fun Project One 10What’s In A Response Message?HTTP/1.0 200 OK\r\nDate: blah-blah-blah\r\nServer: blah-blah-blah\r\nContent-Type: important\r\nContent-Length: 12345\r\nLast-Modified: blah-blah-blah\r\n\r\nRaw dataFun Fun Project One 11What’s a Minimal Response?HTTP/1.0 200 OK\r\nContent-Type: stuff\r\n\r\nDataHTTP/1.0 302 Moved\r\nLocation: newurl\r\n\r\nHTTP/1.0 404 Not Found\r\n\r\nBut alsoConnection: close\r\nContent-Length: yyy\r\nFun Fun Project One 12Response when… File exists? Send itDirectory without “/” suffix? RedirectDirectory with index.html? Send itDirectory with no index.html? List itFor each list entry, add “/” if neededFailure(Not Found)? Send 404Bad Request? Send 400Fun Fun Project One 13How to Test Your Server?Use a browser(Netscape/IE)Use “wget”Support HTTP protocolhttp://www.gnu.org/manual/wgetcreate directory hierarchy for retrievingInclude some big imagesFun Fun Project One 14More Test CasesWhat if Request Message is not send/received in one packet…The server must read all the Request Messages before it gives any response messageRemember the double carriage return and line feed?Your web server must consider this!I’ll distribute more test programs later on, check http://www.cs.princeton.edu/~yongwang/cos318Fun Fun Project One 15What is Content-Type?text/htmlimage/gifimage/jpeg(Other types not needed for project 1.)Fun Fun Project One 16Need more info? HTTP 1.1 Specification – RFC2068HTTP 1.0 – RFC 1945man pagesman manman –k blahread( ), write( ), open( ), close( )Fun Fun Project One 17Why open instead of fopen?Compare fopen, fread, etc., with open, read, etcWe’re dealing with functions closer to the OS – easier to use in some casesFun Fun Project One 18What’s a File Descriptor?Sort of like a FILE *It’s an integer provided by OSUsed to represent a stream of bytesCan represent file or network connectionBehavior is slightly differentEspecially when reading/writing networkFun Fun Project One 19General StepsSetup, and thenGet next connectionIf file, read from diskIf directory, generate listingSend all to clientClose connection, wait for next one(nonpersistent connection)Fun Fun Project One 20What Am I Given?Setup functionMakes server available for connectionsAccept functionGets a connection from a clientFile type functionTells you what kind of file, if it existsTells you how many bytes if a regular fileDirectory listing functionsGives you the file names one at a timeFun Fun Project One 21Help! I’m Lost!Don’t know HTML? Use Netscape composer to see what to doView page source for various pagesDo “telnet www.domain.com 80” and issue the GET manually (need to add “Host: www.domain.com” header)AskFun Fun Project One 22Why Are We Doing This?Infrastructure for future projectsSome OS/Networking interactionIt’s fun, and not too badFun Fun Project One 23Wrap UpThanks!Q&A session next week!Office Hour: Wed
View Full Document