22
33import com .sun .net .httpserver .Headers ;
44import com .sun .net .httpserver .HttpExchange ;
5- import express .utils .ExpressUtils ;
65import express .http .cookie .Cookie ;
76
87import java .io .IOException ;
98import java .io .InputStream ;
109import java .io .OutputStream ;
10+ import java .io .UnsupportedEncodingException ;
1111import java .net .URI ;
12+ import java .net .URLDecoder ;
1213import java .util .HashMap ;
1314import java .util .List ;
15+ import java .util .regex .Matcher ;
16+ import java .util .regex .Pattern ;
1417
1518/**
1619 * @author Simon Reinisch
@@ -34,8 +37,8 @@ public Request(HttpExchange exchange) {
3437 this .BODY = exchange .getRequestBody ();
3538
3639 this .params = new HashMap <>();
37- this .QUERYS = ExpressUtils . parseRawQuery (exchange .getRequestURI ());
38- this .COOKIES = ExpressUtils . parseCookies (exchange .getRequestHeaders ());
40+ this .QUERYS = parseRawQuery (exchange .getRequestURI ());
41+ this .COOKIES = parseCookies (exchange .getRequestHeaders ());
3942 }
4043
4144 /**
@@ -161,4 +164,58 @@ public List<String> getHeader(String header) {
161164 return HEADER .get (header );
162165 }
163166
167+ /**
168+ * Extract the cookies from the 'Cookie' header.
169+ *
170+ * @param headers The Headers
171+ * @return An hashmap with the cookie name as key and the complete cookie as value.
172+ */
173+ private static HashMap <String , Cookie > parseCookies (Headers headers ) {
174+ HashMap <String , Cookie > cookieList = new HashMap <>();
175+ List <String > headerCookies = headers .get ("Cookie" );
176+
177+ if (headerCookies == null || headerCookies .size () == 0 ) {
178+ return cookieList ;
179+ }
180+
181+ String hcookies = headerCookies .get (0 );
182+
183+ String [] cookies = hcookies .split (";" );
184+ for (String cookie : cookies ) {
185+ String [] split = cookie .split ("=" );
186+ String name = split [0 ].trim ();
187+ String value = split [1 ].trim ();
188+ cookieList .put (name , new Cookie (name , value ));
189+ }
190+
191+ return cookieList ;
192+ }
193+
194+ /**
195+ * Method to extract the querys from an url.
196+ *
197+ * @param uri The uri.
198+ * @return An list with key-values which are encoded in UTF8.
199+ */
200+ private static HashMap <String , String > parseRawQuery (URI uri ) {
201+ HashMap <String , String > querys = new HashMap <>();
202+ String rawQuery = uri .getRawQuery ();
203+
204+ if (rawQuery == null )
205+ return querys ;
206+
207+ Matcher mat = Pattern .compile ("(.+?)=(.+?)(&|$)" ).matcher (rawQuery );
208+ while (mat .find ()) {
209+ try {
210+ String key = URLDecoder .decode (mat .group (1 ), "UTF8" );
211+ String val = URLDecoder .decode (mat .group (2 ), "UTF8" );
212+ querys .put (key , val );
213+ } catch (UnsupportedEncodingException e ) {
214+ e .printStackTrace ();
215+ }
216+ }
217+
218+ return querys ;
219+ }
220+
164221}
0 commit comments