Packet.pm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package Git::Packet;
  2. use 5.008;
  3. use strict;
  4. use warnings;
  5. BEGIN {
  6. require Exporter;
  7. if ($] < 5.008003) {
  8. *import = \&Exporter::import;
  9. } else {
  10. # Exporter 5.57 which supports this invocation was
  11. # released with perl 5.8.3
  12. Exporter->import('import');
  13. }
  14. }
  15. our @EXPORT = qw(
  16. packet_compare_lists
  17. packet_bin_read
  18. packet_txt_read
  19. packet_key_val_read
  20. packet_bin_write
  21. packet_txt_write
  22. packet_flush
  23. packet_initialize
  24. packet_read_capabilities
  25. packet_read_and_check_capabilities
  26. packet_check_and_write_capabilities
  27. );
  28. our @EXPORT_OK = @EXPORT;
  29. sub packet_compare_lists {
  30. my ($expect, @result) = @_;
  31. my $ix;
  32. if (scalar @$expect != scalar @result) {
  33. return undef;
  34. }
  35. for ($ix = 0; $ix < $#result; $ix++) {
  36. if ($expect->[$ix] ne $result[$ix]) {
  37. return undef;
  38. }
  39. }
  40. return 1;
  41. }
  42. sub packet_bin_read {
  43. my $buffer;
  44. my $bytes_read = read STDIN, $buffer, 4;
  45. if ( $bytes_read == 0 ) {
  46. # EOF - Git stopped talking to us!
  47. return ( -1, "" );
  48. } elsif ( $bytes_read != 4 ) {
  49. die "invalid packet: '$buffer'";
  50. }
  51. my $pkt_size = hex($buffer);
  52. if ( $pkt_size == 0 ) {
  53. return ( 1, "" );
  54. } elsif ( $pkt_size > 4 ) {
  55. my $content_size = $pkt_size - 4;
  56. $bytes_read = read STDIN, $buffer, $content_size;
  57. if ( $bytes_read != $content_size ) {
  58. die "invalid packet ($content_size bytes expected; $bytes_read bytes read)";
  59. }
  60. return ( 0, $buffer );
  61. } else {
  62. die "invalid packet size: $pkt_size";
  63. }
  64. }
  65. sub remove_final_lf_or_die {
  66. my $buf = shift;
  67. if ( $buf =~ s/\n$// ) {
  68. return $buf;
  69. }
  70. die "A non-binary line MUST be terminated by an LF.\n"
  71. . "Received: '$buf'";
  72. }
  73. sub packet_txt_read {
  74. my ( $res, $buf ) = packet_bin_read();
  75. if ( $res != -1 and $buf ne '' ) {
  76. $buf = remove_final_lf_or_die($buf);
  77. }
  78. return ( $res, $buf );
  79. }
  80. # Read a text packet, expecting that it is in the form "key=value" for
  81. # the given $key. An EOF does not trigger any error and is reported
  82. # back to the caller (like packet_txt_read() does). Die if the "key"
  83. # part of "key=value" does not match the given $key, or the value part
  84. # is empty.
  85. sub packet_key_val_read {
  86. my ( $key ) = @_;
  87. my ( $res, $buf ) = packet_txt_read();
  88. if ( $res == -1 or ( $buf =~ s/^$key=// and $buf ne '' ) ) {
  89. return ( $res, $buf );
  90. }
  91. die "bad $key: '$buf'";
  92. }
  93. sub packet_bin_write {
  94. my $buf = shift;
  95. print STDOUT sprintf( "%04x", length($buf) + 4 );
  96. print STDOUT $buf;
  97. STDOUT->flush();
  98. }
  99. sub packet_txt_write {
  100. packet_bin_write( $_[0] . "\n" );
  101. }
  102. sub packet_flush {
  103. print STDOUT sprintf( "%04x", 0 );
  104. STDOUT->flush();
  105. }
  106. sub packet_initialize {
  107. my ($name, $version) = @_;
  108. packet_compare_lists([0, $name . "-client"], packet_txt_read()) ||
  109. die "bad initialize";
  110. packet_compare_lists([0, "version=" . $version], packet_txt_read()) ||
  111. die "bad version";
  112. packet_compare_lists([1, ""], packet_bin_read()) ||
  113. die "bad version end";
  114. packet_txt_write( $name . "-server" );
  115. packet_txt_write( "version=" . $version );
  116. packet_flush();
  117. }
  118. sub packet_read_capabilities {
  119. my @cap;
  120. while (1) {
  121. my ( $res, $buf ) = packet_bin_read();
  122. if ( $res == -1 ) {
  123. die "unexpected EOF when reading capabilities";
  124. }
  125. return ( $res, @cap ) if ( $res != 0 );
  126. $buf = remove_final_lf_or_die($buf);
  127. unless ( $buf =~ s/capability=// ) {
  128. die "bad capability buf: '$buf'";
  129. }
  130. push @cap, $buf;
  131. }
  132. }
  133. # Read remote capabilities and check them against capabilities we require
  134. sub packet_read_and_check_capabilities {
  135. my @required_caps = @_;
  136. my ($res, @remote_caps) = packet_read_capabilities();
  137. my %remote_caps = map { $_ => 1 } @remote_caps;
  138. foreach (@required_caps) {
  139. unless (exists($remote_caps{$_})) {
  140. die "required '$_' capability not available from remote" ;
  141. }
  142. }
  143. return %remote_caps;
  144. }
  145. # Check our capabilities we want to advertise against the remote ones
  146. # and then advertise our capabilities
  147. sub packet_check_and_write_capabilities {
  148. my ($remote_caps, @our_caps) = @_;
  149. foreach (@our_caps) {
  150. unless (exists($remote_caps->{$_})) {
  151. die "our capability '$_' is not available from remote"
  152. }
  153. packet_txt_write( "capability=" . $_ );
  154. }
  155. packet_flush();
  156. }
  157. 1;