C + + Background practice: ancient CGI and web development

Source: Internet
Author: User
Tags ruby on rails

This article is written to the C + + program Ape, also suitable for other programs interested in history ape

=============================================

When it comes to web development, you first think of PHP, javaee/jsp,. net/asp, Ruby on Rails, Python's Django, and so on. You don't usually think about the relationship between C + + and Web development, but the development of Dynamic Web pages (Web development) has existed before the advent of these dynamic web languages. So C + + can also do web development, it uses the technology is--cgi.

In the beginning of heaven and earth, when chaos is not divided, Dynamic Web language is not yet born, to achieve dynamic Web site relies on CGI. Google/Baidu a bit of CGI, there may be many nouns: CGI scripts, CGI programs, CGI standards and so on. In fact, these are standing in different angles, CGI is common Gateway interface abbreviation, literal translation for "Universal Gateway interface." The first time I heard the name, I didn't know what the hell it was. In the final analysis, CGI is an interface protocol . A protocol is an accepted set of standards (called CGI standards), such as network protocols. We all abide by a set of standards, it reduces the difficulty of communication. CGI Development is the writing of a CGI executable program. In fact, all kinds of languages can be written CGI, not only Java, Python, PHP, C # ... Yes, and the shell can. Of course C and C + + can also. Since much of the early CGI was developed by Perl (scripting language), CGI programs are also called CGI scripts, which are not necessarily accurate. Because C + + compiles executable files that can also be CGI.

In the big line of PHP and Java today, many people seem to use C + + to write CGI is almost eliminated technology (in fact, this is not, but relatively small). So if you are interested in C + + or interested in history, you can read this article.

A Web page request and response when a Web page is browsed, a Web page is usually requested via a URL, and the server returns the Web file to the browser. The browser parses the file locally to render the page we see. However, usually we see the page is not a static page, that is, on the server is not the page file, it is on the page when the request is generated dynamically, such as php/jsp Web page.        Depending on the parameters you request, the content returned is different. Similarly, if you are requesting a CGI program ( For example, when a browser directly enters the URL of a CGI program, or submits a form, it is sent to a CGI program.), the CGI program is responsible for parsing the parameters passed from the front end, understanding its intent and then returning the data, such as returning HTML, XML, or JSON. Warnning:apache no support for CGI is turned on by default, and CGI configuration is required. Specific methods can be self-Baidu. Prepare front end knowledge assume that you are a C + + programmer, you may not be familiar with the front end (OK, I do not know), before the next step, you have to master some of the pre-knowledge (as little as possible front end), you do not need to know how to render a beautiful web page, but you need to know how the front and back Front page How to send data, a normal HTML page usually practices, you only need to know the following several:
    • Form form submission (HTML native)
    • JS-controlled form submission
    • JS requests data via Ajax
Here we know the first (the simplest):


The value of the Action property of the form tag represents the form to be submitted to the URL, that is, the page to jump after the form is submitted (Ajax can reach no jump pull data, refresh the page), where the Action property value is the CGI program URL address. (warnning:/corresponds to the root directory of the Web site, not the Linux file system root.) The method property represents the way data is requested, in two ways: Get and post. Do not repeat.
I enter the user name Jellywang, after password 123456, click the OK button, that is, toCurrent domain/cgi-bin/hello.cgiThe program submitted the form and carries the parameter Username=jellywang.        The page then jumps to this CGI (just like normal page jumps, browser address bar updates). If it is a GET request. Then the URL of the browser address bar looks like this: localhost:/cgi-bin/hello.cgi?username=jelly&password=123456. Obviously this is a less secure way, so we can also use a POST request. This way the address bar will not see the arguments for this submission. (In fact, the post is not safe enough, do not encourage the way to directly submit plaintext password, this article only as an example, the security login is not the focus of this article) environment variables and CGI processing the current page through the Get or POST method to the CGI program submitted data, then the next CGI program how to parse it? The answer isEnvironment Variables。 Both Linux systems and Windows systems have the concept of environment variables. Linux users have to deal with environment variables in the system configuration file when configuring many environments. A CGI program obtains parameters by taking values from the environment variables. Here are a few environment variables (more please own Baidu):

request_method

front page data request method: get/ Span style= "font-family:"arial Unicode ms"" lang= "X-none" >post

Query_string

Information that is transmitted when a get is used

Content_length

Valid information length in stdio

script_name

The name of the CGI program called

server_name

server ip

Server_port

Port number of the host


