5 files changed +38
-9
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ final class AphrontFileResponse extends AphrontResponse {
5
5
private $ content ;
6
6
private $ contentIterator ;
7
7
private $ contentLength ;
8
+ private $ compressResponse ;
8
9
9
10
private $ mimeType ;
10
11
private $ download ;
@@ -69,6 +70,15 @@ public function getContentLength() {
69
70
return $ this ->contentLength ;
70
71
}
71
72
73
+ public function setCompressResponse ($ compress_response ) {
74
+ $ this ->compressResponse = $ compress_response ;
75
+ return $ this ;
76
+ }
77
+
78
+ public function getCompressResponse () {
79
+ return $ this ->compressResponse ;
80
+ }
81
+
72
82
public function setRange ($ min , $ max ) {
73
83
$ this ->rangeMin = $ min ;
74
84
$ this ->rangeMax = $ max ;
@@ -94,7 +104,9 @@ public function getHeaders() {
94
104
$ content_len = $ this ->getContentLength ();
95
105
}
96
106
97
- $ headers [] = array ('Content-Length ' , $ this ->getContentLength ());
107
+ if (!$ this ->shouldCompressResponse ()) {
108
+ $ headers [] = array ('Content-Length ' , $ this ->getContentLength ());
109
+ }
98
110
99
111
if (strlen ($ this ->getDownload ())) {
100
112
$ headers [] = array ('X-Download-Options ' , 'noopen ' );
@@ -118,4 +130,8 @@ public function getHeaders() {
118
130
return $ headers ;
119
131
}
120
132
133
+ protected function shouldCompressResponse () {
134
+ return $ this ->getCompressResponse ();
135
+ }
136
+
121
137
}
Original file line number Diff line number Diff line change @@ -242,6 +242,21 @@ private function formatEpochTimestampForHTTPHeader($epoch_timestamp) {
242
242
return gmdate ('D, d M Y H:i:s ' , $ epoch_timestamp ).' GMT ' ;
243
243
}
244
244
245
+ protected function shouldCompressResponse () {
246
+ return true ;
247
+ }
248
+
249
+ public function willBeginWrite () {
250
+ if ($ this ->shouldCompressResponse ()) {
251
+ // Enable automatic compression here. Webservers sometimes do this for
252
+ // us, but we now detect the absence of compression and warn users about
253
+ // it so try to cover our bases more thoroughly.
254
+ ini_set ('zlib.output_compression ' , 1 );
255
+ } else {
256
+ ini_set ('zlib.output_compression ' , 0 );
257
+ }
258
+ }
259
+
245
260
public function didCompleteWrite ($ aborted ) {
246
261
return ;
247
262
}
Original file line number Diff line number Diff line change @@ -96,6 +96,8 @@ final public function writeData($data) {
96
96
* @return void
97
97
*/
98
98
final public function writeResponse (AphrontResponse $ response ) {
99
+ $ response ->willBeginWrite ();
100
+
99
101
// Build the content iterator first, in case it throws. Ideally, we'd
100
102
// prefer to handle exceptions before we emit the response status or any
101
103
// HTTP headers.
Original file line number Diff line number Diff line change @@ -103,9 +103,10 @@ protected function serveResource(array $spec) {
103
103
}
104
104
}
105
105
106
- $ response = new AphrontFileResponse ();
107
- $ response ->setContent ($ data );
108
- $ response ->setMimeType ($ type_map [$ type ]);
106
+ $ response = id (new AphrontFileResponse ())
107
+ ->setContent ($ data )
108
+ ->setMimeType ($ type_map [$ type ])
109
+ ->setCompressResponse (true );
109
110
110
111
// NOTE: This is a piece of magic required to make WOFF fonts work in
111
112
// Firefox and IE. Possibly we should generalize this more.
Original file line number Diff line number Diff line change @@ -395,11 +395,6 @@ private static function setupPHP() {
395
395
if (function_exists ('libxml_disable_entity_loader ' )) {
396
396
libxml_disable_entity_loader (true );
397
397
}
398
-
399
- // Enable automatic compression here. Webservers sometimes do this for
400
- // us, but we now detect the absence of compression and warn users about
401
- // it so try to cover our bases more thoroughly.
402
- ini_set ('zlib.output_compression ' , 1 );
403
398
}
404
399
405
400
0 commit comments