-
Notifications
You must be signed in to change notification settings - Fork 4
/
staticserver.php
executable file
·255 lines (200 loc) · 5.33 KB
/
staticserver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/*
Class:
StaticServer
Class for serving static files. Caches and compresses CSS-files, should be able
to minify JS files and cache them, serve images as well.
Well, most of the cacheing and minifying is done by Cowl plugins. See frontcontroller.php.
*/
class StaticServer
{
// Property: StaticServer::$files_dir
// The directory where files can be found
static protected $files_dir;
// Property: StaticServer::$VALID_TYPES
// To keep the StaticServer from serving bad files (such as php files)
// the allowed types are defined here. If the request type does not match
// any types here it should not be served statically
protected static $MIMES = array(
'json' => 'text/json',
'css' => 'text/css',
'js' => 'application/javascript',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
'bmp' => 'image/bmp',
'html' => 'text/html',
'rss' => 'application/rss+xml',
'partial' => 'text/html',
'otf' => 'font/opentype',
'ttf' => 'font/ttf'
);
static protected $BAD = array('php', 'phtml', 'ini', 'sql');
// Property: StaticServer::$path
// The path to (possible) serve
protected $path;
// Property: StaticServer::$is_file
// Is the path tied to a file?
protected $is_file = false;
// Property: StaticServer::$type
// The type of the file. (based on the extension)
protected $type;
// Property: StaticServer::$is_locked
// If the path has been locked
protected $is_locked = false;
/*
Constructor
Parameters:
(string) $path - The path to server
*/
public function __construct($path)
{
$this->setPath($path);
}
/*
Method:
parsePath
Parse the path and check for a static file.
*/
private function parsePath()
{
if ( empty($this->path) || (! strstr($this->path, 'gfx') && ! strstr($this->path, 'css') && ! strstr($this->path, 'js')) )
{
$this->is_file = false;
return;
}
// Check to see if it really exists
if ( file_exists($this->path) )
{
$this->is_file = true;
}
// Try to translate to the app-directory
// If it is successful <StaticServer::$path> will be altered
elseif ( file_exists(self::$files_dir . $this->path) )
{
$this->path = self::$files_dir . $this->path;
$this->is_file = true;
}
// Get the extension
$this->type = strtolower(array_last(explode('.', $this->path)));
// Bad filetype!
if ( in_array($this->type, self::$BAD) )
{
$this->is_file = false;
}
}
/*
Method:
isFile
Check if the current path maps to a static path on disk.
Returns:
Wether it exists
*/
public function isFile()
{
return $this->is_file;
}
/*
Method:
render
Render the request. The next course of action is to abort the script.
That is left for the API user to do, so to facilitate clean up and other
scenarios.
*/
public function render()
{
// Sanity check to see that render hasn't been called when there was no file
if ( ! $this->is_file )
return;
header('Cache-Control: private');
header('Pragma: private');
// This is removed because Chrome won't even send a request if it has an expires headers, thus defeating the HTTP_IF_NONE_MATCH
//header('Expires: ' . date(DATE_RFC2822, (time() + 60 * 60 * 24 * 365)));
$mime = isset(self::$MIMES[$this->type]) ? self::$MIMES[$this->type] : 'text/html';
$mod_time = filemtime($this->path);
header('Content-type: ' . $mime);
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $mod_time) . ' GMT');
$etag = 'cowl-' . dechex(crc32($this->path . $mod_time));
header('ETag: "' . $etag . '"');
if ( isset($_SERVER['HTTP_IF_NONE_MATCH'], $_SERVER['HTTP_CACHE_CONTROL'])
&& ! strstr($_SERVER['HTTP_CACHE_CONTROL'], 'no-cache')
&& strstr($_SERVER['HTTP_IF_NONE_MATCH'], $etag) )
{
header('HTTP/1.1 304 Not Modified');
}
else
{
readfile($this->path);
}
}
/*
Method:
setPath
Set the path for the request. This will be modified to the actual path on disk, if
it exists. Otherwise the <StaticServer::$is_file>-property will be set to false.
Parameters:
(string) $path - The path of the request
*/
public function setPath($path)
{
if ( $this->is_locked ) return;
$this->path = $path;
$this->parsePath();
}
/*
Method:
lockPath
Lock path so subsequent calls to <StaticServer::setPath> are ignored.
*/
public function lockPath()
{
$this->is_locked = true;
}
/*
Method:
unlockPath
Unlock the path, if previously locked by <StaticServer::lockPath>
*/
public function unlockPath()
{
$this->is_locked = false;
}
/*
Method:
forceSetPath
Force set path without checking if it exists.
Parameters:
(string) $path - The path of the request
*/
public function forceSetPath($path)
{
$this->path = $path;
}
/*
Method:
setIsFile
Force set if StaticServer::$is_file.
Parameters:
$is_file - The bool value to set it to
*/
public function setIsFile($is_file)
{
$this->is_file = $is_file;
}
/*
Method:
setDir
Set the dir in which all static files are contained.
*/
public static function setDir($dir)
{
self::$files_dir = $dir;
}
// Method: <StaticServer::getPath>
// Return <StaticServer::$path>
public function getPath() { return $this->path; }
// Method: <StaticServer::getType>
// Return <StaticServer::$type>
public function getType() { return $this->type; }
}