-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainHtml.java
More file actions
63 lines (52 loc) · 2.32 KB
/
Copy pathMainHtml.java
File metadata and controls
63 lines (52 loc) · 2.32 KB
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
import http.MyRequest;
import http.MyResponse;
public class MainHtml {
public static void main(String[] args) {
String rawHttpRequest =
"GET / HTTP/1.1\r\n" +
"Host: localhost:8080\r\n" +
"jwtToken: 전여름\r\n" +
"Accept: text/html\r\n" +
"\r\n";
MyRequest request = MyRequest.fromRaw(rawHttpRequest);
String jwtToken = request.header("jwtToken").orElse("알 수 없음");
String htmlBody = "<h1>" + escapeHtml(jwtToken) + "님 안녕하세요! </h1>";
MyResponse response = MyResponse.html(
200,
request,
htmlBody
);
System.out.println("===== HTTP 요청 원본 String =====");
System.out.println(rawHttpRequest);
System.out.println();
System.out.println("===== MyRequest 객체에 담긴 값 =====");
System.out.println("method = " + request.method());
System.out.println("path = " + request.path());
System.out.println("uri = " + request.uri());
System.out.println("version = " + request.httpVersionText());
System.out.println("headers = " + request.headers().map());
System.out.println("jwtToken header = " + request.header("jwtToken").orElse(""));
System.out.println("body = " + request.body());
System.out.println();
System.out.println("===== 응답하기 위한 MyResponse 객체에 담긴 값 =====");
System.out.println("statusCode = " + response.statusCode());
System.out.println("uri = " + response.uri());
System.out.println("version = " + response.version());
System.out.println("headers = " + response.headers().map());
System.out.println("body = " + response.body());
System.out.println();
System.out.println("===== MyResponse가 만든 HTTP 응답 원본 String =====");
System.out.println(response.toRawHttpResponse());
}
private static String escapeHtml(String value) {
if (value == null) {
return "";
}
return value
.replace("&", "&")
.replace("<", "<")
.replace(">", ">")
.replace("\"", """)
.replace("'", "'");
}
}