2013-08-27 15:14:44 +04:00
|
|
|
#!/bin/bash
|
|
|
|
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
2014-07-07 22:06:38 +04:00
|
|
|
source "$(dirname "$0")/lib/ulib/ulib" || exit 1
|
|
|
|
urequire DEFAULTS
|
2013-08-27 15:14:44 +04:00
|
|
|
|
|
|
|
function display_help() {
|
|
|
|
uecho "$scriptname: Créer un fichier de redirections pour Apache à partir d'un certain
|
|
|
|
nombre de règles
|
|
|
|
|
|
|
|
USAGE
|
|
|
|
$scriptname -f rewrite.rules [-o RewriteRules.conf] [-w RewriteRules.html] host
|
|
|
|
|
|
|
|
OPTIONS
|
|
|
|
-p Générer les directives <Proxy...> et tenir compte de proxy_acls
|
|
|
|
Par défaut, le champ proxy_acls est ignoré
|
|
|
|
|
|
|
|
FORMAT des règles de mapping"'
|
|
|
|
============================
|
|
|
|
|
|
|
|
Les commentaires commencent par le signe "#"
|
|
|
|
Les règles sont de la forme:
|
|
|
|
src:dest:host:suffix:OPTS:prot:proxy_acls
|
|
|
|
^prefix
|
|
|
|
=literal
|
|
|
|
|
|
|
|
prot vaut par défaut http. Il peut valoir aussi https
|
|
|
|
|
|
|
|
Si dest ou suffix se terminent par $, on est en mode NO_SLASH
|
|
|
|
En mode NO_SLASH, si src se termine par $, on est en mode NO_TRAIL
|
|
|
|
|
|
|
|
* Si dest est de la forme Application.woa
|
|
|
|
En mode NO_SLASH, on génère
|
|
|
|
RewriteRule ^/src(.*) [prot://host]/cgi-bin/WebObjects/dest[/suffix]$1 [L,OPTS]
|
|
|
|
En mode NO_SLASH+NO_TRAIL, on génère
|
|
|
|
RewriteRule ^/src [prot://host]/cgi-bin/WebObjects/dest[/suffix] [L,OPTS]
|
|
|
|
En mode normal, on génère
|
|
|
|
RewriteRule ^/src$ /src/
|
|
|
|
RewriteRule ^/src/(.*) [prot://host]/cgi-bin/WebObjects/dest[/suffix]/$1 [L,OPTS]
|
|
|
|
|
|
|
|
* Si dest n'\''est pas de la forme Application.woa
|
|
|
|
En mode NO_SLASH, on génère
|
|
|
|
RewriteRule ^/src(.*) [prot://host]/dest[/suffix]$1 [L,OPTS]
|
|
|
|
En mode NO_SLASH+NO_TRAIL, on génère
|
|
|
|
RewriteRule ^/src [prot://host]/dest[/suffix] [L,OPTS]
|
|
|
|
En mode normal, on génère
|
|
|
|
RewriteRule ^/src$ /src/
|
|
|
|
RewriteRule ^/src/(.*) /dest[/suffix]/$1 [L,OPTS]
|
|
|
|
|
|
|
|
Si une règle est précédée d'\''une ou plusieurs lignes de la forme "^prefix",
|
|
|
|
ces lignes sont copiées avant chacune des commandes RewriteRule générées
|
|
|
|
pour une règle. Ceci permet d'\''ajouter des conditions avec RewriteCond pour
|
|
|
|
une règle. e.g.
|
|
|
|
^RewriteCond %{REMOTE_ADDR} 10\..*
|
|
|
|
src:dest.woa
|
|
|
|
qui génère:
|
|
|
|
RewriteCond %{REMOTE_ADDR} 10\..*
|
|
|
|
RewriteRule ^/src$ /src/
|
|
|
|
RewriteCond %{REMOTE_ADDR} 10\..*
|
|
|
|
RewriteRule ^/src/(.*) /cgi-bin/WebObjects/dest.woa/$1 [L]
|
|
|
|
|
|
|
|
Une ligne de la forme "=literal" est recopiée sans modifications (sans le "=")
|
|
|
|
dans le fichier de sortie.
|
|
|
|
|
|
|
|
proxy_acls est utilisé si l'\''option -p est spécifiée et OPTS contient P (comme
|
|
|
|
proxy), ou si le mode de réécriture requière l'\''utilisation d'\''un proxy.
|
|
|
|
|
|
|
|
* Avec la valeur "None", aucune directive <Proxy> n'\''est générée
|
|
|
|
* Si aucune valeur n'\''est spécifiée, la directive suivante est générée:
|
|
|
|
<Proxy $URL>
|
|
|
|
AddDefaultCharset off
|
|
|
|
Order Deny,Allow
|
|
|
|
Allow from all
|
|
|
|
</Proxy>
|
|
|
|
* Si une valeur est spécifiée, la directive suivante est générée:
|
|
|
|
<Proxy $URL>
|
|
|
|
AddDefaultCharset off
|
|
|
|
Order Allow,Deny
|
|
|
|
Allow from $proxy_acls
|
|
|
|
</Proxy>
|
|
|
|
|
|
|
|
Dans les exemples donnés ci-dessus, $URL est l'\''url générée par la réécriture,
|
|
|
|
et $proxy_acls la valeur du champ proxy_acls spécifiée ci-dessus.'
|
|
|
|
}
|
|
|
|
|
2014-08-18 17:11:16 +04:00
|
|
|
function joinurl() {
|
|
|
|
# joindre chaque élément de $1..@ par /, en évitant les slashes en double
|
|
|
|
local i url
|
|
|
|
for i in "$@"; do
|
|
|
|
if [ -n "$url" ]; then
|
|
|
|
url="${url%/}/${i#/}"
|
|
|
|
else
|
|
|
|
url="$i"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
[ -n "$url" ] && echo "$url"
|
|
|
|
}
|
|
|
|
|
2013-08-27 15:14:44 +04:00
|
|
|
proxy_enabled=
|
|
|
|
infile=
|
|
|
|
outfile="RewriteRules.conf"
|
|
|
|
htmlfile=
|
|
|
|
host=
|
|
|
|
parse_opts "${PRETTYOPTS[@]}" \
|
|
|
|
--help '$exit_with display_help' \
|
|
|
|
-p proxy_enabled \
|
|
|
|
-f: infile= \
|
|
|
|
-o: outfile= \
|
|
|
|
-w: htmlfile= \
|
|
|
|
@ args -- "$@" && set -- "${args[@]}" || die "$args"
|
|
|
|
|
|
|
|
[ -n "$infile" ] || die "Il faut spécifier le fichier de règles"
|
|
|
|
[ -f "$infile" ] || die "Fichier de règles non trouvé: $(ppath "$infile")"
|
|
|
|
|
|
|
|
thishost="$1"
|
|
|
|
[ -n "$thishost" ] || die "Il faut spécifier l'hôte pour lequel créer le fichier de configuration"
|
|
|
|
|
|
|
|
function has_proxy() {
|
|
|
|
# vérifier que les options $1 contiennent 'P'
|
|
|
|
local options
|
|
|
|
array_split options "$1" ","
|
|
|
|
array_contains options P
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ -n "$htmlfile" ]; then
|
|
|
|
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
|
|
<!-- -*- coding: utf-8 mode: html -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
|
|
|
-->
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
|
|
<title>'"$thishost</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h2>$thishost</h2>
|
|
|
|
<ul>" >"$htmlfile"
|
|
|
|
fi
|
|
|
|
|
|
|
|
>"$outfile"
|
|
|
|
array_from_lines rules "$(<"$infile" filter_comment)"
|
|
|
|
prefix=
|
|
|
|
for rule in "${rules[@]}"; do
|
|
|
|
if beginswith "$rule" ^; then
|
|
|
|
# Collecter les préfixe pour la règle suivante
|
|
|
|
prefix="${prefix:+$prefix
|
|
|
|
}${rule#^}"
|
|
|
|
continue
|
|
|
|
elif beginswith "$rule" =; then
|
|
|
|
# ligne litérale
|
|
|
|
echo "${rule#=}" >>"$outfile"
|
|
|
|
continue
|
|
|
|
fi
|
2014-08-18 17:11:16 +04:00
|
|
|
|
2013-08-27 15:14:44 +04:00
|
|
|
IFS=:; set -- $rule; unset IFS
|
|
|
|
index=1
|
|
|
|
done=
|
|
|
|
while [ -z "$done" ]; do
|
|
|
|
current="$1"; shift
|
|
|
|
while endswith "$current" "\\"; do
|
|
|
|
current="${current%\\}:$1"; shift
|
|
|
|
done
|
|
|
|
case $index in
|
|
|
|
1) src="$current";;
|
|
|
|
2) dest="$current";;
|
|
|
|
3) host="$current";;
|
|
|
|
4) suffix="$current";;
|
|
|
|
5) options="$current";;
|
|
|
|
6) prot="${current:-http}";;
|
|
|
|
7) proxy_acls="$current";;
|
|
|
|
*) done=1;;
|
|
|
|
esac
|
|
|
|
index=$(($index + 1))
|
|
|
|
done
|
|
|
|
|
2014-08-18 17:11:16 +04:00
|
|
|
# mettre en forme prefix s'il est défini
|
|
|
|
[ -n "$prefix" ] && prefix="$prefix
|
|
|
|
"
|
|
|
|
|
2013-08-27 15:14:44 +04:00
|
|
|
if [ "$thishost" == "$host" ]; then
|
|
|
|
host=
|
|
|
|
fi
|
|
|
|
|
|
|
|
usrc="$src"
|
|
|
|
|
|
|
|
trail=1
|
|
|
|
if endswith "$src" '$'; then
|
|
|
|
trail=
|
|
|
|
usrc="${src%$}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
noslash=
|
|
|
|
if endswith "$suffix" '$'; then
|
|
|
|
noslash=1
|
|
|
|
suffix="${suffix%$}"
|
|
|
|
fi
|
|
|
|
if endswith "$dest" '$'; then
|
|
|
|
noslash=1
|
|
|
|
dest="${dest%$}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
proxy_url=
|
|
|
|
proxy_use=
|
|
|
|
|
|
|
|
if endswith "$dest" .woa; then
|
|
|
|
# lien vers une application
|
|
|
|
if [ -n "$host" ]; then
|
|
|
|
# sur un autre hôte
|
|
|
|
if [ -n "$noslash" ]; then
|
2014-08-18 17:11:16 +04:00
|
|
|
echo "${prefix}RewriteRule ^/$src${trail:+(.*)} $(joinurl "$prot://$host/cgi-bin/WebObjects" "$dest" "$suffix" ${trail:+\$1}) [L${options:+,$options}]" >>"$outfile"
|
|
|
|
setx url joinurl "http://$thishost" "$usrc"
|
|
|
|
setx proxy_url joinurl "prot://$host/cgi-bin/WebObjects" "$dest" "$suffix"
|
2013-08-27 15:14:44 +04:00
|
|
|
else
|
2014-08-18 17:11:16 +04:00
|
|
|
echo "${prefix}RewriteRule ^/$src\$ /$src/" >>"$outfile"
|
|
|
|
echo "${prefix}RewriteRule ^/$src/(.*) $(joinurl "$prot://$host/cgi-bin/WebObjects" "$dest" "$suffix" "\$1") [L${options:+,$options}]" >>"$outfile"
|
|
|
|
setx url joinurl "http://$thishost" "$usrc/"
|
|
|
|
setx proxy_url joinurl "prot://$host/cgi-bin/WebObjects" "$dest" "$suffix/"
|
2013-08-27 15:14:44 +04:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
# sur le même hôte
|
|
|
|
if [ -n "$noslash" ]; then
|
2014-08-18 17:11:16 +04:00
|
|
|
echo "${prefix}RewriteRule ^/$src${trail:+(.*)} $(joinurl /cgi-bin/WebObjects "$dest" "$suffix" ${trail:+\$1}) [L,P${options:+,$options}]" >>"$outfile"
|
|
|
|
setx url joinurl "http://$thishost" "$usrc"
|
|
|
|
setx proxy_url joinurl "prot://$thishost/cgi-bin/WebObjects" "$dest" "$suffix"
|
2013-08-27 15:14:44 +04:00
|
|
|
proxy_use=1
|
|
|
|
else
|
2014-08-18 17:11:16 +04:00
|
|
|
echo "${prefix}RewriteRule ^/$src\$ /$src/" >>"$outfile"
|
|
|
|
echo "${prefix}RewriteRule ^/$src/(.*) $(joinurl /cgi-bin/WebObjects "$dest" "$suffix" "\$1") [L,P${options:+,$options}]" >>"$outfile"
|
|
|
|
setx url joinurl "http://$thishost" "$usrc/"
|
|
|
|
setx proxy_url joinurl "prot://$thishost/cgi-bin/WebObjects" "$dest" "$suffix/"
|
2013-08-27 15:14:44 +04:00
|
|
|
proxy_use=1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
# lien vers une url
|
|
|
|
if [ -n "$host" ]; then
|
|
|
|
# sur un autre hôte
|
|
|
|
if [ -n "$noslash" ]; then
|
2014-08-18 17:11:16 +04:00
|
|
|
echo "${prefix}RewriteRule ^/$src${trail:+(.*)} $(joinurl "$prot://$host" "$dest" "$suffix" ${trail:+\$1}) [L${options:+,$options}]" >>"$outfile"
|
|
|
|
setx url joinurl "http://$thishost" "$usrc"
|
|
|
|
setx proxy_url joinurl "prot://$host" "$dest" "$suffix"
|
2013-08-27 15:14:44 +04:00
|
|
|
else
|
2014-08-18 17:11:16 +04:00
|
|
|
echo "${prefix}RewriteRule ^/$src\$ /$src/" >>"$outfile"
|
|
|
|
echo "${prefix}RewriteRule ^/$src/(.*) $(joinurl "$prot://$host" "$dest" "$suffix" "\$1") [L${options:+,$options}]" >>"$outfile"
|
|
|
|
setx url joinurl "http://$thishost" "$usrc/"
|
|
|
|
setx proxy_url joinurl "$prot://$host" "$dest" "$suffix/"
|
2013-08-27 15:14:44 +04:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
# sur le même hôte
|
|
|
|
if [ -n "$noslash" ]; then
|
2014-08-18 17:11:16 +04:00
|
|
|
echo "${prefix}RewriteRule ^/$src${trail:+(.*)} $(joinurl / "$dest" "$suffix" ${trail:+\$1})${options:+ [$options]}" >>"$outfile"
|
|
|
|
setx url joinurl "http://$thishost" "$usrc"
|
|
|
|
setx proxy_url joinurl "http://$thishost" "$dest" "$suffix"
|
2013-08-27 15:14:44 +04:00
|
|
|
else
|
2014-08-18 17:11:16 +04:00
|
|
|
echo "${prefix}RewriteRule ^/$src\$ /$src/" >>"$outfile"
|
|
|
|
echo "${prefix}RewriteRule ^/$src/(.*) $(joinurl / "$dest" "$suffix" "\$1")${options:+ [$options]}" >>"$outfile"
|
|
|
|
setx url joinurl "http://$thishost" "$usrc/"
|
|
|
|
setx proxy_url joinurl "http://$thishost" "$dest" "$suffix/"
|
2013-08-27 15:14:44 +04:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
has_proxy "$options" && proxy_use=1
|
|
|
|
if [ -n "$proxy_enabled" -a -n "$proxy_use" ]; then
|
|
|
|
if [ "$proxy_acls" == "None" ]; then
|
|
|
|
:
|
|
|
|
elif [ -z "$proxy_acls" ]; then
|
|
|
|
echo "\
|
|
|
|
<Proxy $proxy_url*>
|
|
|
|
AddDefaultCharset off
|
|
|
|
Order Deny,Allow
|
|
|
|
Allow from all
|
|
|
|
</Proxy>" >>"$outfile"
|
|
|
|
else
|
|
|
|
echo "\
|
|
|
|
<Proxy $proxy_url*>
|
|
|
|
AddDefaultCharset off
|
|
|
|
Order Allow,Deny
|
|
|
|
Allow from $proxy_acls
|
|
|
|
</Proxy>" >>"$outfile"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "" >>"$outfile"
|
|
|
|
if [ -n "$htmlfile" ]; then
|
|
|
|
echo "<li><a href=\"$url\">$url</a></li>" >>"$htmlfile"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Réinitialiser les préfixes pour chaque règle
|
|
|
|
prefix=
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ -n "$htmlfile" ]; then
|
|
|
|
echo '</ul>
|
|
|
|
</body>
|
|
|
|
</html>' >>"$htmlfile"
|
|
|
|
fi
|