diff options
Diffstat (limited to 'lib/MasterWebInterface/Handler')
| -rwxr-xr-x | lib/MasterWebInterface/Handler/ErrorPages.pm | 6 | ||||
| -rwxr-xr-x | lib/MasterWebInterface/Handler/Json/JsonServerInfo.pm | 10 | ||||
| -rwxr-xr-x | lib/MasterWebInterface/Handler/Json/JsonServerList.pm | 15 | ||||
| -rwxr-xr-x | lib/MasterWebInterface/Handler/Json/Motd.pm | 20 |
4 files changed, 32 insertions, 19 deletions
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; |
