Learn in 20 Min
Build 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 rn, 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

Build web server

  • 1.
    Learn in 20Min Build a Useless Web Server
  • 2.
    Before Course ● Youneed to know ○ Coding skill ○ Socket ○ Google
  • 3.
    REST (Representational StateTransfer) ● 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: 正妹
  • 4.
    Request ● Request frombrowser or something else ○ Method: GET/POST/ ... ○ Target: /favicon.ico or what you want ○ Options: ■ config, cookie, session, ...
  • 5.
    Response ● Also, responsefollows REST ○ Return the status code ○ Specified the content type: plain, html, or else ○ An empty blank line ○ Content for the web page
  • 6.
    Basically ● Create asocket 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, ...
  • 7.
  • 8.
    Parser ● Simple plainparser (really?) ○ Split by rn, 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
  • 9.
  • 10.
    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.
  • 11.
    Web 2.0 ● Onlyget 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
  • 12.
  • 13.
    Web 2.0 ● Onlyget 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
  • 14.
    Extra Functionality ● Rewriteengine ● Symlink ● more and more ○ As many as possibles you can implement and twist its original concept
  • 15.
    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