From 9dd5f1a20832c04b543f4080f4712b5f5c3028b5 Mon Sep 17 00:00:00 2001 From: eleksir Date: Sun, 20 Oct 2019 17:11:52 +0300 Subject: [PATCH] upload.pm: add diagnostics --- lib/upload.pm | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/upload.pm b/lib/upload.pm index 48c7e7f..b24de00 100644 --- a/lib/upload.pm +++ b/lib/upload.pm @@ -15,14 +15,20 @@ use conf; my $c = loadConf(); sub upload { - my $input = shift; - my $len = shift; - my $name = shift; + my $input = shift; # file descriptor with data being uploaded + my $len = shift; # expected data length (from client) + my $name = shift; # upload "dir" and filename my ($status, $content, $msg) = ('400', 'text/plain', "Bad Request?\n"); + + return ($status, $content, "No Content-Length supplied.\n") unless (defined($len)); + return ($status, $content, "No name supplied.\n") unless (defined($name)); + my $d; ($d, $name) = split(/\//, $name, 2); + + return ($status, $content, "Name does not match pattern.\n") unless ($name =~ /^[A-Z|a-z|0-9|_|\-|\+]+$/); + my $match; - return ($status, $content, $msg) unless ($name =~ /^[A-Z|a-z|0-9|_|\-|\+]+$/); foreach (keys(%{$c->{upload}->{dir}})) { if ($d eq $_) { @@ -31,7 +37,8 @@ sub upload { } } - return ($status, $content, $msg) unless($match); # incorrect destination in url + return ($status, $content, "Incorrect destination dir.\n") unless($match); # incorrect destination in url + $name = sprintf("%s/%s", $c->{upload}->{dir}->{$d}, $name); if ($len > 0) { @@ -64,7 +71,11 @@ sub upload { } else { ($status, $content, $msg) = ('201', $content, "Uploaded.\n"); } + } else { + ($status, $content, $msg) = ('500', $content, "Unable to write: $!\n"); } + } else { + $msg = "Incorrect Content-Length\n"; } return ($status, $content, $msg);