-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyRequest.java
More file actions
157 lines (125 loc) · 3.87 KB
/
Copy pathMyRequest.java
File metadata and controls
157 lines (125 loc) · 3.87 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package http;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.time.Duration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class MyRequest extends HttpRequest {
private final String rawHttpRequest;
private final String method;
private final String path;
private final String httpVersionText;
private final URI uri;
private final HttpHeaders headers;
private final String body;
private final Optional<BodyPublisher> bodyPublisher;
private MyRequest(
String rawHttpRequest,
String method,
String path,
String httpVersionText,
URI uri,
HttpHeaders headers,
String body
) {
this.rawHttpRequest = rawHttpRequest;
this.method = method;
this.path = path;
this.httpVersionText = httpVersionText;
this.uri = uri;
this.headers = headers;
this.body = body;
this.bodyPublisher = body == null || body.isEmpty()
? Optional.empty()
: Optional.of(BodyPublishers.ofString(body));
}
public static MyRequest fromRaw(String rawHttpRequest) {
String[] parts = rawHttpRequest.split("\\r\\n\\r\\n", 2);
String headerPart = parts[0];
String body = parts.length > 1 ? parts[1] : "";
String[] lines = headerPart.split("\\r\\n");
String[] startLine = lines[0].split(" ", 3);
String method = startLine[0];
String path = startLine[1];
String httpVersionText = startLine[2];
Map<String, List<String>> headerMap = new LinkedHashMap<>();
for (int i = 1; i < lines.length; i++) {
String line = lines[i];
int colonIndex = line.indexOf(":");
if (colonIndex == -1) {
continue;
}
String name = line.substring(0, colonIndex).trim();
String value = line.substring(colonIndex + 1).trim();
headerMap.put(name, List.of(value));
}
HttpHeaders headers = HttpHeaders.of(headerMap, (name, value) -> true);
URI uri = createUri(path, headers);
return new MyRequest(
rawHttpRequest,
method,
path,
httpVersionText,
uri,
headers,
body
);
}
private static URI createUri(String path, HttpHeaders headers) {
String host = headers.firstValue("Host").orElse("localhost");
return URI.create("http://" + host + path);
}
public String rawHttpRequest() {
return rawHttpRequest;
}
public String path() {
return path;
}
public String httpVersionText() {
return httpVersionText;
}
public Optional<String> header(String name) {
return headers.firstValue(name);
}
public String body() {
return body;
}
@Override
public Optional<BodyPublisher> bodyPublisher() {
return bodyPublisher;
}
@Override
public String method() {
return method;
}
@Override
public Optional<Duration> timeout() {
return Optional.empty();
}
@Override
public boolean expectContinue() {
return false;
}
@Override
public URI uri() {
return uri;
}
@Override
public Optional<HttpClient.Version> version() {
if ("HTTP/2".equals(httpVersionText) || "HTTP/2.0".equals(httpVersionText)) {
return Optional.of(HttpClient.Version.HTTP_2);
}
if ("HTTP/1.1".equals(httpVersionText)) {
return Optional.of(HttpClient.Version.HTTP_1_1);
}
return Optional.empty();
}
@Override
public HttpHeaders headers() {
return headers;
}
}