My understanding on CGI and enab…

My understanding on CGI and how to support it in http server
        - Ba Jian, 2008-12-30


CGI, the acronym of "common gatway interface", can be used by http server to extend
its ability for more active contents. Usually, an exe file or a script file can be
called by http server to provide dynamic web page. Hense, almost all compiling languages
can be used to write CGI application. And almost all script languages can be used to write
CGI applications.

The interface between CGI exe ( or script) file can be depicted as:

    1) webpage specified cgi executable ( or script) file,or you can directly specify it
          in url, like: http://my_server_name/cgi-bin/helloworld.exe

          for example, webpage: http://computer.bowenwang.com.cn/simpleform.htm
          if you checked the source code, you can find:

      <html>
        <body>
           <h1>A super-simple form</h1>
           <FORM METHOD=GET action="../cgi-bin/simpleform.cgi">
              Enter Your Name: <input name="Name" size=20 maxlength=50>
              <P><INPUT TYPE=submit value="Submit"> <INPUT TYPE=reset value="Reset">
           </FORM>
        </body>
      </html>

        Where:
              1.1) "<FORM METHOD=GET .." means submitted information will be directly encoded
                        in http url, and post to http server
              1.2) "<FORM METHOD=GET action="../cgi-bin/simpleform.cgi">
                        It specified CGI executable file: simpleform.cgi in ../cgi-bin/
                        Usually, cgi executable files are stored in ~/cgi-bin/ under home dir of http server
              1.3) <INPUT TYPE=submit value = "submit_me">, means a button will submit form data to http
                        server if you press it, and the button is with text "submit_me"
                        <INPUT TYPE=reset value="reset_me">, means a button will clear data in form if you press
                        it, and the button is with text "reset_me"
                 
                        This is pure http knowledge, please refer to:
                                    http://www.blabla.cn/z/w3c_html401/cover.html
                                    HTML教程:http://www.blabla.cn/html_tutorials/all_index.html
 
  2) Data transfer: from web-browser to http server
       
        As mentioned in point 1, all web browser following W3C http specification will put form data
        in http url(if METHOD=GET), and all web browser following W3C http specification will put
        form data in http body if METHOD=POST.

        For example, if you input "ba_jian_ni_hao_a" in the form presented by
                    http://computer.bowenwang.com.cn/simpleform.htm
        The final url posted to http server will be:
                    http://computer.bowenwang.com.cn/cgi-bin/ simpleform.cgi? Name=ba_jian_ni_hao_a
        The general format can be written as:
                    http://http_server/cgi-bin/cgi_exe?name1=value1&name2=value2&name3=value3&name4=value4&...
        Where,
                    name1=value1, name2=value2, name3=value3,name4=value4
        "?" is used to link cgi executable file and parameters
        "&" is used to link different parameters
        "%" means there are binary data after it 
 
  3) Data transfer: from http server to cgi executeable file

        CGI executable file can use STDIN to get coressponding parameters.
        Suppose c/C++ language is used, getchar() can be used to get paramters.

        By the way, special characters such as blank characters is encoded using %xx. "+"
        is encoded as ' '.
       

  4) Data transfer: from cgi executable file to http server
        Suppose C/C++ is used, printf() can be used to write output string.
        Http server can intercept STDOUT of CGI executable application using solution like pipe.
        Then all output of CGI executable file can be intercepted by http server.


  5) Data transfer: from http server to web-browser (client)
        Since http server can directly intercept CGI output, it can be simply forward
        to http client through http channel.


In conclusion, to write a simple http server which just supports static webpage
is quite simple, the http server just need map http request to local file.
In general, two days should be enough. To let your simple http server support CGI
is still simple, you just need to create communication channel between http
server and CGI executable files,and use http server to intercept output of
CGI executable file and forward to http client - web browser.

Actually, the principles of ASP/JSP/PHP are quite similar to CGI.The difference
is CGI executable file is run by OS, so you may find there are many many instance of
active processes of your cgi execuable file. However ASP/JSP/PHP is interpreted by engine
in http server.

====================================================================================================
References:
http://blog.ligj.eol.cn/115#
http://blog.csdn.net/ahcen001/archive/2006/08/31/1147618.aspx
http://initiative.yo2.cn/archives/360673
http://cn.blurtit.com/q420414.html
http://bloggermap.org/rss/readblog/5308
http://computer.bowenwang.com.cn/cgi4.htm


             


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值