Lost Kiwi has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, I am new to perl and programming in general, and am enjoying the challenge of learning such an interesting and well supported language. I am impressed by this site and hope to be able to make a positive contribution in the future. I am trying to enable links to documents on a file server on the LAN from a redmine wiki page. The redmine wiki doesn't support UNC paths or the file:/// method of linking to files on the network. I need help using CGI to redirect to a document, so the user would click on a HTTP:// link within the wiki page, maybe the link would look like this: http://wiki/redirect.cgi?unc=\\fileserver\share\document.doc So the redirect.cgi script would read what comes after "unc" and fetch the document for the user. I hope I am making sense, I come from a more systems and infrastructure background so this is all a bit foreign to me! Any ideas and snippets would be much appreciated. Cheers

Replies are listed 'Best First'.
Re: Redirect from URL to UNC path
by Your Mother (Archbishop) on Apr 16, 2009 at 03:23 UTC

    Slightly tested. Fun to mess around with anyway. Be *careful* about what you let the user request or this is a major security hole.

    #!/usr/bin/perl use strict; use warnings; use CGI qw(:standard); use Path::Class; use MIME::Types; my $unc = param("unc") || ""; if ( $unc and -r $unc and -f _ ) { # NORMALIZE/SANITIZE/VERIFY SAFETY OF REQUESTED PATH!!!! my $file = Path::Class::File->new($unc); my ( $ext ) = $file =~ /\.(\w+)\z/; my $type = MIME::Types->new->mimeTypeOf( $ext ); print header($type || "text/plain"); my $fh = $file->openr(); print while <$fh>; close $fh; } elsif ( $unc ) { print header("text/plain"), "$unc is unreadable or doesn't exist"; } else { print header("text/plain"), "You gots t'aks for da unc files, man...\n", "Oh, a-right! This is a recent picture o'me.\n", " ...Well, what'd'ya think? Hot or not?\n\n"; my $self = Path::Class::File->new($0); my $fh = $self->openr(); print while <$fh>; close $fh; }