@@ -107,6 +107,14 @@ public HttpResponseMessage post(String rawUri, String body, String ... headers)
107
107
return request (WebMethods .POST , rawUri , body , headers );
108
108
}
109
109
110
+ /**
111
+ * Perform HTTP request
112
+ * @param method HTTP method. Supports GET and POST
113
+ * @param rawUri Raw URI string
114
+ * @param body Message body
115
+ * @param headers Message Headers
116
+ * @return Received response
117
+ */
110
118
HttpResponseMessage request (String method , String rawUri , String body , String ... headers ) throws IOException {
111
119
assert WebMethods .GET .equals (method ) || WebMethods .POST .equals (method );
112
120
assert rawUri != null ;
@@ -162,34 +170,34 @@ private HttpResponseMessage request(HttpRequestMessage request) throws IOExcepti
162
170
connected = true ;
163
171
}
164
172
165
- // -------------------- Header editing -------------------- //
173
+ // -------------------- 1. Header editing -------------------- //
166
174
request .addHeader ("Host" , hostName );
167
175
request .addHeader (Headers .USER_AGENT , "Wget/1.21.3" );
168
176
request .addHeader (Headers .ACCEPT_ENCODING , "gzip" );
169
177
request .addHeader (Headers .ACCEPT , "*/*" );
170
178
171
179
if (keepAlive ) request .addHeader (Headers .CONNECTION , Headers .KEEP_ALIVE );
172
180
173
- // -------------------- Cache checking -------------------- //
181
+ // -------------------- 2. Cache checking -------------------- //
174
182
checkCache (request );
175
183
176
- // -------------------- Pack and send -------------------- //
184
+ // -------------------- 3. Pack and send -------------------- //
177
185
MessagePacker packer = new MessagePacker (request , null );
178
186
packer .send (aSocket );
179
187
180
188
/* Output 1 */
181
189
System .out .println (present (request ));
182
190
183
- // -------------------- Receive and parse -------------------- //
191
+ // -------------------- 4. Receive and parse -------------------- //
184
192
MessageParser parser = new MessageParser (aSocket , 10000 );
185
193
HttpResponseMessage hrm = parser .parseToHttpResponseMessage ();
186
194
187
- // -------------------- Status Handler -------------------- //
195
+ // -------------------- 5. Status Handler -------------------- //
188
196
hrm = handler .handle (this , hrm );
189
197
190
198
Log .logClient ("Request complete" );
191
199
192
- // -------------------- Caching -------------------- //
200
+ // -------------------- 6. Caching -------------------- //
193
201
String content_type = hrm .getHeaderVal (Headers .CONTENT_TYPE );
194
202
if ( hrm .getStatusCode () != 304
195
203
&& content_type != null
@@ -224,7 +232,7 @@ private void checkCache(HttpRequestMessage hrm) {
224
232
}
225
233
}
226
234
227
- public String present (HttpMessage hrm ) {
235
+ private String present (HttpMessage hrm ) {
228
236
StringBuilder sb = new StringBuilder ();
229
237
sb .append (hrm .getStartLineAndHeaders ());
230
238
var ct = hrm .getHeaderVal (Headers .CONTENT_TYPE );
@@ -247,17 +255,23 @@ Config.CLIENT_CACHE, getHostName() + this.getRawPath()))
247
255
return sb .toString ();
248
256
}
249
257
250
- public String decorate (String s , String mark ) {
258
+ private String decorate (String s , String mark ) {
251
259
return Arrays .stream (s .split ("\n " ))
252
260
.map (ss -> mark + ss )
253
261
.collect (Collectors .joining ("\n " ));
254
262
}
255
263
264
+ /**
265
+ * Present the HTTP Request Message as String
266
+ */
256
267
public String present (HttpRequestMessage hrm ) {
257
268
return "\n >> ==================== HTTP Request Message ==================== <<\n " +
258
269
decorate (present ((HttpMessage ) hrm ), ">> " );
259
270
}
260
271
272
+ /**
273
+ * Present the HTTP Response Message as String
274
+ */
261
275
public String present (HttpResponseMessage hrm ) {
262
276
return "\n << ==================== HTTP Response Message ==================== >>\n " +
263
277
decorate (present ((HttpMessage ) hrm ), "<< " );
0 commit comments