Skip to content

Commit 5bb5ada

Browse files
committed
wip
1 parent de5769a commit 5bb5ada

18 files changed

+62
-478
lines changed

parsers/go/parser.go

+19-85
Original file line numberDiff line numberDiff line change
@@ -7,111 +7,45 @@ import (
77
)
88

99
type Request struct {
10-
proto string
11-
version string
12-
command string
13-
compression string
14-
responseCompression []string
15-
headOnlyIndicator bool
16-
headers []byte
17-
body []byte
18-
}
19-
20-
type Response struct {
21-
responseStatus string
22-
compression string
23-
headers []byte
24-
body []byte
25-
hasBody bool
10+
proto string
11+
version string
12+
command string
13+
headers []byte
14+
body []byte
2615
}
2716

2817
func reqParse(req string) Request {
2918
res := strings.Split(req, "\n")
3019

3120
reqLine := res[0]
32-
payload := strings.Join(res[1:],"\n")
21+
payload := strings.Join(res[1:], "\n")
3322
r := strings.Split(reqLine, " ")
3423
if len(r) < 8 {
3524
r = append(r, " ")
3625
}
3726
headersLen, _ := strconv.Atoi(r[5])
3827
bodyLen, _ := strconv.Atoi(r[6])
3928
headers := payload[0:headersLen]
40-
body := payload[headersLen:headersLen+bodyLen]
29+
body := payload[headersLen : headersLen+bodyLen]
4130

4231
request := Request{
43-
proto: r[0],
44-
version: r[1],
45-
command: r[2],
46-
compression: r[3],
47-
responseCompression: strings.Split(r[4], ","),
48-
headOnlyIndicator: r[7] == "H",
49-
headers: []byte(headers),
50-
body: []byte(body),
32+
proto: r[0],
33+
version: r[1],
34+
command: r[2],
35+
headers: []byte(headers),
36+
body: []byte(body),
5137
}
5238
return request
5339
}
5440

5541
func reqMarshal(req Request) string {
56-
resCompression := strings.Join(req.responseCompression, ",")
57-
var headOnlyIndicator string
58-
if req.headOnlyIndicator {
59-
headOnlyIndicator = " H"
60-
} else {
61-
headOnlyIndicator = ""
62-
}
63-
requestLine := fmt.Sprintf("%s %s %s %s %s %d %d%s",
64-
req.proto,
65-
req.version,
66-
req.command,
67-
req.compression,
68-
resCompression,
42+
requestLine := fmt.Sprintf("%s %s %s %d %d",
43+
req.proto,
44+
req.version,
45+
req.command,
6946
len(req.headers),
70-
len(req.body),
71-
headOnlyIndicator)
72-
73-
r := fmt.Sprintf("%s\n%s%s",requestLine,string(req.headers), string(req.body))
74-
return r
75-
}
76-
77-
func resParse(res string) Response {
78-
hasBody := false
79-
tmp := strings.Split(res,"\n")
47+
len(req.body))
8048

81-
resLine := tmp[0]
82-
resBody := strings.Join(tmp[1:],"\n")
83-
responseLineArr := strings.Split(resLine, " ")
84-
headerLen,err := strconv.Atoi(responseLineArr[2])
85-
if err != nil{
86-
panic(err)
87-
}
88-
header := resBody[0:headerLen]
89-
body := ""
90-
if len(responseLineArr) == 4 {
91-
hasBody = true
92-
bodyLen,err := strconv.Atoi(responseLineArr[3])
93-
if err != nil{
94-
panic(err)
95-
}
96-
body = resBody[headerLen:(headerLen+bodyLen)]
97-
}
98-
response := Response{
99-
responseStatus: responseLineArr[0],
100-
compression: responseLineArr[1],
101-
headers: []byte(header),
102-
body: []byte(body),
103-
hasBody: hasBody,
104-
}
105-
return response
106-
}
107-
108-
func resMarshal(res Response) string {
109-
110-
out := fmt.Sprintf("%s %s %d", res.responseStatus, res.compression, len(res.headers))
111-
112-
if res.hasBody {
113-
out += fmt.Sprintf(" %d",len(res.body));
114-
}
115-
out += fmt.Sprintf("\n%s%s",string(res.headers),string(res.body))
116-
return out
49+
r := fmt.Sprintf("%s\n%s%s", requestLine, string(req.headers), string(req.body))
50+
return r
11751
}

parsers/java/Parser.java

