Axil S/E kdk015
				  1997. 4. 16
				  Kim Do Kyung

SUBJECT: CGI(Common Gateway Interface)

CONTENTS:

¡Ú  CGI(Common Gateway Interface)

CGI(Common Gateway Interface)´Â Á¤º¸¸¦ Á¦°øÇÏ´Â ¼­¹ö¸¦ ÅëÇؼ­ ¿ÜºÎ ÇÁ·Î±×·¥À̳ª 
°ÔÀÌÆ®¿þÀ̸¦ ½ÇÇà½ÃÅ°±â À§ÇÑ ÀÎÅÍÆäÀ̽º ÇÁ·Î±×·¥À» ¸»ÇÑ´Ù. ÀϹÝÀûÀ¸·Î ÀÌ°ÍÀ» Áö
¿øÇÏ´Â Á¤º¸ Á¦°ø ¼­¹ö´Â HTTP¼­¹ö°¡ µÈ´Ù.

	¡Ø CGI ¼Ò°³

   * Gateway´Â ¹«¾ùÀΰ¡?
        o Gateway¶ó´Â °ÍÀº ÇϳªÀÇ ½ÇÁ¦ÀûÀÎ ÇÁ·Î±×·¥À¸·Î¼­ Á¤º¸¿äûÀ»
          °ü¸®ÇÏ°í ±×¿¡ µû¸¥ ÀûÀýÇÑ ¹®¼­µéÀ» »ý¼º ¹× Á¦°øÇÑ´Ù.
          CGI´Â ¼­¹ö°¡ Á¦°øÇÏ´Â Á¤º¸¸¦ Client°¡ ÀÐÀ» ¼ö ÀÖ´Â ÇüÅ·Î
          ÀüȯÇÑ´Ù. Áï, ÀÌ µÑ »çÀÌÀDZ³·® ¿ªÇÒÀ» ÇÑ´Ù°í ÇÒ ¼ö ÀÖ´Ù.
        o Gateway´Â ¿©·¯°¡Áö ¸ñÀû¿¡ ¾²ÀÏ ¼ö ÀÖ´Ù.
          ±× Áß °¡Àå ´ëÇ¥ÀûÀÎ °ÍÀÌ HTTP¸¦ ¿ä±¸ÇÏ´Â ISINDEX¿Í FROMÀ» ´Ù·ç´Â
          ÀÏÀÌ´Ù.

	¡Ø CGIÀÇ ¸î °¡Áö È°¿ë ¿¹

        o ¸Å´º¾óÀ» HTML·Î º¯È¯½ÃÄѼ­ ±× °á°ú¸¦ Client¿¡°Ô Á¦°øÇÑ´Ù.
        o WAIS ¹× archie database¿ÍÀÇ ÀÎÅÍÆäÀ̽º °á°ú¸¦ HTML·Î º¯È¯½ÃÄѼ­ ±×
          °á°ú¸¦ Client·Î º¸³½´Ù.

	¡Ø Gateway ProgramÀ̶õ Á¤È®È÷ ¹«¾ùÀΰ¡?

   * Gateway Program(¶Ç´Â script)Àº µ¶¸³ÀûÀ¸·Î ½ÇÇà µÉ ¼ö ÀÖ´Â ½ÇÇà
     ÇÁ·Î±×·¥À» ¸»ÇÑ´Ù. ¿Ö³ÄÇÏ¸é ´Ù¾çÇÑ Á¤º¸Á¦°ø ¼­¹öÇÏ¿¡¼­ ½ÇÇà µÉ ¼ö ÀÖ°Ô
     Çϱâ À§Çؼ­ÀÌ´Ù.

	¡Ø Gateway¸¦ ¸¸µé±â À§Çؼ­ ¾î¶°ÇÑ ¾ð¾î¸¦ »ç¿ëÇϴ°¡?

* Gateway¸¦ ¸¸µé±â À§ÇØ ÇÊ¿äÇÑ ¾ð¾î´Â ½ÇÇàÈ­ÀÏÀ» »ý¼ºÇÒ ¼ö ÀÖ´Â ¸ðµç ¾ð¾î¸¦
Æ÷ÇÔÇÑ´Ù.

