From 3d591cbbcfa655560895bc437a4c929b22048aed Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 12 Mar 2024 11:43:07 +0100 Subject: [PATCH 1/6] refs #7022 perf: CGI --- images/.htaccess | 4 ++++ images/Dockerfile | 32 +++++++++++++++++++++++++ images/apache2.conf | 11 +++++++++ images/docker-compose.yml | 11 +++++++++ images/www/script.pl | 50 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+) create mode 100644 images/.htaccess create mode 100644 images/Dockerfile create mode 100644 images/apache2.conf create mode 100644 images/docker-compose.yml create mode 100644 images/www/script.pl diff --git a/images/.htaccess b/images/.htaccess new file mode 100644 index 0000000000..2b778acaf1 --- /dev/null +++ b/images/.htaccess @@ -0,0 +1,4 @@ + + RewriteEngine On + RewriteRule ^(.+)\.(jpg|png|jpeg)$ script.pl?value=$1.$2&name=$1 [L,QSA] + diff --git a/images/Dockerfile b/images/Dockerfile new file mode 100644 index 0000000000..d267dcfce9 --- /dev/null +++ b/images/Dockerfile @@ -0,0 +1,32 @@ +FROM httpd:latest + +RUN apt-get update && apt-get install -y curl libcgi-pm-perl + +# Habilitar mod_rewrite +RUN sed -i '/mod_rewrite/s/^#//g' /usr/local/apache2/conf/httpd.conf +RUN echo "AddHandler cgi-script .cgi .pl" >> /usr/local/apache2/conf/httpd.conf +RUN echo "Include conf/extra/httpd-vhosts.conf" >> /usr/local/apache2/conf/httpd.conf + +# Habilitar mod_cgi +RUN sed -i '/mod_cgi/s/^#//g' /usr/local/apache2/conf/httpd.conf +RUN echo "LoadModule cgid_module modules/mod_cgid.so" >> /usr/local/apache2/conf/httpd.conf + +# Habilitar reescritura +RUN echo "RewriteEngine On" >> /usr/local/apache2/conf/extra/httpd-vhosts.conf + + + +# Copiar la configuración de Apache2 y los archivos del sitio +COPY apache2.conf /usr/local/apache2/conf/extra/httpd-vhosts.conf + +# RUN chmod +x www/script.pl +COPY www/ /usr/local/apache2/htdocs/ + +# Copia el archivo .htaccess para configurar el directorio htdocs/images +COPY .htaccess /usr/local/apache2/htdocs/ + +# Habilita permisos de ejecución +RUN chmod +x /usr/local/apache2/htdocs/script.pl + +# CMD y ENTRYPOINT para iniciar Apache2, según la imagen original +CMD ["httpd-foreground"] diff --git a/images/apache2.conf b/images/apache2.conf new file mode 100644 index 0000000000..9e5018bd31 --- /dev/null +++ b/images/apache2.conf @@ -0,0 +1,11 @@ +# apache2.conf + + DocumentRoot /usr/local/apache2/htdocs + + Options Indexes FollowSymLinks + AllowOverride All + Require all granted + Options +ExecCGI + AddHandler cgi-script .cgi .pl + + diff --git a/images/docker-compose.yml b/images/docker-compose.yml new file mode 100644 index 0000000000..651face426 --- /dev/null +++ b/images/docker-compose.yml @@ -0,0 +1,11 @@ +version: '3' +services: + apache: + volumes: + - ./images:/usr/local/apache2/htdocs/images/ + build: + context: . + dockerfile: Dockerfile + ports: + - "8080:80" + diff --git a/images/www/script.pl b/images/www/script.pl new file mode 100644 index 0000000000..0fc4a24c1a --- /dev/null +++ b/images/www/script.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl +use strict; +use warnings; + +use Digest::SHA qw(sha1_hex); +use CGI; + +my $cgi = CGI->new; +my $input_name = $cgi->param('name') || "default"; +my $input_value = $cgi->param('value') || "default"; +my $new_path; + +# Dividir la ruta usando el símbolo "/" para extraaer el nombre del archivo SIN extension +my $fileName= (split('/', $input_name))[-1]; + +# Dividir la ruta usando el símbolo "/" para extraaer el nombre del archivo CON extension +my $file= (split('/', $input_value))[-1]; +my $exists = -e "$input_value"; + +# Compruebo si existe el archivo en la caperta general o no +if($exists) { + $new_path ="$input_value"; +} else { + # Calcula el valor hash MD5 del string + my $hash_value = sha1_hex($fileName); + + # Obtiene solo los 2 primeros caracteres del hash MD5 + my $first_characters = substr($hash_value, 0, 2); + + # Obtiene solo los 2 segundos caracteres del hash MD5 + my $second_characters = substr($hash_value, 1, 2); + + $new_path ="images/$first_characters/$second_characters/$file"; +} +# Verifica si $imagen está definida antes de intentar usarla +if (defined $new_path) { + abrir_imagen($new_path); +} +sub abrir_imagen { + my ($ruta) = @_; + print "Content-type: image/jpeg\n\n"; + open my $imagen, '<', $ruta or die "No se pudo abrir la imagen: $!"; + + # Imprime los bytes de la imagen directamente en la salida estándar + binmode STDOUT; + print while <$imagen>; + + # Cierra el archivo + close $imagen; +} -- 2.40.1 From 30d417473b6cd8cedec0a43412adeab996256dd9 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Wed, 17 Apr 2024 17:12:46 +0200 Subject: [PATCH 2/6] feat(CGI): refs #5576 RewriteMap --- images/Dockerfile | 5 +-- images/apache2.conf | 33 +++++++++++++++---- images/www/extract_characters.pl | 15 +++++++++ images/www/script.pl | 49 +++++----------------------- images/www/script2.pl | 55 ++++++++++++++++++++++++++++++++ images/www/script3.pl | 3 ++ 6 files changed, 110 insertions(+), 50 deletions(-) create mode 100644 images/www/extract_characters.pl create mode 100644 images/www/script2.pl create mode 100644 images/www/script3.pl diff --git a/images/Dockerfile b/images/Dockerfile index d267dcfce9..31a40d18a9 100644 --- a/images/Dockerfile +++ b/images/Dockerfile @@ -22,11 +22,12 @@ COPY apache2.conf /usr/local/apache2/conf/extra/httpd-vhosts.conf # RUN chmod +x www/script.pl COPY www/ /usr/local/apache2/htdocs/ -# Copia el archivo .htaccess para configurar el directorio htdocs/images -COPY .htaccess /usr/local/apache2/htdocs/ # Habilita permisos de ejecución RUN chmod +x /usr/local/apache2/htdocs/script.pl +RUN chmod +x /usr/local/apache2/htdocs/script2.pl +RUN chmod +x /usr/local/apache2/htdocs/script3.pl +RUN chmod +x /usr/local/apache2/htdocs/extract_characters.pl # CMD y ENTRYPOINT para iniciar Apache2, según la imagen original CMD ["httpd-foreground"] diff --git a/images/apache2.conf b/images/apache2.conf index 9e5018bd31..2bfea11409 100644 --- a/images/apache2.conf +++ b/images/apache2.conf @@ -1,11 +1,30 @@ # apache2.conf DocumentRoot /usr/local/apache2/htdocs - - Options Indexes FollowSymLinks - AllowOverride All - Require all granted - Options +ExecCGI - AddHandler cgi-script .cgi .pl - + RewriteMap sub "prg:/usr/local/apache2/htdocs/script3.pl" + + Options Indexes FollowSymLinks + AllowOverride All + Require all granted + Options +ExecCGI + AddHandler cgi-script .cgi .pl + RewriteEngine On + +# RewriteCond %{REQUEST_URI} ^/(.+)$ +# RewriteRule .* - [E=REDIRECT_STATUS:%{REQUEST_URI}] + +# RewriteCond %{ENV:REDIRECT_STATUS} ^(.+)$ +# RewriteRule .* /images/%1 [R,L] + # RewriteCond %{ENV:REDIRECT_STATUS} 75/50 + # /images/75/50 + + # RewriteRule ^(.+)\.(jpg|png|jpeg)$ script.pl?value=/images/${sub:%0}/${sub:%2}/${sub:%1} [L,QSA] + RewriteRule ^(.+)\.(jpg|png|jpeg)$ /asd [L] + + # RewriteCond %{REQUEST_URI} !^/images/asd/ + # RewriteRule ^(.*)$ /75/$1 [L] + # RewriteRule ^(.+)\.(jpg|png|jpeg)$ /${sub:%2}/$1 [L] + + + diff --git a/images/www/extract_characters.pl b/images/www/extract_characters.pl new file mode 100644 index 0000000000..38f05a0a37 --- /dev/null +++ b/images/www/extract_characters.pl @@ -0,0 +1,15 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +# Lee la ruta de la URL desde la variable de entorno QUERY_STRING +my $query_string = $ENV{'QUEsRY_STRING'}; + +# Extrae los dos primeros caracteres del nombre del archivo +my $first_two_characters = substr($query_string, 0, 2); + +# Imprime la ruta que se pasará al RewriteRule +print "Content-type: text/plain\r\n"; +print "\r\n"; +print "/$first_two_characters/"; diff --git a/images/www/script.pl b/images/www/script.pl index 0fc4a24c1a..9954c7c0ba 100644 --- a/images/www/script.pl +++ b/images/www/script.pl @@ -5,46 +5,13 @@ use warnings; use Digest::SHA qw(sha1_hex); use CGI; + my $cgi = CGI->new; -my $input_name = $cgi->param('name') || "default"; -my $input_value = $cgi->param('value') || "default"; -my $new_path; +my $input_value = $cgi->param('value') || "defaudlt"; +# Imprime el encabezado HTTP +print "Content-type: text/plain\r\n"; +print "\r\n"; # Imprime una línea en blanco para indicar el final del encabezado -# Dividir la ruta usando el símbolo "/" para extraaer el nombre del archivo SIN extension -my $fileName= (split('/', $input_name))[-1]; - -# Dividir la ruta usando el símbolo "/" para extraaer el nombre del archivo CON extension -my $file= (split('/', $input_value))[-1]; -my $exists = -e "$input_value"; - -# Compruebo si existe el archivo en la caperta general o no -if($exists) { - $new_path ="$input_value"; -} else { - # Calcula el valor hash MD5 del string - my $hash_value = sha1_hex($fileName); - - # Obtiene solo los 2 primeros caracteres del hash MD5 - my $first_characters = substr($hash_value, 0, 2); - - # Obtiene solo los 2 segundos caracteres del hash MD5 - my $second_characters = substr($hash_value, 1, 2); - - $new_path ="images/$first_characters/$second_characters/$file"; -} -# Verifica si $imagen está definida antes de intentar usarla -if (defined $new_path) { - abrir_imagen($new_path); -} -sub abrir_imagen { - my ($ruta) = @_; - print "Content-type: image/jpeg\n\n"; - open my $imagen, '<', $ruta or die "No se pudo abrir la imagen: $!"; - - # Imprime los bytes de la imagen directamente en la salida estándar - binmode STDOUT; - print while <$imagen>; - - # Cierra el archivo - close $imagen; -} +# Imprime el texto deseado +print $input_value ; +print "¡Hola, mundo!\n"; diff --git a/images/www/script2.pl b/images/www/script2.pl new file mode 100644 index 0000000000..3fdce0362a --- /dev/null +++ b/images/www/script2.pl @@ -0,0 +1,55 @@ +#!/usr/bin/perl +use strict; +use warnings; + +use Digest::SHA qw(sha1_hex); +use CGI; + +# Imprime un texto cualquiera por consola + +my $cgi = CGI->new; +my $input_name = $cgi->param('name') || "default"; +my $input_value = $cgi->param('value') || "default"; +my $new_path; + +# Imprime el encabezado HTTP +print "Content-type: text/plain\r\n"; +print "\r\n"; # Imprime una línea en blanco para indicar el final del encabezado + +# Imprime el texto deseado +print "¡Hola, mundo!\n"; + +# Dividir la ruta usando el símbolo "/" para extraaer el nombre del archivo SIN extension +my $fileName= (split('/', $input_name))[-1]; + +# Dividir la ruta usando el símbolo "/" para extraaer el nombre del archivo CON extension +# my $file=$input_value; +my $file= (split('/', $input_value))[-1]; +my $exists = -e "$input_value"; + +# Compruebo si existe el archivo en la caperta general o no + my $hash_value = sha1_hex($fileName); + + # Obtiene solo los 2 primeros caracteres del hash MD5 + my $first_characters = substr($hash_value, 0, 2); + + # Obtiene solo los 2 segundos caracteres del hash MD5 + my $second_characters = substr($hash_value, 1, 2); + # print "images/$first_characters/$second_characters/$file"; +if($exists) { + $new_path ="$input_value"; +} else { + # Calcula el valor hash MD5 del string + + $new_path ="images/$first_characters/$second_characters/$file"; +} +# Verifica si $imagen está definida antes de intentar usarla +if (defined $new_path) { + xabrir_imagen($new_path); +} +sub abrir_imagen { + my ($ruta) = @_; + + # print "Ruta\n"; + print $ruta . "\n"; +} diff --git a/images/www/script3.pl b/images/www/script3.pl new file mode 100644 index 0000000000..94126d5cf1 --- /dev/null +++ b/images/www/script3.pl @@ -0,0 +1,3 @@ +$ENV{'REDIRECT_STATUS'} = '75/50'; +print "Content-type: text/html\n\n"; +print "Content"; -- 2.40.1 From 7b6a7a438690e6d89aa2cc4fe0b1f1b27237e423 Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 18 Jun 2024 13:00:10 +0200 Subject: [PATCH 3/6] refs #7022 First script --- images/.htaccess | 4 --- images/Dockerfile | 34 +++++++------------- images/apache2.conf | 38 ++++++++-------------- images/docker-compose.yml | 3 +- images/www/extract_characters.pl | 15 --------- images/www/redirect.pl | 27 ++++++++++++++++ images/www/script.pl | 17 ---------- images/www/script2.pl | 55 -------------------------------- images/www/script3.pl | 3 -- 9 files changed, 52 insertions(+), 144 deletions(-) delete mode 100644 images/.htaccess delete mode 100644 images/www/extract_characters.pl create mode 100755 images/www/redirect.pl delete mode 100644 images/www/script.pl delete mode 100644 images/www/script2.pl delete mode 100644 images/www/script3.pl diff --git a/images/.htaccess b/images/.htaccess deleted file mode 100644 index 2b778acaf1..0000000000 --- a/images/.htaccess +++ /dev/null @@ -1,4 +0,0 @@ - - RewriteEngine On - RewriteRule ^(.+)\.(jpg|png|jpeg)$ script.pl?value=$1.$2&name=$1 [L,QSA] - diff --git a/images/Dockerfile b/images/Dockerfile index 31a40d18a9..0bb1e74f4a 100644 --- a/images/Dockerfile +++ b/images/Dockerfile @@ -1,33 +1,21 @@ FROM httpd:latest -RUN apt-get update && apt-get install -y curl libcgi-pm-perl +RUN apt-get update \ + && apt-get install -y curl libcgi-pm-perl \ + && rm -rf /var/lib/apt/lists/* -# Habilitar mod_rewrite -RUN sed -i '/mod_rewrite/s/^#//g' /usr/local/apache2/conf/httpd.conf -RUN echo "AddHandler cgi-script .cgi .pl" >> /usr/local/apache2/conf/httpd.conf -RUN echo "Include conf/extra/httpd-vhosts.conf" >> /usr/local/apache2/conf/httpd.conf +ARG HTTPD_PATH="/usr/local/apache2/conf/httpd.conf" -# Habilitar mod_cgi -RUN sed -i '/mod_cgi/s/^#//g' /usr/local/apache2/conf/httpd.conf -RUN echo "LoadModule cgid_module modules/mod_cgid.so" >> /usr/local/apache2/conf/httpd.conf +RUN echo "ServerName localhost" >> ${HTTPD_PATH} \ + && echo "Include conf/extra/httpd-vhosts.conf" >> ${HTTPD_PATH} \ + && echo "LoadModule rewrite_module modules/mod_rewrite.so" >> ${HTTPD_PATH} \ + && echo "LoadModule cgid_module modules/mod_cgid.so" >> ${HTTPD_PATH} \ + && echo "LoadModule proxy_module modules/mod_proxy.so" >> ${HTTPD_PATH} \ + && echo "LoadModule proxy_http_module modules/mod_proxy_http.so" >> ${HTTPD_PATH} -# Habilitar reescritura -RUN echo "RewriteEngine On" >> /usr/local/apache2/conf/extra/httpd-vhosts.conf - - - -# Copiar la configuración de Apache2 y los archivos del sitio COPY apache2.conf /usr/local/apache2/conf/extra/httpd-vhosts.conf - -# RUN chmod +x www/script.pl COPY www/ /usr/local/apache2/htdocs/ +RUN chmod +x /usr/local/apache2/htdocs/redirect.pl -# Habilita permisos de ejecución -RUN chmod +x /usr/local/apache2/htdocs/script.pl -RUN chmod +x /usr/local/apache2/htdocs/script2.pl -RUN chmod +x /usr/local/apache2/htdocs/script3.pl -RUN chmod +x /usr/local/apache2/htdocs/extract_characters.pl - -# CMD y ENTRYPOINT para iniciar Apache2, según la imagen original CMD ["httpd-foreground"] diff --git a/images/apache2.conf b/images/apache2.conf index 2bfea11409..a3d3d69638 100644 --- a/images/apache2.conf +++ b/images/apache2.conf @@ -1,30 +1,18 @@ -# apache2.conf DocumentRoot /usr/local/apache2/htdocs - RewriteMap sub "prg:/usr/local/apache2/htdocs/script3.pl" - - Options Indexes FollowSymLinks - AllowOverride All - Require all granted - Options +ExecCGI - AddHandler cgi-script .cgi .pl + RewriteEngine On + RewriteMap mymap prg:/usr/local/apache2/htdocs/redirect.pl + RewriteRule ^/image/catalog/(.+)$ ${mymap:$1} [P] -# RewriteCond %{REQUEST_URI} ^/(.+)$ -# RewriteRule .* - [E=REDIRECT_STATUS:%{REQUEST_URI}] + + Require all granted + -# RewriteCond %{ENV:REDIRECT_STATUS} ^(.+)$ -# RewriteRule .* /images/%1 [R,L] - # RewriteCond %{ENV:REDIRECT_STATUS} 75/50 - # /images/75/50 - - # RewriteRule ^(.+)\.(jpg|png|jpeg)$ script.pl?value=/images/${sub:%0}/${sub:%2}/${sub:%1} [L,QSA] - RewriteRule ^(.+)\.(jpg|png|jpeg)$ /asd [L] - - # RewriteCond %{REQUEST_URI} !^/images/asd/ - # RewriteRule ^(.*)$ /75/$1 [L] - # RewriteRule ^(.+)\.(jpg|png|jpeg)$ /${sub:%2}/$1 [L] - - - - + + Options Indexes FollowSymLinks ExecCGI + AllowOverride All + Require all granted + AddHandler cgi-script .pl + + \ No newline at end of file diff --git a/images/docker-compose.yml b/images/docker-compose.yml index 651face426..f646f465b7 100644 --- a/images/docker-compose.yml +++ b/images/docker-compose.yml @@ -7,5 +7,4 @@ services: context: . dockerfile: Dockerfile ports: - - "8080:80" - + - "80:80" \ No newline at end of file diff --git a/images/www/extract_characters.pl b/images/www/extract_characters.pl deleted file mode 100644 index 38f05a0a37..0000000000 --- a/images/www/extract_characters.pl +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/perl - -use strict; -use warnings; - -# Lee la ruta de la URL desde la variable de entorno QUERY_STRING -my $query_string = $ENV{'QUEsRY_STRING'}; - -# Extrae los dos primeros caracteres del nombre del archivo -my $first_two_characters = substr($query_string, 0, 2); - -# Imprime la ruta que se pasará al RewriteRule -print "Content-type: text/plain\r\n"; -print "\r\n"; -print "/$first_two_characters/"; diff --git a/images/www/redirect.pl b/images/www/redirect.pl new file mode 100755 index 0000000000..b0a1563414 --- /dev/null +++ b/images/www/redirect.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Digest::SHA qw(sha1_hex); + +# Desactiva el buffering de salida para que Apache pueda obtener la respuesta inmediatamente +$| = 1; + +while () { + chomp; + my $input = $_; + print STDERR "DEBUG: Input recibido: $input\n"; # Mensaje de depuración + my ($path, $filename) = split('/', $input); + + if ($filename =~ /^(.+)\.(\w+)$/) { + my $name = $1; + my $extension = $2; + + my $sha1 = sha1_hex($name); + my $first = substr($sha1, 0, 2); + my $last = substr($sha1, 1, 2); + print "http://localhost/images/catalog/$path/$first/$last/$name.$extension\n"; + } else { + print STDERR "El string no tiene el formato esperado de nombre de archivo\n"; + print "http://localhost/error.png\n"; # URL de error si el formato no es correcto + } +} diff --git a/images/www/script.pl b/images/www/script.pl deleted file mode 100644 index 9954c7c0ba..0000000000 --- a/images/www/script.pl +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/perl -use strict; -use warnings; - -use Digest::SHA qw(sha1_hex); -use CGI; - - -my $cgi = CGI->new; -my $input_value = $cgi->param('value') || "defaudlt"; -# Imprime el encabezado HTTP -print "Content-type: text/plain\r\n"; -print "\r\n"; # Imprime una línea en blanco para indicar el final del encabezado - -# Imprime el texto deseado -print $input_value ; -print "¡Hola, mundo!\n"; diff --git a/images/www/script2.pl b/images/www/script2.pl deleted file mode 100644 index 3fdce0362a..0000000000 --- a/images/www/script2.pl +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/perl -use strict; -use warnings; - -use Digest::SHA qw(sha1_hex); -use CGI; - -# Imprime un texto cualquiera por consola - -my $cgi = CGI->new; -my $input_name = $cgi->param('name') || "default"; -my $input_value = $cgi->param('value') || "default"; -my $new_path; - -# Imprime el encabezado HTTP -print "Content-type: text/plain\r\n"; -print "\r\n"; # Imprime una línea en blanco para indicar el final del encabezado - -# Imprime el texto deseado -print "¡Hola, mundo!\n"; - -# Dividir la ruta usando el símbolo "/" para extraaer el nombre del archivo SIN extension -my $fileName= (split('/', $input_name))[-1]; - -# Dividir la ruta usando el símbolo "/" para extraaer el nombre del archivo CON extension -# my $file=$input_value; -my $file= (split('/', $input_value))[-1]; -my $exists = -e "$input_value"; - -# Compruebo si existe el archivo en la caperta general o no - my $hash_value = sha1_hex($fileName); - - # Obtiene solo los 2 primeros caracteres del hash MD5 - my $first_characters = substr($hash_value, 0, 2); - - # Obtiene solo los 2 segundos caracteres del hash MD5 - my $second_characters = substr($hash_value, 1, 2); - # print "images/$first_characters/$second_characters/$file"; -if($exists) { - $new_path ="$input_value"; -} else { - # Calcula el valor hash MD5 del string - - $new_path ="images/$first_characters/$second_characters/$file"; -} -# Verifica si $imagen está definida antes de intentar usarla -if (defined $new_path) { - xabrir_imagen($new_path); -} -sub abrir_imagen { - my ($ruta) = @_; - - # print "Ruta\n"; - print $ruta . "\n"; -} diff --git a/images/www/script3.pl b/images/www/script3.pl deleted file mode 100644 index 94126d5cf1..0000000000 --- a/images/www/script3.pl +++ /dev/null @@ -1,3 +0,0 @@ -$ENV{'REDIRECT_STATUS'} = '75/50'; -print "Content-type: text/html\n\n"; -print "Content"; -- 2.40.1 From 8c90dafffcb8e1085f994a9388b10b2939db2a80 Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 18 Jun 2024 13:03:20 +0200 Subject: [PATCH 4/6] refs #7022 Second script --- images/www/redirect.pl | 3 --- 1 file changed, 3 deletions(-) diff --git a/images/www/redirect.pl b/images/www/redirect.pl index b0a1563414..dfcb9deb27 100755 --- a/images/www/redirect.pl +++ b/images/www/redirect.pl @@ -20,8 +20,5 @@ while () { my $first = substr($sha1, 0, 2); my $last = substr($sha1, 1, 2); print "http://localhost/images/catalog/$path/$first/$last/$name.$extension\n"; - } else { - print STDERR "El string no tiene el formato esperado de nombre de archivo\n"; - print "http://localhost/error.png\n"; # URL de error si el formato no es correcto } } -- 2.40.1 From f912503dec8749778a389b78811037180b3070f6 Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 19 Jun 2024 13:55:48 +0200 Subject: [PATCH 5/6] refs #7022 Changes --- images/Dockerfile | 17 ++++++++--------- images/apache2.conf | 4 ++-- images/www/redirect.pl | 24 ------------------------ images/www/rwmap.pl | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 35 deletions(-) delete mode 100755 images/www/redirect.pl create mode 100755 images/www/rwmap.pl diff --git a/images/Dockerfile b/images/Dockerfile index 0bb1e74f4a..740563a060 100644 --- a/images/Dockerfile +++ b/images/Dockerfile @@ -4,18 +4,17 @@ RUN apt-get update \ && apt-get install -y curl libcgi-pm-perl \ && rm -rf /var/lib/apt/lists/* -ARG HTTPD_PATH="/usr/local/apache2/conf/httpd.conf" - -RUN echo "ServerName localhost" >> ${HTTPD_PATH} \ - && echo "Include conf/extra/httpd-vhosts.conf" >> ${HTTPD_PATH} \ - && echo "LoadModule rewrite_module modules/mod_rewrite.so" >> ${HTTPD_PATH} \ - && echo "LoadModule cgid_module modules/mod_cgid.so" >> ${HTTPD_PATH} \ - && echo "LoadModule proxy_module modules/mod_proxy.so" >> ${HTTPD_PATH} \ - && echo "LoadModule proxy_http_module modules/mod_proxy_http.so" >> ${HTTPD_PATH} +RUN echo "ServerName localhost\n" \ + "Include conf/extra/httpd-vhosts.conf\n" \ + "LoadModule rewrite_module modules/mod_rewrite.so\n" \ + "LoadModule cgid_module modules/mod_cgid.so\n" \ + "LoadModule proxy_module modules/mod_proxy.so\n" \ + "LoadModule proxy_http_module modules/mod_proxy_http.so" \ + >> "/usr/local/apache2/conf/httpd.conf" COPY apache2.conf /usr/local/apache2/conf/extra/httpd-vhosts.conf COPY www/ /usr/local/apache2/htdocs/ -RUN chmod +x /usr/local/apache2/htdocs/redirect.pl +RUN chmod +x /usr/local/apache2/htdocs/rwmap.pl CMD ["httpd-foreground"] diff --git a/images/apache2.conf b/images/apache2.conf index a3d3d69638..7be3276231 100644 --- a/images/apache2.conf +++ b/images/apache2.conf @@ -2,8 +2,8 @@ DocumentRoot /usr/local/apache2/htdocs RewriteEngine On - RewriteMap mymap prg:/usr/local/apache2/htdocs/redirect.pl - RewriteRule ^/image/catalog/(.+)$ ${mymap:$1} [P] + RewriteMap rwmap prg:/usr/local/apache2/htdocs/rwmap.pl + RewriteRule ^/image/catalog/(.+)$ ${rwmap:$1} [P] Require all granted diff --git a/images/www/redirect.pl b/images/www/redirect.pl deleted file mode 100755 index dfcb9deb27..0000000000 --- a/images/www/redirect.pl +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/perl -use strict; -use warnings; -use Digest::SHA qw(sha1_hex); - -# Desactiva el buffering de salida para que Apache pueda obtener la respuesta inmediatamente -$| = 1; - -while () { - chomp; - my $input = $_; - print STDERR "DEBUG: Input recibido: $input\n"; # Mensaje de depuración - my ($path, $filename) = split('/', $input); - - if ($filename =~ /^(.+)\.(\w+)$/) { - my $name = $1; - my $extension = $2; - - my $sha1 = sha1_hex($name); - my $first = substr($sha1, 0, 2); - my $last = substr($sha1, 1, 2); - print "http://localhost/images/catalog/$path/$first/$last/$name.$extension\n"; - } -} diff --git a/images/www/rwmap.pl b/images/www/rwmap.pl new file mode 100755 index 0000000000..71a1d9829b --- /dev/null +++ b/images/www/rwmap.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Digest::SHA qw(sha1_hex); +# Desactiva el buffering de salida para que Apache pueda obtener la respuesta inmediatamente +$| = 1; +my $serverName = $ENV{'SERVER_NAME'} || 'localhost'; +my $serverPort = $ENV{'SERVER_PORT'} || '80'; +my $scheme = ($serverPort == 443) ? 'https' : 'http'; +my $baseUrl = "$scheme://$serverName"; +$baseUrl .= ":$serverPort" if ($serverPort != 80 && $serverPort != 443); + +while () { + chomp; + my $input = $_; + print STDERR "DEBUG: Input recibido: $input\n"; # Mensaje de depuración + my ($path, $filename) = split('/', $input); + + if (defined $filename && $filename =~ /^(.+)\.(\w+)$/) { + my $name = $1; + my $extension = $2; + + my $sha1 = sha1_hex($name); + my $first = substr($sha1, 0, 2); + my $last = substr($sha1, 1, 2); + + if (-e "images/catalog/$path/$first/$last/$name.$extension") { + print "$baseUrl/images/catalog/$path/$first/$last/$name.$extension\n"; + } + else { + print "$baseUrl/images/catalog/$path/$name.$extension\n"; + } + } else { + print "Status: 400 Bad Request\n"; + print "Content-type: text/plain\n\n"; + print "Error: Invalid filename\n"; + } +} -- 2.40.1 From fd0a08489efe1c2ad7ee354965bb34d2cd1d7621 Mon Sep 17 00:00:00 2001 From: guillermo Date: Wed, 19 Jun 2024 14:17:23 +0200 Subject: [PATCH 6/6] refs #7022 Changes --- images/apache2.conf | 2 +- images/www/rwmap.pl | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/images/apache2.conf b/images/apache2.conf index 7be3276231..c685548665 100644 --- a/images/apache2.conf +++ b/images/apache2.conf @@ -3,7 +3,7 @@ RewriteEngine On RewriteMap rwmap prg:/usr/local/apache2/htdocs/rwmap.pl - RewriteRule ^/image/catalog/(.+)$ ${rwmap:$1} [P] + RewriteRule ^/image/catalog/(.+)$ ${rwmap:$1} [PT,QSA] Require all granted diff --git a/images/www/rwmap.pl b/images/www/rwmap.pl index 71a1d9829b..4fa488260c 100755 --- a/images/www/rwmap.pl +++ b/images/www/rwmap.pl @@ -13,7 +13,7 @@ $baseUrl .= ":$serverPort" if ($serverPort != 80 && $serverPort != 443); while () { chomp; my $input = $_; - print STDERR "DEBUG: Input recibido: $input\n"; # Mensaje de depuración + print STDERR "DEBUG: Input recibido: $input\n"; # Mensaje de depuración my ($path, $filename) = split('/', $input); if (defined $filename && $filename =~ /^(.+)\.(\w+)$/) { @@ -32,7 +32,5 @@ while () { } } else { print "Status: 400 Bad Request\n"; - print "Content-type: text/plain\n\n"; - print "Error: Invalid filename\n"; } } -- 2.40.1