Use EF to develop a web application (6): parse the parameter values in the query string

Source: Internet
Author: User

Question:There is no precedent for developing Web applications with "easy language. Flying" (EF. However, because the EF Local Development Kit (efndk) has been released, it is not difficult to use C/C ++ to develop an EF class library to support EF in developing web applications. Of course, we can imagine that there are many difficulties to solve. This series of articles is a record of my exploration process and may not be of much value to outsiders. If some netizens are optimistic about it, please wait rationally. Author: liigo. For more information, see http://blog.csdn.net/liigo /. Online messages.

Use EF to develop a web application (6): parse the parameter values in the query string

 

As mentioned in previous articles, the main format of query string is name1 = value1 & name2 = value2 &..., that is, multiple "name = value" texts separated by "&" (sometimes "value" is blank, in the form of "name = "). In the CGI/FastCGI Program (and JSP/asp/PHP/perl), process the text of querystring and parse each "name" and "value ", it is a common operation to obtain the corresponding value based on the "name. This is even understandable.
The core action of the CGI/FastCGI program -- without this action, most programs will not work. In the EF class library FastCGI. efn, I have built-in querystring parsing function.

Querystring is a very regular text. It is not difficult to process it, but the execution efficiency should be considered. Before parsing, there are two preparations: 1. Read the querystring text from the environment variable QUERY_STRING; 2. perform URL Decoding on the querystring obtained in the previous step. Then, the normal parsing starts. The C ++ code for parsing querystring in EF class library FastCGI. efn is listed below:

Void _ fcgiclass_parsequerystring_ifneed (fcgiclass_fieldsdata * pfields)
{
If (pfields-> hasparsedquerystring) return; pfields-> hasparsedquerystring = ef_true;
_ Fcgiclass_read_query_string_ifneed (pfields );

Efchar * szquerystring = ef_get_text (pfields-> querystring );
If (szquerystring [0] = ACC ('/0 '))
{
Ef_dec_ref_count (pfields-> querystringnames); pfields-> querystringnames = ef_empty_array;
Ef_dec_ref_count (pfields-> querystringvalues); pfields-> querystringvalues = ef_empty_array;
Return;
}

Ef_minimem names, values;
Names. addint (0); names. addint (1); names. addint (0 );
Values. addint (0); values. addint (1); values. addint (0 );

Efchar * PS = szquerystring;
Efchar * psfrom = szquerystring;
Int COUNT = 0;
Bool inname = true;

For (; PS ++)
{
If (* PS = ACC ('= '))
{
Names. addint (efint) _ neweftext (psfrom, PS-psfrom ));
Psfrom = Ps + 1;
Count ++;
Inname = false;
}
Else if (* PS = ACC ('&'))
{
If (inname) // key1 &...
{
Names. addint (efint) _ neweftext (psfrom, PS-psfrom ));
Values. addint (efint) ef_empty_text );
Psfrom = Ps + 1;
Count ++;
Continue;
}

Values. addint (efint) _ neweftext (psfrom, PS-psfrom ));
Psfrom = Ps + 1;
Inname = true;
}
Else if (* PS = ACC ('/0 '))
{
If (* (PS-1) = ACC ('&') // key1 = value1 &
Break;

If (inname) // key1
{
Names. addint (efint) _ neweftext (psfrom, PS-psfrom ));
Values. addint (efint) ef_empty_text );
Count ++;
}
Else
Values. addint (efint) _ neweftext (psfrom, PS-psfrom ));

Break;
}
}

Names. replaceint (2 * sizeof (efdword), count );
Values. replaceint (2 * sizeof (efdword), count );

Efarray namearray = ef_gc_reg_text_array_data (names. Detach ());
Efarray valuearray = ef_gc_reg_text_array_data (values. Detach ());
Ef_dec_ref_count (pfields-> querystringnames); pfields-> querystringnames = namearray;
Ef_dec_ref_count (pfields-> querystringvalues); pfields-> querystringvalues = valuearray;

// Cache to hashmap
Eftext * pnames = (eftext *) (efbyte *) namearray + 3 * sizeof (efdword ));
Eftext * pvalues = (eftext *) (efbyte *) valuearray + 3 * sizeof (efdword ));
For (INT I = count-1; I> = 0; I --)
{
Pfields-> querystringnamevaluehashmap-> set (ef_get_text (pnames [I]), pvalues [I]);
}
}

The Code should be clear. The first is to process the cache. If it has been resolved once before, use the last resolution result directly. If you have read (including URL Decoding) querystring, use the value that has been read and decoded directly, do not repeat the work. Next we will parse the text to get the "name" array and its corresponding "value" array. The members of these two arrays correspond one to one. Finally, it is still cache, and each "name" and "value" are paired into a hash table, so that the "value" can be obtained through "name" high-speed query ". In the EF program, when fcgi. when QUERY_STRING (name) is called, in addition to the parsing work required for the first call, subsequent calls will directly query the results from the hash table, and the execution efficiency should be very high. (In the future, I may test the results to find out how obvious the program execution efficiency is .)
In the above C ++ Code, some abnormal and invalid querystrings are also properly fault-tolerant.

If the Code contains errors, omissions, or omissions, please criticize and correct them.

Note: The Code provided in this Article has errors in the processing sequence. You must separate the codes before decoding, instead of decoding them first! In go, the term "wrong order" is used ". This must be corrected and I will handle it right away. 

 

Notice below: "simple calculator" instance Program

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.