removed content headers when no content

This commit is contained in:
Rudis Muiznieks 2020-07-21 00:38:39 -05:00
parent e84c0f397f
commit c8e29ee6a7
2 changed files with 18 additions and 17 deletions

View File

@ -175,6 +175,11 @@ if (defined $ENV{'LOCAL_DOMAINS'}) {
$type = 'application/json';
}
my $length = length($body);
$body = '' if $cgi->request_method() eq 'HEAD';
my $content_header = '';
if ($length > 0) {
$content_header = "\nContent-Type: $type\nContent-Length: $length";
}
my $now = time;
my $date = HTTP::Date::time2str($now);
my $redirect_header = '';
@ -189,9 +194,7 @@ if (defined $ENV{'LOCAL_DOMAINS'}) {
print <<EOF;
HTTP/1.1 $code $header
Server: DotplanApi
Date: $date$mtime_header
Content-Type: $type
Content-Length: $length$redirect_header
Date: $date$mtime_header$content_header$redirect_header
EOF
print "\n$body";
}
@ -387,17 +390,15 @@ EOF
print_response($cgi, 304, '', $format, undef, $mtime);
} else {
# render response
my $body = '';
my $body;
delete $plan->{'mtime'};
if ($cgi->request_method() ne 'HEAD') {
if ($format eq 'application/json') {
$body = encode_json($plan);
} elsif ($format eq 'text/html') {
$body = encode_entities($plan->{'plan'});
$body =~ s/\n/<br>\n/g;
} else {
$body = $plan->{'plan'};
}
if ($format eq 'application/json') {
$body = encode_json($plan);
} elsif ($format eq 'text/html') {
$body = encode_entities($plan->{'plan'});
$body =~ s/\n/<br>\n/g;
} else {
$body = $plan->{'plan'};
}
print_response($cgi, 200, $body, $format, undef, $mtime);
}

View File

@ -181,7 +181,7 @@ curl_test 'Get authentication token' 200 'application/json' -u $TEST_USER:test12
token=$(echo "$TEST_CONTENT" | jq -r '.token')
curl_test 'No plan by default' 404 'text/plain' localhost:$PORT/plan/$TEST_USER
curl_test 'No plan by default' 404 '' localhost:$PORT/plan/$TEST_USER
curl_test 'Reject bad authentication token' 401 'application/json' -XPUT -d '{"plan":"something","auth":"wrong"}' localhost:$PORT/plan/$TEST_USER
@ -218,11 +218,11 @@ curl_test 'Check missing plan in json using accept' 404 'application/json' -H 'A
curl_test 'Check missing plan in json using querystring' 404 'application/json' localhost:$PORT/plan/testuser@exampl3.com?format=json
curl_test 'Check missing plan in html using accept' 404 'text/html' -H 'Accept: text/html' localhost:$PORT/plan/testuser@exampl3.com
curl_test 'Check missing plan in html using accept' 404 '' -H 'Accept: text/html' localhost:$PORT/plan/testuser@exampl3.com
curl_test 'Check missing plan in html using querystring' 404 'text/html' localhost:$PORT/plan/testuser@exampl3.com?format=html
curl_test 'Check missing plan in html using querystring' 404 '' localhost:$PORT/plan/testuser@exampl3.com?format=html
curl_test 'Check missing plan in text by omitting accept' 404 'text/plain' localhost:$PORT/plan/testuser@exampl3.com
curl_test 'Check missing plan in text by omitting accept' 404 '' localhost:$PORT/plan/testuser@exampl3.com
curl_test 'Delete authentication token' 200 'application/json' -u $TEST_USER:test1234 -XDELETE localhost:$PORT/token