+5-61
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,13 @@
1313
*/
1414
public final class Parser {
1515

16-
public static final class Response {
17-
18-
private final int code;
19-
private final String compression;
20-
private final String headers;
21-
private final String body;
22-
23-
Response(int code, String compression, String headers, String body) {
24-
this.code = code;
25-
this.compression = compression;
26-
this.headers = headers;
27-
this.body = body;
28-
}
29-
30-
public String toString() {
31-
return code + " " + compression + " " + headers.length() + " " + body.length() + "\n" + headers + body;
32-
}
33-
}
34-
3516
public static final class Request {
3617

3718
private final String protocol;
3819
private final String version;
3920
private final String command;
4021
private final String compression;
41-
private final String[] responseCompression;
22+
private final String encoding;
4223
private final boolean headOnlyIndicator;
4324
private final String headers;
4425
private final String body;
@@ -48,15 +29,15 @@ public static final class Request {
4829
String version,
4930
String command,
5031
String compression,
51-
String[] responseCompression,
32+
String encoding,
5233
boolean headOnlyIndicator,
5334
String headers,
5435
String body) {
5536
this.protocol = protocol;
5637
this.command = command;
5738
this.version = version;
5839
this.compression = compression;
59-
this.responseCompression = responseCompression;
40+
this.encoding = encoding;
6041
this.headOnlyIndicator = headOnlyIndicator;
6142
this.headers = headers;
6243
this.body = body;
@@ -67,8 +48,7 @@ public String toString() {
6748
builder.append(protocol + " " +
6849
version + " " +
6950
command + " " +
70-
compression + " " +
71-
String.join(",", responseCompression));
51+
compression + " " + encoding);
7252
if (headers == null) {
7353
builder.append(" 0");
7454
} else {
@@ -130,45 +110,12 @@ public static final Request parseRequest(String str) {
130110
requestArguments[1],
131111
requestArguments[2],
132112
requestArguments[3],
133-
requestArguments[4].split(","),
113+
requestArguments[4],
134114
headOnlyIndicator,
135115
headers,
136116
body);
137117
}
138118

139-
/**
140-
* Parses a string into a EWP response.
141-
*
142-
* @param str the string to parse
143-
* @return the response
144-
* @throws IllegalArgumentException if the string doesn't match the EWP spec.
145-
*/
146-
public static Response parseResponse(String str) {
147-
int newline = str.indexOf('\n');
148-
if (newline == -1) {
149-
throw new IllegalArgumentException("No new line found");
150-
}
151-
String respLine = str.substring(0, newline);
152-
String[] segments = respLine.split(" ");
153-
int responseStatus = Integer.parseInt(segments[0]);
154-
String compression = segments[1];
155-
int headerLength = 0;
156-
int bodyLength = 0;
157-
try {
158-
headerLength = Integer.parseInt(segments[2]);
159-
bodyLength = Integer.parseInt(segments[3]);
160-
} catch (NumberFormatException e) {
161-
throw new IllegalArgumentException(e);
162-
}
163-
if (str.length() < newline + 1 + headerLength + bodyLength) {
164-
throw new IllegalArgumentException("Invalid length encoding");
165-
}
166-
String headers = str.substring(newline + 1, newline + 1 + headerLength);
167-
String body = str.substring(newline + 1 + headerLength, newline + 1 + headerLength + bodyLength);
168-
return new Response(responseStatus, compression, headers, body);
169-
170-
}
171-
172119
public static void main(String[] args) throws IOException {
173120
String reqres = args[0];
174121
int length = Integer.parseInt(args[1]);
@@ -178,9 +125,6 @@ public static void main(String[] args) throws IOException {
178125
if ("request".equals(reqres)) {
179126
Request msg = parseRequest(toRead);
180127
System.out.print(msg.toString());
181-
} else if ("response".equals(reqres)) {
182-
Response msg = parseResponse(toRead);
183-
System.out.print(msg.toString());
184128
} else {
185129
throw new IllegalArgumentException("invalid request response given " + reqres);
186130
}

parsers/js/parser.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
const example_ping_req = "EWP 0.1 PING none none 0 5\n12345";
1+
const example_ping_req = "EWP 0.2 PING none none 0 5\n12345";
22

33
// req is a string
44
function parse(req) {
55
let [ reqLine, payload ] = req.split('\n')
66
let r = reqLine.split(' ')
7-
let headersLen = parseInt(r[5])
8-
let bodyLen = parseInt(r[6])
7+
let headersLen = parseInt(r[3])
8+
let bodyLen = parseInt(r[4])
99
let headers = payload.slice(0, headersLen)
1010
let body = payload.slice(headersLen, bodyLen)
1111

1212
return {
1313
proto: r[0],
1414
version: r[1],
1515
command: r[2],
16-
compression: r[3],
17-
responseCompression: r[4].split(','),
18-
headOnlyIndicator: r[7] === 'H',
1916
headers,
2017
body
2118
}

parsers/perl/Hobbit.pm

+1-41
Original file line numberDiff line numberDiff line change
@@ -21,60 +21,24 @@ sub parse_request {
2121
proto => $r[0],
2222
version => $r[1],
2323
command => $r[2],
24-
compression => $r[3],
25-
response_compression => [split(',', $r[4])],
26-
head_only_indicator => (defined $r[7] ? $r[7] eq 'H' : 0),
2724
headers => $headers,
2825
body => $body,
2926
}
3027
}
3128

32-
sub parse_response {
33-
my ($self, $res) = @_;
34-
my ($res_line, $payload) = ($res =~ /(.*?)\n(.*)/s);
35-
my @r = split(' ', $res_line);
36-
my $headers_len = int $r[2];
37-
my $body_len = int $r[3];
38-
my $headers = substr $payload, 0, $headers_len;
39-
my $body = substr $payload, $headers_len, $body_len;
40-
return {
41-
code => int $r[0],
42-
compression => $r[1],
43-
headers => $headers,
44-
body => $body
45-
}
46-
}
47-
4829
sub marshal_request {
4930
my ($self, $req) = @_;
50-
my $res_compression = join(',', @{ $req->{response_compression} });
51-
my $head_only_indicator = $req->{head_only_indicator} ? ' H' : '';
5231
my $request_line = sprintf(
5332
"%s %s %s %s %s %d %d%s",
5433
$req->{proto},
5534
$req->{version},
5635
$req->{command},
57-
$req->{compression},
58-
$res_compression,
5936
length($req->{headers}),
60-
length($req->{body}),
61-
$head_only_indicator);
37+
length($req->{body}));
6238
my $r = sprintf("%s\n%s%s", $request_line, $req->{headers}, $req->{body});
6339
return $r;
6440
}
6541

66-
sub marshal_response {
67-
my ($self, $res) = @_;
68-
my $out = sprintf('%s %s %d', $res->{code}, $res->{compression}, length($res->{headers}));
69-
if ($res->{body}) {
70-
$out .= sprintf(" %d", length($res->{body}));
71-
} else {
72-
$out .= " 0";
73-
}
74-
$out .= sprintf("\n%s%s", $res->{headers}, $res->{body});
75-
return $out;
76-
}
77-
7842
1;
7943

8044
__END__
@@ -99,12 +63,8 @@ a lightweight, multiclient wire protocol for ETH2.0 communications.
9963
10064
=head2 parse_request($request_string)
10165
102-
=head2 parse_response($response_string)
103-
10466
=head2 marshal_request($request_hashref)
10567
106-
=head2 marshal_response($response_hashref)
107-
10868
=head1 SEE ALSO
10969
11070
=over 4

parsers/perl/parser.t

+2-10
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,10 @@ require_ok('./Hobbit.pm');
99
my $hobbit = Hobbit->new();
1010
ok($hobbit->isa('Hobbit'), "Hobbit->new() returned a Hobbit parser instance");
1111

12-
my $request = $hobbit->parse_request("EWP 0.1 PING none none 0 5\n12345");
12+
my $request = $hobbit->parse_request("EWP 0.2 PING 0 5\n12345");
1313
is($request->{proto}, 'EWP', "proto should be 'EWP'");
14-
is($request->{version}, '0.1', "version should be '0.1'");
14+
is($request->{version}, '0.2', "version should be '0.2'");
1515
is($request->{command}, 'PING', "command should be 'PING'");
16-
is($request->{compression}, 'none', "compression should be 'none'");
17-
ok(!$request->{head_only_indicator}, "head_only_indicator should be false");
1816
is($request->{headers}, '', "headers should be an empty string");
1917
is($request->{body}, '12345', "body should be '12345'");
2018

21-
my $response = $hobbit->parse_response("200 none 4 4\na=16body");
22-
is($response->{code}, 200, "code should be 200");
23-
is($response->{compression}, 'none', "compression should be 'none'");
24-
is($response->{headers}, "a=16", "headers should be 'a=16'");
25-
is($response->{body}, "body", "body should be 'body'");
26-

parsers/perl/test.pl

-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
if ($reqres eq 'request') {
1212
my $request = do { local $/; <STDIN> };
1313
print $hobbit->marshal_request($hobbit->parse_request($request))
14-
} elsif ($reqres eq 'response') {
15-
my $response = do { local $/; <STDIN> };
16-
print $hobbit->marshal_response($hobbit->parse_response($response))
1714
} else {
1815
print "invalid request response given\n";
1916
}

0 commit comments

Comments
 (0)