REST.pm untested

filter: Statementstmt 0.0% 0 / 151 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 235 CC 18 SCAR 31.1
CC
18
Cov
0%
get
72.0
run
30.0
new
12.0
CRAP 22.4
1 package MogileFS::REST;
2 use strict;
3 use warnings;
4 use Carp;
5 use HTTP::Status ':constants';
6 use MogileFS::Client;
7 use Plack::Request;
8 use Plack::Response;
9 use Data::Dumper;
10  
11 our $VERSION = '0.04';
12  
13 ## set shortcut methods to log handler
14 for my $lvl (qw/debug info warn error fatal/) {
15 no strict 'refs';
16 *{$lvl} = sub {
17 my $app = shift;
18 my $log = $app->{log};
19 $log->$lvl(@_);
20 };
21 }
22  
23 sub new {
24 my $class = shift;
25 my %opts = @_;
26 unless ($opts{servers}) {
27 croak "servers should be specified.";
28 }
29 my $app = bless \%opts, $class;
30 if (! $app->{log}) {
31 require MogileFS::REST::DumbLogger;
32 $app->{log} = MogileFS::REST::DumbLogger->new;
33 }
34 $app->debug("Config: " . (Dumper { %$app, log => ref $app->{log} } ));
35 return $app;
36 }
37  
38 sub run {
39 my $app = shift;
40 return sub {
41 my $env = shift;
42 my $req = Plack::Request->new($env);
43 my $method = $req->method;
44 my $res;
45 if ($method eq 'GET' or $method eq 'HEAD') {
46 if ($req->path eq '/') {
47 $res = $app->home($req);
48 }
49 else {
50 $res = $app->get($req);
51 }
52 }
53 elsif ($method eq 'DELETE') {
54 $res = $app->delete($req);
55 }
56 elsif ($method eq 'PUT') {
57 $res = $app->put($req);
58 }
59 else {
60 $res = $app->respond_not_found($req);
61 }
62 return $res->finalize;
63 };
64 }
65  
66 sub get_client {
67 my ($app, $domain) = @_;
68 my $client = MogileFS::Client->new(
69 domain => $domain,
70 hosts => $app->{servers},
71 );
72 return $client;
73 }
74  
75 sub home {
76 my ($app, $req) = @_;
77 my $res = $req->new_response(HTTP_OK);
78 $res->content_type('text/plain');
79 $res->body(<<EOA);
80 This is a simple REST API abstraction to MogileFS, so that
81 we can store and retrieve files from mogile, without having to reimplement
82 a MogileFS client in different languages.
83  
84 Files are hosted at:
85 /:domain/:key
86  
87 * you can GET/PUT/DELETE on those resources, please README for more details.
88 * GET /:domain/:key?paths returns the network paths to the storage node for
89 that file. (one per line)
90  
91 EOA
92 return $res;
93 }
94  
95 sub get {
96 my ($app, $req) = @_;
97 my ($domain, $key) = split_path($req->path);
98 $app->debug("Getting: $domain:$key");
99  
100 my $p = $req->query_parameters;
101 if (exists $p->{paths}) {
102 return $app->get_paths($req, $domain, $key);
103 }
104 my $can_reproxy = 0;
105 my $capabilities = $req->header('X-Proxy-Capabilities');
106 if ($capabilities && $capabilities =~ m{\breproxy-file\b}i) {
107 $can_reproxy = 1;
108 }
109 my $client = $app->get_client($domain);
110 my $res = $req->new_response(HTTP_OK);
111  
112 ## so, where is this file?
113 my @paths = $client->get_paths($key, { no_verify => 1 });
114 unless (@paths) {
115 ## nowhere
116 return $req->new_response(HTTP_NOT_FOUND);
117 }
118 if ($can_reproxy) {
119 $res->header('X-Reproxy-URL' => join " ", @paths);
120 ## we can reproxy, so just send headers without any body
121 $app->debug("reproxying to " . $res->header('X-Reproxy-URL'));
122 $res->status(HTTP_NO_CONTENT);
123 return $res;
124 }
125  
126 $res->header('Content-Type' => 'application/octet-stream');
127  
128 if ($req->method eq 'HEAD') {
129 ## deprecated. backward compat only
130 $res->header('X-Reproxy-URL' => join " ", @paths);
131 if ($can_reproxy) {
132 $app->warn("If you want to get paths use GET /d/k?paths instead");
133 }
134 ## end deprecated
135 $app->debug("request is HEAD, returning no content");
136 return $res;
137 }
138 my $handle = $client->read_file($key);
139 return $app->respond_not_found($req) unless $handle;
140 $res->body($handle);
141 return $res;
142 }
143  
144 sub get_paths {
145 my ($app, $req, $domain, $key) = @_;
146 my $res = $req->new_response(HTTP_OK);
147 $res->content_type('text/plain');
148 my $client = $app->get_client($domain);
149 my @paths = $client->get_paths($key, { no_verify => 1 });
150 return $app->respond_not_found($req) unless @paths;
151 my $paths = join "\n", @paths;
152 $res->header('Content-Length', length $paths);
153 if ($req->method ne 'HEAD') {
154 $res->body($paths);
155 }
156 return $res;
157 }
158  
159 sub delete {
160 my ($app, $req) = @_;
161  
162 my ($domain, $key) = split_path($req->path);
163 $app->info("deleting $domain:$key");
164 my $client = $app->get_client($domain);
165 my $rv = $client->delete($key);
166 my $e = $client->errstr;
167 return $app->respond_error($req, "Couldn't delete $domain/$key: $e")
168 unless $rv;
169 my $res = $req->new_response(HTTP_NO_CONTENT);
170 return $res;
171 }
172  
173 sub put {
174 my ($app, $req) = @_;
175  
176 my ($domain, $key) = split_path($req->path);
177 $app->info("creating $domain:$key");
178 my $mogclass = $req->header('X-MogileFS-Class') || $app->{default_class};
179  
180 my $size = $req->content_length;
181 my $opts = { bytes => $size, largefile => $app->{largefile} };
182 my $data_handle = $req->input;
183 my $client = $app->get_client($domain);
184 my $rv = $client->store_file($key, $mogclass, $data_handle, $opts);
185 if (defined $rv) {
186 my $res = $req->new_response(HTTP_CREATED);
187 $res->header( Location => $req->uri );
188 return $res;
189 }
190 else {
191 my $errstr = $client->errstr;
192 my $err = "Can't save key '$domain/$key': $errstr";
193 $app->error($err);
194 return $app->respond_error($req, $err);
195 }
196 }
197  
198 sub respond_not_found {
199 my ($app, $req) = @_;
200 my $res = $req->new_response(HTTP_NOT_FOUND);
201 $res->content_type('text/plain');
202 $res->body('No such file');
203 return $res;
204 }
205  
206 sub respond_error {
207 my ($app, $req, $error) = @_;
208 my $res = $req->new_response(HTTP_INTERNAL_SERVER_ERROR);
209 $res->content_type('text/plain');
210 $res->body($error || "Server Error");
211 return $res;
212 }
213  
214 sub split_path {
215 my $path = shift;
216 $path =~ s{^/+}{};
217 my ($domain, $key) = split m{/}, $path, 2;
218 return ($domain, $key);
219 }
220  
221 1;