* -C/C++
* -PERL
* -TCL
* -Bourne Shell
* -C Shell
* - ..
* - ..

	¡Ø CGI´Â ´©°¡ ¸¸µé¾ú³ª?

HTTP ¼­¹öÀÇ ÁÖ¿äÇÑ ÀúÀڵ鿡 ÀÇÇؼ­ ÀÛ¼ºµÇ¾ú´Ù.

        Tony Sanders sanders@bsdi.com
        Ari Luotonen luotonen@ptsun00.cern.ch
        George Phillips phillps@cs.ubc.ca
        John Franks john@math.nwu.edu


	¡Ø CGI ÇÁ·Î±×·¥ °³¿ä

¼­¹ö·ÎºÎÅÍ gateway ÇÁ·Î±×·¥À¸·ÎÀÇ ÀμöÀü´ÞÀº ¾î¶»°Ô ÀÌ·ç¾îÁö³ª?

Client°¡ URLÀ» ¿äûÇÒ ¶§¸¶´Ù ¼­¹ö´Â ±×¿¡ ÇØ´çÇÏ´Â gateway¸¦ ½ÇÇà½ÃŲ´Ù.
±×¸®°í ±× Ãâ·ÂÀº Á÷Á¢ ȤÀº °£Á¢ÀûÀ¸·Î client¿¡°Ô·Î Àü´ÞµÈ´Ù. CGI´Â programÀ¸·Î
Àμö¸¦ Àü´ÞÇϱâ À§Çؼ­ ȯ°æº¯¼ö¸¦ ÀÌ¿ëÇÑ´Ù.
±× ´ëÇ¥ÀûÀÎ µÎ°¡Áö º¯¼ö´Â ´ÙÀ½°ú °°´Ù.

   * QUERY_STRING
        o ÀÌ°ÍÀº gateway¸¦ Á¢±ÙÇϱâ À§Çؼ­ ÀÌ¿ëµÈ URL ¼ÓÀÇ Ã¹¹ø°
          ¹°À½Ç¥¸¦(?) µÚ¿¡ ¿À´Â °ÍµéÀ» Á¤ÀÇÇϱâ À§Çؼ­ ¾²ÀδÙ. ÀÌ·¯ÇÑ
          Á¤º¸µéÀº HTML ISINDEX ¹®¼­³ª HTML form(getÀ» ¾µ¶§)¿¡ ÀÇÇؼ­
          ÷°¡µÈ´Ù.
   * PATH_INFO
        o CGI´Â URL¿¡ ÷°¡µÈ extra Á¤º¸¸¦ Çã¿ëÇϴµ¥, ÀÌ°ÍÀº ½ºÅ©¸³Æ®·Î
          extra context-specific Á¤º¸¸¦ Àü´ÞÇϱâ À§Çؼ­ »ç¿ëµÈ´Ù. º¸Åë,URLÀÇ
          gateway path µÚ¿¡ µû¶ó°¡°Ô µÇ¸ç ¼­¹ö¿¡ ÀÇÇؼ­ decodeµÇÁö´Â ¾Ê´Â´Ù.

	¡Ø Gateway¿¡¼­ client·ÎÀÇ ¹®¼­ Àü´ÞÀº ¾î¶»°Ô ÀÌ·ç¾îÁö³ª?

