-
Notifications
You must be signed in to change notification settings - Fork 17
/
Hobbit.pm
80 lines (58 loc) · 1.59 KB
/
Hobbit.pm
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
package Hobbit;
use strict;
use warnings;
sub new {
my ($class, $opts) = @_;
$opts ||= {};
my $self = { %$opts };
bless($self, $class);
}
sub parse_request {
my ($self, $req) = @_;
my ($req_line, $payload) = ($req =~ /(.*?)\n(.*)/s);
my @r = split(' ', $req_line);
my $headers_len = int $r[3];
my $body_len = int $r[4];
my $headers = substr $payload, 0, $headers_len;
my $body = substr $payload, $headers_len, $body_len;
return {
proto => $r[0],
version => $r[1],
command => $r[2],
headers => $headers,
body => $body,
}
}
sub marshal_request {
my ($self, $req) = @_;
my $request_line = sprintf(
"%s %s %s %d %d%s",
$req->{proto},
$req->{version},
$req->{command},
length($req->{headers}),
length($req->{body}));
my $r = sprintf("%s\n%s%s", $request_line, $req->{headers}, $req->{body});
return $r;
}
1;
__END__
=head1 NAME
Hobbit - a parser for the Hobbit protocol
=head1 SYNOPSIS
my $hobbit = Hobbit->new();
my $request = $hobbit->parse_request("EWP 0.1 PING none none 0 5\n12345");
=head1 DESCRIPTION
This is a Perl implementation of the Hobbit protocol which is
a lightweight, multiclient wire protocol for ETH2.0 communications.
=head1 API
=head2 new(%opts)
=head2 parse_request($request_string)
=head2 marshal_request($request_hashref)
=head1 SEE ALSO
=over 4
=item https://github.com/Whiteblock/hobbits
=back
=head1 AUTHOR
John Beppu (john.beppu@gmail.com)
=cut