… | |
… | |
72 | |
72 | |
73 | package Net::FCP; |
73 | package Net::FCP; |
74 | |
74 | |
75 | use Carp; |
75 | use Carp; |
76 | |
76 | |
77 | $VERSION = 0.08; |
77 | $VERSION = 0.5; |
78 | |
78 | |
79 | no warnings; |
79 | no warnings; |
80 | |
80 | |
81 | our $EVENT = Net::FCP::Event::Auto::; |
81 | our $EVENT = Net::FCP::Event::Auto::; |
82 | |
82 | |
… | |
… | |
107 | |
107 | |
108 | =item $meta = Net::FCP::parse_metadata $string |
108 | =item $meta = Net::FCP::parse_metadata $string |
109 | |
109 | |
110 | Parse a metadata string and return it. |
110 | Parse a metadata string and return it. |
111 | |
111 | |
112 | The metadata will be a hashref with key C<version> (containing |
112 | The metadata will be a hashref with key C<version> (containing the |
113 | the mandatory version header entries). |
113 | mandatory version header entries) and key C<raw> containing the original |
|
|
114 | metadata string. |
114 | |
115 | |
115 | All other headers are represented by arrayrefs (they can be repeated). |
116 | All other headers are represented by arrayrefs (they can be repeated). |
116 | |
117 | |
117 | Since this is confusing, here is a rather verbose example of a parsed |
118 | Since this description is confusing, here is a rather verbose example of a |
118 | manifest: |
119 | parsed manifest: |
119 | |
120 | |
120 | ( |
121 | ( |
|
|
122 | raw => "Version...", |
121 | version => { revision => 1 }, |
123 | version => { revision => 1 }, |
122 | document => [ |
124 | document => [ |
123 | { |
125 | { |
124 | info => { format" => "image/jpeg" }, |
126 | info => { format" => "image/jpeg" }, |
125 | name => "background.jpg", |
127 | name => "background.jpg", |
… | |
… | |
138 | ) |
140 | ) |
139 | |
141 | |
140 | =cut |
142 | =cut |
141 | |
143 | |
142 | sub parse_metadata { |
144 | sub parse_metadata { |
143 | my $meta; |
|
|
144 | |
|
|
145 | my $data = shift; |
145 | my $data = shift; |
|
|
146 | my $meta = { raw => $data }; |
|
|
147 | |
146 | if ($data =~ /^Version\015?\012/gc) { |
148 | if ($data =~ /^Version\015?\012/gc) { |
147 | my $hdr = $meta->{version} = {}; |
149 | my $hdr = $meta->{version} = {}; |
148 | |
150 | |
149 | for (;;) { |
151 | for (;;) { |
150 | while ($data =~ /\G([^=\015\012]+)=([^\015\012]*)\015?\012/gc) { |
152 | while ($data =~ /\G([^=\015\012]+)=([^\015\012]*)\015?\012/gc) { |
… | |
… | |
312 | my ($self) = @_; |
314 | my ($self) = @_; |
313 | |
315 | |
314 | $self->txn ("client_info"); |
316 | $self->txn ("client_info"); |
315 | }); |
317 | }); |
316 | |
318 | |
317 | =item $txn = $fcp->txn_generate_chk ($metadata, $data) |
319 | =item $txn = $fcp->txn_generate_chk ($metadata, $data[, $cipher]) |
318 | |
320 | |
319 | =item $uri = $fcp->generate_chk ($metadata, $data) |
321 | =item $uri = $fcp->generate_chk ($metadata, $data[, $cipher]) |
320 | |
322 | |
321 | Creates a new CHK, given the metadata and data. UNTESTED. |
323 | Calculcates a CHK, given the metadata and data. C<$cipher> is either |
|
|
324 | C<Rijndael> or C<Twofish>, with the latter being the default. |
322 | |
325 | |
323 | =cut |
326 | =cut |
324 | |
327 | |
325 | $txn->(generate_chk => sub { |
328 | $txn->(generate_chk => sub { |
326 | my ($self, $metadata, $data) = @_; |
329 | my ($self, $metadata, $data, $cipher) = @_; |
327 | |
330 | |
328 | $self->txn (generate_chk => data => "$metadata$data", metadata_length => length $metadata); |
331 | $self->txn (generate_chk => |
|
|
332 | data => "$metadata$data", |
|
|
333 | metadata_length => length $metadata, |
|
|
334 | cipher => $cipher || "Twofish"); |
329 | }); |
335 | }); |
330 | |
336 | |
331 | =item $txn = $fcp->txn_generate_svk_pair |
337 | =item $txn = $fcp->txn_generate_svk_pair |
332 | |
338 | |
333 | =item ($public, $private) = @{ $fcp->generate_svk_pair } |
339 | =item ($public, $private) = @{ $fcp->generate_svk_pair } |
… | |
… | |
477 | while (my ($k, $v) = each %{$self->{attr}}) { |
483 | while (my ($k, $v) = each %{$self->{attr}}) { |
478 | $attr .= (Net::FCP::touc $k) . "=$v\012" |
484 | $attr .= (Net::FCP::touc $k) . "=$v\012" |
479 | } |
485 | } |
480 | |
486 | |
481 | if (defined $data) { |
487 | if (defined $data) { |
482 | $attr .= "DataLength=" . (length $data) . "\012"; |
488 | $attr .= sprintf "DataLength=%x\012", length $data; |
483 | $data = "Data\012$data"; |
489 | $data = "Data\012$data"; |
484 | } else { |
490 | } else { |
485 | $data = "EndMessage\012"; |
491 | $data = "EndMessage\012"; |
486 | } |
492 | } |
487 | |
493 | |
… | |
… | |
494 | and !$!{EINPROGRESS} |
500 | and !$!{EINPROGRESS} |
495 | and Carp::croak "FCP::txn: unable to connect to $self->{fcp}{host}:$self->{fcp}{port}: $!\n"; |
501 | and Carp::croak "FCP::txn: unable to connect to $self->{fcp}{host}:$self->{fcp}{port}: $!\n"; |
496 | |
502 | |
497 | $self->{sbuf} = |
503 | $self->{sbuf} = |
498 | "\x00\x00\x00\x02" |
504 | "\x00\x00\x00\x02" |
499 | . Net::FCP::touc $self->{type} |
505 | . (Net::FCP::touc $self->{type}) |
500 | . "\012$attr$data"; |
506 | . "\012$attr$data"; |
501 | |
507 | |
502 | #$fh->shutdown (1); # freenet buggy?, well, it's java... |
508 | #shutdown $fh, 1; # freenet buggy?, well, it's java... |
503 | |
509 | |
504 | $self->{fh} = $fh; |
510 | $self->{fh} = $fh; |
505 | |
511 | |
506 | $self->{w} = $EVENT->new_from_fh ($fh)->cb(sub { $self->fh_ready_w })->poll(0, 1, 1); |
512 | $self->{w} = $EVENT->new_from_fh ($fh)->cb(sub { $self->fh_ready_w })->poll(0, 1, 1); |
507 | |
513 | |
… | |
… | |
715 | use base Net::FCP::Txn; |
721 | use base Net::FCP::Txn; |
716 | |
722 | |
717 | sub rcv_success { |
723 | sub rcv_success { |
718 | my ($self, $attr) = @_; |
724 | my ($self, $attr) = @_; |
719 | |
725 | |
720 | $self->set_result ($attr); |
726 | $self->set_result ($attr->{uri}); |
721 | } |
727 | } |
722 | |
728 | |
723 | package Net::FCP::Txn::GenerateSVKPair; |
729 | package Net::FCP::Txn::GenerateSVKPair; |
724 | |
730 | |
725 | use base Net::FCP::Txn; |
731 | use base Net::FCP::Txn; |
… | |
… | |
782 | if ($self->{datalength} == length $self->{data}) { |
788 | if ($self->{datalength} == length $self->{data}) { |
783 | my $data = delete $self->{data}; |
789 | my $data = delete $self->{data}; |
784 | my $meta = Net::FCP::parse_metadata substr $data, 0, $self->{metalength}, ""; |
790 | my $meta = Net::FCP::parse_metadata substr $data, 0, $self->{metalength}, ""; |
785 | |
791 | |
786 | $self->set_result ([$meta, $data]); |
792 | $self->set_result ([$meta, $data]); |
|
|
793 | $self->eof; |
787 | } |
794 | } |
788 | } |
795 | } |
789 | |
796 | |
790 | sub rcv_data_found { |
797 | sub rcv_data_found { |
791 | my ($self, $attr, $type) = @_; |
798 | my ($self, $attr, $type) = @_; |
… | |
… | |
829 | |
836 | |
830 | package Net::FCP::Exception; |
837 | package Net::FCP::Exception; |
831 | |
838 | |
832 | use overload |
839 | use overload |
833 | '""' => sub { |
840 | '""' => sub { |
834 | "Net::FCP::Exception<<$_[0][0]," . (join ":", %{$_[0][1]}) . ">>\n"; |
841 | "Net::FCP::Exception<<$_[0][0]," . (join ":", %{$_[0][1]}) . ">>"; |
835 | }; |
842 | }; |
836 | |
843 | |
837 | =item $exc = new Net::FCP::Exception $type, \%attr |
844 | =item $exc = new Net::FCP::Exception $type, \%attr |
838 | |
845 | |
839 | Create a new exception object of the given type (a string like |
846 | Create a new exception object of the given type (a string like |