2828import java .io .IOException ;
2929import java .io .InputStream ;
3030import java .net .InetAddress ;
31+ import java .net .URI ;
32+ import java .net .URISyntaxException ;
3133import java .nio .ByteBuffer ;
3234import java .nio .channels .ReadableByteChannel ;
3335import 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