forked from jettero/jlt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-server.pl
executable file
·150 lines (97 loc) · 3.12 KB
/
test-server.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
use HTTP::Server::Simple;
use base qw(HTTP::Server::Simple::CGI);
use JSON;
$0 = "test-server.pl";
my $json = JSON->new;
my $start = "run";
my $port = 4000;
my $sleep;
my $auth_url;
my $view_url;
Getopt::Long::Configure("bundling");
GetOptions(
"background|daemon|b|d" => sub { $start = "background" },
"port|p=i" => \$port,
"sleep|s=i" => \$sleep,
"auth_url|a=s" => \$auth_url,
"view_url|v=s" => \$view_url,
"help|H" => sub { pod2usage(-verbose=>1) },
"h" => sub { pod2usage() },
) or pod2usage();
my $server = main->new($port);
$server->$start;
sub handle_fix {
my ($this, $fix, $fix_tlist) = @_;
return unless ref $fix eq "HASH";
for my $k (qw(t ll vv al ha va)) {
# NOTE: the request is malformed without these keys
return unless exists $fix->{$k};
}
my $t = delete $fix->{t};
$t = $t->[0] if ref $t;
push @$fix_tlist, $t;
}
sub handle_request {
my ($this, $cgi) = @_;
sleep rand $sleep if $sleep;
my $fix_tlist = [];
if( my $json_fixes = $cgi->param("fixes") ) {
my $fixes = eval { $json->decode($json_fixes) };
if( not ref $fixes ) {
print "HTTP/1.0 400 JSON error\r\nContent-Type: text/plain\r\n\r\n$@";
warn "invalid json: $@\n";
return;
}
$this->handle_fix( $_, $fix_tlist ) for @$fixes;
}
my $response = { fix_tlist=>$fix_tlist };
$response->{meta}{view_url} = $view_url if $view_url;
$response->{meta}{auth_url} = $auth_url if $auth_url;
my $j = $json->encode($response);
print "HTTP/1.0 200 OK\r\nContent-Type: text/javascript\r\n\r\n$j\r\n";
warn "json: $j\n" if $start ne "background";
}
__END__
=head1 NAME
test-server.pl - a very simple test tracking server
=head1 DESCRIPTION
This is just a quick little HTTP::Server::Simple GPS tracking server useful for testing purposes only.
=head1 SYNOPSIS
test-server.pl
--daemon --background -b -d start the server in the background
--port -p specify a port to use (default: 4000)
-h short help
--help -H full help
--view_url -v URL to deliver to the client as the view url
--auth_url -a URL to deliver to the client as the auth url
=head1 OPTIONS
=over
=item B<--daemon> B<--background> B<-b> B<-d>
Background mode. Once the server starts, it should background immediately.
=item B<--port> B<-p>
Speicify a port to run on.
=item B<--sleep> B<-s>
Randomly sleep for 0 - x seconds during the handler process.
=item B<--auth> B<-a>
Auth url to deliver to the client.
=item B<--view> B<-v>
Auth url to deliver to the client.
=item B<-h>
Short help.
=item B<-H> B<--help>
Long help (this).
=back
=head1 COPYRIGHT
Copyright 2009 -- Paul Miller C<< <jettero@cpan.org> >>
Licensed under the current version of the GPL.
=head1 REPORTING BUGS
Bugs can be reported under either github, email, or rt.cpan -- whichever seems most appropriate.
L<http://github.com/jettero/>
=head1 SEE ALSO
perl(1)
=cut