nouvelles fonctions pour le module cgi. cgilsxml, cgiupload et cgiparams sont rassemblés dans le module cgisupport
This commit is contained in:
parent
4bbc3f6bac
commit
4b1c30b14a
13
lib/ulib/cgi
13
lib/ulib/cgi
|
@ -32,6 +32,13 @@ function nocache_header() {
|
||||||
echo "Expires: Thu, 01 Jan 1970 00:00:00 GMT"
|
echo "Expires: Thu, 01 Jan 1970 00:00:00 GMT"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function cgiredirect() {
|
||||||
|
# Rediriger le client vers une autre page
|
||||||
|
echo "Status: 302 Found"
|
||||||
|
echo "Location: $1"
|
||||||
|
echo ""
|
||||||
|
}
|
||||||
|
|
||||||
function cgicontent() {
|
function cgicontent() {
|
||||||
# Générer les en-têtes nécessaire avant de servir le contenu.
|
# Générer les en-têtes nécessaire avant de servir le contenu.
|
||||||
# $1(=text/html) est le type de contenu. S'il faut servir le contenu avec
|
# $1(=text/html) est le type de contenu. S'il faut servir le contenu avec
|
||||||
|
@ -71,3 +78,9 @@ function cgicontent_nocache() {
|
||||||
echo ""
|
echo ""
|
||||||
[ -f "$contentfile" ] && cat "$contentfile"
|
[ -f "$contentfile" ] && cat "$contentfile"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function cgierror() {
|
||||||
|
# Afficher un message d'erreur
|
||||||
|
cgicontent_nocache text/plain
|
||||||
|
echo "ERROR: $*"
|
||||||
|
}
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
##@cooked comments # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
|
||||||
## Fonction de support pour cgilsxml
|
|
||||||
##@cooked nocomments
|
|
||||||
##@require ulib
|
|
||||||
uprovide cgilsxml
|
|
||||||
urequire ulib
|
|
||||||
|
|
||||||
function cgilsxml() { "$ULIBDIR/support/cgilsxml.py" "$@"; }
|
|
|
@ -1,8 +1,10 @@
|
||||||
##@cooked comments # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
##@cooked comments # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
||||||
## Fonction de support pour cgiupload
|
## Fonction de support pour faire des scripts CGI
|
||||||
##@cooked nocomments
|
##@cooked nocomments
|
||||||
##@require ulib
|
##@require ulib
|
||||||
uprovide cgiupload
|
uprovide cgisupport
|
||||||
urequire ulib
|
urequire ulib
|
||||||
|
|
||||||
|
function cgiparams() { "$ULIBDIR/support/cgiparams.py" "$@"; }
|
||||||
|
function cgilsxml() { "$ULIBDIR/support/cgilsxml.py" "$@"; }
|
||||||
function cgiupload() { "$ULIBDIR/support/cgiupload.py" "$@"; }
|
function cgiupload() { "$ULIBDIR/support/cgiupload.py" "$@"; }
|
|
@ -0,0 +1,54 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 mode: python -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
||||||
|
|
||||||
|
u"""Ce script est prévu pour être utilisé dans un script CGI.
|
||||||
|
|
||||||
|
Il permet de lister les paramètres du formulaire et d'y accéder depuis un script bash
|
||||||
|
"""
|
||||||
|
|
||||||
|
import re, cgi
|
||||||
|
|
||||||
|
RE_NAME_VALUE = re.compile(r'([a-zA-Z0-9_-]+)(?:=(.*))?$')
|
||||||
|
|
||||||
|
def quote(value, q=False):
|
||||||
|
if value or q:
|
||||||
|
value = "'%s'" % value.replace("'", "'\\''")
|
||||||
|
value = re.sub(r"(?<!\\)''", "", value)
|
||||||
|
return value
|
||||||
|
else: return ""
|
||||||
|
|
||||||
|
def print_scalar(name, value):
|
||||||
|
print "%s=%s" % (name, quote(value))
|
||||||
|
|
||||||
|
def print_array(name, values):
|
||||||
|
print "%s=(" % name
|
||||||
|
for value in values:
|
||||||
|
print quote(value, True)
|
||||||
|
print ")"
|
||||||
|
|
||||||
|
def run_cgiparams():
|
||||||
|
from optparse import OptionParser
|
||||||
|
OP = OptionParser(usage=u"\n\t%prog [options] params...", description=__doc__)
|
||||||
|
o, args = OP.parse_args()
|
||||||
|
|
||||||
|
form = cgi.FieldStorage(keep_blank_values=True)
|
||||||
|
if args:
|
||||||
|
for nv in args:
|
||||||
|
mo = RE_NAME_VALUE.match(nv)
|
||||||
|
if mo is None: continue
|
||||||
|
fname, defvalue = mo.group(1), mo.group(2)
|
||||||
|
if defvalue is None: defvalue = ''
|
||||||
|
vname = fname.replace('-', '_')
|
||||||
|
values = form.getlist(fname)
|
||||||
|
if len(values) == 0: print_scalar(vname, defvalue)
|
||||||
|
elif len(values) == 1: print_scalar(vname, values[0])
|
||||||
|
else: print_array(vname, values)
|
||||||
|
else:
|
||||||
|
for fname in form.keys():
|
||||||
|
vname = fname.replace('-', '_')
|
||||||
|
values = form.getlist(fname)
|
||||||
|
if len(values) == 1: print_scalar(vname, values[0])
|
||||||
|
else: print_array(vname, values)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
run_cgiparams()
|
Loading…
Reference in New Issue