CGIPARSE(8) | DACS Web Services Manual | CGIPARSE(8) |
cgiparse - CGI parameter parsing utility
cgiparse [mode]
[-enc {none | url | mime | dacs}]
[-in filename] [-checkdup] [-d]
[-duperror]
[-nodups] [-nonewline] [-qs query-string]
[-copy filename]
[[-n name filename]...]
This program is part of the DACS suite. It is a stand-alone program that neither accepts the usual DACS command line options (dacsoptions) nor accesses any DACS configuration files.
This utility is used by web-based scripts (shell scripts in particular) to obtain their CGI parameters, which can be obtained from a URI's query component or in an encoded entity-body read from the standard input (as with the POST method). The form content types[1] application/x-www-form-urlencoded and multipart/form-data are both understood.
The program has several different modes of operation, one of which may be specified by the first command line argument.
cgiparse combines query parameters found in the QUERY_STRING environment variable with parameters found in the message body it reads from the standard input. RFC 3875[2] states (S4.1.7) that the query string value is case-sensitive.
Duplicate parameter names are allowed by default; see -nodups and -duperror, which override duplicate handling described below.
The mode may be exactly one of the following:
-arg variable-name
-checkdup
-targ variable-name
-html
-one
-sh
--shell
variable-name='variable-value'; [...]
It is an error if any variable-name or variable-value is syntactically unsuitable for this format. The returned string can be used as the argument to eval to set the CGI parameters as shell variables. All parameters are output, including duplicates, in which case a variable will be assigned the value from the parameter instance that happens to appear last in the list.
-text
variable-name variable-value
A space separates the name from the corresponding value. The file is typically read by a script to obtain the parameters, or cgiparse can be run with the -in flag to retrieve a parameter. All instances of duplicate parameter names are output.
--version
Additionally, cgiparse recognizes these options and modifiers:
url
mime
dacs
none
For details about these encodings, please see dacs.exprs(5)[3]. The default is none. If reading the parsed CGI parameters (-in), decode the parameter values using the specified method. The default is none, which means that no decoding is performed; if the parameters were encoded, they will be returned in that encoding, but other than this case the decoding method must match the encoding method previously used or an error is likely to occur.
-qs query-string
-nonewline
-nodups
-duperror
-d
-copy filename
-in filename
-n name filename
If the output file exists it is truncated.
The following shell script demonstrates one way of using cgiparse.
#! /bin/sh tmpfile=/tmp/cgiparse.$$ cgiparse > ${tmpfile} chmod 0600 ${tmpfile} echo "Context-Type: text/plain" echo "" done= while [ "${done}x" = x ] do
a=
b=
read a b
if [ $? = 1 ]
then
done=1
break
else
echo "Arg: ${a}"
echo "Is: ${b}"
fi done < ${tmpfile} rm -f ${tmpfile} exit 0
The following code fragment uses cgiparse to save and then look up its CGI parameters:
#! /bin/sh tmpfile=/tmp/cgiparse.$$ trap 'rm -f ${tmpfile}; exit 1' EXIT 1 2 3 13 15 cgiparse -enc mime > ${tmpfile} chmod 0600 ${tmpfile} mode=`cgiparse -in ${tmpfile} -enc mime -arg MODE` target=`cgiparse -in ${tmpfile} -enc mime -arg TARGET`
The following script will print "1 2 3" to its standard output:
#! /bin/sh args=`cgiparse -sh -qs "a=1&b=2&c=3"` eval "$args" echo "$a $b $c"
The program exits 0 if everything was fine, 1 if an error occurred.
There do not appear to be any official recommendations concerning how to handle apparently "malformed" CGI query strings that do not look like a sequence of name=value pairs. The parsing routines that cgiparse uses will flag an error if they see strings containing a component like "=foo", for example, although "foo=" is fine.
The manner in which duplicate CGI parameters is handled is not standardized and context-specific. cgiparse could do a little better in this respect.
RFC 3875[2], RFC 3986[4], The WWW Common Gateway Interface, Version 1.2[5], HTML 4.01 Specification[6], dacs_prenv(8)[7]
Distributed Systems Software (www.dss.ca[8])
Copyright © 2003-2014 Distributed Systems Software. See the LICENSE[9] file that accompanies the distribution for licensing information.
02/19/2019 | DACS 1.4.40 |