Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fun Fun Project One1 Building Your Very Own Web Server.

Similar presentations

Presentation on theme: "Fun Fun Project One1 Building Your Very Own Web Server."— Presentation transcript:

1 Fun Fun Project One1 Building Your Very Own Web Server

2 Fun Fun Project One2 What is a Web Server? Program that understands the HTTP protocol and generates appropriate responses Clients “connect” to the machine Clients send a “request” Server reads request, generates “response” Client interprets response appropriately

3 Fun Fun Project One3 A Simplified Web Server Client asks for file Server finds appropriate file Server sends back a response header followed by the requested file’s data Server closes connection

4 Fun Fun Project One4 What Does “Connect” Mean? For all practical purposes, it looks like there’s data available via a file descriptor Stream of bytes Can be treated like any other file descriptor Not a FILE * (like stdio, stderr) Must use read() and write() system calls

5 Fun Fun Project One5 How Do You Identify Machines Domain names/IP address and ports implies a machine named and a default port of 80 Refers to local box ( is me) Port # is 8080 (used for this project) File is named index.html

6 Fun Fun Project One6 How Do You Identify Files? File name is specified in Request Message Server maps that name to a real file Mapping can be done in whichever way server wants For example, /~vivek/index.html is actually /n/fs/fac/vivek/public_html/index.html In your web server, you can choose your own

7 Fun Fun Project One7 What’s In A Request Message? GET /index.html HTTP/1.0\r\n Connection: Keep-Alive\r\n User-Agent: Mozilla/4.72 [en] (X11..)\r\n Host:\r\n Accept: image/gif, image/jpeg, */*\r\n Accept-Encoding: gzip\r\n Accept-Language: en\r\n Accept-Charset: iso-8859-1,*,utf-8\r\n \r\n

8 Fun Fun Project One8 What Do You Care About? GET /index.html HTTP/1.0 In particular, just index.html Assume “/” means “/index.html”

9 Fun Fun Project One9 What 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 listing A query (we don’t care about these) An invalid/nonexistent file

10 Fun Fun Project One10 What’s In A Response Message? HTTP/1.0 200 OK\r\n Date: blah-blah-blah\r\n Server: blah-blah-blah\r\n Content-Type: important\r\n Content-Length: 12345\r\n Last-Modified: blah-blah-blah\r\n \r\n Raw data

11 Fun Fun Project One11 What’s a Minimal Response? HTTP/1.0 200 OK\r\n Content-Type: stuff\r\n \r\n Data HTTP/1.0 302 Moved\r\n Location: newurl\r\n \r\n HTTP/1.0 404 Not Found\r\n \r\n But also Connection: close\r\n Content-Length: yyy\r\n

12 Fun Fun Project One12 Response when… File exists? Send it Directory without “/” suffix? Redirect Directory with index.html? Send it Directory with no index.html? List it For each list entry, add “/” if needed Failure(Not Found)? Send 404 Bad Request? Send 400

13 Fun Fun Project One13 How to Test Your Server? Use a browser(Netscape/IE) Use “wget” Support HTTP protocol create directory hierarchy for retrieving Include some big images

14 Fun Fun Project One14 More Test Cases What if Request Message is not send/received in one packet… The server must read all the Request Messages before it gives any response message Remember the double carriage return and line feed? Your web server must consider this! I’ll distribute more test programs later on, check http://

15 Fun Fun Project One15 What is Content-Type? text/html image/gif image/jpeg (Other types not needed for project 1.)

16 Fun Fun Project One16 Need more info? HTTP 1.1 Specification – RFC2068 HTTP 1.0 – RFC 1945 man pages man man –k blah read( ), write( ), open( ), close( )

17 Fun Fun Project One17 Why open instead of fopen? Compare fopen, fread, etc., with open, read, etc We’re dealing with functions closer to the OS – easier to use in some cases

18 Fun Fun Project One18 What’s a File Descriptor? Sort of like a FILE * It’s an integer provided by OS Used to represent a stream of bytes Can represent file or network connection Behavior is slightly different Especially when reading/writing network

19 Fun Fun Project One19 General Steps Setup, and then Get next connection If file, read from disk If directory, generate listing Send all to client Close connection, wait for next one(nonpersistent connection)

20 Fun Fun Project One20 What Am I Given? Setup function Makes server available for connections Accept function Gets a connection from a client File type function Tells you what kind of file, if it exists Tells you how many bytes if a regular file Directory listing functions Gives you the file names one at a time

21 Fun Fun Project One21 Help! I’m Lost! Don’t know HTML? Use Netscape composer to see what to do View page source for various pages Do “telnet 80” and issue the GET manually (need to add “Host:” header) Ask

22 Fun Fun Project One22 Why Are We Doing This? Infrastructure for future projects Some OS/Networking interaction It’s fun, and not too bad

23 Fun Fun Project One23 Wrap Up Thanks! Q&A session next week! Office Hour: Wed 4:30-5:30

Download ppt "Fun Fun Project One1 Building Your Very Own Web Server."

Similar presentations

Ads by Google