I have to run a very simplistic piece of perl code in response to HTTP request. The nginx config is:
http {
perl_modules perl/lib;
perl_require errors.pm;
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
location / {
perl errors::response;
}
}
}
The code I try to run is:
package errors;
use strict;
use warnings;
use nginx;
my $errors = {
ThisIsNotOK => {
'code' => '409',
'amzcode' => 'NOK',
'message' => 'Something went really wrong'
},
ItsOK => {
'code' => '200',
'amzcode' => 'OK',
'message' => 'Okay'
},
};
my $fmt =<<ENDFMT;
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>%s</Code>
<Message>%s</Message>
<Resource>/mybucket/whatever.1</Resource>
<RequestId>DEADBEEFBEDAEDA</RequestId>
</Error>
ENDFMT
sub response() {
my $r = shift();
my $curr_err = (keys %$errors)[rand keys %$errors];
$r->send_http_header("application/xml");
$r->status($errors->{$curr_err}{'code'});
$r->print(sprintf($fmt,$errors->{$curr_err}{'amzcode'}, $errors->{$curr_err}{'message'}));
return $errors->{$curr_err}{'code'};
}
1;
__END__
The problem is when this code returns something other than 200/OK, I get the empty response error:
% curl -iX GET localhost
curl: (52) Empty reply from server
nginx version: nginx/1.14.1
built by gcc 8.2.1 20180905 (Red Hat 8.2.1-3) (GCC)
built with OpenSSL 1.1.1 FIPS 11 Sep 2018 (running with OpenSSL 1.1.1c FIPS 28 May 2019)
TLS SNI support enabled
What do I do wrong?