Analog HTTP request return &shell write CGI

Source: Internet
Author: User

There are occasional requirements in the test:
1. A CGI or link address that requires access to the extranet, but does not access
2. CGI-specific returns need to be constructed, but it is difficult to construct them under normal circumstances
To get the results you want, you can simulate the return of an HTTP request to meet your test needs

Method One: Get the result file directly

Create a new file under the corresponding path of the Apache configuration, such as

Documentroot/data/server/xxxx/htdocs

Then use the ip/ file name to access it, or place it under alias 's specified folder

Create a new file name, such as result, and type the following:

result=0&res_info=ok

Enter Http://xxx.xxx.xxx.xxx/result directly in the browser, access the file, you can see the results returned

This is just the equivalent of taking out the contents of the file, and if it really simulates the return of the CGI, I usually write a CGI with the shell.

Method Two: Write a pile of CGI

What is CGI, see Apache Guide: CGI

Http://man.chinaunix.net/newsoft/ApacheManual/howto/cgi.html

in short, writing a CGI yourself requires the following conditions:1.apache Configuration Support CGI

The general machine has deployed CGI , in this directory write a self-written CGI is not what does not support the problem, if it is in other directories new deployment of their own CGI, You might encounter Apache support issues.

For example, create a new shell script directly below the directory , such as test.sh

#!/bin/"result=0&res_info=ok"

running in the browser, the result is to download the script file instead of executing the output

Apache must be configured to allow CGI execution in several ways:

(1) Scriptalias

The scriptalias Directive allows Apache to execute CGI programs in a specific directory . When a client requests a resource in this particular directory,Apache assumes that the file is a CGI program and tries to run it.

Scriptalias directive shape such as: 

scriptalias/cgi-bin//usr/local/apache/cgi-bin/

IfApacheis installed to the default location, the default configuration filehttpd.confwill have the above configuration. Scriptaliasdirective defines a map that maps to a specific directory .URLprefix, withAliasdirectives are very similar, and both are typically used to specifyDocumentRootDirectories other than the directory, the difference isScriptaliasanother layer of meaning, namely itsURLany file in the prefix is consideredCGIprogram. Therefore, the above example will indicateApache,/cgi-bin/should point to/usr/local/apache/cgi-bin/Table of Contents, and it is consideredCGIprogram.

  (2) using the options to explicitly allow CGI execution

You can use the Options directive to explicitly allow the execution of CGI in a particular directory in the primary server configuration file :

<directory/usr/local/apache/htdocs/somedir>options +execcgi</directory>

The above instructions enable Apache to allow CGI file execution. Also, you must tell the server which files are CGI files. The following AddHandler instruction tells the server that all files with CGI or pl suffixes are CGI Program:

AddHandler cgi-script CGI Pl

(3). htaccess file

This is configured in the primary server configuration:

AllowOverride Options

In the . htaccess file, this configuration is required:  

Options +execcgi

To enable Apache to allow the execution of CGI programs in this directory .

2. Output content in CGI format

There must be a MIME -Type header in front of all output of the CGI program , that is, the HTTP Header

There are three types of head : Content-type, location, and Status. Content-type is the most common.

For content-type explanation You can see a description of the HTML , A specific code you can emit like this: Content-type:text/html In this example, the type of output data is text/html; In other words ,  He's a HTML file Other detailed instructions refer to Apache documentation

The basic use method is

" xxxxxxx " ""

Other detailed instructions refer to Apache documentation

After reading the 1 and 2 you can do a simple demo:

Apache is configured as:

scriptalias/cgi-bin//data/server/xxxxxx/htdocs/cgi-bin/

Create a new file under the directory test.sh (Written test.cgi may force a bit taller, the actual is the same)

#!/bin/"content-type:text/html;charset=utf8\n " "" "   "Result=0&res_info=ok"

should be able to return after execution

Result=0&res_info=ok

Let's do something that CGI should do, parse the input, do the processing, and then output

To be able to get the parameters in the request string, you need to remove Request_method and query_string from the environment variable env.

Request_method for Submit mode get or post

Query_string for the request parameter string, may need to split processing

For example, I need to tune a CGI query phone number verification code, the incoming phone number, processing logic is a SQL query, return the query results

#!/bin/Shecho"content-type:text/html;charset=utf8\n"Echo""Method="$REQUEST _method"Mobile= ' echo $QUERY _string | Cut-d"="-F2 ' y=${mobile:0-3:1}xx=${mobile:0-2:2}verifycode=""Verifycode= ' Mysql-uroot-p111111-e"Select Fverify_code from verify_code_$xx.t_code_$y where fmobile_no= ' $mobile ' ORDER by fcreate_time desc LIMIT 1"|grep-v Fverify_code '
if[ !-N $verifycode]; Thenecho"fail"ElseEcho $verifycodefi

Curl "http://172.27.207.203:23080/wx_pay/v1.0/wx_get_sms.cgi?mobile=13xxxxxxxxxx" or access directly in the browser, return the verification code, If there is no parameter or mobile error, the return is "fail"

Analog HTTP request return &shell write CGI

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.