Skip to content

Commit e4a4f90

Browse files
committed
Extract path and parse URL-decoded query parameters
1 parent d75b656 commit e4a4f90

File tree

1 file changed

+70
-9
lines changed

1 file changed

+70
-9
lines changed

src/main/java/org/example/ConnectionHandler.java

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44
import org.example.filter.IpFilter;
55
import org.example.httpparser.HttpParser;
66
import org.example.httpparser.HttpRequest;
7+
78
import java.util.ArrayList;
9+
import java.util.HashMap;
810
import java.util.List;
11+
import java.util.Map;
12+
913
import org.example.filter.Filter;
1014
import org.example.filter.FilterChainImpl;
1115
import org.example.http.HttpResponseBuilder;
1216
import org.example.config.ConfigLoader;
1317

1418
import java.io.IOException;
1519
import java.net.Socket;
20+
import java.net.URLDecoder;
21+
import java.nio.charset.StandardCharsets;
1622

1723
public class ConnectionHandler implements AutoCloseable {
1824

@@ -40,7 +46,6 @@ private List<Filter> buildFilters() {
4046
if (Boolean.TRUE.equals(ipFilterConfig.enabled())) {
4147
list.add(createIpFilterFromConfig(ipFilterConfig));
4248
}
43-
// Add more filters here...
4449
return list;
4550
}
4651

@@ -58,12 +63,18 @@ public void runConnectionHandler() throws IOException {
5863
parser.parseRequest();
5964
parser.parseHttp();
6065

66+
// --- ISSUE FIX ---
67+
String rawUri = parser.getUri();
68+
String pathOnly = extractPath(rawUri);
69+
Map<String, List<String>> queryParams = parseQueryParams(rawUri);
70+
6171
HttpRequest request = new HttpRequest(
6272
parser.getMethod(),
63-
parser.getUri(),
73+
pathOnly,
6474
parser.getVersion(),
6575
parser.getHeadersMap(),
66-
""
76+
"",
77+
queryParams
6778
);
6879

6980
String clientIp = client.getInetAddress().getHostAddress();
@@ -80,16 +91,14 @@ public void runConnectionHandler() throws IOException {
8091
return;
8192
}
8293

83-
resolveTargetFile(parser.getUri());
94+
resolveTargetFile(request.getPath());
8495
sfh.sendGetRequest(client.getOutputStream(), uri);
8596
}
8697

8798
private HttpResponseBuilder applyFilters(HttpRequest request) {
8899
HttpResponseBuilder response = new HttpResponseBuilder();
89-
90100
FilterChainImpl chain = new FilterChainImpl(filters);
91101
chain.doFilter(request, response);
92-
93102
return response;
94103
}
95104

@@ -101,6 +110,61 @@ private void resolveTargetFile(String uri) {
101110
}
102111
}
103112

113+
// -----------------------------
114+
// Query parsing without split()
115+
// -----------------------------
116+
117+
private static String extractPath(String uri) {
118+
if (uri == null) return "/";
119+
int q = uri.indexOf('?');
120+
return (q >= 0) ? uri.substring(0, q) : uri;
121+
}
122+
123+
private static Map<String, List<String>> parseQueryParams(String uri) {
124+
Map<String, List<String>> params = new HashMap<>();
125+
if (uri == null) return params;
126+
127+
int questionMarkIndex = uri.indexOf('?');
128+
if (questionMarkIndex < 0 || questionMarkIndex == uri.length() - 1) {
129+
return params;
130+
}
131+
132+
String query = uri.substring(questionMarkIndex + 1);
133+
134+
int start = 0;
135+
while (start < query.length()) {
136+
137+
int ampIndex = query.indexOf('&', start);
138+
if (ampIndex == -1) {
139+
ampIndex = query.length();
140+
}
141+
142+
String pair = query.substring(start, ampIndex);
143+
144+
int equalsIndex = pair.indexOf('=');
145+
146+
String key;
147+
String value;
148+
149+
if (equalsIndex >= 0) {
150+
key = pair.substring(0, equalsIndex);
151+
value = pair.substring(equalsIndex + 1);
152+
} else {
153+
key = pair;
154+
value = "";
155+
}
156+
157+
key = URLDecoder.decode(key, StandardCharsets.UTF_8);
158+
value = URLDecoder.decode(value, StandardCharsets.UTF_8);
159+
160+
params.computeIfAbsent(key, k -> new ArrayList<>()).add(value);
161+
162+
start = ampIndex + 1;
163+
}
164+
165+
return params;
166+
}
167+
104168
@Override
105169
public void close() throws Exception {
106170
client.close();
@@ -109,19 +173,16 @@ public void close() throws Exception {
109173
private IpFilter createIpFilterFromConfig(AppConfig.IpFilterConfig config) {
110174
IpFilter filter = new IpFilter();
111175

112-
// Set mode
113176
if ("ALLOWLIST".equalsIgnoreCase(config.mode())) {
114177
filter.setMode(IpFilter.FilterMode.ALLOWLIST);
115178
} else {
116179
filter.setMode(IpFilter.FilterMode.BLOCKLIST);
117180
}
118181

119-
// Add blocked IPs
120182
for (String ip : config.blockedIps()) {
121183
filter.addBlockedIp(ip);
122184
}
123185

124-
// Add allowed IPs
125186
for (String ip : config.allowedIps()) {
126187
filter.addAllowedIp(ip);
127188
}

0 commit comments

Comments
 (0)