Skip to content

Commit 7da80d0

Browse files
committed
Use proper URI-parser for processing http-requests, fixes: #737
1 parent 05f0987 commit 7da80d0

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

common/src/main/java/de/bluecolored/bluemap/common/web/LoggingRequestHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import lombok.NonNull;
3232
import lombok.Setter;
3333

34+
import java.net.URI;
35+
3436
@Getter @Setter
3537
@AllArgsConstructor
3638
public class LoggingRequestHandler implements HttpRequestHandler {
@@ -63,7 +65,7 @@ public HttpResponse handle(HttpRequest request) {
6365
}
6466

6567
String method = request.getMethod();
66-
String address = request.getAddress();
68+
URI address = request.getAddress();
6769
String version = request.getVersion();
6870

6971
// run request
@@ -79,7 +81,7 @@ public HttpResponse handle(HttpRequest request) {
7981
source,
8082
xffSource,
8183
method,
82-
address,
84+
address.toString(),
8385
version,
8486
statusCode,
8587
statusMessage

common/src/main/java/de/bluecolored/bluemap/common/web/http/HttpRequest.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import java.io.IOException;
2929
import java.io.InputStream;
3030
import java.net.InetAddress;
31+
import java.net.URI;
32+
import java.net.URISyntaxException;
3133
import java.nio.ByteBuffer;
3234
import java.nio.channels.ReadableByteChannel;
3335
import java.util.*;
@@ -48,11 +50,12 @@ public class HttpRequest {
4850

4951
// request data
5052
private final InetAddress source;
51-
private String method, address, version;
53+
private URI address;
54+
private String method, version;
5255
private final Map<String, HttpHeader> headers = new HashMap<>();
5356
private byte[] data;
5457

55-
// lazy parsed
58+
// these values can be overwritten separately by a HttpRequestHandler for delegation
5659
private String path = null;
5760
private String getParamString = null;
5861
private Map<String, String> getParams = null;
@@ -131,8 +134,13 @@ private void parseHeaders() throws IOException {
131134
method = m.group(1);
132135
if (method == null) throw new IOException("Invalid HTTP Request: Request-Pattern not matching (method)");
133136

134-
address = m.group(2);
135-
if (address == null) throw new IOException("Invalid HTTP Request: Request-Pattern not matching (address)");
137+
String addressString = m.group(2);
138+
if (addressString == null) throw new IOException("Invalid HTTP Request: Request-Pattern not matching (address)");
139+
try {
140+
address = new URI(addressString);
141+
} catch (URISyntaxException ex) {
142+
throw new IOException("Invalid HTTP Request: Request-URI is invalid", ex);
143+
}
136144

137145
version = m.group(3);
138146
if (version == null) throw new IOException("Invalid HTTP Request: Request-Pattern not matching (version)");
@@ -169,11 +177,11 @@ public void setMethod(String method) {
169177
this.method = method;
170178
}
171179

172-
public String getAddress() {
180+
public URI getAddress() {
173181
return address;
174182
}
175183

176-
public void setAddress(String address) {
184+
public void setAddress(URI address) {
177185
this.address = address;
178186
this.path = null;
179187
this.getParams = null;
@@ -235,12 +243,8 @@ public void setGetParamString(String getParamString) {
235243
}
236244

237245
private void parseAddress() {
238-
String address = this.getAddress();
239-
if (address.isEmpty()) address = "/";
240-
String[] addressParts = address.split("\\?", 2);
241-
String path = addressParts[0];
242-
this.getParamString = addressParts.length > 1 ? addressParts[1] : "";
243-
this.path = path;
246+
this.path = address.getPath();
247+
this.getParamString = address.getQuery();
244248
}
245249

246250
private void parseGetParams() {

0 commit comments

Comments
 (0)