Skip to content

Commit 06d4e35

Browse files
committed
bug fixed
1 parent 29cffb4 commit 06d4e35

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

src/main/java/edu/nju/http/client/HttpClient.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ public HttpResponseMessage post(String rawUri, String body, String ... headers)
107107
return request(WebMethods.POST, rawUri, body, headers);
108108
}
109109

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+
*/
110118
HttpResponseMessage request(String method, String rawUri, String body, String ... headers) throws IOException {
111119
assert WebMethods.GET.equals(method) || WebMethods.POST.equals(method);
112120
assert rawUri != null;
@@ -162,34 +170,34 @@ private HttpResponseMessage request(HttpRequestMessage request) throws IOExcepti
162170
connected = true;
163171
}
164172

165-
// -------------------- Header editing -------------------- //
173+
// -------------------- 1. Header editing -------------------- //
166174
request.addHeader("Host", hostName);
167175
request.addHeader(Headers.USER_AGENT, "Wget/1.21.3");
168176
request.addHeader(Headers.ACCEPT_ENCODING, "gzip");
169177
request.addHeader(Headers.ACCEPT, "*/*");
170178

171179
if (keepAlive) request.addHeader(Headers.CONNECTION, Headers.KEEP_ALIVE);
172180

173-
// -------------------- Cache checking -------------------- //
181+
// -------------------- 2. Cache checking -------------------- //
174182
checkCache(request);
175183

176-
// -------------------- Pack and send -------------------- //
184+
// -------------------- 3. Pack and send -------------------- //
177185
MessagePacker packer = new MessagePacker(request, null);
178186
packer.send(aSocket);
179187

180188
/* Output 1 */
181189
System.out.println(present(request));
182190

183-
// -------------------- Receive and parse -------------------- //
191+
// -------------------- 4. Receive and parse -------------------- //
184192
MessageParser parser = new MessageParser(aSocket, 10000);
185193
HttpResponseMessage hrm = parser.parseToHttpResponseMessage();
186194

187-
// -------------------- Status Handler -------------------- //
195+
// -------------------- 5. Status Handler -------------------- //
188196
hrm = handler.handle(this, hrm);
189197

190198
Log.logClient("Request complete");
191199

192-
// -------------------- Caching -------------------- //
200+
// -------------------- 6. Caching -------------------- //
193201
String content_type = hrm.getHeaderVal(Headers.CONTENT_TYPE);
194202
if ( hrm.getStatusCode() != 304
195203
&& content_type != null
@@ -224,7 +232,7 @@ private void checkCache(HttpRequestMessage hrm) {
224232
}
225233
}
226234

227-
public String present(HttpMessage hrm) {
235+
private String present(HttpMessage hrm) {
228236
StringBuilder sb = new StringBuilder();
229237
sb.append(hrm.getStartLineAndHeaders());
230238
var ct = hrm.getHeaderVal(Headers.CONTENT_TYPE);
@@ -247,17 +255,23 @@ Config.CLIENT_CACHE, getHostName() + this.getRawPath()))
247255
return sb.toString();
248256
}
249257

250-
public String decorate(String s, String mark) {
258+
private String decorate(String s, String mark) {
251259
return Arrays.stream(s.split("\n"))
252260
.map(ss -> mark + ss)
253261
.collect(Collectors.joining("\n"));
254262
}
255263

264+
/**
265+
* Present the HTTP Request Message as String
266+
*/
256267
public String present(HttpRequestMessage hrm) {
257268
return "\n>> ==================== HTTP Request Message ==================== <<\n" +
258269
decorate(present((HttpMessage) hrm), ">> ");
259270
}
260271

272+
/**
273+
* Present the HTTP Response Message as String
274+
*/
261275
public String present(HttpResponseMessage hrm) {
262276
return "\n<< ==================== HTTP Response Message ==================== >>\n" +
263277
decorate(present((HttpMessage) hrm), "<< ");

src/main/java/edu/nju/http/server/HttpServer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,10 @@ public void shutdown() {
135135
}
136136

137137
/**
138-
* Should be packed in a Thread <br/>
139-
* Socket handler --> TargetHandler --> Output handler <br/>
138+
* Socket handler
139+
* --> Message Parser
140+
* --> TargetHandler
141+
* --> Message Packer <br/>
140142
*/
141143
private void handleSocket(AsynchronousSocketChannel socket, boolean keepAlive, int timeOut) {
142144
Log.logSocket(socket, "Accepted");

src/main/java/edu/nju/http/server/TargetHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,15 @@ private TargetHandler() {
5656

5757
StringBuilder sb = new StringBuilder();
5858

59+
/* Load preset targets from the config file */
5960
try {
6061
for (String prefix : json.keySet()) {
62+
/* Package name */
6163
for (Object className : json.getJSONArray(prefix).toList()) {
64+
/* Class name */
6265
Class<?> targetClass = Class.forName("%s.%s".formatted(prefix, className));
6366
for (Method method : targetClass.getDeclaredMethods()) {
67+
/* Mapped methods */
6468
if (method.isAnnotationPresent(mappingClass)) {
6569
assert checkMethod(method);
6670
Mapping mapping = method.getDeclaredAnnotation(mappingClass);
@@ -110,8 +114,6 @@ private boolean checkMethod(Method method) {
110114

111115
/**
112116
* Handling the request based on the method and the target
113-
*
114-
* @return Response object
115117
*/
116118
public HttpResponseMessage handle(HttpRequestMessage msg) {
117119
if (!supportedMethods.contains(msg.getMethod()))

0 commit comments

Comments
 (0)