| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Amazon::SQS::Simple::Base; |
|
2
|
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
6
|
use strict; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
39
|
|
|
4
|
2
|
|
|
2
|
|
5
|
use warnings; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
37
|
|
|
5
|
2
|
|
|
2
|
|
5
|
use Carp qw( croak carp ); |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
68
|
|
|
6
|
2
|
|
|
2
|
|
709
|
use Digest::HMAC_SHA1; |
|
|
2
|
|
|
|
|
7526
|
|
|
|
2
|
|
|
|
|
63
|
|
|
7
|
2
|
|
|
2
|
|
9
|
use Digest::SHA qw(hmac_sha256 sha256); |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
66
|
|
|
8
|
2
|
|
|
2
|
|
1064
|
use LWP::UserAgent; |
|
|
2
|
|
|
|
|
59501
|
|
|
|
2
|
|
|
|
|
57
|
|
|
9
|
2
|
|
|
2
|
|
748
|
use MIME::Base64; |
|
|
2
|
|
|
|
|
929
|
|
|
|
2
|
|
|
|
|
96
|
|
|
10
|
2
|
|
|
2
|
|
8
|
use URI::Escape; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
76
|
|
|
11
|
2
|
|
|
2
|
|
1337
|
use XML::Simple; |
|
|
2
|
|
|
|
|
11883
|
|
|
|
2
|
|
|
|
|
9
|
|
|
12
|
2
|
|
|
2
|
|
107
|
use HTTP::Date; |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
83
|
|
|
13
|
2
|
|
|
2
|
|
859
|
use HTTP::Request::Common; |
|
|
2
|
|
|
|
|
2983
|
|
|
|
2
|
|
|
|
|
101
|
|
|
14
|
2
|
|
|
2
|
|
780
|
use AWS::Signature4; |
|
|
2
|
|
|
|
|
25172
|
|
|
|
2
|
|
|
|
|
52
|
|
|
15
|
2
|
|
|
2
|
|
10
|
use POSIX qw(strftime); |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
8
|
|
|
16
|
2
|
|
|
2
|
|
1061
|
use Encode qw(encode); |
|
|
2
|
|
|
|
|
12656
|
|
|
|
2
|
|
|
|
|
110
|
|
|
17
|
2
|
|
|
2
|
|
968
|
use Data::Dumper; |
|
|
2
|
|
|
|
|
7348
|
|
|
|
2
|
|
|
|
|
92
|
|
|
18
|
2
|
|
|
2
|
|
801
|
use VM::EC2::Security::CredentialCache; |
|
|
2
|
|
|
|
|
657261
|
|
|
|
2
|
|
|
|
|
75
|
|
|
19
|
|
|
|
|
|
|
|
|
20
|
2
|
|
|
2
|
|
12
|
use base qw(Exporter); |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
178
|
|
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
use constant ({ |
|
23
|
2
|
|
|
|
|
1883
|
SQS_VERSION_2012_11_05 => '2012-11-05', |
|
24
|
|
|
|
|
|
|
BASE_ENDPOINT => 'http://sqs.us-east-1.amazonaws.com', |
|
25
|
|
|
|
|
|
|
DEF_MAX_GET_MSG_SIZE => 4096, # Messages larger than this size will use a POST request. |
|
26
|
2
|
|
|
2
|
|
7
|
}); |
|
|
2
|
|
|
|
|
4
|
|
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
our $DEFAULT_SQS_VERSION = SQS_VERSION_2012_11_05; |
|
30
|
|
|
|
|
|
|
our @EXPORT = qw(SQS_VERSION_2012_11_05); |
|
31
|
|
|
|
|
|
|
our $URI_SAFE_CHARACTERS = '^A-Za-z0-9-_.~'; # defined by AWS, same as URI::Escape defaults |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub new { |
|
34
|
3
|
|
|
3
|
0
|
123557
|
my $class = shift; |
|
35
|
3
|
|
|
|
|
7
|
my @args = @_; |
|
36
|
3
|
100
|
66
|
|
|
18
|
if (scalar(@args) >= 2 && $args[0] ne 'UseIAMRole') { |
|
37
|
2
|
|
|
|
|
3
|
my $access_key = shift @args; |
|
38
|
2
|
|
|
|
|
2
|
my $secret_key = shift @args; |
|
39
|
2
|
|
|
|
|
5
|
@args = (AWSAccessKeyId => $access_key, |
|
40
|
|
|
|
|
|
|
SecretKey => $secret_key, @args); |
|
41
|
|
|
|
|
|
|
} |
|
42
|
3
|
|
|
|
|
10
|
my $self = { |
|
43
|
|
|
|
|
|
|
Endpoint => +BASE_ENDPOINT, |
|
44
|
|
|
|
|
|
|
SignatureVersion => 4, |
|
45
|
|
|
|
|
|
|
Version => $DEFAULT_SQS_VERSION, |
|
46
|
|
|
|
|
|
|
@args |
|
47
|
|
|
|
|
|
|
}; |
|
48
|
|
|
|
|
|
|
|
|
49
|
3
|
50
|
|
|
|
10
|
if (!defined($self->{UserAgent})) { |
|
50
|
3
|
|
|
|
|
14
|
$self->{UserAgent} = LWP::UserAgent->new(keep_alive => 4); |
|
51
|
|
|
|
|
|
|
} |
|
52
|
|
|
|
|
|
|
|
|
53
|
3
|
50
|
|
|
|
3350
|
if (defined($self->{Timeout})) { |
|
54
|
0
|
|
|
|
|
0
|
$self->{UserAgent}->timeout($self->{Timeout}); |
|
55
|
|
|
|
|
|
|
} |
|
56
|
|
|
|
|
|
|
|
|
57
|
3
|
50
|
|
|
|
7
|
if (!defined($self->{Region})) { |
|
58
|
3
|
|
|
|
|
5
|
$self->{Region} = 'us-east-1'; |
|
59
|
|
|
|
|
|
|
} |
|
60
|
|
|
|
|
|
|
|
|
61
|
3
|
|
|
|
|
9
|
$self->{UserAgent}->env_proxy; |
|
62
|
|
|
|
|
|
|
|
|
63
|
3
|
100
|
66
|
|
|
2851
|
if (!$self->{UseIAMRole} && (!$self->{AWSAccessKeyId} || !$self->{SecretKey})) { |
|
|
|
|
33
|
|
|
|
|
|
64
|
1
|
|
|
|
|
207
|
croak "Missing AWSAccessKey or SecretKey"; |
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
|
|
67
|
2
|
|
|
|
|
3
|
$self = bless($self, $class); |
|
68
|
2
|
|
|
|
|
7
|
return $self; |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub _api_version { |
|
72
|
1
|
|
|
1
|
|
20
|
my $self = shift; |
|
73
|
1
|
|
|
|
|
7
|
return $self->{Version}; |
|
74
|
|
|
|
|
|
|
} |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub _dispatch { |
|
77
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
78
|
0
|
|
0
|
|
|
|
my $params = shift || {}; |
|
79
|
0
|
|
0
|
|
|
|
my $force_array = shift || []; |
|
80
|
0
|
|
|
|
|
|
my $url = $self->{Endpoint}; |
|
81
|
0
|
|
|
|
|
|
my $response; |
|
82
|
|
|
|
|
|
|
my $post_body; |
|
83
|
0
|
|
|
|
|
|
my $post_request = 0; |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
$params = { |
|
86
|
|
|
|
|
|
|
Version => $self->{Version}, |
|
87
|
0
|
|
|
|
|
|
%$params |
|
88
|
|
|
|
|
|
|
}; |
|
89
|
|
|
|
|
|
|
|
|
90
|
0
|
0
|
0
|
|
|
|
if (!$params->{Timestamp} && !$params->{Expires}) { |
|
91
|
0
|
|
|
|
|
|
$params->{Timestamp} = _timestamp(); |
|
92
|
|
|
|
|
|
|
} |
|
93
|
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
foreach my $try (1..4) { |
|
95
|
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
my $req = HTTP::Request->new(POST => $url); |
|
97
|
0
|
|
|
|
|
|
$req->header(host => URI->new($url)->host); |
|
98
|
0
|
|
|
|
|
|
my $now = time; |
|
99
|
0
|
|
|
|
|
|
my $http_date = strftime('%Y%m%dT%H%M%SZ', gmtime($now)); |
|
100
|
0
|
|
|
|
|
|
my $date = strftime('%Y%m%d', gmtime($now)); |
|
101
|
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
$req->protocol('HTTP/1.1'); |
|
103
|
0
|
|
|
|
|
|
$req->header('Date' => $http_date); |
|
104
|
0
|
|
|
|
|
|
$req->header('x-amz-target', 'AmazonSQSv20121105.' . $params->{Action}); |
|
105
|
0
|
|
|
|
|
|
$req->header('content-type' => 'application/x-www-form-urlencoded;charset=utf-8'); |
|
106
|
|
|
|
|
|
|
|
|
107
|
0
|
0
|
|
|
|
|
if ($self->{UseIAMRole}) { |
|
108
|
0
|
|
|
|
|
|
my $creds = VM::EC2::Security::CredentialCache->get(); |
|
109
|
0
|
0
|
|
|
|
|
defined($creds) || die("Unable to retrieve IAM role credentials"); |
|
110
|
0
|
|
|
|
|
|
$self->{AWSAccessKeyId} = $creds->accessKeyId; |
|
111
|
0
|
|
|
|
|
|
$self->{SecretKey} = $creds->secretAccessKey; |
|
112
|
0
|
|
|
|
|
|
$req->header('x-amz-security-token' => $creds->sessionToken); |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
$params->{AWSAccessKeyId} = $self->{AWSAccessKeyId}; |
|
116
|
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
my $escaped_params = $self->_escape_params($params); |
|
118
|
0
|
|
|
|
|
|
my $payload = join('&', map { $_ . '=' . $escaped_params->{$_} } keys %$escaped_params); |
|
|
0
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
|
$req->content($payload); |
|
120
|
0
|
|
|
|
|
|
$req->header('Content-Length', length($payload)); |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
my $signer = AWS::Signature4->new(-access_key => $self->{AWSAccessKeyId}, |
|
123
|
0
|
|
|
|
|
|
-secret_key => $self->{SecretKey}); |
|
124
|
0
|
|
|
|
|
|
$signer->sign($req); |
|
125
|
|
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
|
$self->_debug_log($req->as_string()); |
|
127
|
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
$response = $self->{UserAgent}->request($req); |
|
129
|
|
|
|
|
|
|
|
|
130
|
0
|
0
|
|
|
|
|
if ($response->is_success) { |
|
131
|
0
|
|
|
|
|
|
$self->_debug_log($response->content); |
|
132
|
0
|
|
|
|
|
|
my $href = XMLin($response->content, ForceArray => $force_array, KeyAttr => {}); |
|
133
|
0
|
|
|
|
|
|
return $href; |
|
134
|
|
|
|
|
|
|
} else { |
|
135
|
0
|
|
|
|
|
|
die("Got an error: " . $response->as_string()); |
|
136
|
|
|
|
|
|
|
} |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
# advice from internal AWS support - most client libraries try 3 times in the face |
|
139
|
|
|
|
|
|
|
# of 500 errors, so ours should too |
|
140
|
|
|
|
|
|
|
# use exponential backoff. |
|
141
|
|
|
|
|
|
|
|
|
142
|
0
|
0
|
0
|
|
|
|
if ($response->code == 500 || $response->code == 503) { |
|
143
|
0
|
|
|
|
|
|
my $sleep_amount= 2 ** $try * 50 * 1000; |
|
144
|
0
|
|
|
|
|
|
$self->_debug_log("Doing sleep for: $sleep_amount"); |
|
145
|
0
|
|
|
|
|
|
Time::HiRes::usleep($sleep_amount); |
|
146
|
0
|
|
|
|
|
|
next; |
|
147
|
|
|
|
|
|
|
} |
|
148
|
|
|
|
|
|
|
} |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
# if we fall out of the loop, then we have either a non-500 error or a persistent 500. |
|
151
|
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
|
my $msg; |
|
153
|
0
|
|
|
|
|
|
eval { |
|
154
|
0
|
|
|
|
|
|
my $href = XMLin($response->content); |
|
155
|
0
|
|
|
|
|
|
$msg = $href->{Error}{Message}; |
|
156
|
|
|
|
|
|
|
}; |
|
157
|
|
|
|
|
|
|
|
|
158
|
0
|
|
|
|
|
|
my $error = "ERROR: On calling $params->{Action}: " . $response->status_line; |
|
159
|
0
|
0
|
|
|
|
|
$error .= " ($msg)" if $msg; |
|
160
|
0
|
|
|
|
|
|
croak $error; |
|
161
|
|
|
|
|
|
|
} |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub _debug_log { |
|
164
|
0
|
|
|
0
|
|
|
my ($self, $msg) = @_; |
|
165
|
0
|
0
|
|
|
|
|
return unless $self->{_Debug}; |
|
166
|
0
|
|
|
|
|
|
chomp($msg); |
|
167
|
0
|
|
|
|
|
|
print {$self->{_Debug}} $msg . "\n\n"; |
|
|
0
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
} |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
sub _escape_params { |
|
171
|
0
|
|
|
0
|
|
|
my ($self, $params) = @_; |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
# Need to escape + characters in signature |
|
174
|
|
|
|
|
|
|
# see http://docs.amazonwebservices.com/AWSSimpleQueueService/2006-04-01/Query_QueryAuth.html |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# Likewise, need to escape + characters in ReceiptHandle |
|
177
|
|
|
|
|
|
|
# Many characters are possible in MessageBody: |
|
178
|
|
|
|
|
|
|
# #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] |
|
179
|
|
|
|
|
|
|
# probably should encode all keys and values for consistency and future-proofing |
|
180
|
0
|
|
|
|
|
|
my $to_escape = qr{^(?:Signature|MessageBody|ReceiptHandle)|\.\d+\.(?:MessageBody|ReceiptHandle)$}; |
|
181
|
0
|
|
|
|
|
|
foreach my $key (keys %$params) { |
|
182
|
0
|
0
|
|
|
|
|
next unless $key =~ m/$to_escape/; |
|
183
|
0
|
|
|
|
|
|
my $octets = encode('utf-8-strict', $params->{$key}); |
|
184
|
0
|
|
|
|
|
|
$params->{$key} = uri_escape($octets, $URI_SAFE_CHARACTERS); |
|
185
|
|
|
|
|
|
|
} |
|
186
|
0
|
|
|
|
|
|
return $params; |
|
187
|
|
|
|
|
|
|
} |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
sub _escape_param { |
|
190
|
0
|
|
|
0
|
|
|
my $params = shift; |
|
191
|
0
|
|
|
|
|
|
my $single = shift; |
|
192
|
0
|
|
|
|
|
|
my $multi_n = shift; |
|
193
|
|
|
|
|
|
|
|
|
194
|
0
|
0
|
|
|
|
|
if ($params->{$single}) { |
|
195
|
0
|
|
|
|
|
|
$params->{$single} = uri_escape($params->{$single}); |
|
196
|
|
|
|
|
|
|
} else { |
|
197
|
0
|
|
|
|
|
|
foreach my $i (1..10) { |
|
198
|
0
|
|
|
|
|
|
my $multi = $multi_n; |
|
199
|
0
|
|
|
|
|
|
$multi =~ s/\.n\./\.$i\./; |
|
200
|
0
|
0
|
|
|
|
|
if ($params->{$multi}) { |
|
201
|
0
|
|
|
|
|
|
$params->{$multi} = uri_escape($params->{$multi}); |
|
202
|
|
|
|
|
|
|
} else { |
|
203
|
0
|
|
|
|
|
|
last; |
|
204
|
|
|
|
|
|
|
} |
|
205
|
|
|
|
|
|
|
} |
|
206
|
|
|
|
|
|
|
} |
|
207
|
|
|
|
|
|
|
} |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
sub _max_get_msg_size { |
|
210
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
211
|
|
|
|
|
|
|
# a user-defined cut-off |
|
212
|
0
|
0
|
|
|
|
|
if (defined $self->{MAX_GET_MSG_SIZE}) { |
|
213
|
0
|
|
|
|
|
|
return $self->{MAX_GET_MSG_SIZE}; |
|
214
|
|
|
|
|
|
|
} |
|
215
|
|
|
|
|
|
|
# the default cut-off |
|
216
|
|
|
|
|
|
|
else { |
|
217
|
0
|
|
|
|
|
|
return DEF_MAX_GET_MSG_SIZE; |
|
218
|
|
|
|
|
|
|
} |
|
219
|
|
|
|
|
|
|
} |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
sub _timestamp { |
|
222
|
0
|
|
|
0
|
|
|
my $t = shift; |
|
223
|
0
|
0
|
|
|
|
|
if (!defined($t)) { |
|
224
|
0
|
|
|
|
|
|
$t = time; |
|
225
|
|
|
|
|
|
|
} |
|
226
|
0
|
|
|
|
|
|
my $formatted_time = HTTP::Date::time2isoz($t); |
|
227
|
0
|
|
|
|
|
|
$formatted_time =~ s/ /T/; |
|
228
|
0
|
|
|
|
|
|
return $formatted_time; |
|
229
|
|
|
|
|
|
|
} |
|
230
|
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
1; |
|
232
|
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
__END__ |
|
234
|
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=head1 NAME |
|
236
|
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
Amazon::SQS::Simple::Base - No user-serviceable parts included |
|
238
|
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=head1 AUTHOR |
|
240
|
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
Copyright 2007-2008 Simon Whitaker E<lt>swhitaker@cpan.orgE<gt> |
|
242
|
|
|
|
|
|
|
Copyright 2013-2017 Mike (no relation) Whitaker E<lt>penfold@cpan.orgE<gt> |
|
243
|
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
245
|
|
|
|
|
|
|
under the same terms as Perl itself. |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=cut |
|
248
|
|
|
|
|
|
|
|