13
13
*/
14
14
public final class Parser {
15
15
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
-
35
16
public static final class Request {
36
17
37
18
private final String protocol ;
38
19
private final String version ;
39
20
private final String command ;
40
21
private final String compression ;
41
- private final String [] responseCompression ;
22
+ private final String encoding ;
42
23
private final boolean headOnlyIndicator ;
43
24
private final String headers ;
44
25
private final String body ;
@@ -48,15 +29,15 @@ public static final class Request {
48
29
String version ,
49
30
String command ,
50
31
String compression ,
51
- String [] responseCompression ,
32
+ String encoding ,
52
33
boolean headOnlyIndicator ,
53
34
String headers ,
54
35
String body ) {
55
36
this .protocol = protocol ;
56
37
this .command = command ;
57
38
this .version = version ;
58
39
this .compression = compression ;
59
- this .responseCompression = responseCompression ;
40
+ this .encoding = encoding ;
60
41
this .headOnlyIndicator = headOnlyIndicator ;
61
42
this .headers = headers ;
62
43
this .body = body ;
@@ -67,8 +48,7 @@ public String toString() {
67
48
builder .append (protocol + " " +
68
49
version + " " +
69
50
command + " " +
70
- compression + " " +
71
- String .join ("," , responseCompression ));
51
+ compression + " " + encoding );
72
52
if (headers == null ) {
73
53
builder .append (" 0" );
74
54
} else {
@@ -130,45 +110,12 @@ public static final Request parseRequest(String str) {
130
110
requestArguments [1 ],
131
111
requestArguments [2 ],
132
112
requestArguments [3 ],
133
- requestArguments [4 ]. split ( "," ) ,
113
+ requestArguments [4 ],
134
114
headOnlyIndicator ,
135
115
headers ,
136
116
body );
137
117
}
138
118
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
-
172
119
public static void main (String [] args ) throws IOException {
173
120
String reqres = args [0 ];
174
121
int length = Integer .parseInt (args [1 ]);
@@ -178,9 +125,6 @@ public static void main(String[] args) throws IOException {
178
125
if ("request" .equals (reqres )) {
179
126
Request msg = parseRequest (toRead );
180
127
System .out .print (msg .toString ());
181
- } else if ("response" .equals (reqres )) {
182
- Response msg = parseResponse (toRead );
183
- System .out .print (msg .toString ());
184
128
} else {
185
129
throw new IllegalArgumentException ("invalid request response given " + reqres );
186
130
}
0 commit comments