-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresty.pl
More file actions
executable file
·222 lines (181 loc) · 5.24 KB
/
resty.pl
File metadata and controls
executable file
·222 lines (181 loc) · 5.24 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/perl
$ENV{https_proxy}=undef;
$ENV{http_proxy}=undef;
# $Header: /var/cvsroot/platform/hsync/tests/Attic/resty.pl,v 1.1.4.2 2010/02/09 17:54:42 abaumann Exp $
#
# Example use
# resty.pl -s -h hss02o -u MD1234 -p Rdrd1234 GET /hsync/capabilities
# resty.pl -s -h hss02o -u MD1234 -p Rdrd1234 GET /hsync/account
# resty.pl -s -h hss02o -u MD1234 -p Rdrd1234 PUT /hsync/account text/xml acc.xml
# resty.pl -s -h hss02o -u MD1234 -p Rdrd1234 GET /hsync/logs
# resty.pl -s -h hss02o -u MD1234 -p Rdrd1234 GET /hsync/status
# resty.pl -s -h hss02o -u MD1234 -p Rdrd1234 GET /hsync/motd
# resty.pl -s -h hss02o -u MD1234 -p Rdrd1234 GET /hsync/users
# resty.pl -s -h hss02o -u MD1234 -p Rdrd1234 POST /hsync/users text/ldif 100.ldif
# resty.pl -s -h hss02o -u MD1234 -p Rdrd1234 PUT /hsync/users text/ldif 100.ldif
# resty.pl -s -h hss02o -u MD1234 -p Rdrd1234 GET /logs/hosted_agg01o_100001_10.5.133.41_1_1252339674_1.gz
# resty.pl -s -h hss02o -u MD1234 -p Rdrd1234 DELETE /logs/hosted_agg01o_100001_10.5.133.41_1_1252339674_1.gz
#
use strict;
use warnings;
use HTTP::Request;
use HTTP::Request::Common;
use LWP::UserAgent;
use Getopt::Long;
use Digest::MD5 qw(md5_base64 md5_hex);
use Compress::Zlib;
my $user;
my $password;
my $host;
my $protocol = 'http://';
my $port;
my $ssl;
my $gzip;
my $ta;
my $content = '/tmp/hsyncout';
my $quiet;
my $etag;
my $realm = 'hsync';
GetOptions("user|u=s" => \$user,
"password|p=s" => \$password,
"host|h=s" => \$host,
"content|c=s" => \$content,
"gzip|g" => \$gzip,
"ta" => \$ta,
"ssl|s" => \$ssl,
"realm|r" => \$realm,
"quiet|q" => \$quiet,
"etag|e=s" => \$etag,
"port=s" => \$port,
);
if ($ssl) {
$protocol = 'https://';
}
if (!defined $port) {
$port= '80';
if ($ssl) {
$port = '443';
}
if ($ta) {
$port = '8529';
}
}
my $ua = LWP::UserAgent->new(timeout=>1800);
$ua->agent("resty/0.1 ");
if ($user) {
$ua->credentials("$host:$port", $realm, $user, $password);
}
if ($port != 80) {
$host = "$host:$port";
}
# Quick command line interface
sub subcommand {
my ($expected, $code) = @_;
if ($expected eq $ARGV[0]) {
shift @ARGV;
my $resp = $code->();
print STDERR "Response Code:".$resp->code."\n" unless $quiet;
print STDERR $resp->headers_as_string unless $quiet;
print STDERR "\n" unless $quiet;
print $resp->content unless $quiet;
open my $outfh, '>', $content;
print $outfh $resp->content;
close $outfh;
print STDERR "\n" unless $quiet;
print STDERR "Length: ",length($resp->content),"\n" unless $quiet;
print STDERR "MD5: ", md5_base64($resp->content),"\n" unless $quiet;
if (!$resp->is_success) {
exit 1;
}
exit;
}
}
sub slurp {
my $filename = shift;
# Slurp up the contents of the given filename
open my $slurpy, '<', $filename or die "Cannot open $filename: $!";
return do { local $/; <$slurpy> };
}
subcommand 'GET' => sub {
# generic GET
my $resource = shift @ARGV;
# $ua->max_redirect(0);
# if ($resource =~ /\?/)
# {
# $resource .= '&'
# }
# else
# {
# $resource .= '?';
# }
# $resource .= 'xmlformat=1';
my $resp = $ua->request(GET "$protocol$host$resource");
return $resp;
};
subcommand 'DELETE' => sub {
# generic DELETE
my $resource = shift @ARGV;
my $resp = $ua->request(
HTTP::Request->new(DELETE => "$protocol$host$resource")
);
return $resp;
};
subcommand 'POST' => sub {
# generic POST
my $resource = shift @ARGV;
my $type = shift @ARGV;
my $filename = shift @ARGV;
my $data = slurp($filename);
my @headers = ('Content-Type' => $type);
if ($etag) {
push @headers, ('ETag' => $etag);
}
if ($gzip) {
push @headers, 'Content-Encoding' => 'gzip';
$data = Compress::Zlib::memGzip($data);
}
push @headers, 'Content-MD5' => md5_base64($data)."==";
my $resp = $ua->request(POST "$protocol$host$resource", @headers, Content => $data);
return $resp;
};
subcommand 'PUT' => sub {
# generic PUT
my $resource = shift @ARGV;
my $type = shift @ARGV;
my $filename = shift @ARGV;
my $data = slurp($filename);
my @headers = ('Content-Type' => $type);
if ($etag) {
push @headers, ('ETag' => $etag);
}
if ($gzip) {
push @headers, 'Content-Encoding' => 'gzip';
$data = Compress::Zlib::memGzip($data);
}
push @headers, 'Content-MD5' => md5_base64($data)."==";
my $resp = $ua->request(PUT "$protocol$host$resource", @headers, Content => $data);
return $resp;
};
subcommand 'OPTIONS' => sub {
# generic OPTIONS
my $resource = shift @ARGV;
my $resp = $ua->request(
HTTP::Request->new(OPTIONS => "$protocol$host$resource")
);
return $resp;
};
subcommand 'TRACE' => sub {
# generic TRACE
my $resource = shift @ARGV;
my $resp = $ua->request(
HTTP::Request->new(TRACE => "$protocol$host$resource")
);
return $resp;
};
subcommand 'HEAD' => sub {
# generic HEAD
my $resource = shift @ARGV;
my $resp = $ua->request(HEAD "$protocol$host$resource");
return $resp;
};
exit 0;