77
88import io .jooby .AttachedFile ;
99import io .jooby .Context ;
10- import io .jooby .DefaultContext ;
1110import io .jooby .ForwardingContext ;
1211import io .jooby .MediaType ;
1312import io .jooby .MessageEncoder ;
@@ -43,6 +42,7 @@ public HeadContext(@Nonnull Context context) {
4342 @ Nonnull @ Override public Context send (@ Nonnull Path file ) {
4443 try {
4544 ctx .setResponseLength (Files .size (file ));
45+ checkSizeHeaders ();
4646 ctx .setResponseType (MediaType .byFile (file ));
4747 ctx .send (StatusCode .OK );
4848 return this ;
@@ -53,6 +53,7 @@ public HeadContext(@Nonnull Context context) {
5353
5454 @ Nonnull @ Override public Context send (@ Nonnull byte [] data ) {
5555 ctx .setResponseLength (data .length );
56+ checkSizeHeaders ();
5657 ctx .send (StatusCode .OK );
5758 return this ;
5859 }
@@ -63,13 +64,15 @@ public HeadContext(@Nonnull Context context) {
6364
6465 @ Nonnull @ Override public Context send (@ Nonnull ByteBuffer data ) {
6566 ctx .setResponseLength (data .remaining ());
67+ checkSizeHeaders ();
6668 ctx .send (StatusCode .OK );
6769 return this ;
6870 }
6971
7072 @ Nonnull @ Override public Context send (@ Nonnull FileChannel file ) {
7173 try {
7274 ctx .setResponseLength (file .size ());
75+ checkSizeHeaders ();
7376 ctx .send (StatusCode .OK );
7477 return this ;
7578 } catch (IOException x ) {
@@ -80,12 +83,13 @@ public HeadContext(@Nonnull Context context) {
8083 @ Nonnull @ Override public Context send (@ Nonnull AttachedFile file ) {
8184 ctx .setResponseLength (file .getFileSize ());
8285 ctx .setResponseType (file .getContentType ());
86+ checkSizeHeaders ();
8387 ctx .send (StatusCode .OK );
8488 return this ;
8589 }
8690
8791 @ Nonnull @ Override public Context send (@ Nonnull InputStream input ) {
88- ctx . setResponseHeader ( "Transfer-Encoding" , "chunked" );
92+ checkSizeHeaders ( );
8993 ctx .send (input );
9094 ctx .send (StatusCode .OK );
9195 return this ;
@@ -97,13 +101,14 @@ public HeadContext(@Nonnull Context context) {
97101 }
98102
99103 @ Nonnull @ Override public Context send (@ Nonnull ReadableByteChannel channel ) {
100- ctx . setResponseHeader ( "Transfer-Encoding" , "chunked" );
104+ checkSizeHeaders ( );
101105 ctx .send (StatusCode .OK );
102106 return this ;
103107 }
104108
105109 @ Nonnull @ Override public Context send (@ Nonnull String data , @ Nonnull Charset charset ) {
106110 ctx .setResponseLength (data .getBytes (charset ).length );
111+ checkSizeHeaders ();
107112 ctx .send (StatusCode .OK );
108113 return this ;
109114 }
@@ -127,13 +132,13 @@ public HeadContext(@Nonnull Context context) {
127132 }
128133
129134 @ Nonnull @ Override public Sender responseSender () {
130- ctx . setResponseHeader ( "Transfer-Encoding" , "chunked" );
135+ checkSizeHeaders ( );
131136 ctx .send (StatusCode .OK );
132137 return new NoopSender ();
133138 }
134139
135140 @ Nonnull @ Override public OutputStream responseStream () {
136- ctx . setResponseHeader ( "Transfer-Encoding" , "chunked" );
141+ checkSizeHeaders ( );
137142 ctx .send (StatusCode .OK );
138143 return new NoopOutputStream ();
139144 }
@@ -142,6 +147,14 @@ public HeadContext(@Nonnull Context context) {
142147 return new PrintWriter (responseStream ());
143148 }
144149
150+ private void checkSizeHeaders () {
151+ if (ctx .getResponseLength () < 0 ) {
152+ ctx .setResponseHeader ("Transfer-Encoding" , "chunked" );
153+ } else {
154+ ctx .removeResponseHeader ("Transfer-Encoding" );
155+ }
156+ }
157+
145158 private static class NoopOutputStream extends OutputStream {
146159 @ Override public void write (@ NotNull byte [] b ) throws IOException {
147160 }
0 commit comments