1k likes | 1.11k Views
CGI & HTML forms. -05-. CGI. Common Gateway Interface. A web server is only a pipe between user-agents. and content – it does not generate content. CGI. Common Gateway Interface. A web server is only a pipe between user-agents. and content – it does not generate content.
E N D
CGI & HTML forms -05-
CGI Common Gateway Interface • A web server is only a pipe between user-agents • and content – it does not generate content
CGI Common Gateway Interface • A web server is only a pipe between user-agents • and content – it does not generate content • Works well with static content (e.g. html files)
CGI Common Gateway Interface • A web server is only a pipe between user-agents • and content – it does not generate content • Works well with static content, but what about • dynamic content?
CGI Common Gateway Interface • A web server is only a pipe between user-agents • and content – it does not generate content • Works well with static content, but what about • dynamic content? • A web server needs to delegate content generation • to third party applications
CGI Common Gateway Interface • A web server is only a pipe between user-agents • and content – it does not generate content • Works well with static content, but what about • dynamic content? • A web server needs to delegate content generation • to third party applications • Common Gateway Interface is a standard protocol • which defines how to delegate content generation • from a web server to a console application
CGI Common Gateway Interface • If dynamic content generation is required, the web • server invokes console applications as commands
CGI Common Gateway Interface • If dynamic content generation is required, the web • server invokes console applications as commands These console applications are often called CGI scripts Although they can be written in any programming language, scripting languages are often used
CGI Common Gateway Interface • If dynamic content generation is required, the web • server invokes console applications as commands These console applications are often called CGI scripts Although they can be written in any programming language, scripting languages are often used • For example: wikipedia User makes a requests for an entry The web server activates a script which transforms the source of the entry into an HTML document and sends the result to the user
CGI Common Gateway Interface • How do you tell a server to invoke an application • rather than send back its source code/binary form?
CGI Common Gateway Interface • How do you tell a server to invoke an application • rather than send back its source code/binary form? The CGI scripts are placed in a special directory recognized by the web server
CGI Common Gateway Interface • How do you tell a server to invoke an application • rather than send back its source code/binary form? The CGI scripts are placed in a special directory recognized by the web server Once there is a request for a resource in that directory, the web server knows to invoke the script/executable rather than return its content
CGI Common Gateway Interface • How do you tell a server to invoke an application • rather than send back its source code/binary form? The CGI scripts are placed in a special directory recognized by the web server Once there is a request for a resource in that directory, the web server knows to invoke the script/executable rather than return its content Usually the directory is called cgi-bin under the web server’s path
CGI Common Gateway Interface • How do you tell a server to invoke an application • rather than send back its source code/binary form? The CGI scripts are placed in a special directory recognized by the web server Once there is a request for a resource in that directory, the web server knows to invoke the script/executable rather than return its content Usually the directory is called cgi-bin under the web server’s path For security reasons only webmasters can update this directory or make it active
CGI CGI specification
CGI CGI specification • CGI defines how information about the request • (e.g. search query) is passed to the command
CGI CGI specification • CGI defines how information about the request • (e.g. search query) is passed to the command The web server sets the environment variables with proper information
CGI CGI specification • CGI defines how information about the request • (e.g. search query) is passed to the command The web server sets the environment variables with proper information If there is additional content (e.g. a PUT method), then it is passed using stdin
CGI CGI specification • CGI defines how information about the request • (e.g. search query) is passed to the command The web server sets the environment variables with proper information If there is additional content (e.g. a PUT method), then it is passed using stdin • CGI sets rules for the CGI script output and how • it needs to be handled by the web server
CGI CGI specification • CGI defines how information about the request • (e.g. search query) is passed to the command The web server sets the environment variables with proper information If there is additional content (e.g. a PUT method), then it is passed using stdin • CGI sets rules for the CGI script output and how • it needs to be handled by the web server The response is written to stdout
CGI CGI specification • CGI defines how information about the request • (e.g. search query) is passed to the command The web server sets the environment variables with proper information If there is additional content (e.g. a PUT method), then it is passed using stdin • CGI sets rules for the CGI script output and how • it needs to be handled by the web server The response is written to stdout There are two types of response: NPH and CGI
CGI CGI mechanism
CGI CGI mechanism • CGI Script execution The web server acts like an application gateway
CGI CGI mechanism • CGI Script execution The web server acts like an application gateway as follows: • Receives the request from a client
CGI CGI mechanism • CGI Script execution The web server acts like an application gateway as follows: • Receives the request from a client • Selects a CGI script to handle the request
CGI CGI mechanism • CGI Script execution The web server acts like an application gateway as follows: • Receives the request from a client • Selects a CGI script to handle the request • Converts the client’s request to a CGI request and sets the environment variables
CGI CGI mechanism • CGI Script execution The web server acts like an application gateway as follows: • Receives the request from a client • Selects a CGI script to handle the request • Converts the client’s request to a CGI request and sets the environment variables • Executes the script (request data might not be immediately available through stdin)
CGI CGI mechanism • CGI Script execution The web server acts like an application gateway as follows: • Receives the request from a client • Selects a CGI script to handle the request • Converts the client’s request to a CGI request and sets the environment variables • Executes the script (request data might not be immediately available through stdin) • Converts the CGI response into a response to the client
Request CGI Request Response CGI Response CGI CGI mechanism • CGI Script execution The web server acts like an application gateway User CGI script Web server
CGI CGI mechanism • CGI Script execution The web server acts like an application gateway The web server MAY act in a “non-transparent” manner, modifying the request or response in order to provide some additional service (e.g. media type transformation or protocol reduction)
CGI CGI mechanism • CGI Script execution The web server acts like an application gateway The web server MAY act in a “non-transparent” manner, modifying the request or response in order to provide some additional service (e.g. media type transformation or protocol reduction) The web server has to conform to the client’s request protocol, even if the CGI script fails to
CGI CGI mechanism • CGI Script execution The web server acts like an application gateway The web server MAY act in a “non-transparent” manner, modifying the request or response in order to provide some additional service (e.g. media type transformation or protocol reduction) The web server has to conform to the client’s request protocol, even if the CGI script fails to If authentication is involved, the script can be invoked only if access is granted
CGI CGI mechanism • CGI Script selection
CGI CGI mechanism • CGI Script selection The CGI script is selected based on the request URI
CGI CGI mechanism • CGI Script selection The CGI script is selected based on the request URI The script may match the whole or a leading part of the hierarchical part
CGI CGI mechanism • CGI Script selection The CGI script is selected based on the request URI The script may match the whole or a leading part of the hierarchical part The remainder of the path, if any, is a resource or sub-resource identifier to be interpreted by the script
CGI CGI mechanism • CGI Script selection The CGI script is selected based on the request URI The script may match the whole or a leading part of the hierarchical part The remainder of the path, if any, is a resource or sub-resource identifier to be interpreted by the script Several paths may be associated with the same script
CGI CGI Request
CGI CGI Request • Meta-variables passed as environment variables The variables contain data about the request passed from the server to the script
CGI CGI Request • Meta-variables passed as environment variables The variables contain data about the request passed from the server to the script They are identified by case-insensitive names; there cannot be two different variables whose names differ in case only
CGI CGI Request • Meta-variables passed as environment variables The variables contain data about the request passed from the server to the script They are identified by case-insensitive names; there cannot be two different variables whose names differ in case only Usually they appear as capitals with underscores (e.g. PATH_INFO)
CGI CGI Request • Meta-variables passed as environment variables The variables contain data about the request passed from the server to the script They are identified by case-insensitive names; there cannot be two different variables whose names differ in case only Usually they appear as capitals with underscores (e.g. PATH_INFO) A missing environment variable is equivalent to a zero-length (NULL) value; it is impossible to tell if the value is NULL or missing
CGI CGI Request • Meta-variables passed as environment variables Some variables are set for every request, some don’t
CGI CGI Request • Meta-variables passed as environment variables Some variables are set for every request, some don’t Variables set for every request
CGI CGI Request • Meta-variables passed as environment variables Some variables are set for every request, some don’t Variables set for every request SERVER_SOFTWAREThe name and version of the web server SERVER_NAMEThe server's hostname or IP address GATEWAY_INTERFACEThe revision of the CGI specification to which this server complies (e.g. CGI/1.1)
CGI CGI Request • Meta-variables passed as environment variables Some variables are set for every request, some don’t Request specific variables
CGI CGI Request • Meta-variables passed as environment variables Some variables are set for every request, some don’t Request specific variables SERVER_PROTOCOLThe name and revision of the protocol this request came in with (e.g. HTTP/1.1) SERVER_PORTThe port number to which the request was sent REQUEST_METHODThe method with which the request was made (e.g. POST)
CGI CGI Request • Meta-variables passed as environment variables Some variables are set for every request, some don’t Request specific variables REMOTE_ADDRThe IP address of the client REMOTE_HOSTThe hostname of the client the request
CGI CGI Request • Meta-variables passed as environment variables Some variables are set for every request, some don’t Request specific variables PATH_INFOIdentifies the resource or sub-resource derived from the portion of the URI path hierarchy following the part that identifies the script itself PATH_TRANSLATEDThe server provides a translated version of PATH_INFO, which takes the path and does any virtual-to-physical mapping to it
CGI CGI Request • Meta-variables passed as environment variables Some variables are set for every request, some don’t Request specific variables QUERY_STRINGThe part of the request URI which follows the first ? This string is added either by an HTML form with a GET method or by an HTML anchor This string is encoded in the standard URL format – spaces are replaced by “+” and special characters are encoded with %xx (hex value)for example: “ur/ a%&” “ur%2F+a%25%26”