How add Content-Length to X-Sendfile / X-Accel-Redirect
-
Hello.
When transferring files using modes like “X-Sendfile, X-Lighttpd-Sendfile, or X-Accel-Redirect,” the file size isn’t included because the size parameter is not passed in the template function. Consequently, users downloading files are unaware of the final size of the download.
/** * Download a file using X-Sendfile, X-Lighttpd-Sendfile, or X-Accel-Redirect if available. * * @param string $file_path File path. * @param string $filename File name. */ public static function download_file_xsendfile( $file_path, $filename ) { $parsed_file_path = self::parse_file_path( $file_path ); /** * Fallback on force download method for remote files. This is because: * 1. xsendfile needs proxy configuration to work for remote files, which cannot be assumed to be available on most hosts. * 2. Force download method is more secure than redirect method if
allow_url_fopen
is enabled inphp.ini
. */ if ( $parsed_file_path['remote_file'] && ! apply_filters( 'woocommerce_use_xsendfile_for_remote', false ) ) { do_action( 'woocommerce_download_file_force', $file_path, $filename ); return; } if ( function_exists( 'apache_get_modules' ) && in_array( 'mod_xsendfile', apache_get_modules(), true ) ) { self::download_headers( $parsed_file_path['file_path'], $filename ); $filepath = apply_filters( 'woocommerce_download_file_xsendfile_file_path', $parsed_file_path['file_path'], $file_path, $filename, $parsed_file_path ); header( 'X-Sendfile: ' . $filepath ); exit; } elseif ( stristr( getenv( 'SERVER_SOFTWARE' ), 'lighttpd' ) ) { self::download_headers( $parsed_file_path['file_path'], $filename ); $filepath = apply_filters( 'woocommerce_download_file_xsendfile_lighttpd_file_path', $parsed_file_path['file_path'], $file_path, $filename, $parsed_file_path ); header( 'X-Lighttpd-Sendfile: ' . $filepath ); exit; } elseif ( stristr( getenv( 'SERVER_SOFTWARE' ), 'nginx' ) || stristr( getenv( 'SERVER_SOFTWARE' ), 'cherokee' ) ) { self::download_headers( $parsed_file_path['file_path'], $filename ); $xsendfile_path = trim( preg_replace( '^' . str_replace( '\\', '/', getcwd() ) . '
', '', $parsed_file_path['file_path'] ), '/' ); $xsendfile_path = apply_filters( 'woocommerce_download_file_xsendfile_x_accel_redirect_file_path', $xsendfile_path, $file_path, $filename, $parsed_file_path ); header( "X-Accel-Redirect: /$xsendfile_path" ); exit; } // Fallback. wc_get_logger()->warning( sprintf( /* translators: %1$s contains the filepath of the digital asset. */ __( '%1$s could not be served using the X-Accel-Redirect/X-Sendfile method. A Force Download will be used instead.', 'woocommerce' ), $file_path ) ); self::download_file_force( $file_path, $filename ); }Additionally, the function lacks the ability to convey the file size when a file is downloaded, leaving users unaware of the final download size.
// Absent: header( 'HTTP/1.1 206 Partial Content' ); // Absent: header( "Accept-Ranges: 0-$file_size" ); // Absent: header( "Content-Range: bytes $start-$end/$file_size" ); header( "Content-Length: $file_size" );
Maybe u have a hook?? I try do that on server side but without success((
Viewing 9 replies - 1 through 9 (of 9 total)
Viewing 9 replies - 1 through 9 (of 9 total)
- The topic ‘How add Content-Length to X-Sendfile / X-Accel-Redirect’ is closed to new replies.