build web server
DESCRIPTION
TRANSCRIPT
Learn in 20 MinBuild a Useless Web Server
Before Course● You need to know
○ Coding skill○ Socket○ Google
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: 正妹
Request● Request from browser or something else
○ Method: GET/POST/ ...○ Target: /favicon.ico or what you want○ Options:
■ config, cookie, session, ...
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
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, ...
Example● Sample code...
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
So, Implement It...● Sample code...
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.
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
So, Implement It...
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
Extra Functionality● Rewrite engine● Symlink● more and more
○ As many as possibles you can implement and twist its original concept
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