line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package StreamRoot::Manifest; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
15714
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
27
|
|
4
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
23
|
|
5
|
1
|
|
|
1
|
|
195
|
use WWW::Curl::Easy; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use JSON; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
|
|
|
|
|
|
my $class = shift; |
12
|
|
|
|
|
|
|
my $self = {}; |
13
|
|
|
|
|
|
|
$self->{token} = shift||undef; |
14
|
|
|
|
|
|
|
$self->{curl} = WWW::Curl::Easy->new; |
15
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_TIMEOUT, 180); |
16
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_HEADER, 0); |
17
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'); |
18
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_AUTOREFERER, 1); |
19
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_FOLLOWLOCATION, 1); |
20
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_SSL_VERIFYPEER, 0); |
21
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_VERBOSE, 0); |
22
|
|
|
|
|
|
|
return bless $self, $class; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub curl { |
26
|
|
|
|
|
|
|
my ( $self, $url, $data, $referer ) = @_; |
27
|
|
|
|
|
|
|
my $params = ""; |
28
|
|
|
|
|
|
|
if ($data) { |
29
|
|
|
|
|
|
|
if (ref($data) eq 'HASH') { |
30
|
|
|
|
|
|
|
for(keys %$data){ |
31
|
|
|
|
|
|
|
$params .= "&" if $params; |
32
|
|
|
|
|
|
|
$params .= "$_=$data->{$_}"; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
}else{ |
35
|
|
|
|
|
|
|
$params = "$data"; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_POSTFIELDS, $params); |
39
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_URL, $url); |
40
|
|
|
|
|
|
|
my $content = ""; |
41
|
|
|
|
|
|
|
open(my $writedata, ">", \$content); |
42
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_WRITEDATA, $writedata); |
43
|
|
|
|
|
|
|
if ($self->{curl}->perform == 0) { |
44
|
|
|
|
|
|
|
if ($self->{curl}->getinfo(CURLINFO_HTTP_CODE) =~ /^2/) { |
45
|
|
|
|
|
|
|
decode_json($content) if $content !~ /^$/; |
46
|
|
|
|
|
|
|
}else{ |
47
|
|
|
|
|
|
|
if ($self->{curl}->getinfo(CURLINFO_HTTP_CODE) =~ /^4/) { |
48
|
|
|
|
|
|
|
decode_json($content) if $content !~ /^$/; |
49
|
|
|
|
|
|
|
}else{ |
50
|
|
|
|
|
|
|
die($content); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub setToken { |
57
|
|
|
|
|
|
|
my $self = shift; |
58
|
|
|
|
|
|
|
$self->{token} = shift||undef; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub getToken { |
62
|
|
|
|
|
|
|
my $self = shift; |
63
|
|
|
|
|
|
|
return $self->{token}; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub authenticate { |
67
|
|
|
|
|
|
|
my ( $self, $username, $password ) = @_; |
68
|
|
|
|
|
|
|
my $data = { |
69
|
|
|
|
|
|
|
'username' => $username, |
70
|
|
|
|
|
|
|
'password' => $password |
71
|
|
|
|
|
|
|
}; |
72
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_CUSTOMREQUEST, 'POST'); |
73
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_HTTPHEADER(), ['Content-type: application/x-www-form-urlencoded']); |
74
|
|
|
|
|
|
|
my $content = $self->curl('https://manifests.streamroot.io/auth', $data); |
75
|
|
|
|
|
|
|
$self->setToken($content->{token}) if $content->{token}; |
76
|
|
|
|
|
|
|
return $content; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub addManifest { |
80
|
|
|
|
|
|
|
my $self = shift; |
81
|
|
|
|
|
|
|
my @values = @_; |
82
|
|
|
|
|
|
|
if (ref($values[0]) eq 'HASH') { |
83
|
|
|
|
|
|
|
return $self->loop('add', @values); |
84
|
|
|
|
|
|
|
}else{ |
85
|
|
|
|
|
|
|
my %params = @values; |
86
|
|
|
|
|
|
|
my $data = {}; |
87
|
|
|
|
|
|
|
$data->{url} = $params{url}; |
88
|
|
|
|
|
|
|
$data->{status} = $params{status} if $params{status}; |
89
|
|
|
|
|
|
|
$data->{ttl} = $params{ttl} if $params{ttl}; |
90
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_CUSTOMREQUEST, 'POST'); |
91
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_HTTPHEADER(), ['Authorization:Bearer '.$self->getToken]); |
92
|
|
|
|
|
|
|
my $content = $self->curl('https://manifests.streamroot.io', $data); |
93
|
|
|
|
|
|
|
return $content; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub updateManifest { |
98
|
|
|
|
|
|
|
my $self = shift; |
99
|
|
|
|
|
|
|
my @values = @_; |
100
|
|
|
|
|
|
|
if (ref($values[0]) eq 'HASH') { |
101
|
|
|
|
|
|
|
return $self->loop('update', @values); |
102
|
|
|
|
|
|
|
}else{ |
103
|
|
|
|
|
|
|
my %params = @values; |
104
|
|
|
|
|
|
|
my $data = {}; |
105
|
|
|
|
|
|
|
$data->{status} = $params{status} if $params{status}; |
106
|
|
|
|
|
|
|
$data->{live} = $params{live} if $params{live}; |
107
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_CUSTOMREQUEST, 'PUT'); |
108
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_HTTPHEADER(), ['Authorization:Bearer '.$self->getToken]); |
109
|
|
|
|
|
|
|
my $content = $self->curl('https://manifests.streamroot.io/' . $params{id}, $data); |
110
|
|
|
|
|
|
|
return $content; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub removeManifest { |
115
|
|
|
|
|
|
|
my $self = shift; |
116
|
|
|
|
|
|
|
my @values = @_; |
117
|
|
|
|
|
|
|
if (scalar(@values) > 1) { |
118
|
|
|
|
|
|
|
return $self->loop('remove', @values); |
119
|
|
|
|
|
|
|
}else{ |
120
|
|
|
|
|
|
|
my $id = $values[0]; |
121
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_CUSTOMREQUEST, 'DELETE'); |
122
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_HTTPHEADER(), ['Authorization:Bearer '.$self->getToken]); |
123
|
|
|
|
|
|
|
my $content = $self->curl('https://manifests.streamroot.io/' . $id); |
124
|
|
|
|
|
|
|
return $content; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub showManifest { |
129
|
|
|
|
|
|
|
my $self = shift; |
130
|
|
|
|
|
|
|
my @values = @_; |
131
|
|
|
|
|
|
|
if (scalar(@values) > 1) { |
132
|
|
|
|
|
|
|
return $self->loop('show', @values); |
133
|
|
|
|
|
|
|
}else{ |
134
|
|
|
|
|
|
|
my $id = $values[0]; |
135
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_CUSTOMREQUEST, 'GET'); |
136
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_HTTPHEADER(), ['Authorization:Bearer '.$self->getToken]); |
137
|
|
|
|
|
|
|
my $content = $self->curl('https://manifests.streamroot.io/' . $id); |
138
|
|
|
|
|
|
|
return $content; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub showAllManifests { |
143
|
|
|
|
|
|
|
my ( $self, $pattern ) = @_; |
144
|
|
|
|
|
|
|
my $data = {}; |
145
|
|
|
|
|
|
|
$data->{pattern} = $pattern if $pattern; |
146
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_CUSTOMREQUEST, 'GET'); |
147
|
|
|
|
|
|
|
$self->{curl}->setopt(CURLOPT_HTTPHEADER(), ['Authorization:Bearer '.$self->getToken]); |
148
|
|
|
|
|
|
|
my $content = $self->curl('https://manifests.streamroot.io', $data); |
149
|
|
|
|
|
|
|
return $content; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub loop { |
153
|
|
|
|
|
|
|
my ($self, $type, @values) = @_; |
154
|
|
|
|
|
|
|
my @results; |
155
|
|
|
|
|
|
|
for(@values){ |
156
|
|
|
|
|
|
|
my $content; |
157
|
|
|
|
|
|
|
if ($type eq 'add') { |
158
|
|
|
|
|
|
|
$content = $self->addManifest(%{$_}); |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
if ($type eq 'update') { |
161
|
|
|
|
|
|
|
$content = $self->updateManifest(%{$_}); |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
if ($type eq 'remove') { |
164
|
|
|
|
|
|
|
$content = $self->removeManifest($_); |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
if ($type eq 'show') { |
167
|
|
|
|
|
|
|
$content = $self->showManifest($_); |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
push(@results, $content); |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
return $results[0] if scalar(@results) == 1; |
172
|
|
|
|
|
|
|
return \@results if scalar(@results) > 1; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
1; |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
__END__ |