| 1 | package Apache2::FileHash::PUT; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Carp; | ||
| 7 | use Digest::MD5; | ||
| 8 | use Math::BigInt; | ||
| 9 | use File::Temp; | ||
| 10 | use File::Copy; | ||
| 11 | |||
| 12 | use Apache2::RequestRec (); | ||
| 13 | use Apache2::RequestIO (); | ||
| 14 | use Apache2::RequestUtil (); | ||
| 15 | use Apache2::ServerUtil (); | ||
| 16 | use Apache2::ServerRec (); | ||
| 17 | use Apache2::Process (); | ||
| 18 | use APR::Table (); | ||
| 19 | use Apache2::Const -compile => qw(DECLINED OK REDIRECT); | ||
| 20 | use Math::BigInt; | ||
| 21 | |||
| 22 | use APR::Brigade (); | ||
| 23 | use APR::Bucket (); | ||
| 24 | use Apache2::Const -compile => qw(MODE_READBYTES); | ||
| 25 | use APR::Const -compile => qw(SUCCESS BLOCK_READ); | ||
| 26 | |||
| 27 | use MIME::Base64; | ||
| 28 | use Apache2::FileHash; | ||
| 29 | |||
| 30 | use constant METHOD => "PUT"; | ||
| 31 | |||
| 32 | our $Config = { | ||
| 33 | METHOD => { | ||
| 34 | re_path => qr(^/storeFile/(.*)), | ||
| 35 | }, | ||
| 36 | }; | ||
| 37 | |||
| 38 | sub handler | ||
| 39 | { | ||
| 40 | my $r = shift; | ||
| 41 | |||
| 42 | return(Apache2::Const::DECLINED) unless $r->method() eq METHOD; | ||
| 43 | |||
| 44 | $r->handler("perl-script"); | ||
| 45 | $r->push_handlers(PerlResponseHandler => \&file_handler); | ||
| 46 | |||
| 47 | my $uri = $r->uri(); | ||
| 48 | my $path = ""; | ||
| 49 | if ($uri =~ m#$Config->{METHOD}{re_path}#) { | ||
| 50 | $path = $1; | ||
| 51 | } | ||
| 52 | |||
| 53 | # First PUT | ||
| 54 | unless (&Apache2::FileHash::inbucket($r, $path)) { | ||
| 55 | my $filename = &Apache2::FileHash::hashing_function($r, $path); | ||
| 56 | my $netloc = &Apache2::FileHash::netloc($r, $filename); # need netloc_hashed and netloc_uri | ||
| 57 | |||
| 58 | my $bucket = &Apache2::FileHash::getbucket($r, $filename); | ||
| 59 | |||
| 60 | my $location = $bucket->{location}; | ||
| 61 | my $name = $bucket->{name}; | ||
| 62 | my $method = $bucket->{method}; | ||
| 63 | my $port = $bucket->{port}; | ||
| 64 | |||
| 65 | my $new_netloc = "${method}://${name}:$port$uri"; | ||
| 66 | |||
| 67 | $r->headers_out->set(Location => $new_netloc); | ||
| 68 | $r->status(Apache2::Const::REDIRECT); | ||
| 69 | |||
| 70 | return Apache2::Const::REDIRECT; | ||
| 71 | } | ||
| 72 | |||
| 73 | return(Apache2::Const::OK); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub file_handler | ||
| 77 | { | ||
| 78 | my $r = shift; | ||
| 79 | |||
| 80 | my $uri = $r->uri(); | ||
| 81 | |||
| 82 | my $path = ""; | ||
| 83 | if ($uri =~ m#$Config->{METHOD}{re_path}#) { | ||
| 84 | $path = $1; | ||
| 85 | } | ||
| 86 | |||
| 87 | # Write Many. Read Many. Unlink at your own risk. | ||
| 88 | if ($path) { | ||
| 89 | my $status = &Apache2::FileHash::save_file($r, $path); | ||
| 90 | |||
| 91 | $r->content_type('text/plain'); | ||
| 92 | $r->print($status == Apache2::Const::OK ? "0" : "1"); | ||
| 93 | } | ||
| 94 | |||
| 95 | return(Apache2::Const::OK); | ||
| 96 | } | ||
| 97 | |||
| 98 | 1; |