CGI stands for Common Gateway Interface. This is the name of a small program that offers a reliable way of interacting web servers in a browser, doing it in ways that were impossible using HTML. Most CGI scripts are written in Perl. This is a common programming language that is used to create server-side scripts with a standard text file with the extension (.cgi).
The main working conditions of the program
Before using the characters of CGI scripts, the user must check whether their website supports the site, and also check if the developer has permission to use them. Some hosts do not allow this. If the site developer sees the CGI-bin folder when loading HTML files, he is allowed to use GCI scripts.
Some hosts will allow them to request the CGI-bin function, since it is not always provided by default. Other hosts may test and approve the scripts before giving permission to use CGI scripts.
If the developer does not need editing, he will still need a text editor to write the script for the site’s requests. Whatever editor is planned for use, it is important to save the file as plain text.
In addition, you will need the FTP program that comes with the commercial version of Pro or the free version of Lite. Any option will work as long as it loads in ASCII mode to help the user get through the installation, since creating CGI scripts can take a lot of time.
Configuration and Installation
If the user has a CGI-bin, then there is a good chance of running a CGI script. For proper installation, it is recommended that you first check the web host’s frequently asked questions page, site control panel, support documents, or installation message. One of the most common mistakes for webmasters is to install CGI scripts with the wrong path.
If there is already a script open for use, the path must be correct, otherwise the server will not work. While the script opens, check the feedback regarding any variables that require settings and comments on this line starting with "#".
When the script for the server is defined, you can proceed with the download. Be sure to do this in ASCII mode, since a simple CGI script is a text file. Technically, scripts can be executed from any directory, but most hosts only allow this from a CGI bean. It is also important to use the correct resolution for files that use the CGI script.
Permission for files
There are three types of data access with three different permissions for each group.
UNIX servers allow you to set different access levels. It is important to know what permissions the script has:
- Group - consists of added users on the server. If necessary, it will be allowed to remove their permission.
- User - consists only of the owner of the file in the group, this is the hosting account.
- Other - the group consists of all other aspects on the server.
- Reading - allows users to read and understand the data specified in the file.
- Write - write permission will allow users to decrypt and record a new date, delete old data from the archive.
- Execute - permission to execute, will only allow a file to be sent if the program or the CGI exe script is executed. Pre-make sure that the script will work until the link in public mode.
Application example
CGI - the application simplifies the creation of complex, high-performance, reusable web applications, the implementation of design, recording and development of web projects. It reasonably applies the use of modern technologies and methods that connect the developer with any set of tools of the operating system or server.
It has light weight (in terms of memory usage), which makes it suitable for conventional CGI environments, and a high selection of performance in persistent environments such as FastCGI or mod_perl.
By adding PLUG-INS as your needs grow, you can use complex functions when you need them, for example, you need to write an application to search through the widget database. The instance script is what is actually invoked by the user web server. This is a very small simple file that simply creates an instance of a user application and calls the inherited method, run (). Below is the full description of widgetview.cgi:
#! / usr / bin / perl -wuse WidgetView
my $ webapp = WidgetView-> new ()
$ webapp-> run ()
As you can see, widgetview.cgi just uses an application module that implements a Perl package called WidgetView.pm.
CGI Application takes care of the implementation of the new () and run () methods. In this case, the user should never call print () to send any output to STDOUT. Instead, all output is returned as a scalar.
CGI is the most significant contribution to managing the state of an application. All that is needed to move the application forward is to set the value of the HTML form parameter “rm” to the value of the “runtime”, which must be processed by submitting the form. This is the key to the CGI Application.
Scripting methods
Using the CGI Application, the user gets access to a number of built-in methods. The following are those that are called from the script.
The new () method is a constructor for CGI. It returns a link to the application package (class). It can take a set of parameters as pairs: key => value.
This method may take some specific parameters:
- TMPL_PATH - Defines the path to the template directory.
- QUERY - allows you to specify the already created request object CGI.pm.
- PARAMS - this parameter allows you to set a number of configurable modes at run time. By passing different values in scripts of different instances that use the same application module, a higher level of reuse can be achieved.
Run () is called on the Application Module object from a script. When called, it executes the functionality of the user application module.
This method first determines the state of the application by looking at the value of the CGI parameter specified by the mode_param () parameter. The default is “rm” for the “run mode”, which will contain the name of the operating mode. If not specified, the default state is the value of start_mode (). Once a mode is defined, run () scans the send table stored in run_modes () and finds a function pointer that is entered from the mode name. If found, the function is called, and the print () 'ed returned data is sent to STDOUT and to the browser. If the specified mode is not found, in the table run_modes (), run () will be croak ().
PSGI Support
The app offers native PSGI support. The default request object for this is:
psgi_app ()
$ psgi_coderef = WebApp-> psgi_app ({... args to new () ...})
The easiest way is to create and return a PSGI - compatible coderef. You need to pass the arguments to hashref in the same way as to the new one. This returns a PSGI-compliant coderef using CGI ::: PSGI as the request object. To use another request object, create your own run_as_psgi () object, as shown below:
run_as_psgi ()
my $ psgi_aref = $ webapp-> run_as_psgi
Just like run, printing does not print or return the data structure required by the PSGI specification. Use this if you need to run the application on top of a PSGI-compatible handler, for example, Plack. The end result might look like this:
The PSGI specification allows you to return a file descriptor or subroutine link instead of byte strings. In PSGI mode, this is supported directly by the CGI Application.
Possible Override Methods
CGI implements some methods that are expected to be overridden by their implementation in the subclass module. These methods are as follows:
- Setting (). This method is called by the inherited constructor method new ().
- The setup () method should be used to determine the following property / methods.
The setup () method can call any of the user application instance methods. This function is a good place to define application-specific properties using $ webapp-> param ().
The setup () method can be implemented as the image below:
However, often everything that needs to be in setup () defines AutoRunmode startup modes, which allows you to do this using simple syntax using startup mode attributes.
Teardown () method. If it is implemented, it is called automatically after creating the CGI scripts and launching the application. It can be used for cleaning after operations. A typical use of the teardown () function is to disconnect the database connection that was established in the setup () function. You can also use the teardown () method to store information about the state of the application on the server: cgiapp_init ().
Sending pure URIs to trigger modes
Modern web frameworks go without limits in URIs, providing clean URIs instead:
/cgi-bin/item.cgi?rm=view&id=15
A pure URI for describing the same resource can be:
/ item / 15 / view.
The process of matching these URIs to trigger modes is called dispatching and is handled by CGI :: Application :: Dispatch. An additional dispatcher is not required.
You can work on a CGI :: Application project on your desktop or laptop without installing a full-featured web server such as Apache. Instead, install CGI :: Application :: Server from CPAN. After several minutes of installation, the user will have their own application server.
Conduct automated testing. Test :: WWW :: Mechanize :: CGIApp allows you to perform functional testing of the CGI :: App project without starting a web server. Test :: WWW :: Mechanize can be used to test the application through this web server.
Direct testing is also easy. A CGI application typically prints the output of its operating modes in STDOUT. This can be suppressed with the CGI_APP_RETURN_ONLY environment variable. The application has a plug-in architecture that is easy to use and to develop new plugins.
CGI Custom Content
It was the first specification set to develop dynamic content on a web server. CGI content delivered to users is not static HTML read from a file on the server. This is dynamic content generated in a script that can extract information from a database, adapt the content to the type of browser making the request, whether it is a new user or not.
PHP is a language specifically designed for implementing dynamic content on web servers. This makes CGI script development in PHP much easier than in other languages. When the server starts execution, it provides all the contextual information it needs:
- Full information about the request, including the name / IP of the remote host, the requested URL and its arguments, the query string (if any).
- The cookies associated with the request.
- In the case of a POST request, as a result of sending a web form, the names and values of the parameters.
Basic PHP script example
The script should execute the process and then output valid HTML code, including HTTP headers. PHP script example :
In this script, the first and last lines are HTML code, and the PHP sentences are enclosed in markers, then they get the script from the browser, adding a query string of two arguments “data1” and “data2”.
In addition to the global variable $ _SERVER, the following associative arrays for running CGI scripts are available in the script:
- $ _GET - names and values of the arguments passed to the server in the GET request.
- $ _POST - the names and values of parameters sent to the server in the POST request when submitting the form.
- $ _COOKIE - names and values of cookies attached to the request.
- $ _REQUEST - all elements in $ _GET, $ _POST and $ _COOKIEnpm for running php scripts with nodejs via cgi. This module was designed for deskshell to provide seamless php script processing.
Given that the developer receives the request in nodejs and has a request object called req and a response object called res, he should use this module as follows:
Thus, the request to /test.php will respond to the php module for CGI scripts. The paramsForRequest function reads the request object and sets the correct cgi environment variables for the request. This is provided as a separate function, so you can add additional query variables if necessary. The detectBinary function in the windows looks for an additional module for the php-bin-win32 node, which provides a portable php file. This code works for deskshell even if it is necessary to use it for any other purposes and additional functions.
First program
To write a simple CGI program in Perl, following the tradition, use the phrase "hello world." Here it is fully represented. Save this to a file called “hello” in the cgi-bin directory and launch it by specifying the URL: localhost / cgi-bin / hello in the browser. The user should see the text “Hello world” in the browser. If not, check out the boxout section for debugging CGI programs to get help tracking down the problem.
This is a simple program. There are only a few points that distinguish the development of CGI scripts in Perl from the standard program that starts from the command line. The first of these is the -T option on the shebang line. This puts Perl in taint mode. In this mode, Perl will automatically ignore any data coming from the user, and will not allow this data to be transferred to the operating system until it is cleared. In this sense, taint mode is a good idea.
The second difference is the addition of a line that prints the content header. This will be the first user test with the CGI protocol.
All CGI programs should output a set of headers to the user's browser. In most cases, the only heading to be returned is the content. This tells the browser what data the program sends. This example returns plain text, so the content type was “text / plain”.
HTML is usually returned, so the content type will be “text / html”. There is no reason why the program cannot return more complex data, such as PNG (“image / png”). The set of headers should be separated from the actual data by an empty string. This is achieved by printing two newlines after the final header of the CGI scripts on the server. Also note that the program writes its output to STDOUT.
The web server will be organized to collect any data recorded in STDOUT and to transfer it back to the browser. The previous example did not actually do anything. There is no need to have a CGI program that only displays fixed text. This could be easily done using an HTML page. The user must learn from the example of what has a dynamic output. For example, a script that prints the current time:
This script works on the same principle as the last one, but displays the system time, using the function when developing CGI scripts in Perl - “localtime” and prints this value. Each time the script is reloaded, the time will be updated. Once again, this script displays plain text. It is required to consider the version that returns HTML:
This program looks a bit more complicated, but in fact it does the same thing as in the previous example. The added complexity comes solely from the fact that they should render a full HTML page.
If a user ever looked at their browser settings, they probably saw checkboxes or radio buttons to enable or disable Javascript and Java, but never saw them for CGI or PHP. This is because Javascript and Java are executed by the browser (or not if the user disables them). The browser knows nothing about CGI or PHP. It just gets clean HTML after running scripts on the server. The built-in way to create CGI scripts - PHP, ColdFusion, ASP, is mainly used by developers who create front-end web pages that will access back-end databases, for example, client / server web applications.
The great advantage of the CGI method is that the scripts are stored in files, so there are literally thousands of freely available scripts already written and ready to be downloaded and used on the website. This means that you do not need to learn the scripting language to get the benefits of scripts, someone has already done this work for the user.