Skip to content

Commit d75b656

Browse files
committed
Store query parameters separately from path
1 parent fa1599a commit d75b656

1 file changed

Lines changed: 71 additions & 37 deletions

File tree

src/main/java/org/example/httpparser/HttpRequest.java

Lines changed: 71 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,82 @@
22

33
import java.util.Collections;
44
import java.util.HashMap;
5+
import java.util.List;
56
import java.util.Map;
67

78
/*
8-
*
9-
*This class groups together all information about a request that the server needs
9+
*
10+
* This class groups together all information about a request that the server needs
1011
*/
1112

1213
public class HttpRequest {
1314

14-
private final String method;
15-
private final String path;
16-
private final String version;
17-
private final Map<String, String> headers;
18-
private final String body;
19-
private final Map<String, Object> attributes = new HashMap<>();
20-
21-
public HttpRequest(String method,
22-
String path,
23-
String version,
24-
Map<String, String> headers,
25-
String body) {
26-
this.method = method;
27-
this.path = path;
28-
this.version = version;
29-
this.headers = headers != null ? Map.copyOf(headers) : Collections.emptyMap();
30-
this.body = body;
31-
}
32-
33-
public String getMethod() {
34-
return method; }
35-
public String getPath() {
36-
return path; }
37-
public String getVersion() {
38-
return version; }
39-
public Map<String, String> getHeaders() {
40-
return headers; }
41-
public String getBody() {
42-
return body; }
43-
public void setAttribute(String key, Object value) {
44-
attributes.put(key, value);
45-
}
46-
public Object getAttribute(String key) {
47-
return attributes.get(key);
48-
}
15+
private final String method;
16+
private final String path;
17+
private final String version;
18+
private final Map<String, String> headers;
19+
private final String body;
20+
21+
private final Map<String, List<String>> queryParams;
22+
23+
private final Map<String, Object> attributes = new HashMap<>();
24+
25+
public HttpRequest(String method,
26+
String path,
27+
String version,
28+
Map<String, String> headers,
29+
String body) {
30+
this(method, path, version, headers, body, Collections.emptyMap());
31+
}
32+
33+
public HttpRequest(String method,
34+
String path,
35+
String version,
36+
Map<String, String> headers,
37+
String body,
38+
Map<String, List<String>> queryParams) {
39+
40+
this.method = method;
41+
this.path = path;
42+
this.version = version;
43+
this.headers = headers != null ? Map.copyOf(headers) : Collections.emptyMap();
44+
this.body = body;
45+
this.queryParams = queryParams != null ? Map.copyOf(queryParams) : Collections.emptyMap();
46+
}
47+
48+
public String getMethod() {
49+
return method;
50+
}
51+
52+
public String getPath() {
53+
return path;
54+
}
55+
56+
public String getVersion() {
57+
return version;
58+
}
59+
60+
public Map<String, String> getHeaders() {
61+
return headers;
62+
}
63+
64+
public String getBody() {
65+
return body;
66+
}
67+
68+
public Map<String, List<String>> getQueryParams() {
69+
return queryParams;
70+
}
71+
72+
public List<String> getQueryParam(String key) {
73+
return queryParams.getOrDefault(key, List.of());
74+
}
75+
76+
public void setAttribute(String key, Object value) {
77+
attributes.put(key, value);
78+
}
79+
80+
public Object getAttribute(String key) {
81+
return attributes.get(key);
4982
}
83+
}

0 commit comments

Comments
 (0)