CGI ÇÁ·Î±×·¥Àº À̹ÌÁö³ª HTML ¹®¼­,ÀϹÝÀûÀÎ ¹®¼­ ¶Ç´Â audio clip°ú °°Àº
°ÍµéÀ» µÇµ¹·Á ÁÖ°Ô µÈ´Ù. ¶ÇÇÑ ´Ù¸¥ ¹®¼­¿¡ ´ëÇÑ reference¸¦ µ¹·Á ÁÙ ¼ö µµ
ÀÖ´Ù. ÀÌ·¯ÇÑ °Íµé¿¡ ´ëÇÑ Á¤º¸¸¦ client¿¡°Ô ¾Ë·ÁÁÖ±â À§Çؼ­ CGI´Â Ãâ·ÂÀÇ ¾Õ
ºÎºÐ¿¡ ªÀº header ¸¦ ³õ°ÔµÈ´Ù. ÀÌ°ÍÀ» ¸¶ÀÓÀ̶ó ÇÏ°í, ÀÌ¿Í°°Àº ¹®¼­¸¦ Á÷Á¢ ¸®ÅÏÇÒ ¶§¿Í reference¸¦
¸®ÅÏÇÒ ¶§¸¶´Ù °¢°¢ÀÇ °æ¿ì¿¡ headerÀÇ Çü½ÄÀº ´ÙÀ½°ú °°´Ù.

   * MIME typeÀÇ ¿ÏÀüÇÑ ¹®¼­ÀÇ °æ¿ì:
     MIME type¿¡ °üÇÑ Á¤º¸¸¦ ´ÙÀ½°ú °°ÀÌ ¸í½ÃÇÑ´Ù.


             Content-type : type/subtype


   * ´Ù¸¥ ¹®¼­¿¡ ´ëÇÑ referenceÀÏ °æ¿ì:
     ÇöÀç ¼­¹ö ³»ÀÇ ¹®¼­ÀÏ °æ¿ì¿¡´Â ´ÙÀ½°ú °°ÀÌ ÇÒ ¼öµµ ÀÖ´Ù.

            Location: /dir1/dir2/myfile.html


   * ¸¸ÀÏ °íÆÛ¼­¹ö ³»ÀÇ ¹®¼­¸¦ ÂüÁ¶ÇÒ °æ¿ì¿¡´Â ´ÙÀ½°ú °°ÀÌ ÇÏ¸é µÈ´Ù.

            Location: gopher://httprules.foobar.org/0


     ÀÌ °æ¿ì Ŭ¶óÀ̾ðÆ®´Â ÀÌ°ÍÀ» decodeÇÏ¿© URLÀ» Á÷Á¢ °¡Á®¿Ã °ÍÀÌ´Ù.
     HeaderÀÇ ´ÙÀ½ ¶óÀÎÀº ¹Ýµå½Ã °ø¹é¶óÀÎÀ̾î¾ß ÇÑ´Ù. ÀÌ°ÍÀº headerÀÇ ³¡À»
     ¸í½ÃÇϱâ À§Çؼ­ ÀÌ´Ù.
     -----------------------------------------------------------------------

     CGI·Î FORMÀ» Áö¿øÇϱâ

     CGI´Â Form data¸¦ ¾î¶»°Ô ³Ñ°Ü ¹Þ´Â°¡?

     ´ÙÀ½ °¢ ¹æ¹ý¿¡ µû¶ó¼­ ´Ù¸¥ ¹æ½ÄÀÌ Àû¿ëµÈ´Ù.
        o GET method ȯ°æº¯¼ö QUERY_STRINGÀ» ÅëÇؼ­ Àü´ÞµÈ´Ù.
        o POST method stdinÀ¸·Î ÀÔ·ÂÇÑ´Ù. EOF¸¦ ¸í½ÃÇÏÁö ¾ÊÀ½À¸·Î º¯¼ö
          CONTENT_LENGTH¸¦ »ç¿ëÇØ¾ß ÇÑ´Ù.



	¡Ø SSI(Server Side Include)

  SSI´Â ¿©·¯ºÐÀÇ À¥¼­¹öÀÇ °ü¸®ÀÚ°¡  È°¼ºÈ­(enabled) ½ÃÄѾ߸¸ »ç¿ëÇÒ ¼ö ÀÖ´Ù. ±×
  ¸®°í, ÀÌ°ÍÀº ¼­¹ö°¡ ¸Å SSI ¹®¼­¿¡ ´ëÇØ ´õ¿í  ¿­½ÉÈ÷ µ¿ÀÛÇÒ °ÍÀ» ¿ä±¸ÇÑ´Ù. (SSI
  ¸¦ ¼­¹ö°¡ Áö¿øÇÒ °æ¿ì, ¼­¹ö´Â ¸Å ¶óÀο¡ SSI ¸í·É¾î°¡ ÀÖ´ÂÁö È®ÀÎÇØ¾ß ÇÑ´Ù. µû
  ¶ó¼­ ¼­¹ö¿¡ °úºÎÇϸ¦ ÁÖ°Ô µÈ´Ù.)

  SSIÀÇ Çü½ÄÀº ÁÖ¼® ÅÂ±×¿Í ÇüÅ°¡ ºñ½ÁÇÏ´Ù.

     HTML ű×ÀÇ ÁÖ¼®
      

     SSI ¸í·É¾î Çü½Ä
      

     SSI ¸í·É¾îÀÇ Á¾·ù
      -------------------------------------------------------------------
      config     ½Ã°£, Å©±â ¿Í ¿¡·¯ ¸Þ½ÃÁöÀÇ Æ÷¸ËÀ» ¼³Á¤ÇÑ´Ù.
      echo       SSI º¯¼öÀÇ °ªÀ» À¥ ÆäÀÌÁö¿¡ »ðÀÔÇÑ´Ù.
      exec       ½Ã½ºÅÛ ¸í·É¾î³ª CGI ÇÁ·Î±×·¥À» ½ÇÇà½ÃÅ°°í ±×°ÍÀÇ °á°ú¸¦ 
                 À¥ ÆäÀÌÁö¿¡ »ðÀÔÇÑ´Ù.
      fsize      ÆÄÀÏÀÇ Å©±â¸¦ À¥ ¹®¼­¿¡ »ðÀÔÇÑ´Ù.
      flastmod   ÆÄÀÏÀÌ º¯°æµÈ ÃÖÁ¾ ½Ã°£À» À¥ ¹®¼­¿¡ »ðÀÔÇÑ´Ù.
      include    ¸í½ÃÇÑ HTML ÆÄÀÏÀ» À¥ ¹®¼­¿¡ »ðÀÔÇÑ´Ù.
      -------------------------------------------------------------------

