Upload
lindsay-clementson
View
218
Download
5
Tags:
Embed Size (px)
Citation preview
Fun Fun Project One 1
Fun Fun Project One
Building Your Very Own Web Server
Fun Fun Project One 2
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
Fun Fun Project One 3
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
Fun Fun Project One 4
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
Fun Fun Project One 5
How Do You Identify Machines
Domain names/IP address and ports http://www.cs.princeton.edu implies a
machine named www.cs.princeton.edu and a default port of 80
http://127.0.0.1:8080/index.html Refers to local box (127.0.0.1 is me) Port # is 8080 (used for this project) File is named index.html
Fun Fun Project One 6
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
Fun Fun Project One 7
What’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\n
Fun Fun Project One 8
What Do You Care About?
GET /index.html HTTP/1.0
In particular, just index.html
Assume “/” means “/index.html”
Fun Fun Project One 9
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
Fun Fun Project One 10
What’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 data
Fun Fun Project One 11
What’s a Minimal Response?
HTTP/1.0 200 OK\r\nContent-Type: stuff\r\n\r\nData
HTTP/1.0 302 Moved\r\nLocation: newurl\r\n\r\n
HTTP/1.0 404 Not Found\r\n
\r\n
But alsoConnection: close\r\nContent-Length: yyy\r\n
Fun Fun Project One 12
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
Fun Fun Project One 13
How to Test Your Server?
Use a browser(Netscape/IE) Use “wget”
Support HTTP protocol http://www.gnu.org/manual/wget create directory hierarchy for
retrieving Include some big images
Fun Fun Project One 14
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://www.cs.princeton.edu/~yongwang/cos318
Fun Fun Project One 15
What is Content-Type?
text/htmlimage/gifimage/jpeg
(Other types not needed for project 1.)
Fun Fun Project One 16
Need more info?
HTTP 1.1 Specification – RFC2068HTTP 1.0 – RFC 1945man pagesman manman –k blahread( ), write( ), open( ), close( )
Fun Fun Project One 17
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
Fun Fun Project One 18
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
Fun Fun Project One 19
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)
Fun Fun Project One 20
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
Fun Fun Project One 21
Help! I’m Lost!
Don’t know HTML? Use Netscape composer to see what
to do View page source for various pages Do “telnet www.domain.com 80” and
issue the GET manually (need to add “Host: www.domain.com” header)
Ask
Fun Fun Project One 22
Why Are We Doing This?
Infrastructure for future projects Some OS/Networking interaction It’s fun, and not too bad
Fun Fun Project One 23
Wrap Up
Thanks! Q&A session next week! Office Hour: Wed 4:30-5:30