Skip to content

Commit 8427106

Browse files
author
epriestley
committedDec 13, 2016
Don't combine automatic output compression with "Content-Length"
Summary: Fixes T12013. Send either "Content-Length" or enable output compression, but not both. Prefer compression for static resources (CSS, JS, etc). Test Plan: Ran `curl -v ...`, no longer saw responses with both compression and `Content-Length`. Reviewers: chad, avivey Reviewed By: avivey Subscribers: avivey Maniphest Tasks: T12013 Differential Revision: https://secure.phabricator.com/D17045
1 parent 26127b9 commit 8427106

File tree

5 files changed

+38
-9
lines changed

5 files changed

+38
-9
lines changed
 

‎src/aphront/response/AphrontFileResponse.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ final class AphrontFileResponse extends AphrontResponse {
55
private $content;
66
private $contentIterator;
77
private $contentLength;
8+
private $compressResponse;
89

910
private $mimeType;
1011
private $download;
@@ -69,6 +70,15 @@ public function getContentLength() {
6970
return $this->contentLength;
7071
}
7172

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+
7282
public function setRange($min, $max) {
7383
$this->rangeMin = $min;
7484
$this->rangeMax = $max;
@@ -94,7 +104,9 @@ public function getHeaders() {
94104
$content_len = $this->getContentLength();
95105
}
96106

97-
$headers[] = array('Content-Length', $this->getContentLength());
107+
if (!$this->shouldCompressResponse()) {
108+
$headers[] = array('Content-Length', $this->getContentLength());
109+
}
98110

99111
if (strlen($this->getDownload())) {
100112
$headers[] = array('X-Download-Options', 'noopen');
@@ -118,4 +130,8 @@ public function getHeaders() {
118130
return $headers;
119131
}
120132

133+
protected function shouldCompressResponse() {
134+
return $this->getCompressResponse();
135+
}
136+
121137
}

‎src/aphront/response/AphrontResponse.php

+15
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,21 @@ private function formatEpochTimestampForHTTPHeader($epoch_timestamp) {
242242
return gmdate('D, d M Y H:i:s', $epoch_timestamp).' GMT';
243243
}
244244

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+
245260
public function didCompleteWrite($aborted) {
246261
return;
247262
}

‎src/aphront/sink/AphrontHTTPSink.php

+2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ final public function writeData($data) {
9696
* @return void
9797
*/
9898
final public function writeResponse(AphrontResponse $response) {
99+
$response->willBeginWrite();
100+
99101
// Build the content iterator first, in case it throws. Ideally, we'd
100102
// prefer to handle exceptions before we emit the response status or any
101103
// HTTP headers.

‎src/applications/celerity/controller/CelerityResourceController.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ protected function serveResource(array $spec) {
103103
}
104104
}
105105

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);
109110

110111
// NOTE: This is a piece of magic required to make WOFF fonts work in
111112
// Firefox and IE. Possibly we should generalize this more.

‎support/PhabricatorStartup.php

-5
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,6 @@ private static function setupPHP() {
395395
if (function_exists('libxml_disable_entity_loader')) {
396396
libxml_disable_entity_loader(true);
397397
}
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);
403398
}
404399

405400

0 commit comments

Comments
 (0)
Failed to load comments.