| 1 | package Hg::Lib::Server; | ||
| 2 | |||
| 3 | use 5.10.1; | ||
| 4 | |||
| 5 | use Carp; | ||
| 6 | |||
| 7 | use Moo; | ||
| 8 | use MooX::Types::MooseLike::Base qw[ :all ]; | ||
| 9 | |||
| 10 | use Hg::Lib::Server::Pipe; | ||
| 11 | |||
| 12 | Hg::Lib::Server::Pipe->shadow_attrs; | ||
| 13 | |||
| 14 | has server => ( | ||
| 15 | is => 'ro', | ||
| 16 | lazy => 1, | ||
| 17 | init_arg => undef, | ||
| 18 | handles => [qw[ get_chunk write ]], | ||
| 19 | default => sub { | ||
| 20 | Hg::Lib::Server::Pipe->new( Hg::Lib::Server::Pipe->xtract_attrs($_[0]) ); | ||
| 21 | }, | ||
| 22 | ); | ||
| 23 | |||
| 24 | has capabilities => ( | ||
| 25 | is => 'rwp', | ||
| 26 | predicate => 1, | ||
| 27 | init_arg => undef, | ||
| 28 | ); | ||
| 29 | |||
| 30 | has encoding => ( | ||
| 31 | is => 'rwp', | ||
| 32 | predicate => 1, | ||
| 33 | init_arg => undef, | ||
| 34 | ); | ||
| 35 | |||
| 36 | |||
| 37 | sub BUILD { | ||
| 38 | |||
| 39 | $_[0]->_get_hello; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub _get_hello { | ||
| 43 | |||
| 44 | my $self = shift; | ||
| 45 | |||
| 46 | my $buf; | ||
| 47 | my $ch = $self->get_chunk($buf); | ||
| 48 | |||
| 49 | croak("corrupt or incomplete hello message from server\n") | ||
| 50 | unless $ch eq 'o' && length $buf; | ||
| 51 | |||
| 52 | for my $item ( split( "\n", $buf ) ) { | ||
| 53 | |||
| 54 | my ( $field, $value ) = $item =~ /([a-z0-9]+):\s*(.*)/; | ||
| 55 | |||
| 56 | if ( $field eq 'capabilities' ) { | ||
| 57 | |||
| 58 | $self->_set_capabilities( | ||
| 59 | { map { $_ => 1 } split( ' ', $value ) } ); | ||
| 60 | } | ||
| 61 | |||
| 62 | elsif ( $field eq 'encoding' ) { | ||
| 63 | |||
| 64 | $self->_set_encoding($value); | ||
| 65 | |||
| 66 | } | ||
| 67 | |||
| 68 | # ignore anything else 'cause we don't know what it means | ||
| 69 | |||
| 70 | } | ||
| 71 | |||
| 72 | # make sure hello message meets minimum standards | ||
| 73 | croak("server did not provide capabilities?\n") | ||
| 74 | unless $self->has_capabilities; | ||
| 75 | |||
| 76 | croak("server is missing runcommand capability\n") | ||
| 77 | unless exists $self->capabilities->{runcommand}; | ||
| 78 | |||
| 79 | croak("server did not provide encoding?\n") | ||
| 80 | unless $self->has_encoding; | ||
| 81 | |||
| 82 | return; | ||
| 83 | } | ||
| 84 | |||
| 85 | 1; |