Building Your Very Own Web Server - PowerPoint PPT Presentation

About This Presentation
Title:

Building Your Very Own Web Server

Description:

Fun Fun Project One. 3. A Simplified Web Server ... Fun Fun Project One. 4. What ... Fun Fun Project One. 6. How Do You Identify Files? File name is ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 22
Provided by: valued85
Category:
Tags: browser | building | closes | is | server | web | what

less

Transcript and Presenter's Notes

Title: Building Your Very Own Web Server


1
Fun Fun Project One
  • Building Your Very Own Web Server

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

3
A Simplified Web Server
  • Client asks for file
  • Server finds appropriate file
  • Server sends back a response header followed by
    the files data
  • Server closes connection

4
What Does Connect Mean?
  • For all practical purposes, it looks like theres
    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
How Do You Identify Machines
  • Names or numbers and ports
  • http//www.domain.com implies a machine named
    www.domain.com and a default port of 80
  • http//127.0.0.131415/index.html
  • Refers to current box (127.0.0.1 is me)
  • Port is 31415 (used for this project)
  • File is named index.html

6
How Do You Identify Files?
  • File name is specified in request
  • Server maps that name to a real file
  • Mapping can be whatever server wants
  • For example, /vivek/index.html is really
    /n/fs/fac/vivek/public_html/index.html

7
Whats In A Request?
  • GET /index.html HTTP/1.0\r\n
  • Connection Keep-Alive\r\n
  • User-Agent Mozilla/4.72 en (X11..)\r\n
  • Host 127.0.0.131415\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
What Do You Care About?
  • GET /index.html HTTP/1.0
  • In particular, just index.html
  • Assume / means /index.html

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 dont care about these)
  • An invalid/nonexistent file

10
Whats In A Response?
  • HTTP/1.0 200 OK\r\n
  • Date blah-blah-blah\r\n
  • Server blah-blah-blah\r\n
  • Content-Type important\r\n
  • Last-Modified blah-blah-blah\r\n
  • \r\n
  • Raw data

11
Whats 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
How Do You Decide?
  • 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? Send 404

13
How Do You Test It?
  • Use a browser
  • Use wget or create hierarchy
  • Include some big images

14
What is Content-Type?
  • text/html
  • image/gif
  • image/jpeg
  • Others not needed

15
Where Can I Find More?
  • Google HTTP 1.1 Specification - painful
  • man pages
  • man man
  • man k blah
  • read( ), write( ), open( ), close( )

16
Why open instead of fopen?
  • Compare fopen, fread, etc., with open, read, etc
  • Were dealing with functions closer to the OS
    easier to use in some cases
  • Practice

17
Whats a File Descriptor?
  • Sort of like a FILE
  • Its 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

18
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

19
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

20
Help! Im Lost!
  • Dont 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

21
Why Are We Doing This?
  • Infrastructure for future projects
  • Some OS/Networking interaction
  • Its fun, and not too bad
Write a Comment
User Comments (0)
About PowerShow.com