cgilsxml.py: possibilité de spécifier la disposition du contenu, le type de contenu, ainsi que l'encoding
This commit is contained in:
parent
a730609280
commit
3a9bf0492e
|
@ -272,7 +272,7 @@ def print_files(files, fgroups=None, select_group=None, script_name=None, xslt=N
|
||||||
for name, value in file.vars.items():
|
for name, value in file.vars.items():
|
||||||
ET.SubElement(xfile, name).text = _u(value)
|
ET.SubElement(xfile, name).text = _u(value)
|
||||||
|
|
||||||
sys.stdout.write('<?xml version="1.0" encoding="UTF-8"?>\n')
|
sys.stdout.write('<?xml version="1.0" encoding="utf-8"?>\n')
|
||||||
if xslt is not None:
|
if xslt is not None:
|
||||||
sys.stdout.write('<?xml-stylesheet version="1.0" type="text/xsl" href="%s"?>\n' % xslt)
|
sys.stdout.write('<?xml-stylesheet version="1.0" type="text/xsl" href="%s"?>\n' % xslt)
|
||||||
ET.ElementTree(xresult).write(sys.stdout, "utf-8")
|
ET.ElementTree(xresult).write(sys.stdout, "utf-8")
|
||||||
|
@ -313,8 +313,12 @@ def run_cgilsxml():
|
||||||
help=u"Activer le mode CGI. Ce mode est automatiquement activé si la variable d'environnement REQUEST_METHOD existe.")
|
help=u"Activer le mode CGI. Ce mode est automatiquement activé si la variable d'environnement REQUEST_METHOD existe.")
|
||||||
OP.add_option('--cgi-allow-cache', dest='cgi_allow_cache', action='store_true',
|
OP.add_option('--cgi-allow-cache', dest='cgi_allow_cache', action='store_true',
|
||||||
help=u"En mode CGI, ne pas rajouter les en-tête désactivant la mise en cache du résultat.")
|
help=u"En mode CGI, ne pas rajouter les en-tête désactivant la mise en cache du résultat.")
|
||||||
OP.add_option('--cgi-inline', dest='cgi_inline', action='store_true',
|
OP.add_option('--cgi-content-type', '--cgict', dest='cgi_content_type',
|
||||||
help=u"En mode CGI, servir le fichier avec Content-Disposition=inline au lieu de attachment.")
|
help=u"En mode CGI, forcer le type de contenu avec lequel servir le fichier.")
|
||||||
|
OP.add_option('--cgi-content-charset', '--cgicc', dest='cgi_content_charset',
|
||||||
|
help=u"En mode CGI, forcer l'encoding de contenu avec lequel servir le fichier.")
|
||||||
|
OP.add_option('--cgi-content-disposition', '--cgicd', dest='cgi_content_disposition',
|
||||||
|
help=u"En mode CGI, forcer la disposition de contenu avec lequel servir le fichier. Les valeurs valides sont attachment (par défaut), inline, none")
|
||||||
OP.add_option('-P', '--cgi-path-info', dest='path_info',
|
OP.add_option('-P', '--cgi-path-info', dest='path_info',
|
||||||
help=u"Spécifier un chemin d'un fichier à télécharger. En mode CGI, cette valeur est prise dans la variable d'environnement PATH_INFO")
|
help=u"Spécifier un chemin d'un fichier à télécharger. En mode CGI, cette valeur est prise dans la variable d'environnement PATH_INFO")
|
||||||
OP.add_option('-Q', '--cgi-query-string', dest='query_string',
|
OP.add_option('-Q', '--cgi-query-string', dest='query_string',
|
||||||
|
@ -342,9 +346,20 @@ def run_cgilsxml():
|
||||||
sortby = o.sortby
|
sortby = o.sortby
|
||||||
|
|
||||||
environ = os.environ
|
environ = os.environ
|
||||||
cgi_cache = o.cgi_allow_cache and True or False
|
|
||||||
cgi_inline = o.cgi_inline and True or False
|
|
||||||
cgi_mode = o.cgi_mode
|
cgi_mode = o.cgi_mode
|
||||||
|
cgi_cache = o.cgi_allow_cache and True or False
|
||||||
|
cgi_content_type = o.cgi_content_type
|
||||||
|
cgi_content_charset = o.cgi_content_charset or 'utf-8'
|
||||||
|
cgi_content_disposition = o.cgi_content_disposition or 'attachment'
|
||||||
|
if cgi_content_disposition in ('attachment', 'inline'): pass
|
||||||
|
elif cgi_content_disposition == 'none': cgi_content_disposition = None
|
||||||
|
else:
|
||||||
|
if cgi_mode:
|
||||||
|
if not cgi_cache: cgi_nocache()
|
||||||
|
print "Content-Type: text/plain; charset=utf-8"
|
||||||
|
print
|
||||||
|
print "%s: Disposition de contenu invalide. Ce soit être attachment, inline ou none" % cgi_content_disposition
|
||||||
|
sys.exit(1)
|
||||||
cgi_query_string = o.query_string
|
cgi_query_string = o.query_string
|
||||||
if cgi_query_string is not None: environ['QUERY_STRING'] = cgi_query_string
|
if cgi_query_string is not None: environ['QUERY_STRING'] = cgi_query_string
|
||||||
if cgi_mode is None:
|
if cgi_mode is None:
|
||||||
|
@ -397,28 +412,30 @@ def run_cgilsxml():
|
||||||
if file is None:
|
if file is None:
|
||||||
if cgi_mode:
|
if cgi_mode:
|
||||||
if not cgi_cache: cgi_nocache()
|
if not cgi_cache: cgi_nocache()
|
||||||
print "Content-Type: text/plain; charset=UTF-8"
|
print "Content-Type: text/plain; charset=utf-8"
|
||||||
print
|
print
|
||||||
print "Impossible de trouver le fichier %s" % cgi_path_info
|
print "Impossible de trouver le fichier %s" % cgi_path_info
|
||||||
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
if cgi_mode:
|
if cgi_mode:
|
||||||
if not cgi_cache: cgi_nocache()
|
if not cgi_cache: cgi_nocache()
|
||||||
dlname = file.dlname or file.name
|
dlname = file.dlname or file.name
|
||||||
ctype = file.has_key('type') and file.type or None
|
ctype = cgi_content_type
|
||||||
|
if ctype is None: ctype = file.has_key('type') and file.type or None
|
||||||
if ctype is None: ctype, encoding = mimetypes.guess_type(dlname, False)
|
if ctype is None: ctype, encoding = mimetypes.guess_type(dlname, False)
|
||||||
if ctype is None: ctype = "application/octet-stream"
|
if ctype is None: ctype = "application/octet-stream"
|
||||||
print "Content-Type: %s; charset=utf-8" % ctype
|
print "Content-Type: %s; charset=%s" % (ctype, cgi_content_charset)
|
||||||
disposition = cgi_inline and 'inline' or 'attachment'
|
if cgi_content_disposition is not None:
|
||||||
print "Content-Disposition: %s; filename=\"%s\"" % (disposition, dlname)
|
print "Content-Disposition: %s; filename=\"%s\"" % (cgi_content_disposition, dlname)
|
||||||
print
|
print
|
||||||
inf = open(file.pf, 'rb')
|
inf = open(file.pf, 'rb')
|
||||||
try: shutil.copyfileobj(inf, sys.stdout)
|
try: shutil.copyfileobj(inf, sys.stdout)
|
||||||
finally: inf.close()
|
finally: inf.close()
|
||||||
sys.exit()
|
sys.exit(0)
|
||||||
|
|
||||||
if cgi_mode:
|
if cgi_mode:
|
||||||
if not cgi_cache: cgi_nocache()
|
if not cgi_cache: cgi_nocache()
|
||||||
print "Content-Type: text/xml; charset=UTF-8"
|
print "Content-Type: text/xml; charset=utf-8"
|
||||||
print
|
print
|
||||||
print_files(files, fgroups, cgi_select_group, cgi_script_name, cgi_xslt,
|
print_files(files, fgroups, cgi_select_group, cgi_script_name, cgi_xslt,
|
||||||
cgi_query_string, o.includes, o.excludes, o.prefix)
|
cgi_query_string, o.includes, o.excludes, o.prefix)
|
||||||
|
|
Loading…
Reference in New Issue