22
33import com .sun .net .httpserver .Headers ;
44import com .sun .net .httpserver .HttpExchange ;
5+ import express .ExpressUtils ;
56import express .cookie .Cookie ;
67
8+ import java .io .IOException ;
79import java .io .InputStream ;
10+ import java .io .OutputStream ;
811import java .net .URI ;
912import java .util .HashMap ;
1013import java .util .List ;
1114
12- public class Request {
15+ public class Request {
1316
1417 private final HttpExchange HTTP_EXCHANGE ;
18+ private final URI URI ;
1519 private final InputStream BODY ;
1620 private final Headers HEADER ;
1721
1822 private final HashMap <String , Cookie > COOKIES ;
19-
23+ private final HashMap < String , String > QUERYS ;
2024 private HashMap <String , String > params ;
2125
2226 public Request (HttpExchange exchange ) {
2327 this .HTTP_EXCHANGE = exchange ;
28+ this .URI = exchange .getRequestURI ();
2429 this .HEADER = exchange .getRequestHeaders ();
2530 this .BODY = exchange .getRequestBody ();
26- this .COOKIES = parseCookies (exchange .getRequestHeaders ());
31+
32+ this .QUERYS = ExpressUtils .parseRawQuery (exchange .getRequestURI ());
33+ this .COOKIES = ExpressUtils .parseCookies (exchange .getRequestHeaders ());
34+ }
35+
36+ public InputStream getBody () {
37+ return BODY ;
38+ }
39+
40+ public void pipe (OutputStream os , int bufferSize ) throws IOException {
41+ byte [] buffer = new byte [bufferSize ];
42+ int n ;
43+ while ((n = BODY .read (buffer )) != -1 )
44+ os .write (buffer , 0 , n );
45+ os .close ();
2746 }
2847
2948 public Cookie getCookie (String name ) {
@@ -43,21 +62,25 @@ public String getHost() {
4362 }
4463
4564 public String getContentType () {
46- return HEADER .get ("Accept " ).get (0 );
65+ return HEADER .get ("Content-Type " ).get (0 );
4766 }
4867
49- public URI getRequestURI () {
50- return HTTP_EXCHANGE . getRequestURI () ;
68+ public URI getURI () {
69+ return this . URI ;
5170 }
5271
53- public String getRequestMethod () {
72+ public String getMethod () {
5473 return HTTP_EXCHANGE .getRequestMethod ();
5574 }
5675
5776 public String getParam (String key ) {
5877 return params .get (key );
5978 }
6079
80+ public String getQuery (String key ) {
81+ return QUERYS .get (key );
82+ }
83+
6184 public void setParams (HashMap <String , String > params ) {
6285 this .params = params ;
6386 }
@@ -66,25 +89,4 @@ public List<String> getHeader(String header) {
6689 return HEADER .get (header );
6790 }
6891
69- private HashMap <String , Cookie > parseCookies (Headers headers ) {
70- HashMap <String , Cookie > cookieList = new HashMap <>();
71- List <String > headerCookies = headers .get ("Cookie" );
72-
73- if (headerCookies == null || headerCookies .size () == 0 ) {
74- return cookieList ;
75- }
76-
77- String hcookies = headerCookies .get (0 );
78-
79- String [] cookies = hcookies .split (";" );
80- for (String cookie : cookies ) {
81- String [] split = cookie .split ("=" );
82- String name = split [0 ].trim ();
83- String value = split [1 ].trim ();
84- cookieList .put (name , new Cookie (name , value ));
85- }
86-
87- return cookieList ;
88- }
89-
9092}
0 commit comments