8
0
mirror of https://github.com/weiss/ngx_http_upload.git synced 2025-07-01 18:29:39 +00:00

Improve error handling

This commit is contained in:
Holger Weiss 2018-07-19 21:35:56 +02:00
parent 0f7e5a4b35
commit a3e951d93d

View File

@ -99,8 +99,7 @@ sub handle_put_body {
make_path($dir_path, {chmod => $dir_mode, error => \my $error}); make_path($dir_path, {chmod => $dir_mode, error => \my $error});
if (@$error) { if (@$error) {
$r->log_error($!, "Cannot create directory $dir_path"); return system_error($r, "Cannot create directory $dir_path");
return HTTP_FORBIDDEN; # Assume EACCES.
} }
my $body = $r->request_body; my $body = $r->request_body;
@ -121,30 +120,19 @@ sub store_body_from_buffer {
if (sysopen(my $fh, $dst_path, O_WRONLY|O_CREAT|O_EXCL, $mode)) { if (sysopen(my $fh, $dst_path, O_WRONLY|O_CREAT|O_EXCL, $mode)) {
if (not binmode($fh)) { if (not binmode($fh)) {
$r->log_error($!, "Cannot set binary mode for $dst_path"); return system_error($r, "Cannot set binary mode for $dst_path");
return HTTP_INTERNAL_SERVER_ERROR;
} }
if (not syswrite($fh, $body)) { if (not syswrite($fh, $body)) {
$r->log_error($!, "Cannot write $dst_path"); return system_error($r, "Cannot write $dst_path");
return HTTP_INTERNAL_SERVER_ERROR;
} }
if (not close($fh)) { if (not close($fh)) {
$r->log_error($!, "Cannot close $dst_path"); return system_error($r, "Cannot close $dst_path");
return HTTP_INTERNAL_SERVER_ERROR;
} }
} elsif ($!{EEXIST}) {
$r->log_error($!, "Won't overwrite $dst_path");
return HTTP_CONFLICT;
} elsif ($!{EACCES}) {
$r->log_error($!, "Cannot create $dst_path");
return HTTP_FORBIDDEN;
} else { } else {
$r->log_error($!, "Cannot open $dst_path"); return system_error($r, "Cannot create $dst_path");
return HTTP_INTERNAL_SERVER_ERROR;
} }
if (chmod($mode, $dst_path) < 1) { if (chmod($mode, $dst_path) != 1) {
$r->log_error($!, "Cannot change permissions of $dst_path"); return system_error($r, "Cannot change permissions of $dst_path");
return HTTP_INTERNAL_SERVER_ERROR;
} }
return HTTP_CREATED; return HTTP_CREATED;
} }
@ -155,17 +143,16 @@ sub store_body_from_file {
# We could merge this with the store_body_from_buffer() code by handing over # We could merge this with the store_body_from_buffer() code by handing over
# the file handle created by sysopen() as the second argument to move(), but # the file handle created by sysopen() as the second argument to move(), but
# we want to let move() use rename() if possible. # we want to let move() use rename() if possible.
if (-e $dst_path) { if (-e $dst_path) {
$r->log_error(0, "Won't overwrite $dst_path"); $r->log_error(0, "Won't overwrite $dst_path");
return HTTP_CONFLICT; return HTTP_CONFLICT;
} }
if (not move($src_path, $dst_path)) { if (not move($src_path, $dst_path)) {
$r->log_error($!, "Cannot move data to $dst_path"); return system_error($r, "Cannot move data to $dst_path");
return HTTP_INTERNAL_SERVER_ERROR;
} }
if (chmod($mode, $dst_path) < 1) { if (chmod($mode, $dst_path) != 1) {
$r->log_error($!, "Cannot change permissions of $dst_path"); return system_error($r, "Cannot change permissions of $dst_path");
return HTTP_INTERNAL_SERVER_ERROR;
} }
return HTTP_CREATED; return HTTP_CREATED;
} }
@ -197,5 +184,15 @@ sub safe_eq {
return $r == 0; return $r == 0;
} }
sub system_error {
my ($r, $msg) = @_;
$r->log_error($!, $msg);
return HTTP_FORBIDDEN if $!{EACCES};
return HTTP_CONFLICT if $!{EEXIST};
return HTTP_INTERNAL_SERVER_ERROR;
}
1; 1;
__END__ __END__