¡Ü config

     config¸í·É¾îÀÇ argument¿Í argument °ª
      errmsg     Any ASCII ÅؽºÆ®
      sizefmt    Bytes or abbrev
      timefmt    Any of the date codes 

     config¸í·É¾îÀÇ °¢ argumentÀÇ »ç¿ë¿¹
      
       
      
      
      

     À¥ ÆäÀÌÁö¿¡ ¾î¶»°Ô ½Ã°£À» Ãâ·ÂÇÒ °ÍÀΰ¡¿¡ °üÇÑ date ÄÚµå
      %a      »ý·«ÇÑ ¿äÀÏ À̸§
      %A      »ý·«ÇÏÁö ¾ÊÀº ¿äÀÏ À̸§
      %b      »ý·«µÈ ´Þ À̸§
      %B      »ý·«µÇÁö ¾ÊÀº ´Þ À̸§
      %c      ³¯Â¥¿Í ½Ã°£ Ç¥½Ã
      %d      ³¯Â¥(½ÊÁø¼ö)
      %m      ´Þ(½ÊÁø¼ö)
      %U      ÀÏ¿äÀÏÀ» óÀ½À¸·Î Çؼ­ ¿äÀÏ ¼ö(½ÊÁø¼ö)
      %W      ¿ù¿äÀÏÀ» óÀ½À¸·Î Çؼ­ ¿äÀÏ ¼ö(½ÊÁø¼ö)
      %x      ³¯Â¥ Ç¥½Ã(½Ã°£Àº ¾ø´Â)
      %y      100´ÜÀ§ ÀÌ»óÀº »ý·«ÇÑ ³âµµ(½ÊÁø¼ö)
      %Y      100´ÜÀ§ ÀÌ»óÀ» Æ÷ÇÔÇÑ ³âµµ(½ÊÁø¼ö)
      %H      24½Ã°£Á¦¿¡¼­ÀÇ ½Ã°£(00-23)
      %I      12½Ã°£Á¦¿¡¼­ÀÇ ½Ã°£(01-12)
      %j      ³¯¼ö(001-366)
      %M      ºÐ(½ÊÁø¼ö)
      %p      a.m. ¶Ç´Â p.m. ÁÖ¾îÁø ½Ã°£¿¡ µû¶ó
      %S      ÃÊ(½ÊÁø¼ö)
      %X      ½Ã°£ Ç¥½Ã(³¯Â¥´Â ¾ø´Â)
      %Z      ½Ã°£´ë(Time Zone), À̸§ ¶Ç´Â »ý·«

