-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount.restfs
executable file
·479 lines (426 loc) · 11.7 KB
/
mount.restfs
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/usr/bin/perl
## Standard Modules
use strict;
use warnings;
use Getopt::Std;
use File::Basename;
use POSIX qw(:errno_h :fcntl_h ceil);
use Encode;
use List::Util 'first';
use Digest::MD5 'md5_base64';
use Cwd 'abs_path';
use threads;
use threads::shared;
## Required Modules
use Fuse;
use LWP::UserAgent;
use URI;
## Optional Modules
my $grok_css = eval { require CSS };
my $grok_json = eval { require JSON };
my $grok_yaml = eval { require YAML };
my $grok_xpath = eval { require XML::XPath };
my $grok_html = $grok_xpath
&& eval { require HTML::TreeBuilder };
## Setup and Options
my $prog = basename($0);
my %opt;
getopts('hd', \%opt);
usage() if $opt{h} || @ARGV != 1;
sub usage {
die <<EOU;
usage: $prog [-d] mount-point
-d: daemonize; default is to run in foreground with debugging
EOU
}
## Daemonizing
if ($opt{d}) {
close(STDOUT);
close(STDIN);
close(STDERR);
fork and exit;
}
## Global var initialization
my $JSON = JSON->new->allow_nonref(1);
my $ua = LWP::UserAgent->new(agent => 'Firefox'); # TODO: add sexy useragent
my %hcache :shared; # TODO: replace with Tie::Cache::LRU or equiv.
my %ocache; # TODO: replace with Tie::Cache::LRU or equiv.
my $mount = $ARGV[0]; $mount =~ s,/+$,,;
$mount = abs_path($mount) unless $mount =~ /^\//;
die "No such directory $mount\n" unless -d $mount;
## Extensible handler configuration
my %handlers = (
# links handlers take content and return a hash of name => links
# names must (obviously) be unique, and links may be relative strings
links => {
qr{^text/plain$}i => \&find_text_links,
qr{html$}i => \&find_html_links,
qr{/css$}i => \&find_css_links,
},
# tree handlers take content and path parts and return a valid info hashref
# or undef
tree => {
qr{/(javascript|(x-)?json)$}i => $grok_json && \&build_json_tree,
qr{xml$}i => $grok_xpath && \&build_xml_tree,
qr{html$}i => $grok_html && \&build_html_tree,
qr{/(x-)?yaml$}i => $grok_yaml && \&build_yaml_tree,
qr{/css$}i => 0 && $grok_css && \&build_css_tree,
},
);
## Run filesystem
Fuse::main(
mountpoint => $mount,
debug => 0,
threaded => 1,
mountopts => 'allow_other',
'getattr' => 'main::rfs_getattr',
'readlink' => 'main::rfs_readlink',
'getdir' => 'main::rfs_getdir',
'open' => 'main::rfs_open',
'read' => 'main::rfs_read',
);
exit; # never reached?
## Filesystem calbacks
sub rfs_getattr {
my $info = parse_path(@_) || return -ENOENT();
my $mtime = $info->{mtime} || time();
my $contents = file_contents($info);
my $size = defined($contents) ? length($contents) : 0;
my $blksize = 1024 * 1024;
my $blocks = ceil($size / $blksize);
my $type;
if ($info->{dir}) {
$type = 0040;
}
elsif ($info->{link_to}) {
$type = 0120;
}
elsif (defined $contents) {
$type = 0100;
}
else {
warn "unknown reponse from parse_path()\n";
return -ENOENT();
}
my $mode = 0444 + ($type << 9); # world-readable + type of file
(0, 0, $mode, 1, 0, 0, 0, $size, time(), $mtime, $mtime, $blksize, $blocks);
}
sub rfs_readlink {
my $info = parse_path(@_);
$info && $info->{link_to};
}
sub rfs_getdir {
my $info = parse_path(@_) || return -ENOENT();
return -ENOTDIR() unless $info->{dir};
('.', '..', map { encode_name($_) } @{$info->{dir}}, 0);
}
sub rfs_open {
my ($path, $flags) = @_;
my $info = parse_path($path, 1);
# TODO: check flags
return 0 if $info && !$info->{dir};
return -EACCES();
}
sub rfs_read {
my ($path, $bytes, $off) = @_;
my $info = parse_path($path) || return -ENOENT();
my $contents = file_contents($info) || return -ENOENT();
substr($contents, $off, $bytes);
}
## Utility functions
sub file_contents {
my ($info) = @_;
my $file = $info->{file};
return undef unless defined $file;
Encode::is_utf8($file) ? encode('utf-8', $file) : $file;
}
sub parse_path {
my ($path, $open) = @_;
$path =~ s{(?<=[^/])/+$}{}; # trim off trailing /s
# top level dir: show our cached urls shortened (handy)
return { dir => [map { s,^http://,,; $_ } keys %hcache] }
if $path eq '/';
# skip system .file queries
return undef if $path =~ m{^/\.};
# turn /foo/ba,r/baz -> ('foo', 'ba/r', 'baz')
my @parts = map { decode_name($_) } split(/\//, substr($path, 1));
# decode url and normalize
my $url = shift(@parts);
$url = "http://$url" unless $url =~ m{^[a-z]+:};
# next component is optional cmd
my $cmd = shift(@parts) || '';
# perform fetch and set vars
my $res = fetch_cached($url, $open && $cmd eq 'content');
my $mtime = $res->last_modified || time();
my $content = $res->decoded_content;
my $type = $res->header('Content-type'); $type =~ s/\s*;.*//;
if (!$cmd) {
my @kids = qw(headers content url); # shared virtual files/dirs
push(@kids, 'links') if handler_for('links', $type);
push(@kids, 'tree') if handler_for('tree', $type);
return { dir => \@kids, mtime => $mtime };
}
elsif ($cmd eq 'content') {
return { file => $content, mtime => $mtime };
}
elsif ($cmd eq 'url') {
return { file => "$url\n", mtime => $mtime };
}
elsif ($cmd eq 'headers') {
my $which = shift(@parts);
if (defined $which) {
my $hdr = $res->header($which);
return undef unless defined $hdr;
my $info = file_or_link_to($hdr);
$info->{mtime} = $mtime;
return $info;
}
else {
return { dir => [ $res->header_field_names ], mtime => $mtime };
}
}
elsif ($cmd eq 'links') {
my $which = shift(@parts);
my $handler = handler_for('links', $type) || return undef;
my @links = $handler->($content);
# turn ([ foo => bar ], [ foo => baz ], [ quux => garply ])
# into ( foo => bar, foo2 => baz, quux => garply )
my %links;
for (@links) {
my ($name, $link) = @$_;
my ($base, $n);
for ($base = $name, $n = 2; exists $links{$name}; $name = $base.($n++)) {}
$links{$name} = $link;
}
undef @links;
if (defined $which) {
my $lto = link_to($links{$which}, $url) || return undef;
return { link_to => $lto, mtime => $mtime };
}
else {
return {
dir => [ grep { link_to($links{$_}, $url) } keys %links ],
mtime => $mtime
};
}
}
elsif ($cmd eq 'tree') {
my $handler = handler_for('tree', $type) || return undef;
my $info = $handler->($content, $url, @parts) || return undef;
$info->{mtime} = $mtime;
return $info;
}
}
sub decode_name {
my ($str) = @_;
$str =~ s#,#/#g;
$str =~ s/\%2C/,/ig;
$str;
}
sub encode_name {
my ($url) = @_;
$url =~ s/,/\%2C/g;
$url =~ s#/#,#g;
$url;
}
sub fetch_cached {
my ($url, $force) = @_;
my $res;
{
lock(%hcache); # TODO finer-grained locking
unless ($force) {
my $msg = $hcache{$url};
$res = HTTP::Response->parse($msg) if $msg;
}
unless ($res) {
print "GET $url\n" unless $opt{d};
$res = $ua->get($url);
$hcache{$url} = $res->as_string;
}
}
$res;
}
sub link_to {
my ($str, $base) = @_;
$str = URI->new_abs($str, $base) if $base;
return undef unless $str =~ m{^https?://\S+$};
return $mount . '/' . encode_name($str);
}
sub file_or_link_to {
my ($str, $base) = @_;
my $lto = link_to($str, $base);
return { link_to => $lto } if $lto;
return { file => "$str\n" };
}
sub parse_css {
my ($css) = @_;
ocache($css, sub {
my $obj = new CSS;
$obj->read_string($_[0]);
$obj;
});
}
sub parse_xml {
my ($xml) = @_;
ocache($xml, sub { XML::XPath->new(xml => $_[0]) });
}
sub html2xml {
my ($html) = @_;
ocache($html, sub { HTML::TreeBuilder->new_from_content($_[0])->as_XML });
}
sub ocache {
my ($content, $gen) = @_;
$ocache{md5_base64(encode('utf-8', $content))} ||= eval { $gen->($content) };
}
sub handler_for {
my ($thing, $type) = @_;
my $key = (first { $type =~ $_ } keys(%{$handlers{$thing}})) || return undef;
$handlers{$thing}{$key};
}
## Handler implementations
sub find_text_links {
my ($content) = @_;
return map { [ URI->new($_)->host => $_ ] }
sort ($content =~ m{\bhttp://[^\s"'<>]+}g);
}
sub find_html_links {
my ($html) = @_;
my @links;
# good xpathy version
if ($grok_html) {
my $obj = parse_xml(html2xml($html));
@links = map {
my $name = $_->string_value;
$name =~ s/^\s+|\s+$//g;
$name = 'untitled' unless length $name;
[ $name => scalar($_->getAttribute('href')) ];
} eval { $obj->findnodes('//a[@href]') };
}
# failover regexpy version
else {
while ($html =~ /<a [^>]*\bhref=["']?([^"'>]+)[^>]*>([^<]+)/g) {
my ($href, $name) = ($1, $2);
$name =~ s/^\s+|\s+$//g;
push(@links, [ $name => $href ]) if length $name && $href;
}
}
@links;
}
sub build_html_tree {
my ($html, $url, @parts) = @_;
build_xml_tree(html2xml($html), $url, @parts);
}
sub find_css_links {
my ($css) = @_;
return () unless $css =~ /url\(/;
my @links;
if ($grok_css) {
my $obj = parse_css($css);
for my $style (@{$obj->{styles}}) {
for my $prop (@{$style->{properties}}) {
for my $val (@{$prop->{'values'}}) {
$val->{value} =~ /url\((.+)\)/ || next;
my $url = $1;
for my $sel (map { $_->{name} } @{$style->{selectors}}) {
$sel =~ s,/\*.*\*/,,g;
$sel =~ s/^\s+|\s+$//g;
push(@links, [ $sel => $url ]);
}
}
}
}
}
else {
@links = map { [ url => $1 ] } ($css =~ /url\((.+)\)/g);
}
# common cleanup
for (@links) {
$_->[1] =~ s/^(['"])(.+)\1$/$2/;
$_->[1] =~ s/\\\\/\\/g;
$_->[1] =~ s/\\([(),\s'"])/$1/g;
}
@links;
}
sub build_css_tree {
my ($css, $url, @parts) = @_;
}
sub build_json_tree {
my ($json, $url, @parts) = @_;
my $obj = ocache($json, sub { JSON::from_json($_[0]) }) || return undef;
build_obj_tree($obj, $url, sub {
my $str = $JSON->encode($_[0]);
$str =~ s/^"|"$//g;
$str;
}, @parts);
}
sub build_yaml_tree {
my ($yaml, $url, @parts) = @_;
my $obj = ocache($yaml, sub { YAML::Load($_[0]) }) || return undef;
build_obj_tree($obj, $url, undef, @parts);
}
sub build_obj_tree {
my ($obj, $url, $output, @parts) = @_;
$output ||= sub { $_[0] };
for (@parts) {
if (ref($obj) eq 'HASH') {
$obj = $obj->{$_};
}
elsif (ref($obj) eq 'ARRAY') {
$obj = $obj->[$_];
}
else {
return undef;
}
}
if (ref($obj) eq 'HASH') {
return { dir => [ keys %$obj ] };
}
elsif (ref($obj) eq 'ARRAY') {
return { dir => [ 0..$#$obj ] };
}
else {
return file_or_link_to($output->($obj));
}
}
sub build_xml_tree {
my ($xml, $url, @parts) = @_;
my $path = '/' . join('/', @parts);
my $xp = parse_xml($xml) || return undef;
my $ns = eval { $xp->find($path) };
return undef unless $ns && $ns->size;
my $node = $ns->get_node(1);
if ($node->isa('XML::XPath::Node::Element')) {
my (%kids, %count);
for my $kid ($node->getChildNodes()) {
if (my $name = $kid->getName) {
if ($count{$name}++) {
if ($count{$name} == 2) {
$kids{$name.'[1]'} = 1;
delete $kids{$name};
}
$kids{$name.'['.$count{$name}.']'} = 1;
}
else {
$kids{$name} = 1;
}
}
else {
$kids{'text()'} = 1;
}
}
return { dir => [
keys(%kids),
map { '@' . $_->getName } $node->getAttributes()
]};
}
elsif ($node->isa('XML::XPath::Node::Attribute')) {
return file_or_link_to($node->getNodeValue,
($node->getName =~ /^(href|src)$/i) && $url);
}
elsif ($node->isa('XML::XPath::Node::Text')) {
return file_or_link_to($node->getValue);
}
else {
return undef;
}
}