404 response respects accepted content type

This commit is contained in:
Rudis Muiznieks 2021-06-19 12:27:58 -05:00
parent 5da2397f47
commit 554bbcec2e
3 changed files with 12 additions and 4 deletions

View File

@ -441,6 +441,7 @@ EOF
my ($cgi, $email) = @_;
my $plan = util_get_plan($email);
my $format = $cgi->param('accept')->match(qw(text/plain application/json));
if (defined $plan && defined $plan->{'redirect'}) {
# found external plan service, redirect request
@ -448,7 +449,8 @@ EOF
return;
}
if (!defined $plan) {
print_response($cgi, 404);
my $body = $format eq 'text/plain' ? 'No Plan.' : encode_json({error => 'No Plan.'});
print_response($cgi, 404, {'Content-Type' => $format}, $body);
return;
}
my $pubkey = $cgi->http('X-Dotplan-Pubkey');
@ -469,7 +471,6 @@ EOF
# render response
my $body;
delete $plan->{'mtime'};
my $format = $cgi->param('accept')->match(qw(text/plain application/json));
if ($format eq 'application/json') {
$body = encode_json($plan);
} else {

View File

@ -8,7 +8,14 @@
<body>
<main>
<h1>Dotplan Online</h1>
<p>This site is coming soon. For now you can check out <a href='https://github.com/rudism/dotplan-online'>the source code</a>.
<h2>What is Dotplan?</h2>
<p>Dotplan is a modern re-imagining of the <a href='https://unix.stackexchange.com/questions/122782/what-is-plan-for-users'>Plan</a> file from the days of yesteryear. Well, I mean technically it's still around today, but when was the last time you <code>finger</code>ed someone?</p>
<p>On second thought, never mind. Don't answer that...</p>
<p>Essentially it's a loosey-goosey API specification through which the technically elite can express their personalities (or lack thereof) with one another.</p>
<h2>What is Dotplan Online?</h2>
<p>This site is a free Dotplan provider and relay running the <a href='https://github.com/rudism/dotplan-online'>Dotplan reference implementation</a>. If you're some kind of simpleton who is incapable of hosting your own, you can point your email domain here via a <code>SRV</code> record and use it as your Dotplan provider.</p>
<h2>How does it all work?</h2>
<p></p>
</main>
</body>
</html>

View File

@ -183,7 +183,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 'application/json' localhost:$PORT/plan/$TEST_USER
curl_test 'No plan by default' 404 'text/plain' 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