-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainJSON.java
More file actions
90 lines (71 loc) · 3.24 KB
/
Copy pathMainJSON.java
File metadata and controls
90 lines (71 loc) · 3.24 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
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
import http.MyRequest;
import http.MyResponse;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainJSON {
public static void main(String[] args) {
String requestBody = "{\"name\":\"허혜\",\"age\":20}";
String rawHttpRequest =
"POST /users HTTP/1.1\r\n" +
"Host: localhost:8080\r\n" +
"id: 허연혜\r\n" +
"Content-Type: application/json; charset=UTF-8\r\n" +
"Content-Length: " + requestBody.getBytes(StandardCharsets.UTF_8).length + "\r\n" +
"\r\n" +
requestBody;
MyRequest request = MyRequest.fromRaw(rawHttpRequest);
String id = request.header("id").orElse("알 수 없음");
String name = extractJsonString(request.body(), "name");
String age = extractJsonNumber(request.body(), "age");
String content = "id " + id + "님의 name: " + name + ", age: " + age + "으로 등록되었습니다.";
String responseJson = """
{
"content": "%s"
}
""".formatted(escapeJson(content));
MyResponse response = MyResponse.json(201, request, responseJson);
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("id header = " + request.header("id").orElse(""));
System.out.println("body = " + request.body());
System.out.println();
System.out.println("===== MyResponse 객체에 담긴 값 =====");
System.out.println("statusCode = " + response.statusCode());
System.out.println("headers = " + response.headers().map());
System.out.println("body = " + response.body());
System.out.println();
System.out.println("===== MyResponse가 만든 HTTP 응답 원본 =====");
System.out.println(response.toRawHttpResponse());
}
private static String extractJsonString(String json, String key) {
Pattern pattern = Pattern.compile("\"" + key + "\"\\s*:\\s*\"([^\"]*)\"");
Matcher matcher = pattern.matcher(json);
if (matcher.find()) {
return matcher.group(1);
}
return "";
}
private static String extractJsonNumber(String json, String key) {
Pattern pattern = Pattern.compile("\"" + key + "\"\\s*:\\s*(\\d+)");
Matcher matcher = pattern.matcher(json);
if (matcher.find()) {
return matcher.group(1);
}
return "";
}
private static String escapeJson(String value) {
return value
.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\n", "\\n")
.replace("\r", "\\r");
}
}