#!/usr/bin/perl $thisurl = $ENV{'SERVER_URL'}.$ENV{'SCRIPT_NAME'}; $upload_dir = ''; # location for uploaded files $authorurl = 'kmeltz@cris.com'; main(); sub main { #init(); read_net_input(); # print_header(); # while( ($n, $v) = each(%ENV)) { print "$n = $v
"; } print "
"; # while( ($n, $v) = each(%GLOBAL)) { print "$n = $v
"; } print "
"; if( $GLOBAL{'UPLOAD'} ) { handle_upload(); } elsif( !$ENV{'PATH_INFO'} || $ENV{'PATH_INFO'} eq '/' ) { show_dir_content(); } else { start_download( $ENV{'PATH_INFO'} ); } 1; # for fun } # end of main #sub init { # $thisurl = $ENV{'SERVER_URL'}.$ENV{'SCRIPT_NAME'}; # $upload_dir = ''; # location for uploaded files # $authorurl = 'kmeltz@cris.com'; # 1; #} # end of init sub print_header { print "Content-Type: text/html\n\n"; 1; } # end of print_header sub urldecode { local($in) = @_; local($i, @input_list); @input_list = split(/&/,$in); foreach $i (@input_list) { $i =~ s/\+/ /g; # Convert plus's to spaces # Convert %XX from hex numbers to alphanumeric $i =~ s/%(..)/pack("c",hex($2))/ge; # Split into key and value. $loc = index($i,"="); $key = substr($i,0,$loc); $val = substr($i,$loc+1); $GLOBAL{$key} = $val; } 1; } # end of urldecode sub read_net_input { local ($i, $loc, $key, $val, $input); local($f,$header, $header_body, $len, $buf); if ($ENV{'REQUEST_METHOD'} eq "GET") { $input = $ENV{'QUERY_STRING'}; } elsif ($ENV{'REQUEST_METHOD'} eq "POST") { # Need to read TILL we got all bytes $len = 0; $input = ''; while( $len != $ENV{'CONTENT_LENGTH'} ) { $buf = ''; $len += sysread(STDIN, $buf, $ENV{'CONTENT_LENGTH'}); $input .= $buf; } } # conform to RFC1867 for upload specific if( $ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/ ) { $boundary = '--'.$1; # please refer to RFC1867 @list = split(/$boundary/, $input); $header_body = $list[1]; $header_body =~ /\r\n\r\n|\n\n/; # separate header and body $header = $`; # front part $body = $'; # rear part $body =~ s/\r\n$//; # the last \r\n was put in by Netscape $GLOBAL{'FILE_CONTENT'} = $body; # open(FD,">input.txt"); print FD $input; close(FD); # for tracking # parse header $header =~ /filename=\"(.+)\"/; $GLOBAL{'FILE_NAME'} = $1; $GLOBAL{'FILE_NAME'} =~ s/\"//g; # remove "s $GLOBAL{'FILE_NAME'} =~ s/\s//g; # make sure no space(include \n, \r..) in the file name # parse trailer for( $i=2; $list[$i]; $i++) { $list[$i] =~ s/^.+name=$//; $list[$i] =~ /\"(\w+)\"/; $GLOBAL{$1} = $'; } return 1; } urldecode($input); 1; } # end of read_net_input sub read_file { local($fname) = @_; local($content); open(FILE, "<$fname") || return ''; while() { $content .= $_; } close(FILE); $content; } # end of read_file sub read_dir { local($target_dir) = @_ ; local($filename, $dir_content); return 0 if( !$target_dir ); opendir(DIR, $target_dir) || return 0; $target_dir =~ s/^\.\///; # remove ./ $target_dir =~ /(.+)\/(.+)\/$/; # find out upper level $GLOBAL{'UP_LEVEL'} = $1; # save upper level as a global if( $target_dir ) { $dir_content = "..back\n\n\n"; } while($filename = readdir(DIR)) { if( $filename =~ /^\.|^\#|~$/ ) { next; } # skip hidden files $dir_content .= "$target_dir$filename\n"; } closedir(DIR); $dir_content; } # end of read_dir sub format_html_output { # Was first done listing files to download using an # unordered list (