¡Ü echo

     echo ¸í·É¾îÀÇ º¯¼ö¿Í ±× ÀǹÌ

      ------------------------------------------------------------------
      DATE_LOCAL     ÇöÀçÀÇ ³¯Â¥¿Í ½Ã°£, ÀÌ°ÍÀÇ Ãâ·Â ¾ç½ÄÀº SSIÀÇ config 
                     timefmt¿¡ ÀÇÁ¸ÇÑ´Ù.
      DATE_GMT       ±×¸®´ÏÄ¡ õ¹®´ëÀÇ ÇöÀçÀÇ ³¯Â¥¿Í ½Ã°£
      DOCUMENT_NAME  ÆÄÀÏ À̸§
      DOCUMENT_URI   ÆÄÀÏÀÇ °æ·Î¿Í À̸§
      LAST_MODIFIED  ÃÖÁ¾ º¯°æµÈ ³¯Â¥¿Í ½Ã°£
      ------------------------------------------------------------------

     echo¸í·É¾îÀÇ °¢ argumentÀÇ »ç¿ë¿¹

      The  file was last modified.
      .
¡Ü exec exec¸í·É¾îÀÇ »ç¿ë¿¹ execÀÇ argument·Î ½ÇÇà½ÃÅ°´Â ÇÁ·Î±×·¥Àº Unis ShellÀÌ µÉ ¼öµµ ÀÖ´Ù. À̶§´Â ´õ ¿í °­·ÂÇÑ À§·ÂÀ» ¹ßÈÖÇÒ °ÍÀÌ´Ù. ÇÏÁö¸¸ CGI¸¦ ½ÇÇà½Ãų ¼ö ÀÖ´Ù¸é ´õ¿í °­·ÂÇÒ °ÍÀÌ´Ù. exec´Â ÀÌ°ÍÀ» Áö¿øÇÑ´Ù. exec¿Í cgiÀÇ »ç¿ë Çü½Ä ¡Ü fsize À¥ ÆäÀÌÁö¿¡ È­ÀÏÀÇ Å©±â¸¦ Áý¾î ³ÖÀ» ¶§ »ç¿ëµÈ´Ù. ¡Ü flastmod ¿©±â¼­ Àá½Ã »ó½ÄÇϳª¸¦ ¤°í ³Ñ¾î°¡¸é, flastmod¶õ À̸§Àº last¿Í modified¸¦ ÇÕ Ãļ­ ¸¸µç À̸§ÀÌ´Ù. ƯÈ÷ Á¢µÎ¾î f´Â file¿¡ ´ëÇÑ ¿¬»êÀ» ÀǹÌÇÑ´Ù. ÀÌ ¸í·É¾î´Â ¿ì¸®ÀÇ À¥ ÆäÀÌÁö°¡ °¡Àå ÃÖ±Ù¿¡ ¾÷µ¥ÀÌÆ®µÈ ³¯Â¥¸¦ ÀÚµ¿À¸·Î ¾Ë¸± ¼ö ÀÖ´Â ¹æ¹ý À» Á¦°øÇÑ´Ù. ¡Ü include include¸í·É¾îÀÇ argument¿Í argument °ª file Çö µð·ºÅ丮³ª Çö µð·ºÅ丮ÀÇ ¼­ºêµð·ºÅ丮¿¡¼­ ½ÃÀÛÇÏ´Â ¾î¶² °æ·Î³ª È­ÀÏ virtual ¼­¹öÀÇ root¿¡¼­ ½ÃÀÛÇÏ´Â ¾î¶² °æ·Î³ª È­ÀÏ include¸í·É¾îÀÇ °¢ argumentÀÇ »ç¿ë¿¹ À§ÀÇ Ç¥¿¡ ÀÖµíÀÌ argument°¡ fileÀÏ °æ¿ì Çö µð·ºÅ丮³ª Çö µð·ºÅ丮ÀÇ ¼­ºêµð·º Å丮°¡ µÇ¾î¾ß ÇÑ´Ù. µû¶ó¼­, °æ·ÎÀÇ Ã³À½Àº /, ..ÀÌ µÇ¾î¼­´Â ¾ÈµÈ´Ù. ¡Ú PERL Larry Wall¿¡ ÀÇÇØ ¸¸µé¾îÁø ÀÌ ¾ð¾î´Â Practical Extraction and Report Language ÀÇ ¾àÀÚ·Î À̸§¿¡¼­ ¾Ë ¼ö ÀÖµíÀÌ ¿ø·¡ µ¥ÀÌŸ¸¦ ó¸®ÇÏ°í µ¥ÀÌŸ·ÎºÎÅÍ º¸°í¼­¸¦ »ý¼ºÇϱâ À§ÇØ ¸¸µé¾î Á³´Ù. ¶ÇÇÑ, ÀÌ ¾ð¾îÀÇ ÃÖ´ë ÀåÁ¡À¸·Î PERL·Î Â¥¿©Áø ÇÁ·Î±×·¥ Àº °ÅÀÇ º°´Ù¸¥ ¼öÁ¤¾øÀÌ ´Ù¸¥ Ç÷§Æû¿¡¼­ ½ÇÇàÀÌ °¡´ÉÇÏ´Ù´Â °ÍÀÌ´Ù. ¡Ø Hello World Code #! /usr/local/bin/perl print "Content-type: text/html\n\n"; <= HTML ¹®¼­ÀÓÀ» ¾Ë·ÁÁÜ <ÁÖÀÇ>¹Ýµå½Ã \n\nÀ̾î¾ß ÇÑ´Ù. print <<`END` <= END°¡ Àִ°÷±îÁö Ãâ·ÂÇ϶ó My First CGI