Where do these environment variables come from and who define them? Is it Linux? POSIX, huh? Of course not. It's time to state again that CGI is an interface protocol, and these are the contents of the protocol, so whether your server is operating on Linux or Windows, or whether your server is Apache or Nginx, The names and meanings of these variables are the same.        The fact is that Apache/nginx is populating these content into environment variables, and the specific fill specification comes from the CGI interface protocol. In the C language standard, there is a library function--getenv to get the environment variables. (header file stdlib.h)
For example chr* str = NULL;STR = getenv ("query_string");
For GET requests, you can remove the string username=jelly&password=123456 from the environment variable query_string. Then the program does its own parsing of the string, parsing out the parameter key and value.        For a POST request, the parameter string is obtained directly from the callout input (STDIN), such as using scanf or CIN. After parsing the request and making the appropriate logical processing (such as checking the user name password is consistent), the CGI program will return the content to the front page, which is done through standard output (STDOUT), such as printf or cout, you can return Xml,json,plain Text or an HTML page, and so on. This step is done by the HTTP response process. So before returning the direct data, first output the HTTP protocol header. For example, if you want to return an HTML page, you first need to output:
   cout<< "content-type:text/html\n\n" <<endl;
warnning:Note here that you must output two line breaks (\ n). Because the HTTP protocol header and message entities (such as HTML code)Separated by a blank line. The following directly cout out the HTML code (such as the output you just entered the user name successfully logged in). The front-end page receives these HTML code, and the browser renders it as a Web page. This is a CGI completed Dynamic Web page operation. The CGICC Library does CGI programming for C + +, requires manual parsing of strings, and a self-managing header. For example, if the resource is transferred, return 302, and the new address is given at the header. Obviously, these things have built-in solutions for languages like PHP and Python. Third-party libraries are required for C + +. A GNU Open Source Library--CGICC is recommended here.        Can meet the common needs of various types, in addition to parsing get/post requests, but also can redirect, you can set cookies, you can upload files and so on and so on. The drawback is that the CGICC library does not support the session. But this is not a big problem, and we can easily use cookies to implement session functions. BecauseThe CGI itself is a request to create a process once, and the process ends after returning (except for fastcgi below). At this point, to maintain a session on the server variable optional solution is: file storage or in the Redis, memcached and other memory database storage. The SessionID sent to the client is done with the cookie function that CGICC already supports. The pain points of CGI with fastcgi        CGI is a standard and does not qualify language. So Java, PHP, and Python can all generate dynamic Web pages in this way. But in fact these dynamic languages are seldom used in this way. The original CGI has a big mishap. That is, every time the CGI request, then Apache has started a process to execute the CGI program, that is, Unix-specific fork-and-execute. When the user request is large, this Fork-and-execute operation can seriously slow down the server process. While Java's servlet technology is a kind of resident memory technology, the process context creation and destruction operations do not occur frequently.         The Times create heroes, FastCGI technology came into being. Simply put, its essence is a process pool technology that resides in memory, which is the responsibility of the scheduler to send a CGI request to the handler process that handles CGI. After a request processing is complete, the processing process does not destroy and continues waiting for the next request to arrive. FCGI technology, CGI to a certain extent, the second spring. PHP-FPM itself is a patch that enables PHP to support fcgi technology and is now included in the PHP standard. Of course, fcgi technology that supports C + + also appears, and Apache has fcgi modules that can be installed, such as Mod_fcgid. Modern CGI programming paradigms         Earlier we know that CGI can return directly to an HTML page. The CGI program itself can also perform various computational and logical processing tasks. With the development of various kinds of web-front and back-end technologies, there are more and more server usage scenarios with big data and high concurrency. The use of modern CGI has changed.         Now, more and more tasks are moving from the backend to the front end, and the front-end page uses the rich JS technology for more processing.
    1. JS can use AJAX technology to initiate data requests back to the CGI. Ajax does not need to refresh the entire page to load back-end data (such as out of the database).
    2. CGI is no longer used to directly return HTML pages, while complex compute and IO tasks are dropped to the back end (the backend can be further routed forward for load balancing). Make the CGI the middle tier between the front and back ends. The function of CGI is to complete the basic data exchange: Parse the front-end data request, then forward to the corresponding backend, then retrieve the data from the backend and return the XML or JSON to the front end.
    3. Front-end JS uses the data in the Xml/json to populate, drawing a rich page.

C + + Background practice: ancient CGI and web development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.