Implementing Web Services CGI using bash scripting

Source: Internet
Author: User
Tags curl html form html page reset web services

2014-01-09 WCDJ

I have previously summarized an article on implementing Web Services using Perl, "Web server implementation (minimal Perl Web server)." Interacting with the CGI interface is often required at work, and when the dependent interface is not yet implemented, it is possible to write a simple CGI interface to emulate the dependent interface to complete debugging. Bash is also one of the scripts you use daily, and this article summarizes how to use bash for simple CGI interactions.


0 Implement a CGI that returns a JSON-formatted reply

#!/bin/bash

echo "content-type:text/html"
echo "

# OK, we ' ve sent the header, now send some Content
Echo "{\" ret\ ": 0,\" msg\ ": \" Ok\ "}"
Test method:

Placing the above bash script under Websvr, you can send an HTTP request via the Curl command, for example: Curl "http://172.25.81.16/cgi-bin/ret_json_ok.sh", and then return: {"ret": 0, "msg ":" OK "}.


1 Implementing a form to send an HTTP request

The effect is shown in the following figure:


The implementation method is divided into two steps:
(1) Write a simple HTML page, for example, form.html

<form action= "/cgi-bin/form_test.sh" >
Enter Host: <input name= "Host" >
<input type=submit Value= "Submit" >
</FORM>
(2) Write a request to receive the CGI, the above action has been specified, that is, form_test.sh
#!/bin/bash

echo ' content-type:test/html ' echo ' echo
$QUERY _string

2 implement a return HTML page with the title and content of Hello worldThe page effect returned is as follows:

The implementation method, also implements a CGI, just returns the content is an HTML:
#!/bin/bash echo ' content-type:test/html ' echo ' echo '

3 restudyingWith a few very simple examples above, the following summarizes some basic concepts:

(1) Introduction to CGI

Web CGI programs can written in any language which can process standard input (stdin), environment variables and write T o Standard output (stdout). The Web server would interact with all CGI programs using the ' Common Gateway Interface ' (CGI) standard as set by RFC 3875. This capability was possessed by most modern computer programming and scripting languages, including the bash shell.


(2) Basic Bash CGI Example

CGI programs typically perform the Following:all CGI scripts must write out a header used by the browser to identify the Content. They typically process some input. (URL, form data or Isindex) CGI can access environment variables set by the Web server. CGI scripts would write out HTML content to be viewed. This typically has the structure of the "head" which contains non-viewable content and "body" which provides the viewable Content.

Script Location: Various Distributions of Linux Locate the CGI directory in different directory paths. The path is set by the Web server configuration file. For the Apache Web server, the "scriptalias" directive defines the CGI path:

Linux Distribution Path
Red Hat Enterprise, 7.x-9, Fedora Core, CentOS /var/www/cgi-bin/
Red Hat 6.x and older /home/httpd/cgi-bin/
Suse /srv/www/cgi-bin/
Ubuntu/debian /usr/lib/cgi-bin/

Script Permissions: The script would require system executable Permissions:chmod +x/var/www/cgi-bin/hello.sh

If using SELinux, the security context must also permit execution:chcon-t httpd_sys_content_t/var/www/cgi-bin/hello.sh

executing Shell Commands:

Typically one would want to process shell or system commands:add the paths required to find the commands:


(3) processing Bash CGI Input

Accessing environment Variables: The Web server would pass environment variables to the CGI which it can access and use. This was very simple for bash.

#!/bin/bash
 
echo "content-type:text/html"
echo "echo
 

Typically one would want to process input from the URL "query_string" such as "namex=valuex&namey=valuey&namez=v Aluez "extracted from the following url:http://localhost/cgi-bin/env.sh?namex=valuex&namey=valuey&namez= Valuez Script Description:script would loop through all of the arguments in environment variable "query_string" as Separat Ed by the delimiter "&". Thus the script loops three times with the following ' args ': Namex=valuex namey=valuey Namez=valuez for each ' args ' line, Look for each token separated by the delimeter "=". Component 1 ($) and Component 2 ($). Use the "sed" to parse and substitute characters. A Blank Space is substituted for all%20 ' s.

#!/bin/bash echo "content-type:text/html" echo "Echo Output:

   
Parsed Values:
valuex
valuey
Valuez
You'll get the same results for:http://node1.megawww.com/cgi-bin/env.sh?namex=valuex&namez=valuez&namey= Valuey

Typically one would also want to produce and process input from an HTML form:

url:http://localhost/cgi-bin/exampleform.sh

#!/bin/bash echo "content-type:text/html" echo "Echo  

Note that the environment variables $REQUEST _method and $QUERY _string can is processed by the shell directly.

You can string together more "sed" translators as needed (depending on your content): | sed "s/%20//g" | Sed "s/%3a/:/g" | Sed "s/%2f/\//g" (4) CGI Security

One must filter the input to avoid cross site scripting. Filter out "<>&*?". /"To avoid trouble from hackers.


4 References

[1] Bash Shell CGI

[2] Simple CGI and Apache examples on Ubuntu Linux





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.