Skip to content

Commit

Permalink
(#4): scripts/urnresolver-self-test.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
fititnt committed Oct 5, 2022
1 parent 3b2758a commit 5793ebc
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 30 deletions.
82 changes: 55 additions & 27 deletions app/lib/urnresolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@
class CConst
{
/**
* Content-Disposition: inline
* content-disposition: inline
*
* @see https://www.iana.org/assignments/cont-disp/cont-disp.xhtml
*/
public const CC_INLINE = 1;

/**
* Content-Disposition: attachment; filename=''
* content-disposition: attachment; filename=''
*
* @see @see https://www.iana.org/assignments/cont-disp/cont-disp.xhtml
* @see https://www.rfc-editor.org/rfc/rfc2183
*/
public const CC_ATTACHMENT = 2;

// @TODO maybe also "Content-Disposition: recipient-list" (?)
// @TODO maybe also "content-disposition: recipient-list" (?)
// https://www.rfc-editor.org/rfc/rfc5363
// public const CC_RECIPENTLIST = -1;

Expand Down Expand Up @@ -386,23 +386,28 @@ public function __construct(string $id, ?string $type = '.jsonld')

public function get_http_content_disposition()
{

// var_dump($this->type, Common::EXTMETA[$this->type]);die;
$extmeta = Common::EXTMETA[$this->type];
if ($extmeta === CConst::CC_INLINE) {
if ($extmeta[1] === CConst::CC_INLINE) {
return 'inline';
}
// $fileextension = $this->type;

if ($extmeta === CConst::CC_ATTACHMENT) {
if ($extmeta[1] === CConst::CC_ATTACHMENT) {
$filename = $this->id . $this->type;
// Not ideal, but on failed scenarios avoid generate bad filenames
$filename = str_replace('http://', '', $filename);
$filename = str_replace('https://', '', $filename);
$filename = str_replace('/', '__', $filename);
$filename = str_replace('"', '', $filename);
$filename = str_replace("'", '', $filename);
return "attachment; filename='$filename'";
return "attachment; filename=\"{$filename}\"";
}

throw new \Exception("Syntax error");
return false;

// throw new \Exception("Syntax error");
}

public function get_http_mediatype()
Expand Down Expand Up @@ -504,8 +509,14 @@ public function execute_output_2xx(
int $http_status_code = 200
) {
http_response_code($http_status_code);
header("Cache-Control: {$this->_cc_prefix}, max-age={$this->max_age}, s-maxage={$this->s_maxage}, stale-while-revalidate={$this->stale_while_revalidate}, stale-if-error={$this->stale_if_error}");
header("Content-type: {$this->outf->get_http_mediatype()}");
header("cache-control: {$this->_cc_prefix}, max-age={$this->max_age}, s-maxage={$this->s_maxage}, stale-while-revalidate={$this->stale_while_revalidate}, stale-if-error={$this->stale_if_error}");
header("content-type: {$this->outf->get_http_mediatype()}");

$content_disposition = $this->outf->get_http_content_disposition();
if (!empty($content_disposition)) {
header("content-disposition: {$content_disposition}");
}

// header("Access-Control-Allow-Origin: *");

$output = new Output($this->outf, $data);
Expand Down Expand Up @@ -560,11 +571,15 @@ public function execute_output_4xx(
$this->_set_options($this->global_conf['Cache-Control']['default404']);

http_response_code($http_status_code);
header("Cache-Control: {$this->_cc_prefix}, max-age={$this->max_age}, s-maxage={$this->s_maxage}, stale-while-revalidate={$this->stale_while_revalidate}, stale-if-error={$this->stale_if_error}");
// header('Content-Type: application/json; charset=utf-8');
// header("Content-type: application/json; charset=utf-8");
header("Content-type: {$this->outf->get_http_mediatype()}");
// header("Access-Control-Allow-Origin: *");
header("cache-control: {$this->_cc_prefix}, max-age={$this->max_age}, s-maxage={$this->s_maxage}, stale-while-revalidate={$this->stale_while_revalidate}, stale-if-error={$this->stale_if_error}");
// header('content-type: application/json; charset=utf-8');
// header("content-type: application/json; charset=utf-8");
header("content-type: {$this->outf->get_http_mediatype()}");

$content_disposition = $this->outf->get_http_content_disposition();
if (!empty($content_disposition)) {
header("content-disposition: {$content_disposition}");
}

// @TODO remove this part. Output already pre-parse errors
$error = [
Expand Down Expand Up @@ -593,8 +608,13 @@ public function execute_output_5xx(
$this->_set_options($this->global_conf['Cache-Control'][$mode]);

http_response_code($http_status_code);
header("Cache-Control: {$this->_cc_prefix}, max-age={$this->max_age}, s-maxage={$this->s_maxage}, stale-while-revalidate={$this->stale_while_revalidate}, stale-if-error={$this->stale_if_error}");
header("Content-type: {$this->outf->get_http_mediatype()}");
header("cache-control: {$this->_cc_prefix}, max-age={$this->max_age}, s-maxage={$this->s_maxage}, stale-while-revalidate={$this->stale_while_revalidate}, stale-if-error={$this->stale_if_error}");
header("content-type: {$this->outf->get_http_mediatype()}");

$content_disposition = $this->outf->get_http_content_disposition();
if (!empty($content_disposition)) {
header("content-disposition: {$content_disposition}");
}

// @TODO remove this part. Output already pre-parse errors
$error = [
Expand All @@ -620,7 +640,7 @@ public function execute_redirect(
) {
http_response_code($http_status_code);
// @see https://developers.cloudflare.com/cache/about/cache-control/
header("Cache-Control: {$this->_cc_prefix}, max-age={$this->max_age}, s-maxage={$this->s_maxage}, stale-while-revalidate={$this->stale_while_revalidate}, stale-if-error={$this->stale_if_error}");
header("cache-control: {$this->_cc_prefix}, max-age={$this->max_age}, s-maxage={$this->s_maxage}, stale-while-revalidate={$this->stale_while_revalidate}, stale-if-error={$this->stale_if_error}");
// header('Vary: Accept-Encoding');
// header("Access-Control-Allow-Origin: *");
// header('Location: ' . $this->active_urn_to_uri);
Expand Down Expand Up @@ -934,7 +954,7 @@ public function execute()

// if ($this->urn->raw_urn === 'urn:resolver:index') {
if ($this->urn->get_levenshtein('urn:resolver:index') === 0) {
return $this->operation_index();
return $this->operationi_index();
}

if ($this->urn->get_levenshtein('urn:resolver:help') === 0) {
Expand All @@ -952,9 +972,9 @@ public function execute()
// if (strpos($this->urn->raw_urn, 'urn:resolver:_summary') === 0) {
return $this->operation_summary();
}
if ($this->urn->get_levenshtein('urn:resolver:_allexamples') === 0) {
if ($this->urn->get_levenshtein('urn:resolver:exemplum') === 0) {
// if (strpos($this->urn->raw_urn, 'urn:resolver:_summary') === 0) {
return $this->operation_all_examples();
return $this->operation_exemplum();
}

$this->http_status = 501;
Expand Down Expand Up @@ -993,7 +1013,11 @@ public function operation_explore()
// return $this->is_success();
}

public function operation_all_examples()
/**
* - exemplum: s, n, nominativus, https://en.wiktionary.org/wiki/exemplum#Latin
* - operātiōnī: s, f, dativus, https://en.wiktionary.org/wiki/index#Latin
*/
public function operation_exemplum()
{
$resolver_paths = [];
// echo "Oi";
Expand Down Expand Up @@ -1055,7 +1079,7 @@ public function operation_all_examples()
}

if (!$this->is_tabular) {
$this->data = ['urn:resolver:_allexamples' => $this->data];
$this->data = ['urn:resolver:exemplum' => $this->data];
}
return true;
// return $this->is_success();
Expand All @@ -1074,7 +1098,11 @@ public function operation_summary()
// return $this->is_success();
}

public function operation_index()
/**
* - index: s, m/f, nominativus, https://en.wiktionary.org/wiki/index#Latin
* - operātiōnī: s, f, dativus, https://en.wiktionary.org/wiki/index#Latin
*/
public function operationi_index()
{
$resolver_paths = [];
foreach ($this->router->resolvers as $key => $value) {
Expand All @@ -1099,14 +1127,14 @@ public function operation_ping(string $envelope = null)

// @TODO generalize
if ($envelope === '.txt') {
header("Content-type: text/plain; charset=utf-8");
header("content-type: text/plain; charset=utf-8");
echo "PONG\n";
echo date("c") . "\n";
die;
}

if ($envelope === '.tsv') {
header("Content-type: text/tab-separated-values; charset=utf-8");
header("content-type: text/tab-separated-values; charset=utf-8");
echo "#item+request+id\t#item+response+body\t#date\n";
echo "urn:resolver:ping\tPONG\t" . date("c") . "\n";
// echo . "\n";
Expand Down Expand Up @@ -1380,8 +1408,8 @@ public function execute_welcome()
die;
}

header('Cache-Control: public, max-age=600, s-maxage=60, stale-while-revalidate=600, stale-if-error=600');
header("Content-type: application/json; charset=utf-8");
header('cache-control: public, max-age=600, s-maxage=60, stale-while-revalidate=600, stale-if-error=600');
header("content-type: application/json; charset=utf-8");

$resolver_paths = [];
foreach ($this->resolvers as $key => $value) {
Expand Down
2 changes: 1 addition & 1 deletion app/public/.well-known/urn/urn:resolver:.urnr.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"in.urn": "urn:resolver:index"
},
{
"in.urn": "urn:resolver:_allexamples"
"in.urn": "urn:resolver:exemplum?=u2709=.tsv"
},
{
"in.urn": "urn:resolver:ping?=u2709=.txt"
Expand Down
7 changes: 6 additions & 1 deletion resolvers/urn:resolver:.urnr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

# @TODO get more potential informal URN namespaces from
# https://project-thor.readme.io/docs/project-glossary
# @TODO maybe list OSM-related software on this list
# https://wambachers-osm.website/SoftwareWatchlist.html

$schema: urn:resolver:schema:urnr
"@id": urn:resolver
Expand All @@ -17,8 +19,11 @@ meta:
_cc_mode: internal

examples:
# index, s, m/s, nominativus, https://en.wiktionary.org/wiki/index#Latin
- in.urn: urn:resolver:index
- in.urn: urn:resolver:_allexamples
# exemplum, s, n, nominativus, https://en.wiktionary.org/wiki/exemplum#Latin

- in.urn: urn:resolver:exemplum?=u2709=.tsv
# - in.urn: urn:resolver:ping?u2709=.txt
- in.urn: urn:resolver:ping?=u2709=.txt
# - in.urn: urn:resolver:help
Expand Down
2 changes: 1 addition & 1 deletion scripts/urnresolver-self-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ red=$(tput setaf 1)
normal=$(tput sgr0)

# set -x
curl --silent "$URNRESOLVER_ENTRYPOINT/urn:resolver:_allexamples?=u2709=.tsv" >"$TEMPDIR/urn:resolver:_allexamples.tsv"
curl --silent "$URNRESOLVER_ENTRYPOINT/urn:resolver:exemplum?=u2709=.tsv" >"$TEMPDIR/urn:resolver:_allexamples.tsv"

# set +x

Expand Down

0 comments on commit 5793ebc

Please sign in to comment.