aboutsummaryrefslogtreecommitdiff
path: root/lib/MasterWebInterface/Handler/Json
diff options
context:
space:
mode:
Diffstat (limited to 'lib/MasterWebInterface/Handler/Json')
-rwxr-xr-xlib/MasterWebInterface/Handler/Json/JsonServerInfo.pm51
-rwxr-xr-xlib/MasterWebInterface/Handler/Json/JsonServerList.pm81
-rwxr-xr-xlib/MasterWebInterface/Handler/Json/Motd.pm2
3 files changed, 40 insertions, 94 deletions
diff --git a/lib/MasterWebInterface/Handler/Json/JsonServerInfo.pm b/lib/MasterWebInterface/Handler/Json/JsonServerInfo.pm
index e6f5b44..5669003 100755
--- a/lib/MasterWebInterface/Handler/Json/JsonServerInfo.pm
+++ b/lib/MasterWebInterface/Handler/Json/JsonServerInfo.pm
@@ -5,21 +5,16 @@ use Exporter 'import';
use JSON;
TUWF::register(
- qr{json/(.[\w]{1,20})/([\:\.\w]{9,35})} => \&json_serverinfo,
+ qr{json/([\w]{1,20})/(\w{4}:\w{4}:\w{4}:\w{4}:\w{4}:\w{4}:\w{4}:\w{4}):(\d{1,5})} => \&json_serverinfo, # ipv6
+ qr{json/([\w]{1,20})/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})} => \&json_serverinfo, # ipv4
);
-################################################################################
-# Server Info
-# Show server info for an individual server
-# Same as &server_info, but with json output.
-# returns "error:1" if errors occurred
-################################################################################
+#
+# Show server info for an individual server.
+#
sub json_serverinfo
{
- my ($self, $gamename, $s_addr, $s_port) = @_;
-
- # parse from ipv4/6 and soft sanity check
- my ($ip, $port) = $self->from_addr_str($s_addr);
+ my ($self, $gamename, $ip, $port) = @_;
# select server from database
my $info = $self->dbGetServerInfo(
@@ -28,20 +23,16 @@ sub json_serverinfo
limit => 1,
)->[0] if ($ip && $port);
- # display an error in case of an invalid IP or port
+ # return error state on invalid IP/port
unless ($info)
{
- my %err = (error => 1, ip => $ip, port => $port);
- my $e = \%err;
- my $json_data = encode_json $e;
- my $json_data_size = keys %$e;
-
- # return json data as the response
- print { $self->resFd() } $json_data;
-
- # set content type at the end
- $self->resHeader("Access-Control-Allow-Origin", "*");
$self->resHeader("Content-Type", "application/json; charset=UTF-8");
+ $self->resJSON({
+ error => 1,
+ in => "not_in_db",
+ ip => $ip,
+ port => $port,
+ });
return;
}
@@ -54,14 +45,9 @@ sub json_serverinfo
$players{"player_$i"} = $pl_list->[$i];
}
- use Data::Dumper 'Dumper';
- my $str = Dumper $pl_list;
-
- # merge
- #$info = { %$info, %$details } if $details;
+ # merge with rest of info
$info = { %$info, %players } if %players;
-
# find the correct thumbnail, otherwise game default, otherwise 333 default
my $mapname = lc $info->{mapname};
@@ -83,17 +69,10 @@ sub json_serverinfo
# 333networks default
$info->{mapurl} = "/map/default/333networks.jpg";
}
-
- # encode
- my $json_data = encode_json $info;
- my $json_data_size = keys %$info;
# return json data as the response
- print { $self->resFd() } $json_data;
-
- # set content type and allow off-domain access (for example jQuery)
- $self->resHeader("Access-Control-Allow-Origin", "*");
$self->resHeader("Content-Type", "application/json; charset=UTF-8");
+ $self->resJSON($info);
}
1;
diff --git a/lib/MasterWebInterface/Handler/Json/JsonServerList.pm b/lib/MasterWebInterface/Handler/Json/JsonServerList.pm
index 7781d94..702a02e 100755
--- a/lib/MasterWebInterface/Handler/Json/JsonServerList.pm
+++ b/lib/MasterWebInterface/Handler/Json/JsonServerList.pm
@@ -5,15 +5,12 @@ use Exporter 'import';
use JSON;
TUWF::register(
- qr{json/(.[\w]{1,20})} => \&serverlist_json,
- qr{json/(.[\w]{1,20})/(all|[0a-z])} => \&serverlist_json,
+ qr{json/([\w]{1,20})} => \&serverlist_json, # valid list
);
-################################################################################
-# LIST SERVERS
+#
# Generate a list of selected games in the database per game (arg: gamename)
-# Same as &serverlist, but with json output.
-################################################################################
+#
sub serverlist_json
{
my($self, $gamename) = @_;
@@ -21,50 +18,26 @@ sub serverlist_json
# sorting, page
my $f = $self->formValidate(
- {
- get => 's',
- required => 0,
- default => 'gamename',
- enum => [ qw| hostname gamename country added gametype numplayers mapname | ]
- },
- {
- get => 'o',
- required => 0,
- default => 'a',
- enum => [ 'a','d' ]
- },
- {
- get => 'p',
- required => 0,
- default => 1,
- template => 'page',
- },
- {
- get => 'q',
- required => 0,
- default => '',
- maxlength => 90
- },
- {
- get => 'r',
- required => 0,
- default => 100,
- template => 'page'
- },
- {
- get => 'g',
- required => 0,
- default => '',
- maxlength => 90
- },
- {
- get => 'a',
- required => 0,
- default => '',
- maxlength => 200
- },
+ { get => 's', required => 0, default => 'gamename', enum => [qw|hostname gamename country added gametype numplayers mapname|] },
+ { get => 'o', required => 0, default => 'a', enum => ['a','d'] },
+ { get => 'p', required => 0, default => 1, template => 'page' },
+ { get => 'r', required => 0, default => 100, template => 'page' },
+ { get => 'q', required => 0, default => '', maxlength => 90 },
+ { get => 'g', required => 0, default => '', maxlength => 90 },
+ { get => 'a', required => 0, default => '', maxlength => 200 },
);
- return $self->resNotFound if $f->{_err};
+
+ # generate json error data if errors in field
+ if ( $f->{_err} )
+ {
+ $self->resHeader("Content-Type", "application/json; charset=UTF-8");
+ $self->resJSON({
+ error => 1,
+ in => "options",
+ options => $f->{_err}
+ });
+ return;
+ }
# load server list from database
my ( $list, $np, $p ) = $self->dbServerListGet(
@@ -77,7 +50,7 @@ sub serverlist_json
updated => $self->{window_time},
gametype => $f->{g},
- # parse extra request parameters like version, populated, etc
+ # parse extra request parameters for ubrowser.333networks.com
($f->{a} =~ m/popserv/ig) ? (popserv => 1) : (),
($f->{a} =~ m/utdemo/ig) ? (utdemo => 1) : (),
);
@@ -90,14 +63,8 @@ sub serverlist_json
}
# return json data as the response
- my $json_data = encode_json [$list, {total => $p, players => $pl}];
- print {
- $self->resFd()
- } $json_data;
-
- # set content type and allow off-domain access (for example jQuery)
- $self->resHeader("Access-Control-Allow-Origin", "*");
$self->resHeader("Content-Type", "application/json; charset=UTF-8");
+ $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 c473f12..869ae5f 100755
--- a/lib/MasterWebInterface/Handler/Json/Motd.pm
+++ b/lib/MasterWebInterface/Handler/Json/Motd.pm
@@ -8,7 +8,7 @@ use Exporter 'import';
our @EXPORT = qw| motd_static |;
TUWF::register(
- qr{json/(.[\w]{1,20})/motd} => \&json_motd,
+ qr{json/([\w]{1,20})/motd} => \&json_motd,
);
# Message of the Day for things like the JSON API or updateserver page