aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDarkelarious <darkelarious@333networks.com>2022-08-07 17:58:59 +0200
committerDarkelarious <darkelarious@333networks.com>2022-08-07 17:58:59 +0200
commit1ca9cdc52f3b63c8bf5e4550bbb922608beb437f (patch)
tree90802c7f7800c1b9d2d5d22f0e6b41b373ec39b9
parente9b4cd5fc79ca2d044eba4fed8bf62b138f44249 (diff)
downloadWebInterface-Perl-1ca9cdc52f3b63c8bf5e4550bbb922608beb437f.tar.gz
WebInterface-Perl-1ca9cdc52f3b63c8bf5e4550bbb922608beb437f.zip
fixes in Json API
-rwxr-xr-xChangelog5
-rwxr-xr-xREADME.md2
-rwxr-xr-xlib/MasterWebInterface/Handler/ErrorPages.pm6
-rwxr-xr-xlib/MasterWebInterface/Handler/Json/JsonServerInfo.pm10
-rwxr-xr-xlib/MasterWebInterface/Handler/Json/JsonServerList.pm15
-rwxr-xr-xlib/MasterWebInterface/Handler/Json/Motd.pm20
-rwxr-xr-xlib/MasterWebInterface/Util/Layout.pm6
7 files changed, 40 insertions, 24 deletions
diff --git a/Changelog b/Changelog
index 7d8ac30..936eafe 100755
--- a/Changelog
+++ b/Changelog
@@ -1,4 +1,9 @@
Changelog:
+
+07-08-2022: fixes in Json API
+ * update manual to reflect use of JSON instead of JSON::XS
+ * fix a few specific ubrowser functions
+ * set access policies to allow all sources
19-07-2022: simplify address format
* narrow address matching regexes
diff --git a/README.md b/README.md
index fd2bbf7..314e445 100755
--- a/README.md
+++ b/README.md
@@ -16,7 +16,7 @@ This repository contains software for a web interface to display information obt
* `DBI`
* `DBD::SQLite`
* `TUWF`
- * `JSON:XS`
+ * `JSON`
* `Image::Size` (optional for style generation)
* `AnyEvent` (optional for IP to Country lookup)
* `LWP::Simple` (optional for IP to Country lookup)
diff --git a/lib/MasterWebInterface/Handler/ErrorPages.pm b/lib/MasterWebInterface/Handler/ErrorPages.pm
index 8c0b5e9..ccf5555 100755
--- a/lib/MasterWebInterface/Handler/ErrorPages.pm
+++ b/lib/MasterWebInterface/Handler/ErrorPages.pm
@@ -21,7 +21,8 @@ sub handle404
# json error status separately
if ( $self->reqPath() =~ m/^\/json/ig)
{
- $self->resHeader("Content-Type", "application/json; charset=UTF-8");
+ # response as json data
+ $self->resHeader("Access-Control-Allow-Origin", "*");
$self->resJSON({
error => 1,
in => "url_format"
@@ -59,7 +60,8 @@ sub handle500
# json error status separately
if ( $self->reqPath() =~ m/^\/json/ig)
{
- $self->resHeader("Content-Type", "application/json; charset=UTF-8");
+ # response as json data
+ $self->resHeader("Access-Control-Allow-Origin", "*");
$self->resJSON({
error => 1,
in => "internal_error",
diff --git a/lib/MasterWebInterface/Handler/Json/JsonServerInfo.pm b/lib/MasterWebInterface/Handler/Json/JsonServerInfo.pm
index 5669003..2ff6194 100755
--- a/lib/MasterWebInterface/Handler/Json/JsonServerInfo.pm
+++ b/lib/MasterWebInterface/Handler/Json/JsonServerInfo.pm
@@ -22,11 +22,14 @@ sub json_serverinfo
hostport => $port,
limit => 1,
)->[0] if ($ip && $port);
-
+
+ # allow all outside sources to access the json api
+ $self->resHeader("Access-Control-Allow-Origin", "*");
+
# return error state on invalid IP/port
unless ($info)
{
- $self->resHeader("Content-Type", "application/json; charset=UTF-8");
+ # response as json data
$self->resJSON({
error => 1,
in => "not_in_db",
@@ -70,8 +73,7 @@ sub json_serverinfo
$info->{mapurl} = "/map/default/333networks.jpg";
}
- # return json data as the response
- $self->resHeader("Content-Type", "application/json; charset=UTF-8");
+ # response as json data
$self->resJSON($info);
}
diff --git a/lib/MasterWebInterface/Handler/Json/JsonServerList.pm b/lib/MasterWebInterface/Handler/Json/JsonServerList.pm
index 702a02e..e108066 100755
--- a/lib/MasterWebInterface/Handler/Json/JsonServerList.pm
+++ b/lib/MasterWebInterface/Handler/Json/JsonServerList.pm
@@ -27,10 +27,13 @@ sub serverlist_json
{ get => 'a', required => 0, default => '', maxlength => 200 },
);
+ # allow all outside sources to access the json api
+ $self->resHeader("Access-Control-Allow-Origin", "*");
+
# generate json error data if errors in field
if ( $f->{_err} )
{
- $self->resHeader("Content-Type", "application/json; charset=UTF-8");
+ # response as json data
$self->resJSON({
error => 1,
in => "options",
@@ -55,16 +58,18 @@ sub serverlist_json
($f->{a} =~ m/utdemo/ig) ? (utdemo => 1) : (),
);
- # get total number of players
+ # get total number of players in selected page(s)
my $pl = 0;
for (@{$list})
{
$pl += $_->{numplayers}
}
- # return json data as the response
- $self->resHeader("Content-Type", "application/json; charset=UTF-8");
- $self->resJSON( [$list, {total => $p, players => $pl}] );
+ # response as json data
+ $self->resJSON([
+ $list,
+ {total => $p, players => $pl}
+ ]);
}
1;
diff --git a/lib/MasterWebInterface/Handler/Json/Motd.pm b/lib/MasterWebInterface/Handler/Json/Motd.pm
index 869ae5f..15c57a6 100755
--- a/lib/MasterWebInterface/Handler/Json/Motd.pm
+++ b/lib/MasterWebInterface/Handler/Json/Motd.pm
@@ -30,22 +30,26 @@ sub json_motd
# get numServers
my ($l,$x,$s) = $self->dbServerListGet(
gamename => $gamename,
- results => 100,
+ updated => $self->{window_time},
+ limit => 9999,
+ sort => "numplayers",
+ reverse => 1,
);
my $p = 0;
for (@{$l})
{
- $p += $_->{numplayers}
+ $p += $_->{numplayers};
+ last unless $_->{numplayers}
}
- # return json data as the response
- my $json_data = encode_json [{motd => $html}, {total => $s, players => $p}];
- print { $self->resFd() } $json_data;
-
- # set content type and allow off-domain access (for example jQuery)
+ # response as json data
$self->resHeader("Access-Control-Allow-Origin", "*");
- $self->resHeader("Content-Type", "application/json; charset=UTF-8");
+ $self->resJSON([
+ {motd => $html},
+ {total => $s, players => $p}
+ ]);
+
}
1;
diff --git a/lib/MasterWebInterface/Util/Layout.pm b/lib/MasterWebInterface/Util/Layout.pm
index 33a7a8f..d9f5516 100755
--- a/lib/MasterWebInterface/Util/Layout.pm
+++ b/lib/MasterWebInterface/Util/Layout.pm
@@ -107,15 +107,13 @@ sub htmlHeader
################################################################################
sub htmlFooter
{
- my ($self, %o) = @_;
+ my $self = shift;
br style => "clear:both";
div id => 'footer';
txt "$self->{site_name} | Powered by ";
- a href => "http://333networks.com", "333networks";
- txt " | ";
- txt $o{last_edited} || "2021";
+ a href => "https://333networks.com", "333networks";
end;
end 'div'; # body
script type => 'text/javascript', src => "/masterscript.js", '';