public function createResponse(IAsset $asset, Request $request) { $response = new DispatchResponse(); //Set the correct content type based on the asset $response->headers->set('Content-Type', $asset->getContentType()); $response->headers->set('X-Content-Type-Options', 'nosniff'); //Ensure the cache varies on the encoding //Domain specific content will vary on the uri itself $response->headers->set("Vary", "Accept-Encoding"); $content = $asset->getContent(); //Set the etag to the hash of the request uri, as it is in itself a hash $response->setEtag(md5($content)); $response->setPublic(); //This resource should last for 30 days in cache $response->setMaxAge(2592000); $response->setSharedMaxAge(2592000); $response->setExpires((new \DateTime())->add(new \DateInterval('P30D'))); //Set the last modified date to now $date = new \DateTime(); $date->setTimezone(new \DateTimeZone('UTC')); $response->headers->set('Last-Modified', $date->format('D, d M Y H:i:s') . ' GMT'); //Check to see if the client already has the content if ($request->server->has('HTTP_IF_MODIFIED_SINCE')) { $response->setNotModified(); } else { $response->setContent($content); } return $response; }
/** * @dataProvider assetProvider */ public function testAsset($ext, \Packaged\Dispatch\Assets\IAsset $class) { $extType = ["ico" => "image/x-icon", "css" => "text/css", "js" => "text/javascript", "json" => "application/json", "png" => "image/png", "jpg" => "image/jpeg", "jpeg" => "image/jpeg", "gif" => "image/gif", "swf" => "application/x-shockwave-flash", "flv" => "video/x-flv", "mp4" => "video/mp4", "mpeg" => "video/mpeg", "webm" => "video/webm", "mov" => "video/quicktime", "ttf" => "application/x-font-ttf", "ttc" => "application/x-font-ttc", "pfb" => "application/x-font-pfb", "pfm" => "application/x-font-pfm", "otf" => "application/x-font-opentype", "dfont" => "application/x-font-dfont", "pfa" => "application/x-font-pfa", "afm" => "application/x-font-afm", "svg" => "image/svg+xml", "eot" => "application/vnd.ms-fontobject", "woff" => "application/x-font-woff", "zip" => "application/zip", "pdf" => "application/pdf"]; $this->assertEquals($ext, $class->getExtension()); $this->assertEquals($extType[$ext], $class->getContentType()); }