66import express .expressfilter .ExpressFilter ;
77import express .http .cookie .Cookie ;
88
9- import java .io .IOException ;
10- import java .io .InputStream ;
11- import java .io .OutputStream ;
9+ import java .io .*;
1210import java .net .URI ;
1311import java .util .HashMap ;
1412import java .util .List ;
@@ -25,6 +23,7 @@ public class Request {
2523 private final Headers HEADERS ; // Request Headers
2624 private final String CONTENT_TYPE ; // Request content-type
2725 private final Authorization AUTH ; // Authorization header parsed
26+ private final long CONTENT_LENGTH ;
2827
2928 private final HashMap <String , Object > MIDDLEWARE ; // Middleware Data
3029 private final HashMap <String , Cookie > COOKIES ; // Request cookies
@@ -46,6 +45,14 @@ public Request(HttpExchange exchange) {
4645 this .HEADERS = exchange .getRequestHeaders ();
4746 this .BODY = exchange .getRequestBody ();
4847
48+ // Parse content length
49+ String contentLength = HEADERS .get ("Content-Length" ) != null ? HEADERS .get ("Content-Length" ).get (0 ) : null ;
50+ if (contentLength != null && contentLength .matches ("^[0-9]+$" )) {
51+ CONTENT_LENGTH = Long .parseLong (contentLength );
52+ } else
53+ CONTENT_LENGTH = -1 ;
54+
55+
4956 // Check if the request contains an body-content
5057 this .CONTENT_TYPE = HEADERS .get ("Content-Type" ) == null ? "" : HEADERS .get ("Content-Type" ).get (0 );
5158
@@ -84,6 +91,19 @@ public void pipe(OutputStream os, int bufferSize) throws IOException {
8491 os .close ();
8592 }
8693
94+ /**
95+ * Pipe the body from this request to an file.
96+ * If the file not exists, it will be created.
97+ *
98+ * @param f The target file
99+ * @param bufferSize Buffersize, eg. 4096.
100+ * @throws IOException If an IO-Error occurs.
101+ */
102+ public void pipe (File f , int bufferSize ) throws IOException {
103+ if (!f .exists ()) f .createNewFile ();
104+ pipe (new FileOutputStream (f ), bufferSize );
105+ }
106+
87107 /**
88108 * Get an request cookie by name.
89109 *
@@ -145,6 +165,15 @@ public String getContentType() {
145165 return CONTENT_TYPE ;
146166 }
147167
168+ /**
169+ * Returns the to long parsed content-length.
170+ *
171+ * @return The content-length, -1 if the header was invalid.
172+ */
173+ public long getContentLength () {
174+ return CONTENT_LENGTH ;
175+ }
176+
148177 /**
149178 * @return The request path.
150179 */
@@ -277,7 +306,7 @@ public String getRedirect() {
277306 }
278307
279308 public boolean hadRedirect () {
280- if (!hadRedirect ) return false ;
309+ if (!hadRedirect ) return false ;
281310 return !(hadRedirect = !hadRedirect );
282311 }
283312}
0 commit comments