My First CGI Program


Hello World? END PERLÀÇ ±âº» ÄÚµåÀÌ´Ù. ¡Ø Summary of Regular Expression ¡Ü set of pattern /abc/ Matches abc anywhere within the string. /^abc/ Matches abc at the beginning of the string. /abc$/ Matches abc at the end of the string. /a|b/ Matches either a or b Can also be used with words (i.e., /perl|tcl/) /ab{2,4}c/ Matches an a followed by 2-4 b's, followed by c. If the second number is omitted, such as /ab{2,}c/, the expression will match two or more b's. /ab*c/ Matches an a followed by zero or more b's, followed by c Expressions are greedy-it will match as many as possible. Same as /ab{0,}c/ /ab+c/ Matches an a followed by one or more b's followed by c Same as /ab{1,}c/ /ab?c/ Matches an a followed by an optional b followed by c Same as /ab{0,1}c/. This has a different meaning in Perl 5. In Perl 5, the expression: /ab*?c/ matches an a followed by as few b's as possible(non-greedy) /./ Matches any single character except a newline(\n) /p..1/ matches a p followed by any two characters, followed by 1, so it will match such strings as perl, pall, pdgl, p3gl, etc. /[abc]/ A character class-matches any one of the three characters listed. A pattern of /[abc]+/ matches strings such as abcab, acbc, abbac, aaa, abcacbac, ccc, etc. /\d/ Matches a digit Same as /[0-9]/ Multipliers can be used (/\d+/ matches one or more digits) /\w/ Matches a character classified as a word Same as /[a-zA-Z0-9_]/ /\s/ Matches a character classified as whitespace Same as /[ \r\t\n\f]/ /\b/ Matches a word boundary or a backspace /test\b/ matches test, but not testing However, \b matches a backspace character inside a class (i.e., [\b]) /[^abc]/ Matches a character that is not in the class /[^abc]+/ will match such strings as hello, test, perl, etc. /\D/ Matches a character that is not a digit Same as /[^0-9]/ /\W/ Matches a character that is not a word Same as /[^a-zA-Z0-9_]/ /\S/ Matches a character that is not whitespace Same as /[^ \r\t\n\f]/ /\B/ Requires that there is no word boundary /hello\B/ matches hello, but not hello there /\*/ Matches the * character, Use the \ character to escape characters that have significance in a regular expression. /(abc)/ Matches abc anywhere within the string, but the parentheses act as memory, storing abc in the variables $1, $2, $3. Example 1: /name=(.*)/ will store zero or more characters after name= in variable $1. Example 2: /name=(.*)&user=\1/ will store zero or more characters after name= in $1. Then, Perl will replace \1 with the value in $1, and check to see if the pattern matches. Example 3: /name=([^&]*) will store zero or more characters after name= but before the & character in variable $1. Example 4: /name=([^&]+)&age=(.*)$/ will store one or more characters after name= but before & in $1. It then matches the & character. All characters after age= but before the end of the line are stored in $2. /abc/i Ignores case. Matches either abc, Abc, ABC, aBc, aBC, etc.