From 7223a8b292316330a08472469b4e6c474b0be7fb Mon Sep 17 00:00:00 2001 From: Jephte CLAIN Date: Mon, 18 Aug 2014 17:11:16 +0400 Subject: [PATCH] =?UTF-8?q?simplifier=20le=20code,=20et=20=C3=A9viter=20le?= =?UTF-8?q?s=20/=20multiples=20=C3=A0=20la=20suite?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mkRewriteRules | 87 ++++++++++++++++++++++++++------------------------ 1 file changed, 46 insertions(+), 41 deletions(-) diff --git a/mkRewriteRules b/mkRewriteRules index 7cdf9f7..9205455 100755 --- a/mkRewriteRules +++ b/mkRewriteRules @@ -82,6 +82,19 @@ Dans les exemples donnés ci-dessus, $URL est l'\''url générée par la réécr et $proxy_acls la valeur du champ proxy_acls spécifiée ci-dessus.' } +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" +} + proxy_enabled= infile= outfile="RewriteRules.conf" @@ -136,7 +149,7 @@ for rule in "${rules[@]}"; do echo "${rule#=}" >>"$outfile" continue fi - + IFS=:; set -- $rule; unset IFS index=1 done= @@ -158,6 +171,10 @@ for rule in "${rules[@]}"; do index=$(($index + 1)) done + # mettre en forme prefix s'il est défini + [ -n "$prefix" ] && prefix="$prefix +" + if [ "$thishost" == "$host" ]; then host= fi @@ -188,34 +205,28 @@ for rule in "${rules[@]}"; do if [ -n "$host" ]; then # sur un autre hôte if [ -n "$noslash" ]; then - echo "${prefix:+$prefix -}RewriteRule ^/$src${trail:+(.*)} $prot://$host/cgi-bin/WebObjects/$dest${suffix:+/$suffix}${trail:+\$1} [L${options:+,$options}]" >>"$outfile" - url="http://$thishost/$usrc" - proxy_url="$prot://$host/cgi-bin/WebObjects/$dest${suffix:+/$suffix}" + 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" else - echo "${prefix:+$prefix -}RewriteRule ^/$src\$ /$src/" >>"$outfile" - echo "${prefix:+$prefix -}RewriteRule ^/$src/(.*) $prot://$host/cgi-bin/WebObjects/$dest${suffix:+/$suffix}/\$1 [L${options:+,$options}]" >>"$outfile" - url="http://$thishost/$usrc/" - proxy_url="$prot://$host/cgi-bin/WebObjects/$dest${suffix:+/$suffix}/" + 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/" fi else # sur le même hôte if [ -n "$noslash" ]; then - echo "${prefix:+$prefix -}RewriteRule ^/$src${trail:+(.*)} /cgi-bin/WebObjects/$dest${suffix:+/$suffix}${trail:+\$1} [L,P${options:+,$options}]" >>"$outfile" - url="http://$thishost/$usrc" + 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" proxy_use=1 - proxy_url="http://$thishost/cgi-bin/WebObjects/$dest${suffix:+/$suffix}" else - echo "${prefix:+$prefix -}RewriteRule ^/$src\$ /$src/" >>"$outfile" - echo "${prefix:+$prefix -}RewriteRule ^/$src/(.*) /cgi-bin/WebObjects/$dest${suffix:+/$suffix}/\$1 [L,P${options:+,$options}]" >>"$outfile" - url="http://$thishost/$usrc/" + 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/" proxy_use=1 - proxy_url="http://$thishost/cgi-bin/WebObjects/$dest${suffix:+/$suffix}/" fi fi else @@ -223,32 +234,26 @@ for rule in "${rules[@]}"; do if [ -n "$host" ]; then # sur un autre hôte if [ -n "$noslash" ]; then - echo "${prefix:+$prefix -}RewriteRule ^/$src${trail:+(.*)} $prot://$host/$dest${suffix:+/$suffix}${trail:+\$1} [L${options:+,$options}]" >>"$outfile" - url="http://$thishost/$usrc" - proxy_url="$prot://$host/$dest${suffix:+/$suffix}" + 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" else - echo "${prefix:+$prefix -}RewriteRule ^/$src\$ /$src/" >>"$outfile" - echo "${prefix:+$prefix -}RewriteRule ^/$src/(.*) $prot://$host/$dest${suffix:+/$suffix}/\$1 [L${options:+,$options}]" >>"$outfile" - url="http://$thishost/$usrc/" - proxy_url="$prot://$host/$dest${suffix:+/$suffix}/" + 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/" fi else # sur le même hôte if [ -n "$noslash" ]; then - echo "${prefix:+$prefix -}RewriteRule ^/$src${trail:+(.*)} /$dest${suffix:+/$suffix}${trail:+\$1}${options:+ [$options]}" >>"$outfile" - url="http://$thishost/$usrc" - proxy_url="http://$thishost/$dest${suffix:+/$suffix}" + 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" else - echo "${prefix:+$prefix -}RewriteRule ^/$src\$ /$src/" >>"$outfile" - echo "${prefix:+$prefix -}RewriteRule ^/$src/(.*) /$dest${suffix:+/$suffix}/\$1${options:+ [$options]}" >>"$outfile" - url="http://$thishost/$usrc/" - proxy_url="http://$thishost/$dest${suffix:+/$suffix}/" + 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/" fi fi fi