Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/aphront/response/AphrontFileResponse.php
12241 views
1
<?php
2
3
final class AphrontFileResponse extends AphrontResponse {
4
5
private $content;
6
private $contentIterator;
7
private $contentLength;
8
private $compressResponse;
9
10
private $mimeType;
11
private $download;
12
private $rangeMin;
13
private $rangeMax;
14
private $allowOrigins = array();
15
16
public function addAllowOrigin($origin) {
17
$this->allowOrigins[] = $origin;
18
return $this;
19
}
20
21
public function setDownload($download) {
22
if ($download === null || !strlen($download)) {
23
$download = 'untitled';
24
}
25
$this->download = $download;
26
return $this;
27
}
28
29
public function getDownload() {
30
return $this->download;
31
}
32
33
public function setMimeType($mime_type) {
34
$this->mimeType = $mime_type;
35
return $this;
36
}
37
38
public function getMimeType() {
39
return $this->mimeType;
40
}
41
42
public function setContent($content) {
43
$this->setContentLength(strlen($content));
44
$this->content = $content;
45
return $this;
46
}
47
48
public function setContentIterator($iterator) {
49
$this->contentIterator = $iterator;
50
return $this;
51
}
52
53
public function buildResponseString() {
54
return $this->content;
55
}
56
57
public function getContentIterator() {
58
if ($this->contentIterator) {
59
return $this->contentIterator;
60
}
61
return parent::getContentIterator();
62
}
63
64
public function setContentLength($length) {
65
$this->contentLength = $length;
66
return $this;
67
}
68
69
public function getContentLength() {
70
return $this->contentLength;
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
82
public function setRange($min, $max) {
83
$this->rangeMin = $min;
84
$this->rangeMax = $max;
85
return $this;
86
}
87
88
public function getHeaders() {
89
$headers = array(
90
array('Content-Type', $this->getMimeType()),
91
// This tells clients that we can support requests with a "Range" header,
92
// which allows downloads to be resumed, in some browsers, some of the
93
// time, if the stars align.
94
array('Accept-Ranges', 'bytes'),
95
);
96
97
if ($this->rangeMin !== null || $this->rangeMax !== null) {
98
$len = $this->getContentLength();
99
$min = $this->rangeMin;
100
101
$max = $this->rangeMax;
102
if ($max === null) {
103
$max = ($len - 1);
104
}
105
106
$headers[] = array('Content-Range', "bytes {$min}-{$max}/{$len}");
107
$content_len = ($max - $min) + 1;
108
} else {
109
$content_len = $this->getContentLength();
110
}
111
112
if (!$this->shouldCompressResponse()) {
113
$headers[] = array('Content-Length', $content_len);
114
}
115
116
$download = $this->getDownload();
117
if ($download !== null && strlen($download)) {
118
$headers[] = array('X-Download-Options', 'noopen');
119
120
$filename = $this->getDownload();
121
$filename = addcslashes($filename, '"\\');
122
$headers[] = array(
123
'Content-Disposition',
124
'attachment; filename="'.$filename.'"',
125
);
126
}
127
128
if ($this->allowOrigins) {
129
$headers[] = array(
130
'Access-Control-Allow-Origin',
131
implode(',', $this->allowOrigins),
132
);
133
}
134
135
$headers = array_merge(parent::getHeaders(), $headers);
136
return $headers;
137
}
138
139
protected function shouldCompressResponse() {
140
return $this->getCompressResponse();
141
}
142
143
public function parseHTTPRange($range) {
144
$begin = null;
145
$end = null;
146
147
$matches = null;
148
if (preg_match('/^bytes=(\d+)-(\d*)$/', $range, $matches)) {
149
// Note that the "Range" header specifies bytes differently than
150
// we do internally: the range 0-1 has 2 bytes (byte 0 and byte 1).
151
$begin = (int)$matches[1];
152
153
// The "Range" may be "200-299" or "200-", meaning "until end of file".
154
if ($matches[2] !== null && strlen($matches[2])) {
155
$range_end = (int)$matches[2];
156
$end = $range_end + 1;
157
} else {
158
$range_end = null;
159
}
160
161
$this->setHTTPResponseCode(206);
162
$this->setRange($begin, $range_end);
163
}
164
165
return array($begin, $end);
166
}
167
168
}
169
170