forked from whiteblock/hobbits
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathParser.java
112 lines (104 loc) · 3.6 KB
/
Parser.java
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//import java.net.http.HttpResponse.BodySubscriber;
import java.nio.charset.StandardCharsets;
/**
* Copyright Whiteblock, Inc. and others 2019
*
* Licensed under MIT license.
*/
import java.util.Arrays;
import java.io.IOException;
/**
* Example parser in Java for EWP.
*/
public final class Parser {
public static final class Request {
private final String protocol;
private final String version;
private final String command;
private final String headers;
private final String body;
Request(String protocol,
String version,
String command,
String headers,
String body) {
this.protocol = protocol;
this.command = command;
this.version = version;
this.headers = headers;
this.body = body;
}
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(protocol + " " +
version + " " +
command);
if (headers == null) {
builder.append(" 0");
} else {
builder.append(" ").append(headers.length());
}
if (body == null) {
builder.append(" 0");
} else {
builder.append(" ").append(body.length());
}
builder.append("\n");
if (headers != null) {
builder.append(headers);
}
if (body != null) {
builder.append(body);
}
return builder.toString();
}
}
/**
* Parses a string into a EWP request
* @param str the string to parse
* @return the request
* @throws IllegalArgumentException if the string doesn't match the EWP spec.
*/
public static final Request parseRequest(String str) {
int newline = str.indexOf('\n');
if (newline == -1) {
throw new IllegalArgumentException("No new line found");
}
String reqLine = str.substring(0, newline);
String[] requestArguments = reqLine.split(" ");
if (requestArguments.length < 5) {
throw new IllegalArgumentException("Not enough elements in request line");
}
int headerLength = 0;
int bodyLength = 0;
try {
headerLength = Integer.parseInt(requestArguments[3]);
bodyLength = Integer.parseInt(requestArguments[4]);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(e);
}
if (str.length() < newline + 1 + headerLength + bodyLength) {
throw new IllegalArgumentException("Invalid length encoding");
}
String headers = str.substring(newline + 1, newline + 1 + headerLength);
String body = str.substring(newline + 1 + headerLength, newline + 1 + headerLength + bodyLength);
return new Request(requestArguments[0],
requestArguments[1],
requestArguments[2],
headers,
body);
}
public static void main(String[] args) throws IOException {
String reqres = args[0];
int length = Integer.parseInt(args[1]);
byte[] input = new byte[length];
System.in.read(input, 0, length);
String toRead = new String(input, StandardCharsets.UTF_8);
if ("request".equals(reqres)) {
Request msg = parseRequest(toRead);
System.out.print(msg.toString());
} else {
throw new IllegalArgumentException("invalid request response given " + reqres);
}
}
}