| 1 | package Lufs::Http; | ||
| 2 | |||
| 3 | use Net::HTTP; | ||
| 4 | use HTTP::Headers; | ||
| 5 | use HTTP::Response; | ||
| 6 | use HTTP::Date; | ||
| 7 | use URI::WithBase; | ||
| 8 | use Fcntl qw/:DEFAULT :mode/; | ||
| 9 | use HTML::TokeParser::Simple; | ||
| 10 | use strict; | ||
| 11 | |||
| 12 | sub init { | ||
| 13 | my $self = shift; | ||
| 14 | $self->{config} = shift; | ||
| 15 | $self->{config}{uri} ||= 'http://ftp.student.utwente.nl/'; | ||
| 16 | $self->{root} = URI->new($self->{config}{uri}); | ||
| 17 | 1; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub mount { | ||
| 21 | my $self = shift; | ||
| 22 | $self->head->is_success; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub stat { | ||
| 26 | my $self = shift; | ||
| 27 | my $node = shift; | ||
| 28 | $node =~ s{/\.$}{}; | ||
| 29 | my $resp = $self->head($node); | ||
| 30 | if ($resp->code =~ /^[45]/) { | ||
| 31 | return 0; | ||
| 32 | } | ||
| 33 | my $add = $self->_format_statref($node, $resp); | ||
| 34 | |||
| 35 | map {$_[0]->{$_}=$add->{$_}} grep /^f_/, keys %{$add}; | ||
| 36 | 1; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub readlink { | ||
| 40 | my $self = shift; | ||
| 41 | my $node = shift; | ||
| 42 | my $resp = $self->head($node); | ||
| 43 | my $stat = $self->_format_statref($node, $resp); | ||
| 44 | if (( $stat->{f_mode} & S_IFLNK) == S_IFLNK) { | ||
| 45 | my $to = URI->new($resp->header('Location')); | ||
| 46 | my $root = $self->{root}->path; | ||
| 47 | my $path = $to->path; | ||
| 48 | unless ($path =~ s/^\Q$root\E//) { return 0 } | ||
| 49 | $_[0] = $path; | ||
| 50 | return 1; | ||
| 51 | } | ||
| 52 | return 0; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub open { | ||
| 56 | my $self = shift; | ||
| 57 | my $node = shift; | ||
| 58 | my $mode = shift; | ||
| 59 | if (exists $self->{handles}{$node}) { | ||
| 60 | return 0; | ||
| 61 | } | ||
| 62 | if (($mode & O_WRONLY) == O_WRONLY) { | ||
| 63 | return 0; | ||
| 64 | } | ||
| 65 | my ($h, $resp) = $self->get($node); | ||
| 66 | unless ($resp->is_success) { return 0 } | ||
| 67 | $self->{handles}{$node} = $h; | ||
| 68 | 1; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub release { | ||
| 72 | my $self = shift; | ||
| 73 | my $node = shift; | ||
| 74 | delete $self->{handles}{$node}; | ||
| 75 | 1; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub umount { | ||
| 79 | my $self = shift; | ||
| 80 | |||
| 81 | } | ||
| 82 | |||
| 83 | sub read { | ||
| 84 | my $self = shift; | ||
| 85 | my $node = shift; | ||
| 86 | my $offset = shift; | ||
| 87 | my $count = shift; | ||
| 88 | my $len; | ||
| 89 | $_[0] = ''; | ||
| 90 | my $handle; | ||
| 91 | unless ($handle = $self->{handles}{$node}) { | ||
| 92 | return -1; | ||
| 93 | } | ||
| 94 | if ($handle->_rbuf_length) { | ||
| 95 | my $buf = ${*$handle}{http_buf}; | ||
| 96 | $len = length($buf); | ||
| 97 | $_[0] = substr($buf, 0, $len); | ||
| 98 | ${*$handle}{http_buf} = ''; | ||
| 99 | } | ||
| 100 | my $buf; | ||
| 101 | my $total = $len; | ||
| 102 | while ($total < $count) { | ||
| 103 | my $ret = $handle->sysread($buf, $count - $total); | ||
| 104 | $_[0] .= substr($buf, 0, $ret) if $ret; | ||
| 105 | $total += $ret; | ||
| 106 | last if $ret == 0; | ||
| 107 | } | ||
| 108 | $total; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub write { 0 } | ||
| 112 | |||
| 113 | sub readdir { | ||
| 114 | my $self = shift; | ||
| 115 | my $node = shift; | ||
| 116 | my $list = shift; | ||
| 117 | $self->{_pwd} = ''; | ||
| 118 | my ($c, $r) = $self->get($node.'/'); | ||
| 119 | unless ($r->is_success) { return 0 } | ||
| 120 | my $html; | ||
| 121 | my %dir; | ||
| 122 | while (<$c>) { $html .= $_ } | ||
| 123 | my $parser = HTML::TokeParser::Simple->new(\$html) or return 1; | ||
| 124 | while ( my $token = $parser->get_token ) { | ||
| 125 | if ($token->is_start_tag) { | ||
| 126 | my $ref = $token->return_attr->{href} || $token->return_attr->{src}; | ||
| 127 | next unless $ref; | ||
| 128 | my $uri = URI::WithBase->new($ref, $self->{root}.'/')->rel; | ||
| 129 | # print STDERR sprintf "REL: '%s' ABS '%s'\n", $uri, $uri->abs; | ||
| 130 | # we want only links that to stuff in this dir | ||
| 131 | next if $uri->scheme; | ||
| 132 | next unless $uri->can('host'); | ||
| 133 | next if $uri->host; | ||
| 134 | next if length($self->{root}->path) and $uri->path =~ /^\//; | ||
| 135 | my $path = $uri->path; | ||
| 136 | $path =~ s{^\.?/}{}; | ||
| 137 | my @s = split/\//, $path; | ||
| 138 | if (@s > 1) { | ||
| 139 | my $d = $s[0]; | ||
| 140 | my $stat = $self->_statref($d); | ||
| 141 | $stat->{_forcedir} = 1; | ||
| 142 | $dir{$d}++; | ||
| 143 | } | ||
| 144 | else { | ||
| 145 | $dir{$path}++; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | push @$list, keys %dir; | ||
| 150 | $self->{_pwd} = $node; | ||
| 151 | return 1; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub head { | ||
| 155 | my $self = shift; | ||
| 156 | my $path = $self->base_uri($_[0]); | ||
| 157 | my $c = Net::HTTP->new(Host => $self->{root}->host); | ||
| 158 | $c->write_request(HEAD => $path); | ||
| 159 | my($code, $mess, %h) = $c->read_response_headers; | ||
| 160 | $self->TRACE('HEAD', $path, $code, $mess, \%h); | ||
| 161 | HTTP::Response->new($code, $mess, HTTP::Headers->new(%h)); | ||
| 162 | } | ||
| 163 | |||
| 164 | sub get { | ||
| 165 | my $self = shift; | ||
| 166 | my $path = $self->base_uri($_[0]); | ||
| 167 | my $c = Net::HTTP->new(Host => $self->{root}->host); | ||
| 168 | $c->write_request(GET => $path); | ||
| 169 | my($code, $mess, %h) = $c->read_response_headers; | ||
| 170 | $self->TRACE('GET', $path, $code, $mess, \%h); | ||
| 171 | ($c,HTTP::Response->new($code, $mess, HTTP::Headers->new(%h))); | ||
| 172 | } | ||
| 173 | |||
| 174 | sub _format_statref { | ||
| 175 | my $self = shift; | ||
| 176 | my $node = shift; | ||
| 177 | my $resp = shift; | ||
| 178 | my $ref = $self->_statref($node); | ||
| 179 | $ref->{f_ino} = $self->inode($node); | ||
| 180 | if ($self->{_forcedir}) { | ||
| 181 | print STDERR "FORCE DIR voor $node\n"; | ||
| 182 | $ref->{f_mode} |= S_IFDIR; | ||
| 183 | } | ||
| 184 | elsif ($resp->is_redirect) { | ||
| 185 | my $to = URI->new($resp->header('Location'))->path; | ||
| 186 | my $from = $self->base_uri($node); | ||
| 187 | if ($to eq $from.'/') { | ||
| 188 | $ref->{f_mode} |= S_IFDIR; | ||
| 189 | } | ||
| 190 | else { | ||
| 191 | $ref->{f_mode} |= S_IFLNK; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | elsif ((split/\//, $node)[-1] =~ /\.\w+/) { | ||
| 195 | $ref->{f_mode} |= S_IFREG; | ||
| 196 | } | ||
| 197 | else { | ||
| 198 | $ref->{f_mode} |= S_IFREG; | ||
| 199 | #$ref->{f_mode} |= S_IFDIR; | ||
| 200 | } | ||
| 201 | if ($resp->code != 403) { | ||
| 202 | $ref->{f_mode} |= 0755; | ||
| 203 | } | ||
| 204 | $ref->{f_size} = $resp->header('Content-Length') || ~1; | ||
| 205 | if ($resp->header('Date')) { | ||
| 206 | $ref->{f_mtime} = str2time($resp->header('Date')); | ||
| 207 | } | ||
| 208 | if ($node eq '/' || $node eq '') { | ||
| 209 | $ref->{f_mode} = S_IFDIR; | ||
| 210 | } | ||
| 211 | if (($ref->{f_mode} & S_IFDIR) == S_IFDIR) { | ||
| 212 | $ref->{f_mode} |= 0755; | ||
| 213 | $ref->{f_size} = 4096; | ||
| 214 | } | ||
| 215 | $ref; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub base_uri { | ||
| 219 | my $self = shift; | ||
| 220 | my $node = shift; | ||
| 221 | if ($node !~ /^\//) { $node = "$self->{_pwd}/$node" } | ||
| 222 | my $u = $self->{root}->path. '/' . $node; | ||
| 223 | $u =~ s{/+}{/}g; | ||
| 224 | $u; | ||
| 225 | } | ||
| 226 | |||
| 227 | sub inode { | ||
| 228 | my $self = shift; | ||
| 229 | my $node = $self->base_uri($_[0]); | ||
| 230 | if (exists $self->{_inodes}{$node}) { | ||
| 231 | return $self->{_inodes}{$node}; | ||
| 232 | } | ||
| 233 | else { | ||
| 234 | return $self->{_inodes}{$node} = ++$self->{_maxino}; | ||
| 235 | } | ||
| 236 | } | ||
| 237 | |||
| 238 | sub _statref { | ||
| 239 | my $self = shift; | ||
| 240 | my $ino = $self->inode($_[0]); | ||
| 241 | $self->{_stat}{$ino} ||= { f_ino => $ino}; | ||
| 242 | } | ||
| 243 | |||
| 244 | 1; | ||
| 245 |