build web server

15
Learn in 20 Min Build a Useless Web Server

Upload: -

Post on 15-Jan-2015

616 views

Category:

Design


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Build web server

Learn in 20 MinBuild a Useless Web Server

Page 2: Build web server

Before Course● You need to know

○ Coding skill○ Socket○ Google

Page 3: Build web server

REST (Representational State Transfer)

● Web Server is based on REST○ Who: Assign the particular web page○ Want: Do the particular method○ What: Ask the particular resource

● www.example.com/image.html?img="正妹"○ Who: image.html○ What: GET method○ What: 正妹

Page 4: Build web server

Request● Request from browser or something else

○ Method: GET/POST/ ...○ Target: /favicon.ico or what you want○ Options:

■ config, cookie, session, ...

Page 5: Build web server

Response● Also, response follows REST

○ Return the status code○ Specified the content type: plain, html, or else○ An empty blank line○ Content for the web page

Page 6: Build web server

Basically● Create a socket and listen request, parse

○ Find out the user's target resource.○ Find out the method user's want.○ Capture the content if need.

● Based on above information, return○ Static file: CSS, js or image○ CGI result: .cgi .php or some else I unknow○ Else: 403, 404, 500, ...

Page 7: Build web server

Example● Sample code...

Page 8: Build web server

Parser● Simple plain parser (really?)

○ Split by \r\n, and find out the blank line○ Line[0] contains the method, resource and protocol○ Line[1]~line[m] contains options information○ Line[m+1]~ contains content

● For example: Get file content

Page 9: Build web server

So, Implement It...● Sample code...

Page 10: Build web server

But Something Bad...● In this case

○ We return anything from request...● But we have a smart and kind client agent

○ Request 127.0.0.1/../../../index.html will transfer to 127.0.0.1/index.html

○ We must believe the people as good as VXXXX.

Page 11: Build web server

Web 2.0● Only get the static web page is so boring

○ We must interactive with other○ Calling HCG CGI is the simple way

● CGI is Common Gateway Interface○ In the easiest way to explain: executable binary○ Execute and return the web page○ Usually store in cgi-bin/ folder○ Call as 127.0.0.1/cgi-bin/index.cgi

Page 12: Build web server

So, Implement It...

Page 13: Build web server

Web 2.0● Only get the static web page is so boring as

cmj○ We must interactive with other○ Calling HCG CGI is the simple way

● CGI is Common Gateway Interface○ In the easiest way to explain: executable binary○ Usually store in cgi-bin/ folder○ Call as 127.0.0.1/cgi-bin/index.cgi

● But Someone say○ We are as good as ?F○ We will never link to 127.0.0.1/cgi-bin/../../../../bin/ls

to list the files and folders

Page 14: Build web server

Extra Functionality● Rewrite engine● Symlink● more and more

○ As many as possibles you can implement and twist its original concept

Page 15: Build web server

Reference○ http://www.ics.uci.

edu/~fielding/pubs/dissertation/top.htm○ http://en.wikipedia.

org/wiki/Common_Gateway_Interface○ http://en.wikipedia.org/wiki/Rewrite_engine